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