Added parser test about brackets and for bug 21261
[lhc/web/wiklou.git] / includes / api / ApiUpload.php
index 3a8326f..6c3562c 100644 (file)
  * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( "ApiBase.php" );
-}
-
 /**
  * @ingroup API
  */
@@ -46,19 +41,20 @@ class ApiUpload extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
-
                // Check whether upload is enabled
                if ( !UploadBase::isEnabled() ) {
                        $this->dieUsageMsg( 'uploaddisabled' );
                }
 
+               $user = $this->getUser();
+
                // Parameter handling
                $this->mParams = $this->extractRequestParams();
                $request = $this->getMain()->getRequest();
                // Add the uploaded file to the params array
                $this->mParams['file'] = $request->getFileName( 'file' );
-               
+               $this->mParams['chunk'] = $request->getFileName( 'chunk' );
+
                // Copy the session key to the file key, for backward compatibility.
                if( !$this->mParams['filekey'] && $this->mParams['sessionkey'] ) {
                        $this->mParams['filekey'] = $this->mParams['sessionkey'];
@@ -74,7 +70,7 @@ class ApiUpload extends ApiBase {
                }
 
                // First check permission to upload
-               $this->checkPermissions( $wgUser );
+               $this->checkPermissions( $user );
 
                // Fetch the file
                $status = $this->mUpload->fetchFile();
@@ -85,14 +81,21 @@ class ApiUpload extends ApiBase {
                }
 
                // Check if the uploaded file is sane
-               $this->verifyUpload();
+               if ( $this->mParams['chunk'] ) {
+                       $maxSize = $this->mUpload->getMaxUploadSize( );
+                       if( $this->mParams['filesize'] > $maxSize ) {
+                               $this->dieUsage( 'The file you submitted was too large', 'file-too-large' );
+                       }
+               } else {
+                       $this->verifyUpload();
+               }
 
 
                // Check if the user has the rights to modify or overwrite the requested title
                // (This check is irrelevant if stashing is already requested, since the errors
                //  can always be fixed by changing the title)
                if ( ! $this->mParams['stash'] ) {
-                       $permErrors = $this->mUpload->verifyTitlePermissions( $wgUser );
+                       $permErrors = $this->mUpload->verifyTitlePermissions( $user );
                        if ( $permErrors !== true ) {
                                $this->dieRecoverableError( $permErrors[0], 'filename' );
                        }
@@ -113,6 +116,26 @@ class ApiUpload extends ApiBase {
                        } catch ( MWException $e ) {
                                $result['warnings']['stashfailed'] = $e->getMessage();
                        }
+               } elseif ( $this->mParams['chunk'] ) {
+                       $result['result'] = 'Continue';
+                       $chunk = $request->getFileTempName( 'chunk' );
+                       $chunkSize = $request->getUpload( 'chunk' )->getSize();
+                       if ($this->mParams['offset'] == 0) {
+                               $result['filekey'] = $this->performStash();
+                       } else {
+                               $status = $this->mUpload->appendChunk($chunk, $chunkSize,
+                                                                                                         $this->mParams['offset']);
+                               if ( !$status->isGood() ) {
+                                       $this->dieUsage( $status->getWikiText(), 'stashfailed' );
+                               } else {
+                                       $result['filekey'] = $this->mParams['filekey'];
+                                       if($this->mParams['offset'] + $chunkSize == $this->mParams['filesize']) {
+                                               $this->mUpload->finalizeFile();
+                                               $result['result'] = 'Success';
+                                       }
+                               }
+                       }
+                       $result['offset'] = $this->mParams['offset'] + $chunkSize;
                } elseif ( $this->mParams['stash'] ) {
                        // 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
@@ -147,7 +170,12 @@ class ApiUpload extends ApiBase {
         */
        function performStash() {
                try {
-                       $fileKey = $this->mUpload->stashFile()->getFileKey();
+                       $stashFile = $this->mUpload->stashFile();
+
+                       if ( !$stashFile ) {
+                               throw new MWException( 'Invalid stashed file' );
+                       }
+                       $fileKey = $stashFile->getFileKey();
                } catch ( MWException $e ) {
                        $message = 'Stashing temporary file failed: ' . get_class( $e ) . ' ' . $e->getMessage();
                        wfDebug( __METHOD__ . ' ' . $message . "\n");
@@ -188,9 +216,11 @@ class ApiUpload extends ApiBase {
        protected function selectUploadModule() {
                $request = $this->getMain()->getRequest();
 
-               // One and only one of the following parameters is needed
-               $this->requireOnlyOneParameter( $this->mParams,
-                       'filekey', 'file', 'url', 'statuskey' );
+               // chunk or one and only one of the following parameters is needed
+               if( !$this->mParams['chunk'] ) {
+                       $this->requireOnlyOneParameter( $this->mParams,
+                               'filekey', 'file', 'url', 'statuskey' );
+               }
 
                if ( $this->mParams['statuskey'] ) {
                        $this->checkAsyncDownloadEnabled();
@@ -220,11 +250,17 @@ class ApiUpload extends ApiBase {
                                $this->dieUsageMsg( 'invalid-file-key' );
                        }
 
-                       // context allows access to the current user without creating new $wgUser references
-                       $context = $this->createContext();
-                       $this->mUpload = new UploadFromStash( $context->getUser() );
+                       $this->mUpload = new UploadFromStash( $this->getUser() );
+
                        $this->mUpload->initialize( $this->mParams['filekey'], $this->mParams['filename'] );
 
+               } elseif ( isset( $this->mParams['chunk'] ) ) {
+                       // Start new Chunk upload
+                       $this->mUpload = new UploadFromFile();
+                       $this->mUpload->initialize(
+                               $this->mParams['filename'],
+                               $request->getUpload( 'chunk' )
+                       );
                } elseif ( isset( $this->mParams['file'] ) ) {
                        $this->mUpload = new UploadFromFile();
                        $this->mUpload->initialize(
@@ -300,6 +336,9 @@ class ApiUpload extends ApiBase {
                                $this->dieRecoverableError( 'illegal-filename', 'filename',
                                                array( 'filename' => $verification['filtered'] ) );
                                break;
+                       case UploadBase::FILENAME_TOO_LONG:
+                               $this->dieRecoverableError( 'filename-toolong', 'filename' );
+                               break;
                        case UploadBase::FILETYPE_MISSING:
                                $this->dieRecoverableError( 'filetype-missing', 'filename' );
                                break;
@@ -383,10 +422,10 @@ class ApiUpload extends ApiBase {
        /**
         * Perform the actual upload. Returns a suitable result array on success;
         * dies on failure.
+        *
+        * @return array
         */
        protected function performUpload() {
-               global $wgUser;
-
                // Use comment as initial page text by default
                if ( is_null( $this->mParams['text'] ) ) {
                        $this->mParams['text'] = $this->mParams['comment'];
@@ -402,7 +441,7 @@ class ApiUpload extends ApiBase {
 
                // No errors, no warnings: do the upload
                $status = $this->mUpload->performUpload( $this->mParams['comment'],
-                       $this->mParams['text'], $watch, $wgUser );
+                       $this->mParams['text'], $watch, $this->getUser() );
 
                if ( !$status->isGood() ) {
                        $error = $status->getErrorsArray();
@@ -479,6 +518,10 @@ class ApiUpload extends ApiBase {
                        ),
                        'stash' => false,
 
+                       'filesize' => null,
+                       'offset' => null,
+                       'chunk' => null,
+
                        'asyncdownload' => false,
                        'leavemessage' => false,
                        'statuskey' => null,
@@ -502,6 +545,10 @@ class ApiUpload extends ApiBase {
                        'sessionkey' => 'Same as filekey, maintained for backward compatibility.',
                        'stash' => 'If set, the server will not add the file to the repository and stash it temporarily.',
 
+                       'chunk' => 'Chunk contents',
+                       'offset' => 'Offset of chunk in bytes',
+                       'filesize' => 'Filesize of entire upload',
+
                        'asyncdownload' => 'Make fetching a URL asynchronous',
                        'leavemessage' => 'If asyncdownload is used, leave a message on the user talk page if finished',
                        'statuskey' => 'Fetch the upload status for this file key',
@@ -552,7 +599,7 @@ class ApiUpload extends ApiBase {
                return '';
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
                        'Upload from a URL:',
                        '    api.php?action=upload&filename=Wiki.png&url=http%3A//upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png',
@@ -561,6 +608,10 @@ class ApiUpload extends ApiBase {
                );
        }
 
+       public function getHelpUrls() {
+               return 'http://www.mediawiki.org/wiki/API:Upload';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }