r25315 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r25314‎ | r25315 | r25316 >
Date:12:13, 30 August 2007
Author:abernala
Status:old
Tags:
Comment:
[SpecialUpload.php] processUpload handle with defined constants values, and evaluated in processRest.
Modified paths:
  • /branches/ApiEdit_Vodafone/includes/SpecialUpload.php (modified) (history)

Diff [purge]

Index: branches/ApiEdit_Vodafone/includes/SpecialUpload.php
@@ -19,6 +19,20 @@
2020 * @addtogroup SpecialPage
2121 */
2222 class UploadForm {
 23+ const SUCCESS = 0;
 24+ const BEFORE_PROCESSING = 1;
 25+ const LARGE_FILE_SERVER = 2;
 26+ const EMPTY_FILE = 3;
 27+ const MIN_LENGHT_PARTNAME = 4;
 28+ const ILLEGAL_FILENAME = 5;
 29+ const PROTECTED_PAGE = 6;
 30+ const OVERWRITE_EXISTING_FILE = 7;
 31+ const FILETYPE_MISSING = 8;
 32+ const FILETYPE_BADTYPE = 9;
 33+ const VERIFICATION_ERROR = 10;
 34+ const UPLOAD_VERIFICATION_ERROR = 11;
 35+ const UPLOAD_WARNING = 12;
 36+
2337 /**#@+
2438 * @access private
2539 */
@@ -245,7 +259,9 @@
246260 }
247261 $this->mainUploadForm();
248262 } else if( 'submit' == $this->mAction || $this->mUploadClicked ) {
249 - $this->processUpload();
 263+ $details = null;
 264+ $retval = $this->processUpload( $details );
 265+ $this->processRest($retval, $details);
250266 } else {
251267 $this->mainUploadForm();
252268 }
@@ -253,34 +269,107 @@
254270 $this->cleanupTempFile();
255271 }
256272
 273+ function processRest($value, $details) {
 274+ global $wgUser, $wgOut, $wgFileExtensions;
 275+
 276+ switch ($value)
 277+ {
 278+ case self::SUCCESS:
 279+ $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
 280+ break;
 281+
 282+ case self::BEFORE_PROCESSING:
 283+ return false;
 284+ break;
 285+
 286+ case self::LARGE_FILE_SERVER:
 287+ $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) );
 288+ break;
 289+
 290+ case self::EMPTY_FILE:
 291+ $this->mainUploadForm( wfMsgHtml( 'emptyfile' ) );
 292+ break;
 293+
 294+ case self::MIN_LENGHT_PARTNAME:
 295+ $this->mainUploadForm( wfMsgHtml( 'minlength1' ) );
 296+ break;
 297+
 298+ case self::ILLEGAL_FILENAME:
 299+ $filtered = $details['filtered'];
 300+ $this->uploadError( wfMsgWikiHtml( 'illegalfilename', htmlspecialchars( $filtered ) ) );
 301+ break;
 302+
 303+ case self::PROTECTED_PAGE:
 304+ $this->uploadError( wfMsgWikiHtml( 'protectedpage' ) );
 305+ break;
 306+
 307+ case self::OVERWRITE_EXISTING_FILE:
 308+ $overwrite = $details['overwrite'];
 309+ $overwrite = new WikiError( $wgOut->parse( $overwrite ) );
 310+
 311+ if( WikiError::isError( $overwrite ) ) {
 312+ $this->uploadError( $overwrite->toString() );
 313+ }
 314+ break;
 315+
 316+ case self::FILETYPE_MISSING:
 317+ $this->uploadError( wfMsgExt( 'filetype-missing', array ( 'parseinline' ) ) );
 318+ break;
 319+
 320+ case self::FILETYPE_BADTYPE:
 321+ $finalExt = $details['finalExt'];
 322+ $this->uploadError( wfMsgExt( 'filetype-badtype', array ( 'parseinline' ), htmlspecialchars( $finalExt ), implode ( ', ', $wgFileExtensions ) ) );
 323+ break;
 324+
 325+ case self::VERIFICATION_ERROR:
 326+ $veri = $details['veri'];
 327+ $this->uploadError( $veri->toString() );
 328+ break;
 329+
 330+ case self::UPLOAD_VERIFICATION_ERROR:
 331+ $error = $details['error'];
 332+ $this->uploadError( $error );
 333+ break;
 334+
 335+ case self::UPLOAD_WARNING:
 336+ $warning = $details['warning'];
 337+ $this->uploadWarning( $warning );
 338+ break;
 339+
 340+ }
 341+ }
 342+
 343+
