r84768 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84767‎ | r84768 | r84769 >
Date:21:22, 25 March 2011
Author:btongminh
Status:ok
Tags:
Comment:
API upload errors may now return the parameter that needs to be changed and a sessionkey to fix the error. This is for now only done for uploads that can be fixed by changing the filename, but may be extended later to comment and pagetext.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiUpload.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiBase.php
@@ -1087,6 +1087,10 @@
10881088 'nouploadmodule' => array( 'code' => 'nouploadmodule', 'info' => 'No upload module set' ),
10891089 'uploaddisabled' => array( 'code' => 'uploaddisabled', 'info' => 'Uploads are not enabled. Make sure $wgEnableUploads is set to true in LocalSettings.php and the PHP ini setting file_uploads is true' ),
10901090 'copyuploaddisabled' => array( 'code' => 'copyuploaddisabled', 'info' => 'Uploads by URL is not enabled. Make sure $wgAllowCopyUploads is set to true in LocalSettings.php.' ),
 1091+
 1092+ 'filename-tooshort' => array( 'code' => 'filename-tooshort', 'info' => 'The filename is too short' ),
 1093+ 'illegal-filename' => array( 'code' => 'illegal-filename', 'info' => 'The filename is not allowed' ),
 1094+ 'filetype-missing' => array( 'code' => 'filetype-missing', 'info' => 'The file is missing an extension' ),
10911095 );
10921096
10931097 /**
Index: trunk/phase3/includes/api/ApiUpload.php
@@ -82,14 +82,14 @@
8383 // Check if the uploaded file is sane
8484 $this->verifyUpload();
8585
 86+
8687 // Check if the user has the rights to modify or overwrite the requested title
8788 // (This check is irrelevant if stashing is already requested, since the errors
8889 // can always be fixed by changing the title)
8990 if ( ! $this->mParams['stash'] ) {
9091 $permErrors = $this->mUpload->verifyTitlePermissions( $wgUser );
9192 if ( $permErrors !== true ) {
92 - // TODO: stash the upload and allow choosing a new name
93 - $this->dieUsageMsg( array( 'badaccess-groups' ) );
 93+ $this->dieRecoverableError( $permErrors[0], 'filename' );
9494 }
9595 }
9696
@@ -146,6 +146,27 @@
147147 }
148148 return $sessionKey;
149149 }
 150+
 151+ /**
 152+ * Throw an error that the user can recover from by providing a better
 153+ * value for $parameter
 154+ *
 155+ * @param $error array Error array suitable for passing to dieUsageMsg()
 156+ * @param $parameter string Parameter that needs revising
 157+ * @param $data array Optional extra data to pass to the user
 158+ * @throws UsageException
 159+ */
 160+ function dieRecoverableError( $error, $parameter, $data = array() ) {
 161+ try {
 162+ $data['sessionkey'] = $this->performStash();
 163+ } catch ( MWException $e ) {
 164+ $data['stashfailed'] = $e->getMessage();
 165+ }
 166+ $data['invalidparameter'] = $parameter;
 167+
 168+ $parsed = $this->parseMsg( $error );
 169+ $this->dieUsage( $parsed['info'], $parsed['code'], 0, $data );
 170+ }
150171
151172 /**
152173 * Select an upload module and set it to mUpload. Dies on failure. If the
@@ -262,15 +283,26 @@
263284
264285 // TODO: Move them to ApiBase's message map
265286 switch( $verification['status'] ) {
 287+ // Recoverable errors
 288+ case UploadBase::MIN_LENGTH_PARTNAME:
 289+ $this->dieRecoverableError( 'filename-tooshort', 'filename' );
 290+ break;
 291+ case UploadBase::ILLEGAL_FILENAME:
 292+ $this->dieRecoverableError( 'illegal-filename', 'filename',
 293+ array( 'filename' => $verification['filtered'] ) );
 294+ break;
 295+ case UploadBase::FILETYPE_MISSING:
 296+ $this->dieRecoverableError( 'filetype-missing', 'filename' );
 297+ break;
 298+
 299+ // Unrecoverable errors
266300 case UploadBase::EMPTY_FILE:
267301 $this->dieUsage( 'The file you submitted was empty', 'empty-file' );
268302 break;
269303 case UploadBase::FILE_TOO_LARGE:
270304 $this->dieUsage( 'The file you submitted was too large', 'file-too-large' );
271305 break;
272 - case UploadBase::FILETYPE_MISSING:
273 - $this->dieUsage( 'The file is missing an extension', 'filetype-missing' );
274 - break;
 306+
275307 case UploadBase::FILETYPE_BADTYPE:
276308 $this->dieUsage( 'This type of file is banned', 'filetype-banned',
277309 0, array(
@@ -278,13 +310,6 @@
279311 'allowed' => $wgFileExtensions
280312 ) );
281313 break;
282 - case UploadBase::MIN_LENGTH_PARTNAME:
283 - $this->dieUsage( 'The filename is too short', 'filename-tooshort' );
284 - break;
285 - case UploadBase::ILLEGAL_FILENAME:
286 - $this->dieUsage( 'The filename is not allowed', 'illegal-filename',
287 - 0, array( 'filename' => $verification['filtered'] ) );
288 - break;
289314 case UploadBase::VERIFICATION_ERROR:
290315 $this->getResult()->setIndexedTagName( $verification['details'], 'detail' );
291316 $this->dieUsage( 'This file did not pass file verification', 'verification-error',
Index: trunk/phase3/RELEASE-NOTES
@@ -285,6 +285,8 @@
286286 * (bug 28226) prop=extlinks&eloffset should be an integer
287287 * (bug 28070) Fix watchlist RSS for databases that store timestamps in a
288288 real timestamp field.
 289+* API upload errors may now return the parameter that needs to be changed and
 290+ a sessionkey to fix the error.
289291
290292 === Languages updated in 1.18 ===
291293

Status & tagging log