Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/300842

Change subject: Pass $forImmediatePublishing to UploadVerifyFile hook
......................................................................

Pass $forImmediatePublishing to UploadVerifyFile hook

Bug: T140521
Change-Id: I01c8f1271398574073b61c38387ca2e8d96d2871
---
M docs/hooks.txt
M includes/api/ApiUpload.php
M includes/jobqueue/jobs/PublishStashedFileJob.php
M includes/specials/SpecialUpload.php
M includes/upload/UploadBase.php
M includes/upload/UploadFromChunks.php
M includes/upload/UploadFromFile.php
7 files changed, 23 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/42/300842/1

diff --git a/docs/hooks.txt b/docs/hooks.txt
index e1b3974..1b47199 100644
--- a/docs/hooks.txt
+++ b/docs/hooks.txt
@@ -3307,6 +3307,10 @@
   in the form of array( messagename, param1, param2, ... ) or a 
MessageSpecifier
   instance (you might want to use ApiMessage to provide machine-readable 
details
   for the API).
+$forImmediatePublishing: (bool) If true, indicates that the file will be
+  published immediately if the checks are successful, calling the
+  UploadVerifyUpload hook beforehand. In this case extensions may skip
+  expensive checks that they also perform from the UploadVerifyUpload hook.
 
 'UploadVerifyUpload': Upload verification, based on both file properties like
 MIME type (same as UploadVerifyFile) and the information entered by the user
diff --git a/includes/api/ApiUpload.php b/includes/api/ApiUpload.php
index 95659e5..3c2cc9e 100644
--- a/includes/api/ApiUpload.php
+++ b/includes/api/ApiUpload.php
@@ -92,7 +92,8 @@
                        // defer verification to background process
                } else {
                        wfDebug( __METHOD__ . " about to verify\n" );
-                       $this->verifyUpload();
+                       $forImmediatePublishing = !$this->mParams['chunk'] && 
!$this->mParams['stash'];
+                       $this->verifyUpload( $forImmediatePublishing );
                }
 
                // Check if the user has the rights to modify or overwrite the 
requested title
@@ -507,8 +508,8 @@
        /**
         * Performs file verification, dies on error.
         */
-       protected function verifyUpload() {
-               $verification = $this->mUpload->verifyUpload();
+       protected function verifyUpload( $forImmediatePublishing ) {
+               $verification = $this->mUpload->verifyUpload( 
$forImmediatePublishing );
                if ( $verification['status'] === UploadBase::OK ) {
                        return;
                }
diff --git a/includes/jobqueue/jobs/PublishStashedFileJob.php 
b/includes/jobqueue/jobs/PublishStashedFileJob.php
index 78531dc..e462a31 100644
--- a/includes/jobqueue/jobs/PublishStashedFileJob.php
+++ b/includes/jobqueue/jobs/PublishStashedFileJob.php
@@ -63,7 +63,7 @@
                        $upload->initialize( $this->params['filekey'], 
$this->params['filename'] );
 
                        // Check if the local file checks out (this is 
generally a no-op)
-                       $verification = $upload->verifyUpload();
+                       $verification = $upload->verifyUpload( 
/*$forImmediatePublishing*/ true );
                        if ( $verification['status'] !== UploadBase::OK ) {
                                $status = Status::newFatal( 
'verification-error' );
                                $status->value = [ 'verification' => 
$verification ];
diff --git a/includes/specials/SpecialUpload.php 
b/includes/specials/SpecialUpload.php
index 0ef6af1..4f35a74 100644
--- a/includes/specials/SpecialUpload.php
+++ b/includes/specials/SpecialUpload.php
@@ -467,7 +467,7 @@
                }
 
                // Upload verification
-               $details = $this->mUpload->verifyUpload();
+               $details = $this->mUpload->verifyUpload( 
/*$forImmediatePublishing*/ true );
                if ( $details['status'] != UploadBase::OK ) {
                        $this->processVerificationError( $details );
 
diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php
index 71d032f..3cfcd92 100644
--- a/includes/upload/UploadBase.php
+++ b/includes/upload/UploadBase.php
@@ -308,9 +308,14 @@
 
        /**
         * Verify whether the upload is sane.
+        *
+        * @param bool $forImmediatePublishing Caller may pass 'true' to 
indicate that they will call
+        *     performUpload() immediately if the checks are successful, rather 
than stash the file. This
+        *     is passed to the UploadVerifyFile hook to allow extensions to 
skip expensive checks that
+        *     can also be performed from the UploadVerifyUpload hook when 
attempting to publish the file.
         * @return mixed Const self::OK or else an array with error information
         */
-       public function verifyUpload() {
+       public function verifyUpload( $forImmediatePublishing = false ) {
 
                /**
                 * If there was no filename or a zero size given, give up quick.
@@ -335,7 +340,7 @@
                 * type but it's corrupt or data of the wrong type, we should
                 * probably not accept it.
                 */
-               $verification = $this->verifyFile();
+               $verification = $this->verifyFile( $forImmediatePublishing );
                if ( $verification !== true ) {
                        return [
                                'status' => self::VERIFICATION_ERROR,
@@ -427,9 +432,10 @@
        /**
         * Verifies that it's ok to include the uploaded file
         *
+        * @param bool $forImmediatePublishing See verifyUpload().
         * @return mixed True of the file is verified, array otherwise.
         */
-       protected function verifyFile() {
+       protected function verifyFile( $forImmediatePublishing = false ) {
                global $wgVerifyMimeType, $wgDisableUploadScriptChecks;
 
                $status = $this->verifyPartialFile();
@@ -468,7 +474,7 @@
                }
 
                $error = true;
-               Hooks::run( 'UploadVerifyFile', [ $this, $mime, &$error ] );
+               Hooks::run( 'UploadVerifyFile', [ $this, $mime, &$error, 
$forImmediatePublishing ] );
                if ( $error !== true ) {
                        if ( !is_array( $error ) ) {
                                $error = [ $error ];
diff --git a/includes/upload/UploadFromChunks.php 
b/includes/upload/UploadFromChunks.php
index 0323b68..782dda0 100644
--- a/includes/upload/UploadFromChunks.php
+++ b/includes/upload/UploadFromChunks.php
@@ -151,7 +151,7 @@
                // File system path of the actual full temp file
                $this->setTempFile( $tmpPath );
 
-               $ret = $this->verifyUpload();
+               $ret = $this->verifyUpload( /*$forImmediatePublishing*/ false );
                if ( $ret['status'] !== UploadBase::OK ) {
                        wfDebugLog( 'fileconcatenate', "Verification failed for 
chunked upload" );
                        $status->fatal( $this->getVerificationErrorCode( 
$ret['status'] ) );
diff --git a/includes/upload/UploadFromFile.php 
b/includes/upload/UploadFromFile.php
index acfabba..1e4bfe9 100644
--- a/includes/upload/UploadFromFile.php
+++ b/includes/upload/UploadFromFile.php
@@ -74,10 +74,7 @@
                return 'file';
        }
 
-       /**
-        * @return array
-        */
-       public function verifyUpload() {
+       public function verifyUpload( $forImmediatePublishing = false ) {
                # Check for a post_max_size or upload_max_size overflow, so 
that a
                # proper error can be shown to the user
                if ( is_null( $this->mTempPath ) || $this->isEmptyFile() ) {
@@ -92,6 +89,6 @@
                        }
                }
 
-               return parent::verifyUpload();
+               return parent::verifyUpload( $forImmediatePublishing );
        }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/300842
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I01c8f1271398574073b61c38387ca2e8d96d2871
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to