257344 /* -------------------------------------------------------------- */
258345
259346 /**
260347 * Really do the upload
261348 * Checks are made in SpecialUpload::execute()
 349+ *
 350+ * @param array $resultDetails contains result-specific dict of additional values
 351+ *
262352 * @access private
263353 */
264 - function processUpload() {
 354+ function processUpload( &$resultDetails="" ) {
265355 global $wgUser, $wgOut;
266356
267357 if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) )
268358 {
269359 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file." );
270 - return false;
 360+ return self::BEFORE_PROCESSING;
 361+
271362 }
272363
273364 /* Check for PHP error if any, requires php 4.2 or newer */
274365 if( $this->mCurlError == 1/*UPLOAD_ERR_INI_SIZE*/ ) {
275 - $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) );
276 - return;
 366+ return self::LARGE_FILE_SERVER;
277367 }
278368
279369 /**
280370 * If there was no filename or a zero size given, give up quick.
281371 */
282372 if( trim( $this->mSrcName ) == '' || empty( $this->mFileSize ) ) {
283 - $this->mainUploadForm( wfMsgHtml( 'emptyfile' ) );
284 - return;
 373+ return self::EMPTY_FILE;
285374 }
286375
287376 # Chop off any directories in the given filename
@@ -309,10 +398,10 @@
310399 for( $i = 0; $i < count( $ext ) - 1; $i++ )
311400 $partname .= '.' . $ext[$i];
312401 }
 402+
313403
314404 if( strlen( $partname ) < 1 ) {
315 - $this->mainUploadForm( wfMsgHtml( 'minlength1' ) );
316 - return;
 405+ return self::MIN_LENGHT_PARTNAME;
317406 }
318407
319408 /**
@@ -321,9 +410,10 @@
322411 */
323412 $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $filtered );
324413 $nt = Title::makeTitleSafe( NS_IMAGE, $filtered );
 414+
