MediaWiki  REL1_22
ApiUpload.php
Go to the documentation of this file.
00001 <?php
00030 class ApiUpload extends ApiBase {
00031 
00035     protected $mUpload = null;
00036 
00037     protected $mParams;
00038 
00039     public function execute() {
00040         global $wgEnableAsyncUploads;
00041 
00042         // Check whether upload is enabled
00043         if ( !UploadBase::isEnabled() ) {
00044             $this->dieUsageMsg( 'uploaddisabled' );
00045         }
00046 
00047         $user = $this->getUser();
00048 
00049         // Parameter handling
00050         $this->mParams = $this->extractRequestParams();
00051         $request = $this->getMain()->getRequest();
00052         // Check if async mode is actually supported (jobs done in cli mode)
00053         $this->mParams['async'] = ( $this->mParams['async'] && $wgEnableAsyncUploads );
00054         // Add the uploaded file to the params array
00055         $this->mParams['file'] = $request->getFileName( 'file' );
00056         $this->mParams['chunk'] = $request->getFileName( 'chunk' );
00057 
00058         // Copy the session key to the file key, for backward compatibility.
00059         if ( !$this->mParams['filekey'] && $this->mParams['sessionkey'] ) {
00060             $this->mParams['filekey'] = $this->mParams['sessionkey'];
00061         }
00062 
00063         // Select an upload module
00064         try {
00065             if ( !$this->selectUploadModule() ) {
00066                 return; // not a true upload, but a status request or similar
00067             } elseif ( !isset( $this->mUpload ) ) {
00068                 $this->dieUsage( 'No upload module set', 'nomodule' );
00069             }
00070         } catch ( UploadStashException $e ) { // XXX: don't spam exception log
00071             $this->dieUsage( get_class( $e ) . ": " . $e->getMessage(), 'stasherror' );
00072         }
00073 
00074         // First check permission to upload
00075         $this->checkPermissions( $user );
00076 
00077         // Fetch the file (usually a no-op)
00079         $status = $this->mUpload->fetchFile();
00080         if ( !$status->isGood() ) {
00081             $errors = $status->getErrorsArray();
00082             $error = array_shift( $errors[0] );
00083             $this->dieUsage( 'Error fetching file from remote source', $error, 0, $errors[0] );
00084         }
00085 
00086         // Check if the uploaded file is sane
00087         if ( $this->mParams['chunk'] ) {
00088             $maxSize = $this->mUpload->getMaxUploadSize();
00089             if ( $this->mParams['filesize'] > $maxSize ) {
00090                 $this->dieUsage( 'The file you submitted was too large', 'file-too-large' );
00091             }
00092             if ( !$this->mUpload->getTitle() ) {
00093                 $this->dieUsage( 'Invalid file title supplied', 'internal-error' );
00094             }
00095         } elseif ( $this->mParams['async'] && $this->mParams['filekey'] ) {
00096             // defer verification to background process
00097         } else {
00098             wfDebug( __METHOD__ . 'about to verify' );
00099             $this->verifyUpload();
00100         }
00101 
00102         // Check if the user has the rights to modify or overwrite the requested title
00103         // (This check is irrelevant if stashing is already requested, since the errors
00104         //  can always be fixed by changing the title)
00105         if ( !$this->mParams['stash'] ) {
00106             $permErrors = $this->mUpload->verifyTitlePermissions( $user );
00107             if ( $permErrors !== true ) {
00108                 $this->dieRecoverableError( $permErrors[0], 'filename' );
00109             }
00110         }
00111 
00112         // Get the result based on the current upload context:
00113         try {
00114             $result = $this->getContextResult();
00115             if ( $result['result'] === 'Success' ) {
00116                 $result['imageinfo'] = $this->mUpload->getImageInfo( $this->getResult() );
00117             }
00118         } catch ( UploadStashException $e ) { // XXX: don't spam exception log
00119             $this->dieUsage( get_class( $e ) . ": " . $e->getMessage(), 'stasherror' );
00120         }
00121 
00122         $this->getResult()->addValue( null, $this->getModuleName(), $result );
00123 
00124         // Cleanup any temporary mess
00125         $this->mUpload->cleanupTempFile();
00126     }
00127 
00132     private function getContextResult() {
00133         $warnings = $this->getApiWarnings();
00134         if ( $warnings && !$this->mParams['ignorewarnings'] ) {
00135             // Get warnings formatted in result array format
00136             return $this->getWarningsResult( $warnings );
00137         } elseif ( $this->mParams['chunk'] ) {
00138             // Add chunk, and get result
00139             return $this->getChunkResult( $warnings );
00140         } elseif ( $this->mParams['stash'] ) {
00141             // Stash the file and get stash result
00142             return $this->getStashResult( $warnings );
00143         }
00144         // This is the most common case -- a normal upload with no warnings
00145         // performUpload will return a formatted properly for the API with status
00146         return $this->performUpload( $warnings );
00147     }
00148 
00154     private function getStashResult( $warnings ) {
00155         $result = array();
00156         // Some uploads can request they be stashed, so as not to publish them immediately.
00157         // In this case, a failure to stash ought to be fatal
00158         try {
00159             $result['result'] = 'Success';
00160             $result['filekey'] = $this->performStash();
00161             $result['sessionkey'] = $result['filekey']; // backwards compatibility
00162             if ( $warnings && count( $warnings ) > 0 ) {
00163                 $result['warnings'] = $warnings;
00164             }
00165         } catch ( MWException $e ) {
00166             $this->dieUsage( $e->getMessage(), 'stashfailed' );
00167         }
00168         return $result;
00169     }
00170 
00176     private function getWarningsResult( $warnings ) {
00177         $result = array();
00178         $result['result'] = 'Warning';
00179         $result['warnings'] = $warnings;
00180         // in case the warnings can be fixed with some further user action, let's stash this upload
00181         // and return a key they can use to restart it
00182         try {
00183             $result['filekey'] = $this->performStash();
00184             $result['sessionkey'] = $result['filekey']; // backwards compatibility
00185         } catch ( MWException $e ) {
00186             $result['warnings']['stashfailed'] = $e->getMessage();
00187         }
00188         return $result;
00189     }
00190 
00196     private function getChunkResult( $warnings ) {
00197         $result = array();
00198 
00199         $result['result'] = 'Continue';
00200         if ( $warnings && count( $warnings ) > 0 ) {
00201             $result['warnings'] = $warnings;
00202         }
00203         $request = $this->getMain()->getRequest();
00204         $chunkPath = $request->getFileTempname( 'chunk' );
00205         $chunkSize = $request->getUpload( 'chunk' )->getSize();
00206         if ( $this->mParams['offset'] == 0 ) {
00207             try {
00208                 $filekey = $this->performStash();
00209             } catch ( MWException $e ) {
00210                 // FIXME: Error handling here is wrong/different from rest of this
00211                 $this->dieUsage( $e->getMessage(), 'stashfailed' );
00212             }
00213         } else {
00214             $filekey = $this->mParams['filekey'];
00216             $status = $this->mUpload->addChunk(
00217                 $chunkPath, $chunkSize, $this->mParams['offset'] );
00218             if ( !$status->isGood() ) {
00219                 $this->dieUsage( $status->getWikiText(), 'stashfailed' );
00220                 return array();
00221             }
00222         }
00223 
00224         // Check we added the last chunk:
00225         if ( $this->mParams['offset'] + $chunkSize == $this->mParams['filesize'] ) {
00226             if ( $this->mParams['async'] ) {
00227                 $progress = UploadBase::getSessionStatus( $filekey );
00228                 if ( $progress && $progress['result'] === 'Poll' ) {
00229                     $this->dieUsage( "Chunk assembly already in progress.", 'stashfailed' );
00230                 }
00231                 UploadBase::setSessionStatus(
00232                     $filekey,
00233                     array( 'result' => 'Poll',
00234                         'stage' => 'queued', 'status' => Status::newGood() )
00235                 );
00236                 $ok = JobQueueGroup::singleton()->push( new AssembleUploadChunksJob(
00237                     Title::makeTitle( NS_FILE, $filekey ),
00238                     array(
00239                         'filename' => $this->mParams['filename'],
00240                         'filekey' => $filekey,
00241                         'session' => $this->getContext()->exportSession()
00242                     )
00243                 ) );
00244                 if ( $ok ) {
00245                     $result['result'] = 'Poll';
00246                 } else {
00247                     UploadBase::setSessionStatus( $filekey, false );
00248                     $this->dieUsage(
00249                         "Failed to start AssembleUploadChunks.php", 'stashfailed' );
00250                 }
00251             } else {
00252                 $status = $this->mUpload->concatenateChunks();
00253                 if ( !$status->isGood() ) {
00254                     $this->dieUsage( $status->getWikiText(), 'stashfailed' );
00255                     return array();
00256                 }
00257 
00258                 // The fully concatenated file has a new filekey. So remove
00259                 // the old filekey and fetch the new one.
00260                 $this->mUpload->stash->removeFile( $filekey );
00261                 $filekey = $this->mUpload->getLocalFile()->getFileKey();
00262 
00263                 $result['result'] = 'Success';
00264             }
00265         }
00266         $result['filekey'] = $filekey;
00267         $result['offset'] = $this->mParams['offset'] + $chunkSize;
00268         return $result;
00269     }
00270 
00277     private function performStash() {
00278         try {
00279             $stashFile = $this->mUpload->stashFile();
00280 
00281             if ( !$stashFile ) {
00282                 throw new MWException( 'Invalid stashed file' );
00283             }
00284             $fileKey = $stashFile->getFileKey();
00285         } catch ( MWException $e ) {
00286             $message = 'Stashing temporary file failed: ' . get_class( $e ) . ' ' . $e->getMessage();
00287             wfDebug( __METHOD__ . ' ' . $message . "\n" );
00288             throw new MWException( $message );
00289         }
00290         return $fileKey;
00291     }
00292 
00302     private function dieRecoverableError( $error, $parameter, $data = array() ) {
00303         try {
00304             $data['filekey'] = $this->performStash();
00305             $data['sessionkey'] = $data['filekey'];
00306         } catch ( MWException $e ) {
00307             $data['stashfailed'] = $e->getMessage();
00308         }
00309         $data['invalidparameter'] = $parameter;
00310 
00311         $parsed = $this->parseMsg( $error );
00312         $this->dieUsage( $parsed['info'], $parsed['code'], 0, $data );
00313     }
00314 
00322     protected function selectUploadModule() {
00323         $request = $this->getMain()->getRequest();
00324 
00325         // chunk or one and only one of the following parameters is needed
00326         if ( !$this->mParams['chunk'] ) {
00327             $this->requireOnlyOneParameter( $this->mParams,
00328                 'filekey', 'file', 'url', 'statuskey' );
00329         }
00330 
00331         // Status report for "upload to stash"/"upload from stash"
00332         if ( $this->mParams['filekey'] && $this->mParams['checkstatus'] ) {
00333             $progress = UploadBase::getSessionStatus( $this->mParams['filekey'] );
00334             if ( !$progress ) {
00335                 $this->dieUsage( 'No result in status data', 'missingresult' );
00336             } elseif ( !$progress['status']->isGood() ) {
00337                 $this->dieUsage( $progress['status']->getWikiText(), 'stashfailed' );
00338             }
00339             if ( isset( $progress['status']->value['verification'] ) ) {
00340                 $this->checkVerification( $progress['status']->value['verification'] );
00341             }
00342             unset( $progress['status'] ); // remove Status object
00343             $this->getResult()->addValue( null, $this->getModuleName(), $progress );
00344             return false;
00345         }
00346 
00347         if ( $this->mParams['statuskey'] ) {
00348             $this->checkAsyncDownloadEnabled();
00349 
00350             // Status request for an async upload
00351             $sessionData = UploadFromUrlJob::getSessionData( $this->mParams['statuskey'] );
00352             if ( !isset( $sessionData['result'] ) ) {
00353                 $this->dieUsage( 'No result in session data', 'missingresult' );
00354             }
00355             if ( $sessionData['result'] == 'Warning' ) {
00356                 $sessionData['warnings'] = $this->transformWarnings( $sessionData['warnings'] );
00357                 $sessionData['sessionkey'] = $this->mParams['statuskey'];
00358             }
00359             $this->getResult()->addValue( null, $this->getModuleName(), $sessionData );
00360             return false;
00361         }
00362 
00363         // The following modules all require the filename parameter to be set
00364         if ( is_null( $this->mParams['filename'] ) ) {
00365             $this->dieUsageMsg( array( 'missingparam', 'filename' ) );
00366         }
00367 
00368         if ( $this->mParams['chunk'] ) {
00369             // Chunk upload
00370             $this->mUpload = new UploadFromChunks();
00371             if ( isset( $this->mParams['filekey'] ) ) {
00372                 // handle new chunk
00373                 $this->mUpload->continueChunks(
00374                     $this->mParams['filename'],
00375                     $this->mParams['filekey'],
00376                     $request->getUpload( 'chunk' )
00377                 );
00378             } else {
00379                 // handle first chunk
00380                 $this->mUpload->initialize(
00381                     $this->mParams['filename'],
00382                     $request->getUpload( 'chunk' )
00383                 );
00384             }
00385         } elseif ( isset( $this->mParams['filekey'] ) ) {
00386             // Upload stashed in a previous request
00387             if ( !UploadFromStash::isValidKey( $this->mParams['filekey'] ) ) {
00388                 $this->dieUsageMsg( 'invalid-file-key' );
00389             }
00390 
00391             $this->mUpload = new UploadFromStash( $this->getUser() );
00392             // This will not download the temp file in initialize() in async mode.
00393             // We still have enough information to call checkWarnings() and such.
00394             $this->mUpload->initialize(
00395                 $this->mParams['filekey'], $this->mParams['filename'], !$this->mParams['async']
00396             );
00397         } elseif ( isset( $this->mParams['file'] ) ) {
00398             $this->mUpload = new UploadFromFile();
00399             $this->mUpload->initialize(
00400                 $this->mParams['filename'],
00401                 $request->getUpload( 'file' )
00402             );
00403         } elseif ( isset( $this->mParams['url'] ) ) {
00404             // Make sure upload by URL is enabled:
00405             if ( !UploadFromUrl::isEnabled() ) {
00406                 $this->dieUsageMsg( 'copyuploaddisabled' );
00407             }
00408 
00409             if ( !UploadFromUrl::isAllowedHost( $this->mParams['url'] ) ) {
00410                 $this->dieUsageMsg( 'copyuploadbaddomain' );
00411             }
00412 
00413             if ( !UploadFromUrl::isAllowedUrl( $this->mParams['url'] ) ) {
00414                 $this->dieUsageMsg( 'copyuploadbadurl' );
00415             }
00416 
00417             $async = false;
00418             if ( $this->mParams['asyncdownload'] ) {
00419                 $this->checkAsyncDownloadEnabled();
00420 
00421                 if ( $this->mParams['leavemessage'] && !$this->mParams['ignorewarnings'] ) {
00422                     $this->dieUsage( 'Using leavemessage without ignorewarnings is not supported',
00423                         'missing-ignorewarnings' );
00424                 }
00425 
00426                 if ( $this->mParams['leavemessage'] ) {
00427                     $async = 'async-leavemessage';
00428                 } else {
00429                     $async = 'async';
00430                 }
00431             }
00432             $this->mUpload = new UploadFromUrl;
00433             $this->mUpload->initialize( $this->mParams['filename'],
00434                 $this->mParams['url'], $async );
00435         }
00436 
00437         return true;
00438     }
00439 
00445     protected function checkPermissions( $user ) {
00446         // Check whether the user has the appropriate permissions to upload anyway
00447         $permission = $this->mUpload->isAllowed( $user );
00448 
00449         if ( $permission !== true ) {
00450             if ( !$user->isLoggedIn() ) {
00451                 $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) );
00452             } else {
00453                 $this->dieUsageMsg( 'badaccess-groups' );
00454             }
00455         }
00456     }
00457 
00461     protected function verifyUpload() {
00462         $verification = $this->mUpload->verifyUpload();
00463         if ( $verification['status'] === UploadBase::OK ) {
00464             return;
00465         }
00466 
00467         $this->checkVerification( $verification );
00468     }
00469 
00473     protected function checkVerification( array $verification ) {
00474         global $wgFileExtensions;
00475 
00476         // @todo Move them to ApiBase's message map
00477         switch ( $verification['status'] ) {
00478             // Recoverable errors
00479             case UploadBase::MIN_LENGTH_PARTNAME:
00480                 $this->dieRecoverableError( 'filename-tooshort', 'filename' );
00481                 break;
00482             case UploadBase::ILLEGAL_FILENAME:
00483                 $this->dieRecoverableError( 'illegal-filename', 'filename',
00484                         array( 'filename' => $verification['filtered'] ) );
00485                 break;
00486             case UploadBase::FILENAME_TOO_LONG:
00487                 $this->dieRecoverableError( 'filename-toolong', 'filename' );
00488                 break;
00489             case UploadBase::FILETYPE_MISSING:
00490                 $this->dieRecoverableError( 'filetype-missing', 'filename' );
00491                 break;
00492             case UploadBase::WINDOWS_NONASCII_FILENAME:
00493                 $this->dieRecoverableError( 'windows-nonascii-filename', 'filename' );
00494                 break;
00495 
00496             // Unrecoverable errors
00497             case UploadBase::EMPTY_FILE:
00498                 $this->dieUsage( 'The file you submitted was empty', 'empty-file' );
00499                 break;
00500             case UploadBase::FILE_TOO_LARGE:
00501                 $this->dieUsage( 'The file you submitted was too large', 'file-too-large' );
00502                 break;
00503 
00504             case UploadBase::FILETYPE_BADTYPE:
00505                 $extradata = array(
00506                     'filetype' => $verification['finalExt'],
00507                     'allowed' => array_values( array_unique( $wgFileExtensions ) )
00508                 );
00509                 $this->getResult()->setIndexedTagName( $extradata['allowed'], 'ext' );
00510 
00511                 $msg = "Filetype not permitted: ";
00512                 if ( isset( $verification['blacklistedExt'] ) ) {
00513                     $msg .= join( ', ', $verification['blacklistedExt'] );
00514                     $extradata['blacklisted'] = array_values( $verification['blacklistedExt'] );
00515                     $this->getResult()->setIndexedTagName( $extradata['blacklisted'], 'ext' );
00516                 } else {
00517                     $msg .= $verification['finalExt'];
00518                 }
00519                 $this->dieUsage( $msg, 'filetype-banned', 0, $extradata );
00520                 break;
00521             case UploadBase::VERIFICATION_ERROR:
00522                 $this->getResult()->setIndexedTagName( $verification['details'], 'detail' );
00523                 $this->dieUsage( 'This file did not pass file verification', 'verification-error',
00524                         0, array( 'details' => $verification['details'] ) );
00525                 break;
00526             case UploadBase::HOOK_ABORTED:
00527                 $this->dieUsage( "The modification you tried to make was aborted by an extension hook",
00528                         'hookaborted', 0, array( 'error' => $verification['error'] ) );
00529                 break;
00530             default:
00531                 $this->dieUsage( 'An unknown error occurred', 'unknown-error',
00532                         0, array( 'code' => $verification['status'] ) );
00533                 break;
00534         }
00535     }
00536 
00544     protected function getApiWarnings() {
00545         $warnings = $this->mUpload->checkWarnings();
00546 
00547         return $this->transformWarnings( $warnings );
00548     }
00549 
00550     protected function transformWarnings( $warnings ) {
00551         if ( $warnings ) {
00552             // Add indices
00553             $result = $this->getResult();
00554             $result->setIndexedTagName( $warnings, 'warning' );
00555 
00556             if ( isset( $warnings['duplicate'] ) ) {
00557                 $dupes = array();
00558                 foreach ( $warnings['duplicate'] as $dupe ) {
00559                     $dupes[] = $dupe->getName();
00560                 }
00561                 $result->setIndexedTagName( $dupes, 'duplicate' );
00562                 $warnings['duplicate'] = $dupes;
00563             }
00564 
00565             if ( isset( $warnings['exists'] ) ) {
00566                 $warning = $warnings['exists'];
00567                 unset( $warnings['exists'] );
00568                 $localFile = isset( $warning['normalizedFile'] ) ? $warning['normalizedFile'] : $warning['file'];
00569                 $warnings[$warning['warning']] = $localFile->getName();
00570             }
00571         }
00572         return $warnings;
00573     }
00574 
00582     protected function performUpload( $warnings ) {
00583         // Use comment as initial page text by default
00584         if ( is_null( $this->mParams['text'] ) ) {
00585             $this->mParams['text'] = $this->mParams['comment'];
00586         }
00587 
00589         $file = $this->mUpload->getLocalFile();
00590         $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() );
00591 
00592         // Deprecated parameters
00593         if ( $this->mParams['watch'] ) {
00594             $watch = true;
00595         }
00596 
00597         // No errors, no warnings: do the upload
00598         if ( $this->mParams['async'] ) {
00599             $progress = UploadBase::getSessionStatus( $this->mParams['filekey'] );
00600             if ( $progress && $progress['result'] === 'Poll' ) {
00601                 $this->dieUsage( "Upload from stash already in progress.", 'publishfailed' );
00602             }
00603             UploadBase::setSessionStatus(
00604                 $this->mParams['filekey'],
00605                 array( 'result' => 'Poll', 'stage' => 'queued', 'status' => Status::newGood() )
00606             );
00607             $ok = JobQueueGroup::singleton()->push( new PublishStashedFileJob(
00608                 Title::makeTitle( NS_FILE, $this->mParams['filename'] ),
00609                 array(
00610                     'filename' => $this->mParams['filename'],
00611                     'filekey' => $this->mParams['filekey'],
00612                     'comment' => $this->mParams['comment'],
00613                     'text' => $this->mParams['text'],
00614                     'watch' => $watch,
00615                     'session' => $this->getContext()->exportSession()
00616                 )
00617             ) );
00618             if ( $ok ) {
00619                 $result['result'] = 'Poll';
00620             } else {
00621                 UploadBase::setSessionStatus( $this->mParams['filekey'], false );
00622                 $this->dieUsage(
00623                     "Failed to start PublishStashedFile.php", 'publishfailed' );
00624             }
00625         } else {
00627             $status = $this->mUpload->performUpload( $this->mParams['comment'],
00628                 $this->mParams['text'], $watch, $this->getUser() );
00629 
00630             if ( !$status->isGood() ) {
00631                 $error = $status->getErrorsArray();
00632 
00633                 if ( count( $error ) == 1 && $error[0][0] == 'async' ) {
00634                     // The upload can not be performed right now, because the user
00635                     // requested so
00636                     return array(
00637                         'result' => 'Queued',
00638                         'statuskey' => $error[0][1],
00639                     );
00640                 } else {
00641                     $this->getResult()->setIndexedTagName( $error, 'error' );
00642 
00643                     $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error );
00644                 }
00645             }
00646             $result['result'] = 'Success';
00647         }
00648 
00649         $result['filename'] = $file->getName();
00650         if ( $warnings && count( $warnings ) > 0 ) {
00651             $result['warnings'] = $warnings;
00652         }
00653 
00654         return $result;
00655     }
00656 
00660     protected function checkAsyncDownloadEnabled() {
00661         global $wgAllowAsyncCopyUploads;
00662         if ( !$wgAllowAsyncCopyUploads ) {
00663             $this->dieUsage( 'Asynchronous copy uploads disabled', 'asynccopyuploaddisabled' );
00664         }
00665     }
00666 
00667     public function mustBePosted() {
00668         return true;
00669     }
00670 
00671     public function isWriteMode() {
00672         return true;
00673     }
00674 
00675     public function getAllowedParams() {
00676         $params = array(
00677             'filename' => array(
00678                 ApiBase::PARAM_TYPE => 'string',
00679             ),
00680             'comment' => array(
00681                 ApiBase::PARAM_DFLT => ''
00682             ),
00683             'text' => null,
00684             'token' => array(
00685                 ApiBase::PARAM_TYPE => 'string',
00686                 ApiBase::PARAM_REQUIRED => true
00687             ),
00688             'watch' => array(
00689                 ApiBase::PARAM_DFLT => false,
00690                 ApiBase::PARAM_DEPRECATED => true,
00691             ),
00692             'watchlist' => array(
00693                 ApiBase::PARAM_DFLT => 'preferences',
00694                 ApiBase::PARAM_TYPE => array(
00695                     'watch',
00696                     'preferences',
00697                     'nochange'
00698                 ),
00699             ),
00700             'ignorewarnings' => false,
00701             'file' => array(
00702                 ApiBase::PARAM_TYPE => 'upload',
00703             ),
00704             'url' => null,
00705             'filekey' => null,
00706             'sessionkey' => array(
00707                 ApiBase::PARAM_DFLT => null,
00708                 ApiBase::PARAM_DEPRECATED => true,
00709             ),
00710             'stash' => false,
00711 
00712             'filesize' => null,
00713             'offset' => null,
00714             'chunk' => array(
00715                 ApiBase::PARAM_TYPE => 'upload',
00716             ),
00717 
00718             'async' => false,
00719             'asyncdownload' => false,
00720             'leavemessage' => false,
00721             'statuskey' => null,
00722             'checkstatus' => false,
00723         );
00724 
00725         return $params;
00726     }
00727 
00728     public function getParamDescription() {
00729         $params = array(
00730             'filename' => 'Target filename',
00731             'token' => 'Edit token. You can get one of these through prop=info',
00732             'comment' => 'Upload comment. Also used as the initial page text for new files if "text" is not specified',
00733             'text' => 'Initial page text for new files',
00734             'watch' => 'Watch the page',
00735             'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
00736             'ignorewarnings' => 'Ignore any warnings',
00737             'file' => 'File contents',
00738             'url' => 'URL to fetch the file from',
00739             'filekey' => 'Key that identifies a previous upload that was stashed temporarily.',
00740             'sessionkey' => 'Same as filekey, maintained for backward compatibility.',
00741             'stash' => 'If set, the server will not add the file to the repository and stash it temporarily.',
00742 
00743             'chunk' => 'Chunk contents',
00744             'offset' => 'Offset of chunk in bytes',
00745             'filesize' => 'Filesize of entire upload',
00746 
00747             'async' => 'Make potentially large file operations asynchronous when possible',
00748             'asyncdownload' => 'Make fetching a URL asynchronous',
00749             'leavemessage' => 'If asyncdownload is used, leave a message on the user talk page if finished',
00750             'statuskey' => 'Fetch the upload status for this file key (upload by URL)',
00751             'checkstatus' => 'Only fetch the upload status for the given file key',
00752         );
00753 
00754         return $params;
00755 
00756     }
00757 
00758     public function getResultProperties() {
00759         return array(
00760             '' => array(
00761                 'result' => array(
00762                     ApiBase::PROP_TYPE => array(
00763                         'Success',
00764                         'Warning',
00765                         'Continue',
00766                         'Queued'
00767                     ),
00768                 ),
00769                 'filekey' => array(
00770                     ApiBase::PROP_TYPE => 'string',
00771                     ApiBase::PROP_NULLABLE => true
00772                 ),
00773                 'sessionkey' => array(
00774                     ApiBase::PROP_TYPE => 'string',
00775                     ApiBase::PROP_NULLABLE => true
00776                 ),
00777                 'offset' => array(
00778                     ApiBase::PROP_TYPE => 'integer',
00779                     ApiBase::PROP_NULLABLE => true
00780                 ),
00781                 'statuskey' => array(
00782                     ApiBase::PROP_TYPE => 'string',
00783                     ApiBase::PROP_NULLABLE => true
00784                 ),
00785                 'filename' => array(
00786                     ApiBase::PROP_TYPE => 'string',
00787                     ApiBase::PROP_NULLABLE => true
00788                 )
00789             )
00790         );
00791     }
00792 
00793     public function getDescription() {
00794         return array(
00795             'Upload a file, or get the status of pending uploads. Several methods are available:',
00796             ' * Upload file contents directly, using the "file" parameter',
00797             ' * Have the MediaWiki server fetch a file from a URL, using the "url" parameter',
00798             ' * Complete an earlier upload that failed due to warnings, using the "filekey" parameter',
00799             'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
00800             'sending the "file". Also you must get and send an edit token before doing any upload stuff'
00801         );
00802     }
00803 
00804     public function getPossibleErrors() {
00805         return array_merge( parent::getPossibleErrors(),
00806             $this->getRequireOnlyOneParameterErrorMessages( array( 'filekey', 'file', 'url', 'statuskey' ) ),
00807             array(
00808                 array( 'uploaddisabled' ),
00809                 array( 'invalid-file-key' ),
00810                 array( 'uploaddisabled' ),
00811                 array( 'mustbeloggedin', 'upload' ),
00812                 array( 'badaccess-groups' ),
00813                 array( 'code' => 'fetchfileerror', 'info' => '' ),
00814                 array( 'code' => 'nomodule', 'info' => 'No upload module set' ),
00815                 array( 'code' => 'empty-file', 'info' => 'The file you submitted was empty' ),
00816                 array( 'code' => 'filetype-missing', 'info' => 'The file is missing an extension' ),
00817                 array( 'code' => 'filename-tooshort', 'info' => 'The filename is too short' ),
00818                 array( 'code' => 'overwrite', 'info' => 'Overwriting an existing file is not allowed' ),
00819                 array( 'code' => 'stashfailed', 'info' => 'Stashing temporary file failed' ),
00820                 array( 'code' => 'publishfailed', 'info' => 'Publishing of stashed file failed' ),
00821                 array( 'code' => 'internal-error', 'info' => 'An internal error occurred' ),
00822                 array( 'code' => 'asynccopyuploaddisabled', 'info' => 'Asynchronous copy uploads disabled' ),
00823                 array( 'code' => 'stasherror', 'info' => 'An upload stash error occurred' ),
00824                 array( 'fileexists-forbidden' ),
00825                 array( 'fileexists-shared-forbidden' ),
00826             )
00827         );
00828     }
00829 
00830     public function needsToken() {
00831         return true;
00832     }
00833 
00834     public function getTokenSalt() {
00835         return '';
00836     }
00837 
00838     public function getExamples() {
00839         return array(
00840             'api.php?action=upload&filename=Wiki.png&url=http%3A//upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png'
00841                 => 'Upload from a URL',
00842             'api.php?action=upload&filename=Wiki.png&filekey=filekey&ignorewarnings=1'
00843                 => 'Complete an upload that failed due to warnings',
00844         );
00845     }
00846 
00847     public function getHelpUrls() {
00848         return 'https://www.mediawiki.org/wiki/API:Upload';
00849     }
00850 }