Replace uses of join() by implode()
[lhc/web/wiklou.git] / includes / api / ApiUpload.php
index e76b365..326f8ba 100644 (file)
@@ -155,7 +155,7 @@ class ApiUpload extends ApiBase {
         * @return array
         */
        private function getStashResult( $warnings ) {
-               $result = array();
+               $result = [];
                // Some uploads can request they be stashed, so as not to publish them immediately.
                // In this case, a failure to stash ought to be fatal
                try {
@@ -180,7 +180,7 @@ class ApiUpload extends ApiBase {
         * @return array
         */
        private function getWarningsResult( $warnings ) {
-               $result = array();
+               $result = [];
                $result['result'] = 'Warning';
                $result['warnings'] = $warnings;
                // in case the warnings can be fixed with some further user action, let's stash this upload
@@ -201,7 +201,7 @@ class ApiUpload extends ApiBase {
         * @return array
         */
        private function getChunkResult( $warnings ) {
-               $result = array();
+               $result = [];
 
                if ( $warnings && count( $warnings ) > 0 ) {
                        $result['warnings'] = $warnings;
@@ -253,9 +253,9 @@ class ApiUpload extends ApiBase {
                        $status = $this->mUpload->addChunk(
                                $chunkPath, $chunkSize, $this->mParams['offset'] );
                        if ( !$status->isGood() ) {
-                               $extradata = array(
+                               $extradata = [
                                        'offset' => $this->mUpload->getOffset(),
-                               );
+                               ];
 
                                $this->dieUsage( $status->getWikiText(), 'stashfailed', 0, $extradata );
                        }
@@ -267,16 +267,16 @@ class ApiUpload extends ApiBase {
                                UploadBase::setSessionStatus(
                                        $this->getUser(),
                                        $filekey,
-                                       array( 'result' => 'Poll',
-                                               'stage' => 'queued', 'status' => Status::newGood() )
+                                       [ 'result' => 'Poll',
+                                               'stage' => 'queued', 'status' => Status::newGood() ]
                                );
                                JobQueueGroup::singleton()->push( new AssembleUploadChunksJob(
                                        Title::makeTitle( NS_FILE, $filekey ),
-                                       array(
+                                       [
                                                'filename' => $this->mParams['filename'],
                                                'filekey' => $filekey,
                                                'session' => $this->getContext()->exportSession()
-                                       )
+                                       ]
                                ) );
                                $result['result'] = 'Poll';
                                $result['stage'] = 'queued';
@@ -286,7 +286,7 @@ class ApiUpload extends ApiBase {
                                        UploadBase::setSessionStatus(
                                                $this->getUser(),
                                                $filekey,
-                                               array( 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status )
+                                               [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ]
                                        );
                                        $this->dieUsage( $status->getWikiText(), 'stashfailed' );
                                }
@@ -303,12 +303,12 @@ class ApiUpload extends ApiBase {
                        UploadBase::setSessionStatus(
                                $this->getUser(),
                                $filekey,
-                               array(
+                               [
                                        'result' => 'Continue',
                                        'stage' => 'uploading',
                                        'offset' => $totalSoFar,
                                        'status' => Status::newGood(),
-                               )
+                               ]
                        );
                        $result['result'] = 'Continue';
                        $result['offset'] = $totalSoFar;
@@ -352,7 +352,7 @@ class ApiUpload extends ApiBase {
         * @param array $data Optional extra data to pass to the user
         * @throws UsageException
         */
-       private function dieRecoverableError( $error, $parameter, $data = array() ) {
+       private function dieRecoverableError( $error, $parameter, $data = [] ) {
                try {
                        $data['filekey'] = $this->performStash();
                        $data['sessionkey'] = $data['filekey'];
@@ -382,7 +382,7 @@ class ApiUpload extends ApiBase {
                // chunk or one and only one of the following parameters is needed
                if ( !$this->mParams['chunk'] ) {
                        $this->requireOnlyOneParameter( $this->mParams,
-                               'filekey', 'file', 'url', 'statuskey' );
+                               'filekey', 'file', 'url' );
                }
 
                // Status report for "upload to stash"/"upload from stash"
@@ -402,26 +402,9 @@ class ApiUpload extends ApiBase {
                        return false;
                }
 
-               if ( $this->mParams['statuskey'] ) {
-                       $this->checkAsyncDownloadEnabled();
-
-                       // Status request for an async upload
-                       $sessionData = UploadFromUrlJob::getSessionData( $this->mParams['statuskey'] );
-                       if ( !isset( $sessionData['result'] ) ) {
-                               $this->dieUsage( 'No result in session data', 'missingresult' );
-                       }
-                       if ( $sessionData['result'] == 'Warning' ) {
-                               $sessionData['warnings'] = $this->transformWarnings( $sessionData['warnings'] );
-                               $sessionData['sessionkey'] = $this->mParams['statuskey'];
-                       }
-                       $this->getResult()->addValue( null, $this->getModuleName(), $sessionData );
-
-                       return false;
-               }
-
                // The following modules all require the filename parameter to be set
                if ( is_null( $this->mParams['filename'] ) ) {
-                       $this->dieUsageMsg( array( 'missingparam', 'filename' ) );
+                       $this->dieUsageMsg( [ 'missingparam', 'filename' ] );
                }
 
                if ( $this->mParams['chunk'] ) {
@@ -481,24 +464,9 @@ class ApiUpload extends ApiBase {
                                $this->dieUsageMsg( 'copyuploadbadurl' );
                        }
 
-                       $async = false;
-                       if ( $this->mParams['asyncdownload'] ) {
-                               $this->checkAsyncDownloadEnabled();
-
-                               if ( $this->mParams['leavemessage'] && !$this->mParams['ignorewarnings'] ) {
-                                       $this->dieUsage( 'Using leavemessage without ignorewarnings is not supported',
-                                               'missing-ignorewarnings' );
-                               }
-
-                               if ( $this->mParams['leavemessage'] ) {
-                                       $async = 'async-leavemessage';
-                               } else {
-                                       $async = 'async';
-                               }
-                       }
                        $this->mUpload = new UploadFromUrl;
                        $this->mUpload->initialize( $this->mParams['filename'],
-                               $this->mParams['url'], $async );
+                               $this->mParams['url'] );
                }
 
                return true;
@@ -515,7 +483,7 @@ class ApiUpload extends ApiBase {
 
                if ( $permission !== true ) {
                        if ( !$user->isLoggedIn() ) {
-                               $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) );
+                               $this->dieUsageMsg( [ 'mustbeloggedin', 'upload' ] );
                        }
 
                        $this->dieUsageMsg( 'badaccess-groups' );
@@ -547,7 +515,7 @@ class ApiUpload extends ApiBase {
                                break;
                        case UploadBase::ILLEGAL_FILENAME:
                                $this->dieRecoverableError( 'illegal-filename', 'filename',
-                                       array( 'filename' => $verification['filtered'] ) );
+                                       [ 'filename' => $verification['filtered'] ] );
                                break;
                        case UploadBase::FILENAME_TOO_LONG:
                                $this->dieRecoverableError( 'filename-toolong', 'filename' );
@@ -568,15 +536,15 @@ class ApiUpload extends ApiBase {
                                break;
 
                        case UploadBase::FILETYPE_BADTYPE:
-                               $extradata = array(
+                               $extradata = [
                                        'filetype' => $verification['finalExt'],
                                        'allowed' => array_values( array_unique( $this->getConfig()->get( 'FileExtensions' ) ) )
-                               );
+                               ];
                                ApiResult::setIndexedTagName( $extradata['allowed'], 'ext' );
 
-                               $msg = "Filetype not permitted: ";
+                               $msg = 'Filetype not permitted: ';
                                if ( isset( $verification['blacklistedExt'] ) ) {
-                                       $msg .= join( ', ', $verification['blacklistedExt'] );
+                                       $msg .= implode( ', ', $verification['blacklistedExt'] );
                                        $extradata['blacklisted'] = array_values( $verification['blacklistedExt'] );
                                        ApiResult::setIndexedTagName( $extradata['blacklisted'], 'ext' );
                                } else {
@@ -590,23 +558,23 @@ class ApiUpload extends ApiBase {
                                $msg = $this->msg( $key, $params )->inLanguage( 'en' )->useDatabase( false )->text();
                                ApiResult::setIndexedTagName( $verification['details'], 'detail' );
                                $this->dieUsage( "This file did not pass file verification: $msg", 'verification-error',
-                                       0, array( 'details' => $verification['details'] ) );
+                                       0, [ 'details' => $verification['details'] ] );
                                break;
                        case UploadBase::HOOK_ABORTED:
                                if ( is_array( $verification['error'] ) ) {
                                        $params = $verification['error'];
                                } elseif ( $verification['error'] !== '' ) {
-                                       $params = array( $verification['error'] );
+                                       $params = [ $verification['error'] ];
                                } else {
-                                       $params = array( 'hookaborted' );
+                                       $params = [ 'hookaborted' ];
                                }
                                $key = array_shift( $params );
                                $msg = $this->msg( $key, $params )->inLanguage( 'en' )->useDatabase( false )->text();
-                               $this->dieUsage( $msg, 'hookaborted', 0, array( 'details' => $verification['error'] ) );
+                               $this->dieUsage( $msg, 'hookaborted', 0, [ 'details' => $verification['error'] ] );
                                break;
                        default:
                                $this->dieUsage( 'An unknown error occurred', 'unknown-error',
-                                       0, array( 'details' => array( 'code' => $verification['status'] ) ) );
+                                       0, [ 'details' => [ 'code' => $verification['status'] ] ] );
                                break;
                }
        }
@@ -630,7 +598,7 @@ class ApiUpload extends ApiBase {
                        ApiResult::setIndexedTagName( $warnings, 'warning' );
 
                        if ( isset( $warnings['duplicate'] ) ) {
-                               $dupes = array();
+                               $dupes = [];
                                /** @var File $dupe */
                                foreach ( $warnings['duplicate'] as $dupe ) {
                                        $dupes[] = $dupe->getName();
@@ -696,7 +664,7 @@ class ApiUpload extends ApiBase {
                                $this->dieUsage( 'No such filekey: ' . $e->getMessage(), 'stashnosuchfilekey' );
                                break;
                        default:
-                               $this->dieUsage( $exceptionType . ": " . $e->getMessage(), 'stasherror' );
+                               $this->dieUsage( $exceptionType . ': ' . $e->getMessage(), 'stasherror' );
                                break;
                }
        }
@@ -735,47 +703,45 @@ class ApiUpload extends ApiBase {
                        $watch = true;
                }
 
+               if ( $this->mParams['tags'] ) {
+                       $status = ChangeTags::canAddTagsAccompanyingChange( $this->mParams['tags'], $this->getUser() );
+                       if ( !$status->isOK() ) {
+                               $this->dieStatus( $status );
+                       }
+               }
+
                // No errors, no warnings: do the upload
                if ( $this->mParams['async'] ) {
                        $progress = UploadBase::getSessionStatus( $this->getUser(), $this->mParams['filekey'] );
                        if ( $progress && $progress['result'] === 'Poll' ) {
-                               $this->dieUsage( "Upload from stash already in progress.", 'publishfailed' );
+                               $this->dieUsage( 'Upload from stash already in progress.', 'publishfailed' );
                        }
                        UploadBase::setSessionStatus(
                                $this->getUser(),
                                $this->mParams['filekey'],
-                               array( 'result' => 'Poll', 'stage' => 'queued', 'status' => Status::newGood() )
+                               [ 'result' => 'Poll', 'stage' => 'queued', 'status' => Status::newGood() ]
                        );
                        JobQueueGroup::singleton()->push( new PublishStashedFileJob(
                                Title::makeTitle( NS_FILE, $this->mParams['filename'] ),
-                               array(
+                               [
                                        'filename' => $this->mParams['filename'],
                                        'filekey' => $this->mParams['filekey'],
                                        'comment' => $this->mParams['comment'],
+                                       'tags' => $this->mParams['tags'],
                                        'text' => $this->mParams['text'],
                                        'watch' => $watch,
                                        'session' => $this->getContext()->exportSession()
-                               )
+                               ]
                        ) );
                        $result['result'] = 'Poll';
                        $result['stage'] = 'queued';
                } else {
                        /** @var $status Status */
                        $status = $this->mUpload->performUpload( $this->mParams['comment'],
-                               $this->mParams['text'], $watch, $this->getUser() );
+                               $this->mParams['text'], $watch, $this->getUser(), $this->mParams['tags'] );
 
                        if ( !$status->isGood() ) {
                                $error = $status->getErrorsArray();
-
-                               if ( count( $error ) == 1 && $error[0][0] == 'async' ) {
-                                       // The upload can not be performed right now, because the user
-                                       // requested so
-                                       return array(
-                                               'result' => 'Queued',
-                                               'statuskey' => $error[0][1],
-                                       );
-                               }
-
                                ApiResult::setIndexedTagName( $error, 'error' );
                                $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error );
                        }
@@ -790,15 +756,6 @@ class ApiUpload extends ApiBase {
                return $result;
        }
 
-       /**
-        * Checks if asynchronous copy uploads are enabled and throws an error if they are not.
-        */
-       protected function checkAsyncDownloadEnabled() {
-               if ( !$this->getConfig()->get( 'AllowAsyncCopyUploads' ) ) {
-                       $this->dieUsage( 'Asynchronous copy uploads disabled', 'asynccopyuploaddisabled' );
-               }
-       }
-
        public function mustBePosted() {
                return true;
        }
@@ -808,58 +765,59 @@ class ApiUpload extends ApiBase {
        }
 
        public function getAllowedParams() {
-               $params = array(
-                       'filename' => array(
+               $params = [
+                       'filename' => [
                                ApiBase::PARAM_TYPE => 'string',
-                       ),
-                       'comment' => array(
+                       ],
+                       'comment' => [
                                ApiBase::PARAM_DFLT => ''
-                       ),
-                       'text' => array(
+                       ],
+                       'tags' => [
+                               ApiBase::PARAM_TYPE => 'tags',
+                               ApiBase::PARAM_ISMULTI => true,
+                       ],
+                       'text' => [
                                ApiBase::PARAM_TYPE => 'text',
-                       ),
-                       'watch' => array(
+                       ],
+                       'watch' => [
                                ApiBase::PARAM_DFLT => false,
                                ApiBase::PARAM_DEPRECATED => true,
-                       ),
-                       'watchlist' => array(
+                       ],
+                       'watchlist' => [
                                ApiBase::PARAM_DFLT => 'preferences',
-                               ApiBase::PARAM_TYPE => array(
+                               ApiBase::PARAM_TYPE => [
                                        'watch',
                                        'preferences',
                                        'nochange'
-                               ),
-                       ),
+                               ],
+                       ],
                        'ignorewarnings' => false,
-                       'file' => array(
+                       'file' => [
                                ApiBase::PARAM_TYPE => 'upload',
-                       ),
+                       ],
                        'url' => null,
                        'filekey' => null,
-                       'sessionkey' => array(
+                       'sessionkey' => [
                                ApiBase::PARAM_DEPRECATED => true,
-                       ),
+                       ],
                        'stash' => false,
 
-                       'filesize' => array(
+                       'filesize' => [
                                ApiBase::PARAM_TYPE => 'integer',
                                ApiBase::PARAM_MIN => 0,
                                ApiBase::PARAM_MAX => UploadBase::getMaxUploadSize(),
-                       ),
-                       'offset' => array(
+                       ],
+                       'offset' => [
                                ApiBase::PARAM_TYPE => 'integer',
                                ApiBase::PARAM_MIN => 0,
-                       ),
-                       'chunk' => array(
+                       ],
+                       'chunk' => [
                                ApiBase::PARAM_TYPE => 'upload',
-                       ),
+                       ],
 
                        'async' => false,
-                       'asyncdownload' => false,
-                       'leavemessage' => false,
-                       'statuskey' => null,
                        'checkstatus' => false,
-               );
+               ];
 
                return $params;
        }
@@ -869,13 +827,13 @@ class ApiUpload extends ApiBase {
        }
 
        protected function getExamplesMessages() {
-               return array(
+               return [
                        'action=upload&filename=Wiki.png' .
                                '&url=http%3A//upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png&token=123ABC'
                                => 'apihelp-upload-example-url',
                        'action=upload&filename=Wiki.png&filekey=filekey&ignorewarnings=1&token=123ABC'
                                => 'apihelp-upload-example-filekey',
-               );
+               ];
        }
 
        public function getHelpUrls() {