Merge "Rewrite ORMTable::unprefixFieldNames"
[lhc/web/wiklou.git] / includes / api / ApiUpload.php
1 <?php
2 /**
3 *
4 *
5 * Created on Aug 21, 2008
6 *
7 * Copyright © 2008 - 2010 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * @ingroup API
29 */
30 class ApiUpload extends ApiBase {
31 /** @var UploadBase|UploadFromChunks */
32 protected $mUpload = null;
33
34 protected $mParams;
35
36 public function execute() {
37 // Check whether upload is enabled
38 if ( !UploadBase::isEnabled() ) {
39 $this->dieUsageMsg( 'uploaddisabled' );
40 }
41
42 $user = $this->getUser();
43
44 // Parameter handling
45 $this->mParams = $this->extractRequestParams();
46 $request = $this->getMain()->getRequest();
47 // Check if async mode is actually supported (jobs done in cli mode)
48 $this->mParams['async'] = ( $this->mParams['async'] && $this->getConfig()->get( 'EnableAsyncUploads' ) );
49 // Add the uploaded file to the params array
50 $this->mParams['file'] = $request->getFileName( 'file' );
51 $this->mParams['chunk'] = $request->getFileName( 'chunk' );
52
53 // Copy the session key to the file key, for backward compatibility.
54 if ( !$this->mParams['filekey'] && $this->mParams['sessionkey'] ) {
55 $this->logFeatureUsage( 'action=upload&sessionkey' );
56 $this->mParams['filekey'] = $this->mParams['sessionkey'];
57 }
58
59 // Select an upload module
60 try {
61 if ( !$this->selectUploadModule() ) {
62 return; // not a true upload, but a status request or similar
63 } elseif ( !isset( $this->mUpload ) ) {
64 $this->dieUsage( 'No upload module set', 'nomodule' );
65 }
66 } catch ( UploadStashException $e ) { // XXX: don't spam exception log
67 $this->dieUsage( get_class( $e ) . ": " . $e->getMessage(), 'stasherror' );
68 }
69
70 // First check permission to upload
71 $this->checkPermissions( $user );
72
73 // Fetch the file (usually a no-op)
74 /** @var $status Status */
75 $status = $this->mUpload->fetchFile();
76 if ( !$status->isGood() ) {
77 $errors = $status->getErrorsArray();
78 $error = array_shift( $errors[0] );
79 $this->dieUsage( 'Error fetching file from remote source', $error, 0, $errors[0] );
80 }
81
82 // Check if the uploaded file is sane
83 if ( $this->mParams['chunk'] ) {
84 $maxSize = $this->mUpload->getMaxUploadSize();
85 if ( $this->mParams['filesize'] > $maxSize ) {
86 $this->dieUsage( 'The file you submitted was too large', 'file-too-large' );
87 }
88 if ( !$this->mUpload->getTitle() ) {
89 $this->dieUsage( 'Invalid file title supplied', 'internal-error' );
90 }
91 } elseif ( $this->mParams['async'] && $this->mParams['filekey'] ) {
92 // defer verification to background process
93 } else {
94 wfDebug( __METHOD__ . " about to verify\n" );
95 $this->verifyUpload();
96 }
97
98 // Check if the user has the rights to modify or overwrite the requested title
99 // (This check is irrelevant if stashing is already requested, since the errors
100 // can always be fixed by changing the title)
101 if ( !$this->mParams['stash'] ) {
102 $permErrors = $this->mUpload->verifyTitlePermissions( $user );
103 if ( $permErrors !== true ) {
104 $this->dieRecoverableError( $permErrors[0], 'filename' );
105 }
106 }
107
108 // Get the result based on the current upload context:
109 try {
110 $result = $this->getContextResult();
111 if ( $result['result'] === 'Success' ) {
112 $result['imageinfo'] = $this->mUpload->getImageInfo( $this->getResult() );
113 }
114 } catch ( UploadStashException $e ) { // XXX: don't spam exception log
115 $this->dieUsage( get_class( $e ) . ": " . $e->getMessage(), 'stasherror' );
116 }
117
118 $this->getResult()->addValue( null, $this->getModuleName(), $result );
119
120 // Cleanup any temporary mess
121 $this->mUpload->cleanupTempFile();
122 }
123
124 /**
125 * Get an upload result based on upload context
126 * @return array
127 */
128 private function getContextResult() {
129 $warnings = $this->getApiWarnings();
130 if ( $warnings && !$this->mParams['ignorewarnings'] ) {
131 // Get warnings formatted in result array format
132 return $this->getWarningsResult( $warnings );
133 } elseif ( $this->mParams['chunk'] ) {
134 // Add chunk, and get result
135 return $this->getChunkResult( $warnings );
136 } elseif ( $this->mParams['stash'] ) {
137 // Stash the file and get stash result
138 return $this->getStashResult( $warnings );
139 }
140
141 // This is the most common case -- a normal upload with no warnings
142 // performUpload will return a formatted properly for the API with status
143 return $this->performUpload( $warnings );
144 }
145
146 /**
147 * Get Stash Result, throws an exception if the file could not be stashed.
148 * @param array $warnings Array of Api upload warnings
149 * @return array
150 */
151 private function getStashResult( $warnings ) {
152 $result = array();
153 // Some uploads can request they be stashed, so as not to publish them immediately.
154 // In this case, a failure to stash ought to be fatal
155 try {
156 $result['result'] = 'Success';
157 $result['filekey'] = $this->performStash();
158 $result['sessionkey'] = $result['filekey']; // backwards compatibility
159 if ( $warnings && count( $warnings ) > 0 ) {
160 $result['warnings'] = $warnings;
161 }
162 } catch ( MWException $e ) {
163 $this->dieUsage( $e->getMessage(), 'stashfailed' );
164 }
165
166 return $result;
167 }
168
169 /**
170 * Get Warnings Result
171 * @param array $warnings Array of Api upload warnings
172 * @return array
173 */
174 private function getWarningsResult( $warnings ) {
175 $result = array();
176 $result['result'] = 'Warning';
177 $result['warnings'] = $warnings;
178 // in case the warnings can be fixed with some further user action, let's stash this upload
179 // and return a key they can use to restart it
180 try {
181 $result['filekey'] = $this->performStash();
182 $result['sessionkey'] = $result['filekey']; // backwards compatibility
183 } catch ( MWException $e ) {
184 $result['warnings']['stashfailed'] = $e->getMessage();
185 }
186
187 return $result;
188 }
189
190 /**
191 * Get the result of a chunk upload.
192 * @param array $warnings Array of Api upload warnings
193 * @return array
194 */
195 private function getChunkResult( $warnings ) {
196 $result = array();
197
198 $result['result'] = 'Continue';
199 if ( $warnings && count( $warnings ) > 0 ) {
200 $result['warnings'] = $warnings;
201 }
202 $request = $this->getMain()->getRequest();
203 $chunkPath = $request->getFileTempname( 'chunk' );
204 $chunkSize = $request->getUpload( 'chunk' )->getSize();
205 if ( $this->mParams['offset'] == 0 ) {
206 try {
207 $filekey = $this->performStash();
208 } catch ( MWException $e ) {
209 // FIXME: Error handling here is wrong/different from rest of this
210 $this->dieUsage( $e->getMessage(), 'stashfailed' );
211 }
212 } else {
213 $filekey = $this->mParams['filekey'];
214 $status = $this->mUpload->addChunk(
215 $chunkPath, $chunkSize, $this->mParams['offset'] );
216 if ( !$status->isGood() ) {
217 $this->dieUsage( $status->getWikiText(), 'stashfailed' );
218
219 return array();
220 }
221 }
222
223 // Check we added the last chunk:
224 if ( $this->mParams['offset'] + $chunkSize == $this->mParams['filesize'] ) {
225 if ( $this->mParams['async'] ) {
226 $progress = UploadBase::getSessionStatus( $this->getUser(), $filekey );
227 if ( $progress && $progress['result'] === 'Poll' ) {
228 $this->dieUsage( "Chunk assembly already in progress.", 'stashfailed' );
229 }
230 UploadBase::setSessionStatus(
231 $this->getUser(),
232 $filekey,
233 array( 'result' => 'Poll',
234 'stage' => 'queued', 'status' => Status::newGood() )
235 );
236 JobQueueGroup::singleton()->push( new AssembleUploadChunksJob(
237 Title::makeTitle( NS_FILE, $filekey ),
238 array(
239 'filename' => $this->mParams['filename'],
240 'filekey' => $filekey,
241 'session' => $this->getContext()->exportSession()
242 )
243 ) );
244 $result['result'] = 'Poll';
245 $result['stage'] = 'queued';
246 } else {
247 $status = $this->mUpload->concatenateChunks();
248 if ( !$status->isGood() ) {
249 $this->dieUsage( $status->getWikiText(), 'stashfailed' );
250
251 return array();
252 }
253
254 // The fully concatenated file has a new filekey. So remove
255 // the old filekey and fetch the new one.
256 $this->mUpload->stash->removeFile( $filekey );
257 $filekey = $this->mUpload->getLocalFile()->getFileKey();
258
259 $result['result'] = 'Success';
260 }
261 }
262 $result['filekey'] = $filekey;
263 $result['offset'] = $this->mParams['offset'] + $chunkSize;
264
265 return $result;
266 }
267
268 /**
269 * Stash the file and return the file key
270 * Also re-raises exceptions with slightly more informative message strings (useful for API)
271 * @throws MWException
272 * @return string File key
273 */
274 private function performStash() {
275 try {
276 $stashFile = $this->mUpload->stashFile();
277
278 if ( !$stashFile ) {
279 throw new MWException( 'Invalid stashed file' );
280 }
281 $fileKey = $stashFile->getFileKey();
282 } catch ( MWException $e ) {
283 $message = 'Stashing temporary file failed: ' . get_class( $e ) . ' ' . $e->getMessage();
284 wfDebug( __METHOD__ . ' ' . $message . "\n" );
285 throw new MWException( $message );
286 }
287
288 return $fileKey;
289 }
290
291 /**
292 * Throw an error that the user can recover from by providing a better
293 * value for $parameter
294 *
295 * @param array $error Error array suitable for passing to dieUsageMsg()
296 * @param string $parameter Parameter that needs revising
297 * @param array $data Optional extra data to pass to the user
298 * @throws UsageException
299 */
300 private function dieRecoverableError( $error, $parameter, $data = array() ) {
301 try {
302 $data['filekey'] = $this->performStash();
303 $data['sessionkey'] = $data['filekey'];
304 } catch ( MWException $e ) {
305 $data['stashfailed'] = $e->getMessage();
306 }
307 $data['invalidparameter'] = $parameter;
308
309 $parsed = $this->parseMsg( $error );
310 $this->dieUsage( $parsed['info'], $parsed['code'], 0, $data );
311 }
312
313 /**
314 * Select an upload module and set it to mUpload. Dies on failure. If the
315 * request was a status request and not a true upload, returns false;
316 * otherwise true
317 *
318 * @return bool
319 */
320 protected function selectUploadModule() {
321 $request = $this->getMain()->getRequest();
322
323 // chunk or one and only one of the following parameters is needed
324 if ( !$this->mParams['chunk'] ) {
325 $this->requireOnlyOneParameter( $this->mParams,
326 'filekey', 'file', 'url', 'statuskey' );
327 }
328
329 // Status report for "upload to stash"/"upload from stash"
330 if ( $this->mParams['filekey'] && $this->mParams['checkstatus'] ) {
331 $progress = UploadBase::getSessionStatus( $this->getUser(), $this->mParams['filekey'] );
332 if ( !$progress ) {
333 $this->dieUsage( 'No result in status data', 'missingresult' );
334 } elseif ( !$progress['status']->isGood() ) {
335 $this->dieUsage( $progress['status']->getWikiText(), 'stashfailed' );
336 }
337 if ( isset( $progress['status']->value['verification'] ) ) {
338 $this->checkVerification( $progress['status']->value['verification'] );
339 }
340 unset( $progress['status'] ); // remove Status object
341 $this->getResult()->addValue( null, $this->getModuleName(), $progress );
342
343 return false;
344 }
345
346 if ( $this->mParams['statuskey'] ) {
347 $this->checkAsyncDownloadEnabled();
348
349 // Status request for an async upload
350 $sessionData = UploadFromUrlJob::getSessionData( $this->mParams['statuskey'] );
351 if ( !isset( $sessionData['result'] ) ) {
352 $this->dieUsage( 'No result in session data', 'missingresult' );
353 }
354 if ( $sessionData['result'] == 'Warning' ) {
355 $sessionData['warnings'] = $this->transformWarnings( $sessionData['warnings'] );
356 $sessionData['sessionkey'] = $this->mParams['statuskey'];
357 }
358 $this->getResult()->addValue( null, $this->getModuleName(), $sessionData );
359
360 return false;
361 }
362
363 // The following modules all require the filename parameter to be set
364 if ( is_null( $this->mParams['filename'] ) ) {
365 $this->dieUsageMsg( array( 'missingparam', 'filename' ) );
366 }
367
368 if ( $this->mParams['chunk'] ) {
369 // Chunk upload
370 $this->mUpload = new UploadFromChunks();
371 if ( isset( $this->mParams['filekey'] ) ) {
372 // handle new chunk
373 $this->mUpload->continueChunks(
374 $this->mParams['filename'],
375 $this->mParams['filekey'],
376 $request->getUpload( 'chunk' )
377 );
378 } else {
379 // handle first chunk
380 $this->mUpload->initialize(
381 $this->mParams['filename'],
382 $request->getUpload( 'chunk' )
383 );
384 }
385 } elseif ( isset( $this->mParams['filekey'] ) ) {
386 // Upload stashed in a previous request
387 if ( !UploadFromStash::isValidKey( $this->mParams['filekey'] ) ) {
388 $this->dieUsageMsg( 'invalid-file-key' );
389 }
390
391 $this->mUpload = new UploadFromStash( $this->getUser() );
392 // This will not download the temp file in initialize() in async mode.
393 // We still have enough information to call checkWarnings() and such.
394 $this->mUpload->initialize(
395 $this->mParams['filekey'], $this->mParams['filename'], !$this->mParams['async']
396 );
397 } elseif ( isset( $this->mParams['file'] ) ) {
398 $this->mUpload = new UploadFromFile();
399 $this->mUpload->initialize(
400 $this->mParams['filename'],
401 $request->getUpload( 'file' )
402 );
403 } elseif ( isset( $this->mParams['url'] ) ) {
404 // Make sure upload by URL is enabled:
405 if ( !UploadFromUrl::isEnabled() ) {
406 $this->dieUsageMsg( 'copyuploaddisabled' );
407 }
408
409 if ( !UploadFromUrl::isAllowedHost( $this->mParams['url'] ) ) {
410 $this->dieUsageMsg( 'copyuploadbaddomain' );
411 }
412
413 if ( !UploadFromUrl::isAllowedUrl( $this->mParams['url'] ) ) {
414 $this->dieUsageMsg( 'copyuploadbadurl' );
415 }
416
417 $async = false;
418 if ( $this->mParams['asyncdownload'] ) {
419 $this->checkAsyncDownloadEnabled();
420
421 if ( $this->mParams['leavemessage'] && !$this->mParams['ignorewarnings'] ) {
422 $this->dieUsage( 'Using leavemessage without ignorewarnings is not supported',
423 'missing-ignorewarnings' );
424 }
425
426 if ( $this->mParams['leavemessage'] ) {
427 $async = 'async-leavemessage';
428 } else {
429 $async = 'async';
430 }
431 }
432 $this->mUpload = new UploadFromUrl;
433 $this->mUpload->initialize( $this->mParams['filename'],
434 $this->mParams['url'], $async );
435 }
436
437 return true;
438 }
439
440 /**
441 * Checks that the user has permissions to perform this upload.
442 * Dies with usage message on inadequate permissions.
443 * @param User $user The user to check.
444 */
445 protected function checkPermissions( $user ) {
446 // Check whether the user has the appropriate permissions to upload anyway
447 $permission = $this->mUpload->isAllowed( $user );
448
449 if ( $permission !== true ) {
450 if ( !$user->isLoggedIn() ) {
451 $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) );
452 }
453
454 $this->dieUsageMsg( 'badaccess-groups' );
455 }
456 }
457
458 /**
459 * Performs file verification, dies on error.
460 */
461 protected function verifyUpload() {
462 $verification = $this->mUpload->verifyUpload();
463 if ( $verification['status'] === UploadBase::OK ) {
464 return;
465 }
466
467 $this->checkVerification( $verification );
468 }
469
470 /**
471 * Performs file verification, dies on error.
472 * @param array $verification
473 */
474 protected function checkVerification( array $verification ) {
475 // @todo Move them to ApiBase's message map
476 switch ( $verification['status'] ) {
477 // Recoverable errors
478 case UploadBase::MIN_LENGTH_PARTNAME:
479 $this->dieRecoverableError( 'filename-tooshort', 'filename' );
480 break;
481 case UploadBase::ILLEGAL_FILENAME:
482 $this->dieRecoverableError( 'illegal-filename', 'filename',
483 array( 'filename' => $verification['filtered'] ) );
484 break;
485 case UploadBase::FILENAME_TOO_LONG:
486 $this->dieRecoverableError( 'filename-toolong', 'filename' );
487 break;
488 case UploadBase::FILETYPE_MISSING:
489 $this->dieRecoverableError( 'filetype-missing', 'filename' );
490 break;
491 case UploadBase::WINDOWS_NONASCII_FILENAME:
492 $this->dieRecoverableError( 'windows-nonascii-filename', 'filename' );
493 break;
494
495 // Unrecoverable errors
496 case UploadBase::EMPTY_FILE:
497 $this->dieUsage( 'The file you submitted was empty', 'empty-file' );
498 break;
499 case UploadBase::FILE_TOO_LARGE:
500 $this->dieUsage( 'The file you submitted was too large', 'file-too-large' );
501 break;
502
503 case UploadBase::FILETYPE_BADTYPE:
504 $extradata = array(
505 'filetype' => $verification['finalExt'],
506 'allowed' => array_values( array_unique( $this->getConfig()->get( 'FileExtensions' ) ) )
507 );
508 $this->getResult()->setIndexedTagName( $extradata['allowed'], 'ext' );
509
510 $msg = "Filetype not permitted: ";
511 if ( isset( $verification['blacklistedExt'] ) ) {
512 $msg .= join( ', ', $verification['blacklistedExt'] );
513 $extradata['blacklisted'] = array_values( $verification['blacklistedExt'] );
514 $this->getResult()->setIndexedTagName( $extradata['blacklisted'], 'ext' );
515 } else {
516 $msg .= $verification['finalExt'];
517 }
518 $this->dieUsage( $msg, 'filetype-banned', 0, $extradata );
519 break;
520 case UploadBase::VERIFICATION_ERROR:
521 $this->getResult()->setIndexedTagName( $verification['details'], 'detail' );
522 $this->dieUsage( 'This file did not pass file verification', 'verification-error',
523 0, array( 'details' => $verification['details'] ) );
524 break;
525 case UploadBase::HOOK_ABORTED:
526 $this->dieUsage( "The modification you tried to make was aborted by an extension hook",
527 'hookaborted', 0, array( 'error' => $verification['error'] ) );
528 break;
529 default:
530 $this->dieUsage( 'An unknown error occurred', 'unknown-error',
531 0, array( 'code' => $verification['status'] ) );
532 break;
533 }
534 }
535
536 /**
537 * Check warnings.
538 * Returns a suitable array for inclusion into API results if there were warnings
539 * Returns the empty array if there were no warnings
540 *
541 * @return array
542 */
543 protected function getApiWarnings() {
544 $warnings = $this->mUpload->checkWarnings();
545
546 return $this->transformWarnings( $warnings );
547 }
548
549 protected function transformWarnings( $warnings ) {
550 if ( $warnings ) {
551 // Add indices
552 $result = $this->getResult();
553 $result->setIndexedTagName( $warnings, 'warning' );
554
555 if ( isset( $warnings['duplicate'] ) ) {
556 $dupes = array();
557 /** @var File $dupe */
558 foreach ( $warnings['duplicate'] as $dupe ) {
559 $dupes[] = $dupe->getName();
560 }
561 $result->setIndexedTagName( $dupes, 'duplicate' );
562 $warnings['duplicate'] = $dupes;
563 }
564
565 if ( isset( $warnings['exists'] ) ) {
566 $warning = $warnings['exists'];
567 unset( $warnings['exists'] );
568 /** @var LocalFile $localFile */
569 $localFile = isset( $warning['normalizedFile'] )
570 ? $warning['normalizedFile']
571 : $warning['file'];
572 $warnings[$warning['warning']] = $localFile->getName();
573 }
574 }
575
576 return $warnings;
577 }
578
579 /**
580 * Perform the actual upload. Returns a suitable result array on success;
581 * dies on failure.
582 *
583 * @param array $warnings Array of Api upload warnings
584 * @return array
585 */
586 protected function performUpload( $warnings ) {
587 // Use comment as initial page text by default
588 if ( is_null( $this->mParams['text'] ) ) {
589 $this->mParams['text'] = $this->mParams['comment'];
590 }
591
592 /** @var $file File */
593 $file = $this->mUpload->getLocalFile();
594
595 // For preferences mode, we want to watch if 'watchdefault' is set or
596 // if the *file* doesn't exist and 'watchcreations' is set. But
597 // getWatchlistValue()'s automatic handling checks if the *title*
598 // exists or not, so we need to check both prefs manually.
599 $watch = $this->getWatchlistValue(
600 $this->mParams['watchlist'], $file->getTitle(), 'watchdefault'
601 );
602 if ( !$watch && $this->mParams['watchlist'] == 'preferences' && !$file->exists() ) {
603 $watch = $this->getWatchlistValue(
604 $this->mParams['watchlist'], $file->getTitle(), 'watchcreations'
605 );
606 }
607
608 // Deprecated parameters
609 if ( $this->mParams['watch'] ) {
610 $this->logFeatureUsage( 'action=upload&watch' );
611 $watch = true;
612 }
613
614 // No errors, no warnings: do the upload
615 if ( $this->mParams['async'] ) {
616 $progress = UploadBase::getSessionStatus( $this->getUser(), $this->mParams['filekey'] );
617 if ( $progress && $progress['result'] === 'Poll' ) {
618 $this->dieUsage( "Upload from stash already in progress.", 'publishfailed' );
619 }
620 UploadBase::setSessionStatus(
621 $this->getUser(),
622 $this->mParams['filekey'],
623 array( 'result' => 'Poll', 'stage' => 'queued', 'status' => Status::newGood() )
624 );
625 JobQueueGroup::singleton()->push( new PublishStashedFileJob(
626 Title::makeTitle( NS_FILE, $this->mParams['filename'] ),
627 array(
628 'filename' => $this->mParams['filename'],
629 'filekey' => $this->mParams['filekey'],
630 'comment' => $this->mParams['comment'],
631 'text' => $this->mParams['text'],
632 'watch' => $watch,
633 'session' => $this->getContext()->exportSession()
634 )
635 ) );
636 $result['result'] = 'Poll';
637 $result['stage'] = 'queued';
638 } else {
639 /** @var $status Status */
640 $status = $this->mUpload->performUpload( $this->mParams['comment'],
641 $this->mParams['text'], $watch, $this->getUser() );
642
643 if ( !$status->isGood() ) {
644 $error = $status->getErrorsArray();
645
646 if ( count( $error ) == 1 && $error[0][0] == 'async' ) {
647 // The upload can not be performed right now, because the user
648 // requested so
649 return array(
650 'result' => 'Queued',
651 'statuskey' => $error[0][1],
652 );
653 }
654
655 $this->getResult()->setIndexedTagName( $error, 'error' );
656 $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error );
657 }
658 $result['result'] = 'Success';
659 }
660
661 $result['filename'] = $file->getName();
662 if ( $warnings && count( $warnings ) > 0 ) {
663 $result['warnings'] = $warnings;
664 }
665
666 return $result;
667 }
668
669 /**
670 * Checks if asynchronous copy uploads are enabled and throws an error if they are not.
671 */
672 protected function checkAsyncDownloadEnabled() {
673 if ( !$this->getConfig()->get( 'AllowAsyncCopyUploads' ) ) {
674 $this->dieUsage( 'Asynchronous copy uploads disabled', 'asynccopyuploaddisabled' );
675 }
676 }
677
678 public function mustBePosted() {
679 return true;
680 }
681
682 public function isWriteMode() {
683 return true;
684 }
685
686 public function getAllowedParams() {
687 $params = array(
688 'filename' => array(
689 ApiBase::PARAM_TYPE => 'string',
690 ),
691 'comment' => array(
692 ApiBase::PARAM_DFLT => ''
693 ),
694 'text' => null,
695 'watch' => array(
696 ApiBase::PARAM_DFLT => false,
697 ApiBase::PARAM_DEPRECATED => true,
698 ),
699 'watchlist' => array(
700 ApiBase::PARAM_DFLT => 'preferences',
701 ApiBase::PARAM_TYPE => array(
702 'watch',
703 'preferences',
704 'nochange'
705 ),
706 ),
707 'ignorewarnings' => false,
708 'file' => array(
709 ApiBase::PARAM_TYPE => 'upload',
710 ),
711 'url' => null,
712 'filekey' => null,
713 'sessionkey' => array(
714 ApiBase::PARAM_DFLT => null,
715 ApiBase::PARAM_DEPRECATED => true,
716 ),
717 'stash' => false,
718
719 'filesize' => null,
720 'offset' => null,
721 'chunk' => array(
722 ApiBase::PARAM_TYPE => 'upload',
723 ),
724
725 'async' => false,
726 'asyncdownload' => false,
727 'leavemessage' => false,
728 'statuskey' => null,
729 'checkstatus' => false,
730 );
731
732 return $params;
733 }
734
735 public function needsToken() {
736 return 'csrf';
737 }
738
739 public function getExamplesMessages() {
740 return array(
741 'action=upload&filename=Wiki.png' .
742 '&url=http%3A//upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png&token=123ABC'
743 => 'apihelp-upload-example-url',
744 'action=upload&filename=Wiki.png&filekey=filekey&ignorewarnings=1&token=123ABC'
745 => 'apihelp-upload-example-filekey',
746 );
747 }
748
749 public function getHelpUrls() {
750 return 'https://www.mediawiki.org/wiki/API:Upload';
751 }
752 }