325415 if( is_null( $nt ) ) {
326 - $this->uploadError( wfMsgWikiHtml( 'illegalfilename', htmlspecialchars( $filtered ) ) );
327 - return;
 416+ $resultDetails = array( 'filtered' => $filtered );
 417+ return self::ILLEGAL_FILENAME;
328418 }
329419 $this->mLocalFile = wfLocalFile( $nt );
330420 $this->mDestName = $this->mLocalFile->getName();
@@ -333,26 +423,30 @@
334424 * to modify it by uploading a new revision.
335425 */
336426 if( !$nt->userCan( 'edit' ) ) {
337 - return $this->uploadError( wfMsgWikiHtml( 'protectedpage' ) );
 427+ return self::PROTECTED_PAGE;
338428 }
339429
340430 /**
341431 * In some cases we may forbid overwriting of existing files.
342432 */
343433 $overwrite = $this->checkOverwrite( $this->mDestName );
344 - if( WikiError::isError( $overwrite ) ) {
345 - return $this->uploadError( $overwrite->toString() );
 434+
 435+ if( !is_null( $overwrite ) && $overwrite != 1 ) {
 436+ $resultDetails = array( 'overwrite' => $overwrite );
 437+ return self::OVERWRITE_EXISTING_FILE;
346438 }
347439
348440 /* Don't allow users to override the blacklist (check file extension) */
349441 global $wgStrictFileExtensions;
350442 global $wgFileExtensions, $wgFileBlacklist;
 443+
351444 if ($finalExt == '') {
352 - return $this->uploadError( wfMsgExt( 'filetype-missing', array ( 'parseinline' ) ) );
 445+ return self::FILETYPE_MISSING;
 446+
353447 } elseif ( $this->checkFileExtensionList( $ext, $wgFileBlacklist ) ||
354448 ($wgStrictFileExtensions && !$this->checkFileExtension( $finalExt, $wgFileExtensions ) ) ) {
355 - return $this->uploadError( wfMsgExt( 'filetype-badtype', array ( 'parseinline' ),
356 - htmlspecialchars( $finalExt ), implode ( ', ', $wgFileExtensions ) ) );
 449+ $resultDetails = array( 'finalExt' => $finalExt );
 450+ return self::FILETYPE_BADTYPE;
357451 }
358452
359453 /**
@@ -366,7 +460,8 @@
367461 $veri = $this->verify( $this->mTempPath, $finalExt );
368462
369463 if( $veri !== true ) { //it's a wiki error...
370 - return $this->uploadError( $veri->toString() );
 464+ $resultDetails = array( 'veri' => $veri );
 465+ return self::VERIFICATION_ERROR;
371466 }
372467
373468 /**
@@ -375,7 +470,8 @@
376471 $error = '';
377472 if( !wfRunHooks( 'UploadVerification',
378473 array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
379 - return $this->uploadError( $error );
 474+ $resultDetails = array( 'error' => $error );
 475+ return self::UPLOAD_VERIFICATION_ERROR;
380476 }
381477 }
382478
@@ -385,7 +481,6 @@
386482 */
387483 if ( ! $this->mIgnoreWarning ) {
388484 $warning = '';
389 -
390485 global $wgCapitalLinks;
391486 if( $wgCapitalLinks ) {
392487 $filtered = ucfirst( $filtered );
@@ -416,12 +511,14 @@
417512 if ( !$this->mDestWarningAck ) {
418513 $warning .= self::getExistsWarning( $this->mLocalFile );
419514 }
 515+
420516 if( $warning != '' ) {
421517 /**
422518 * Stash the file in a temporary location; the user can choose
423519 * to let it through and we'll complete the upload then.
424520 */
425 - return $this->uploadWarning( $warning );
 521+ $resultDetails = array( 'warning' => $warning );
 522+ return self::UPLOAD_WARNING;
426523 }
427524 }
428525
@@ -442,9 +539,9 @@
443540 $wgUser->addWatch( $this->mLocalFile->getTitle() );
444541 }
445542 // Success, redirect to description page
446 - $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
447543 $img = null; // @todo: added to avoid passing a ref to null - should this be defined somewhere?
448544 wfRunHooks( 'UploadComplete', array( &$img ) );
 545+ return self::SUCCESS;
449546 }
450547 }
451548
@@ -544,6 +641,7 @@
545642 $llink = $sk->makeKnownLinkObj( $ltitle, wfMsgHtml( 'deletionlog' ),
546643 'type=delete&page=' . $file->getTitle()->getPrefixedUrl() );
547644 $warning .= '<li>' . wfMsgWikiHtml( 'filewasdeleted', $llink ) . '</li>';
 645+
548646 }
549647 return $warning;
550648 }
@@ -758,7 +856,7 @@
759857 wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" );
760858 return false;
761859 }
762 -
 860+
763861 if( $this->mDesiredDestName && $wgUser->isAllowed( 'deletedhistory' ) ) {
764862 $title = Title::makeTitleSafe( NS_IMAGE, $this->mDesiredDestName );
765863 if( $title instanceof Title && ( $count = $title->isDeleted() ) > 0 ) {
@@ -1009,7 +1107,6 @@
10101108 #check mime type, if desired
10111109 global $wgVerifyMimeType;
10121110 if ($wgVerifyMimeType) {
1013 -
10141111 wfDebug ( "\n\nmime: <$mime> extension: <$extension>\n\n");
10151112 #check mime type against file extension
10161113 if( !$this->verifyExtension( $mime, $extension ) ) {
@@ -1020,6 +1117,7 @@
10211118 global $wgMimeTypeBlacklist;
10221119 if( isset($wgMimeTypeBlacklist) && !is_null($wgMimeTypeBlacklist)
10231120 && $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) ) {
 1121+
10241122 return new WikiErrorMsg( 'filetype-badmime', htmlspecialchars( $mime ) );
10251123 }
10261124 }
@@ -1038,6 +1136,7 @@
10391137 }
10401138
10411139 wfDebug( __METHOD__.": all clear; passing.\n" );
 1140+
10421141 return true;
10431142 }
10441143
@@ -1343,7 +1442,7 @@
13441443
13451444 if( $error ) {
13461445 $errorText = wfMsg( $error, wfEscapeWikiText( $img->getName() ) );
1347 - return new WikiError( $wgOut->parse( $errorText ) );
 1446+ return $errorText;
13481447 }
13491448
13501449 // Rockin', go ahead and upload
@@ -1411,3 +1510,5 @@
14121511 return $pageText;
14131512 }
14141513 }
 1514+
 1515+

Status & tagging log