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