Re-instate most of the revisions for bug 33147 "API examples should explain what...
[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
32 /**
33 * @var UploadBase
34 */
35 protected $mUpload = null;
36
37 protected $mParams;
38
39 public function __construct( $main, $action ) {
40 parent::__construct( $main, $action );
41 }
42
43 public function execute() {
44 // Check whether upload is enabled
45 if ( !UploadBase::isEnabled() ) {
46 $this->dieUsageMsg( 'uploaddisabled' );
47 }
48
49 $user = $this->getUser();
50
51 // Parameter handling
52 $this->mParams = $this->extractRequestParams();
53 $request = $this->getMain()->getRequest();
54 // Add the uploaded file to the params array
55 $this->mParams['file'] = $request->getFileName( 'file' );
56 $this->mParams['chunk'] = $request->getFileName( 'chunk' );
57
58 // Copy the session key to the file key, for backward compatibility.
59 if( !$this->mParams['filekey'] && $this->mParams['sessionkey'] ) {
60 $this->mParams['filekey'] = $this->mParams['sessionkey'];
61 }
62
63 // Select an upload module
64 if ( !$this->selectUploadModule() ) {
65 // This is not a true upload, but a status request or similar
66 return;
67 }
68 if ( !isset( $this->mUpload ) ) {
69 $this->dieUsage( 'No upload module set', 'nomodule' );
70 }
71
72 // First check permission to upload
73 $this->checkPermissions( $user );
74
75 // Fetch the file
76 $status = $this->mUpload->fetchFile();
77 if ( !$status->isGood() ) {
78 $errors = $status->getErrorsArray();
79 $error = array_shift( $errors[0] );
80 $this->dieUsage( 'Error fetching file from remote source', $error, 0, $errors[0] );
81 }
82
83 // Check if the uploaded file is sane
84 if ( $this->mParams['chunk'] ) {
85 $maxSize = $this->mUpload->getMaxUploadSize( );
86 if( $this->mParams['filesize'] > $maxSize ) {
87 $this->dieUsage( 'The file you submitted was too large', 'file-too-large' );
88 }
89 } else {
90 $this->verifyUpload();
91 }
92
93 // Check if the user has the rights to modify or overwrite the requested title
94 // (This check is irrelevant if stashing is already requested, since the errors
95 // can always be fixed by changing the title)
96 if ( ! $this->mParams['stash'] ) {
97 $permErrors = $this->mUpload->verifyTitlePermissions( $user );
98 if ( $permErrors !== true ) {
99 $this->dieRecoverableError( $permErrors[0], 'filename' );
100 }
101 }
102 // Get the result based on the current upload context:
103 $result = $this->getContextResult();
104
105 if ( $result['result'] === 'Success' ) {
106 $result['imageinfo'] = $this->mUpload->getImageInfo( $this->getResult() );
107 }
108
109 $this->getResult()->addValue( null, $this->getModuleName(), $result );
110
111 // Cleanup any temporary mess
112 $this->mUpload->cleanupTempFile();
113 }
114 /**
115 * Get an uplaod result based on upload context
116 */
117 private function getContextResult(){
118 $warnings = $this->getApiWarnings();
119 if ( $warnings ) {
120 // Get warnings formated in result array format
121 return $this->getWarningsResult( $warnings );
122 } elseif ( $this->mParams['chunk'] ) {
123 // Add chunk, and get result
124 return $this->getChunkResult();
125 } elseif ( $this->mParams['stash'] ) {
126 // Stash the file and get stash result
127 return $this->getStashResult();
128 }
129 // This is the most common case -- a normal upload with no warnings
130 // performUpload will return a formatted properly for the API with status
131 return $this->performUpload();
132 }
133 /**
134 * Get Stash Result, throws an expetion if the file could not be stashed.
135 */
136 private function getStashResult(){
137 $result = array ();
138 // Some uploads can request they be stashed, so as not to publish them immediately.
139 // In this case, a failure to stash ought to be fatal
140 try {
141 $result['result'] = 'Success';
142 $result['filekey'] = $this->performStash();
143 $result['sessionkey'] = $result['filekey']; // backwards compatibility
144 } catch ( MWException $e ) {
145 $this->dieUsage( $e->getMessage(), 'stashfailed' );
146 }
147 return $result;
148 }
149 /**
150 * Get Warnings Result
151 * @param $warnings Array of Api upload warnings
152 */
153 private function getWarningsResult( $warnings ){
154 $result = array();
155 $result['result'] = 'Warning';
156 $result['warnings'] = $warnings;
157 // in case the warnings can be fixed with some further user action, let's stash this upload
158 // and return a key they can use to restart it
159 try {
160 $result['filekey'] = $this->performStash();
161 $result['sessionkey'] = $result['filekey']; // backwards compatibility
162 } catch ( MWException $e ) {
163 $result['warnings']['stashfailed'] = $e->getMessage();
164 }
165 return $result;
166 }
167 /**
168 * Get the result of a chunk upload.
169 */
170 private function getChunkResult(){
171 $result = array();
172
173 $result['result'] = 'Continue';
174 $request = $this->getMain()->getRequest();
175 $chunkPath = $request->getFileTempname( 'chunk' );
176 $chunkSize = $request->getUpload( 'chunk' )->getSize();
177 if ($this->mParams['offset'] == 0) {
178 $result['filekey'] = $this->performStash();
179 } else {
180 $status = $this->mUpload->addChunk($chunkPath, $chunkSize,
181 $this->mParams['offset']);
182 if ( !$status->isGood() ) {
183 $this->dieUsage( $status->getWikiText(), 'stashfailed' );
184 return ;
185 }
186 $result['filekey'] = $this->mParams['filekey'];
187 // Check we added the last chunk:
188 if( $this->mParams['offset'] + $chunkSize == $this->mParams['filesize'] ) {
189 $status = $this->mUpload->concatenateChunks();
190 if ( !$status->isGood() ) {
191 $this->dieUsage( $status->getWikiText(), 'stashfailed' );
192 return ;
193 }
194 $result['result'] = 'Success';
195 }
196 }
197 $result['offset'] = $this->mParams['offset'] + $chunkSize;
198 return $result;
199 }
200
201 /**
202 * Stash the file and return the file key
203 * Also re-raises exceptions with slightly more informative message strings (useful for API)
204 * @throws MWException
205 * @return String file key
206 */
207 function performStash() {
208 try {
209 $stashFile = $this->mUpload->stashFile();
210
211 if ( !$stashFile ) {
212 throw new MWException( 'Invalid stashed file' );
213 }
214 $fileKey = $stashFile->getFileKey();
215 } catch ( MWException $e ) {
216 $message = 'Stashing temporary file failed: ' . get_class( $e ) . ' ' . $e->getMessage();
217 wfDebug( __METHOD__ . ' ' . $message . "\n");
218 throw new MWException( $message );
219 }
220 return $fileKey;
221 }
222
223 /**
224 * Throw an error that the user can recover from by providing a better
225 * value for $parameter
226 *
227 * @param $error array Error array suitable for passing to dieUsageMsg()
228 * @param $parameter string Parameter that needs revising
229 * @param $data array Optional extra data to pass to the user
230 * @throws UsageException
231 */
232 function dieRecoverableError( $error, $parameter, $data = array() ) {
233 try {
234 $data['filekey'] = $this->performStash();
235 $data['sessionkey'] = $data['filekey'];
236 } catch ( MWException $e ) {
237 $data['stashfailed'] = $e->getMessage();
238 }
239 $data['invalidparameter'] = $parameter;
240
241 $parsed = $this->parseMsg( $error );
242 $this->dieUsage( $parsed['info'], $parsed['code'], 0, $data );
243 }
244
245 /**
246 * Select an upload module and set it to mUpload. Dies on failure. If the
247 * request was a status request and not a true upload, returns false;
248 * otherwise true
249 *
250 * @return bool
251 */
252 protected function selectUploadModule() {
253 $request = $this->getMain()->getRequest();
254
255 // chunk or one and only one of the following parameters is needed
256 if( !$this->mParams['chunk'] ) {
257 $this->requireOnlyOneParameter( $this->mParams,
258 'filekey', 'file', 'url', 'statuskey' );
259 }
260
261 if ( $this->mParams['statuskey'] ) {
262 $this->checkAsyncDownloadEnabled();
263
264 // Status request for an async upload
265 $sessionData = UploadFromUrlJob::getSessionData( $this->mParams['statuskey'] );
266 if ( !isset( $sessionData['result'] ) ) {
267 $this->dieUsage( 'No result in session data', 'missingresult' );
268 }
269 if ( $sessionData['result'] == 'Warning' ) {
270 $sessionData['warnings'] = $this->transformWarnings( $sessionData['warnings'] );
271 $sessionData['sessionkey'] = $this->mParams['statuskey'];
272 }
273 $this->getResult()->addValue( null, $this->getModuleName(), $sessionData );
274 return false;
275
276 }
277
278 // The following modules all require the filename parameter to be set
279 if ( is_null( $this->mParams['filename'] ) ) {
280 $this->dieUsageMsg( array( 'missingparam', 'filename' ) );
281 }
282
283 if ( $this->mParams['chunk'] ) {
284 // Chunk upload
285 $this->mUpload = new UploadFromChunks();
286 if( isset( $this->mParams['filekey'] ) ){
287 // handle new chunk
288 $this->mUpload->continueChunks(
289 $this->mParams['filename'],
290 $this->mParams['filekey'],
291 $request->getUpload( 'chunk' )
292 );
293 } else {
294 // handle first chunk
295 $this->mUpload->initialize(
296 $this->mParams['filename'],
297 $request->getUpload( 'chunk' )
298 );
299 }
300 } elseif ( isset( $this->mParams['filekey'] ) ) {
301 // Upload stashed in a previous request
302 if ( !UploadFromStash::isValidKey( $this->mParams['filekey'] ) ) {
303 $this->dieUsageMsg( 'invalid-file-key' );
304 }
305
306 $this->mUpload = new UploadFromStash( $this->getUser() );
307
308 $this->mUpload->initialize( $this->mParams['filekey'], $this->mParams['filename'] );
309 } elseif ( isset( $this->mParams['file'] ) ) {
310 $this->mUpload = new UploadFromFile();
311 $this->mUpload->initialize(
312 $this->mParams['filename'],
313 $request->getUpload( 'file' )
314 );
315 } elseif ( isset( $this->mParams['url'] ) ) {
316 // Make sure upload by URL is enabled:
317 if ( !UploadFromUrl::isEnabled() ) {
318 $this->dieUsageMsg( 'copyuploaddisabled' );
319 }
320
321 $async = false;
322 if ( $this->mParams['asyncdownload'] ) {
323 $this->checkAsyncDownloadEnabled();
324
325 if ( $this->mParams['leavemessage'] && !$this->mParams['ignorewarnings'] ) {
326 $this->dieUsage( 'Using leavemessage without ignorewarnings is not supported',
327 'missing-ignorewarnings' );
328 }
329
330 if ( $this->mParams['leavemessage'] ) {
331 $async = 'async-leavemessage';
332 } else {
333 $async = 'async';
334 }
335 }
336 $this->mUpload = new UploadFromUrl;
337 $this->mUpload->initialize( $this->mParams['filename'],
338 $this->mParams['url'], $async );
339
340 }
341
342 return true;
343 }
344
345 /**
346 * Checks that the user has permissions to perform this upload.
347 * Dies with usage message on inadequate permissions.
348 * @param $user User The user to check.
349 */
350 protected function checkPermissions( $user ) {
351 // Check whether the user has the appropriate permissions to upload anyway
352 $permission = $this->mUpload->isAllowed( $user );
353
354 if ( $permission !== true ) {
355 if ( !$user->isLoggedIn() ) {
356 $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) );
357 } else {
358 $this->dieUsageMsg( 'badaccess-groups' );
359 }
360 }
361 }
362
363 /**
364 * Performs file verification, dies on error.
365 */
366 protected function verifyUpload( ) {
367 global $wgFileExtensions;
368
369 $verification = $this->mUpload->verifyUpload( );
370 if ( $verification['status'] === UploadBase::OK ) {
371 return;
372 }
373
374 // TODO: Move them to ApiBase's message map
375 switch( $verification['status'] ) {
376 // Recoverable errors
377 case UploadBase::MIN_LENGTH_PARTNAME:
378 $this->dieRecoverableError( 'filename-tooshort', 'filename' );
379 break;
380 case UploadBase::ILLEGAL_FILENAME:
381 $this->dieRecoverableError( 'illegal-filename', 'filename',
382 array( 'filename' => $verification['filtered'] ) );
383 break;
384 case UploadBase::FILENAME_TOO_LONG:
385 $this->dieRecoverableError( 'filename-toolong', 'filename' );
386 break;
387 case UploadBase::FILETYPE_MISSING:
388 $this->dieRecoverableError( 'filetype-missing', 'filename' );
389 break;
390 case UploadBase::WINDOWS_NONASCII_FILENAME:
391 $this->dieRecoverableError( 'windows-nonascii-filename', 'filename' );
392 break;
393
394 // Unrecoverable errors
395 case UploadBase::EMPTY_FILE:
396 $this->dieUsage( 'The file you submitted was empty', 'empty-file' );
397 break;
398 case UploadBase::FILE_TOO_LARGE:
399 $this->dieUsage( 'The file you submitted was too large', 'file-too-large' );
400 break;
401
402 case UploadBase::FILETYPE_BADTYPE:
403 $this->dieUsage( 'This type of file is banned', 'filetype-banned',
404 0, array(
405 'filetype' => $verification['finalExt'],
406 'allowed' => $wgFileExtensions
407 ) );
408 break;
409 case UploadBase::VERIFICATION_ERROR:
410 $this->getResult()->setIndexedTagName( $verification['details'], 'detail' );
411 $this->dieUsage( 'This file did not pass file verification', 'verification-error',
412 0, array( 'details' => $verification['details'] ) );
413 break;
414 case UploadBase::HOOK_ABORTED:
415 $this->dieUsage( "The modification you tried to make was aborted by an extension hook",
416 'hookaborted', 0, array( 'error' => $verification['error'] ) );
417 break;
418 default:
419 $this->dieUsage( 'An unknown error occurred', 'unknown-error',
420 0, array( 'code' => $verification['status'] ) );
421 break;
422 }
423 }
424
425
426 /**
427 * Check warnings if ignorewarnings is not set.
428 * Returns a suitable array for inclusion into API results if there were warnings
429 * Returns the empty array if there were no warnings
430 *
431 * @return array
432 */
433 protected function getApiWarnings() {
434 $warnings = array();
435
436 if ( !$this->mParams['ignorewarnings'] ) {
437 $warnings = $this->mUpload->checkWarnings();
438 }
439 return $this->transformWarnings( $warnings );
440 }
441
442 protected function transformWarnings( $warnings ) {
443 if ( $warnings ) {
444 // Add indices
445 $result = $this->getResult();
446 $result->setIndexedTagName( $warnings, 'warning' );
447
448 if ( isset( $warnings['duplicate'] ) ) {
449 $dupes = array();
450 foreach ( $warnings['duplicate'] as $dupe ) {
451 $dupes[] = $dupe->getName();
452 }
453 $result->setIndexedTagName( $dupes, 'duplicate' );
454 $warnings['duplicate'] = $dupes;
455 }
456
457 if ( isset( $warnings['exists'] ) ) {
458 $warning = $warnings['exists'];
459 unset( $warnings['exists'] );
460 $warnings[$warning['warning']] = $warning['file']->getName();
461 }
462 }
463 return $warnings;
464 }
465
466
467 /**
468 * Perform the actual upload. Returns a suitable result array on success;
469 * dies on failure.
470 *
471 * @return array
472 */
473 protected function performUpload() {
474 // Use comment as initial page text by default
475 if ( is_null( $this->mParams['text'] ) ) {
476 $this->mParams['text'] = $this->mParams['comment'];
477 }
478
479 $file = $this->mUpload->getLocalFile();
480 $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() );
481
482 // Deprecated parameters
483 if ( $this->mParams['watch'] ) {
484 $watch = true;
485 }
486
487 // No errors, no warnings: do the upload
488 $status = $this->mUpload->performUpload( $this->mParams['comment'],
489 $this->mParams['text'], $watch, $this->getUser() );
490
491 if ( !$status->isGood() ) {
492 $error = $status->getErrorsArray();
493
494 if ( count( $error ) == 1 && $error[0][0] == 'async' ) {
495 // The upload can not be performed right now, because the user
496 // requested so
497 return array(
498 'result' => 'Queued',
499 'statuskey' => $error[0][1],
500 );
501 } else {
502 $this->getResult()->setIndexedTagName( $error, 'error' );
503
504 $this->dieUsage( 'An internal error occurred', 'internal-error', 0, $error );
505 }
506 }
507
508 $file = $this->mUpload->getLocalFile();
509
510 $result['result'] = 'Success';
511 $result['filename'] = $file->getName();
512
513 return $result;
514 }
515
516 /**
517 * Checks if asynchronous copy uploads are enabled and throws an error if they are not.
518 */
519 protected function checkAsyncDownloadEnabled() {
520 global $wgAllowAsyncCopyUploads;
521 if ( !$wgAllowAsyncCopyUploads ) {
522 $this->dieUsage( 'Asynchronous copy uploads disabled', 'asynccopyuploaddisabled');
523 }
524 }
525
526 public function mustBePosted() {
527 return true;
528 }
529
530 public function isWriteMode() {
531 return true;
532 }
533
534 public function getAllowedParams() {
535 $params = array(
536 'filename' => array(
537 ApiBase::PARAM_TYPE => 'string',
538 ),
539 'comment' => array(
540 ApiBase::PARAM_DFLT => ''
541 ),
542 'text' => null,
543 'token' => null,
544 'watch' => array(
545 ApiBase::PARAM_DFLT => false,
546 ApiBase::PARAM_DEPRECATED => true,
547 ),
548 'watchlist' => array(
549 ApiBase::PARAM_DFLT => 'preferences',
550 ApiBase::PARAM_TYPE => array(
551 'watch',
552 'preferences',
553 'nochange'
554 ),
555 ),
556 'ignorewarnings' => false,
557 'file' => null,
558 'url' => null,
559 'filekey' => null,
560 'sessionkey' => array(
561 ApiBase::PARAM_DFLT => null,
562 ApiBase::PARAM_DEPRECATED => true,
563 ),
564 'stash' => false,
565
566 'filesize' => null,
567 'offset' => null,
568 'chunk' => null,
569
570 'asyncdownload' => false,
571 'leavemessage' => false,
572 'statuskey' => null,
573 );
574
575 return $params;
576 }
577
578 public function getParamDescription() {
579 $params = array(
580 'filename' => 'Target filename',
581 'token' => 'Edit token. You can get one of these through prop=info',
582 'comment' => 'Upload comment. Also used as the initial page text for new files if "text" is not specified',
583 'text' => 'Initial page text for new files',
584 'watch' => 'Watch the page',
585 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
586 'ignorewarnings' => 'Ignore any warnings',
587 'file' => 'File contents',
588 'url' => 'URL to fetch the file from',
589 'filekey' => 'Key that identifies a previous upload that was stashed temporarily.',
590 'sessionkey' => 'Same as filekey, maintained for backward compatibility.',
591 'stash' => 'If set, the server will not add the file to the repository and stash it temporarily.',
592
593 'chunk' => 'Chunk contents',
594 'offset' => 'Offset of chunk in bytes',
595 'filesize' => 'Filesize of entire upload',
596
597 'asyncdownload' => 'Make fetching a URL asynchronous',
598 'leavemessage' => 'If asyncdownload is used, leave a message on the user talk page if finished',
599 'statuskey' => 'Fetch the upload status for this file key',
600 );
601
602 return $params;
603
604 }
605
606 public function getDescription() {
607 return array(
608 'Upload a file, or get the status of pending uploads. Several methods are available:',
609 ' * Upload file contents directly, using the "file" parameter',
610 ' * Have the MediaWiki server fetch a file from a URL, using the "url" parameter',
611 ' * Complete an earlier upload that failed due to warnings, using the "filekey" parameter',
612 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
613 'sending the "file". Also you must get and send an edit token before doing any upload stuff'
614 );
615 }
616
617 public function getPossibleErrors() {
618 return array_merge( parent::getPossibleErrors(),
619 $this->getRequireOnlyOneParameterErrorMessages( array( 'filekey', 'file', 'url', 'statuskey' ) ),
620 array(
621 array( 'uploaddisabled' ),
622 array( 'invalid-file-key' ),
623 array( 'uploaddisabled' ),
624 array( 'mustbeloggedin', 'upload' ),
625 array( 'badaccess-groups' ),
626 array( 'code' => 'fetchfileerror', 'info' => '' ),
627 array( 'code' => 'nomodule', 'info' => 'No upload module set' ),
628 array( 'code' => 'empty-file', 'info' => 'The file you submitted was empty' ),
629 array( 'code' => 'filetype-missing', 'info' => 'The file is missing an extension' ),
630 array( 'code' => 'filename-tooshort', 'info' => 'The filename is too short' ),
631 array( 'code' => 'overwrite', 'info' => 'Overwriting an existing file is not allowed' ),
632 array( 'code' => 'stashfailed', 'info' => 'Stashing temporary file failed' ),
633 array( 'code' => 'internal-error', 'info' => 'An internal error occurred' ),
634 array( 'code' => 'asynccopyuploaddisabled', 'info' => 'Asynchronous copy uploads disabled' ),
635 )
636 );
637 }
638
639 public function needsToken() {
640 return true;
641 }
642
643 public function getTokenSalt() {
644 return '';
645 }
646
647 public function getExamples() {
648 return array(
649 'api.php?action=upload&filename=Wiki.png&url=http%3A//upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png'
650 => 'Upload from a URL',
651 'api.php?action=upload&filename=Wiki.png&filekey=filekey&ignorewarnings=1'
652 => 'Complete an upload that failed due to warnings',
653 );
654 }
655
656 public function getHelpUrls() {
657 return 'https://www.mediawiki.org/wiki/API:Upload';
658 }
659
660 public function getVersion() {
661 return __CLASS__ . ': $Id$';
662 }
663 }