r26237 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26236‎ | r26237 | r26238 >
Date:05:32, 29 September 2007
Author:yurik
Status:old
Tags:
Comment:
Merged from apiedit_vodafone branch, r26236.
The functionality must not have changed - this commit separates the file upload into internalProcessUpload(), while leaving processUpload() to handle web-UI only.
Modified paths:
  • /trunk/phase3/includes/SpecialUpload.php (modified) (history)

Diff [purge]

Index: trunk/phase3/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,7 @@
246260 }
247261 $this->mainUploadForm();
248262 } else if( 'submit' == $this->mAction || $this->mUploadClicked ) {
249 - $this->processUpload();
 263+ $this->processUpload();
250264 } else {
251265 $this->mainUploadForm();
252266 }
@@ -253,34 +267,104 @@
254268 $this->cleanupTempFile();
255269 }
256270
257 - /* -------------------------------------------------------------- */
 271+ /**
 272+ * Do the upload
 273+ * Checks are made in SpecialUpload::execute()
 274+ *
 275+ * @access private
 276+ */
 277+ function processUpload(){
 278+ global $wgUser, $wgOut, $wgFileExtensions;
 279+ $details = null;
 280+ $value = null;
 281+ $value = $this->internalProcessUpload( $details );
 282+
 283+ switch($value) {
 284+ case self::SUCCESS:
 285+ $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
 286+ return;
258287
 288+ case self::BEFORE_PROCESSING:
 289+ return false;
 290+
 291+ case self::LARGE_FILE_SERVER:
 292+ $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) );
 293+ return;
 294+
 295+ case self::EMPTY_FILE:
 296+ $this->mainUploadForm( wfMsgHtml( 'emptyfile' ) );
 297+ return;
 298+
 299+ case self::MIN_LENGHT_PARTNAME:
 300+ $this->mainUploadForm( wfMsgHtml( 'minlength1' ) );
 301+ return;
 302+
 303+ case self::ILLEGAL_FILENAME:
 304+ $filtered = $details['filtered'];
 305+ $this->uploadError( wfMsgWikiHtml( 'illegalfilename', htmlspecialchars( $filtered ) ) );
 306+ return;
 307+
 308+ case self::PROTECTED_PAGE:
 309+ return $this->uploadError( wfMsgWikiHtml( 'protectedpage' ) );
 310+
 311+ case self::OVERWRITE_EXISTING_FILE:
 312+ $errorText = $details['overwrite'];
 313+ $overwrite = new WikiError( $wgOut->parse( $errorText ) );
 314+ return $this->uploadError( $overwrite->toString() );
 315+
 316+ case self::FILETYPE_MISSING:
 317+ return $this->uploadError( wfMsgExt( 'filetype-missing', array ( 'parseinline' ) ) );
 318+
 319+ case self::FILETYPE_BADTYPE:
 320+ $finalExt = $details['finalExt'];
 321+ return $this->uploadError( wfMsgExt( 'filetype-badtype', array ( 'parseinline' ), htmlspecialchars( $finalExt ), implode ( ', ', $wgFileExtensions ) ) );
 322+
 323+ case self::VERIFICATION_ERROR:
 324+ $veri = $details['veri'];
 325+ return $this->uploadError( $veri->toString() );
 326+
 327+ case self::UPLOAD_VERIFICATION_ERROR:
 328+ $error = $details['error'];
 329+ return $this->uploadError( $error );
 330+
 331+ case self::UPLOAD_WARNING:
 332+ $warning = $details['warning'];
 333+ return $this->uploadWarning( $warning );
 334+ }
 335+
 336+ /* TODO: Each case returns instead of breaking to maintain the highest level of compatibility during branch merging.
 337+ They should be reviewed and corrected separatelly.
 338+ */
 339+ new MWException( __METHOD__ . ": Unknown value `{$value}`" );
 340+ }
 341+
259342 /**
260343 * Really do the upload
261344 * Checks are made in SpecialUpload::execute()
 345+ *
 346+ * @param array $resultDetails contains result-specific dict of additional values
 347+ *
262348 * @access private
263349 */
264 - function processUpload() {
265 - global $wgUser, $wgOut;
 350+ function internalProcessUpload( &$resultDetails ) {
 351+ global $wgUser;
266352
267353 if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) )
268354 {
269355 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file." );
270 - return false;
 356+ return self::BEFORE_PROCESSING;
271357 }
272358
273359 /* Check for PHP error if any, requires php 4.2 or newer */
274360 if( $this->mCurlError == 1/*UPLOAD_ERR_INI_SIZE*/ ) {
275 - $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) );
276 - return;
 361+ return self::LARGE_FILE_SERVER;
277362 }
278363
279364 /**
280365 * If there was no filename or a zero size given, give up quick.
281366 */
282367 if( trim( $this->mSrcName ) == '' || empty( $this->mFileSize ) ) {
283 - $this->mainUploadForm( wfMsgHtml( 'emptyfile' ) );
284 - return;
 368+ return self::EMPTY_FILE;
285369 }
286370
287371 # Chop off any directories in the given filename
@@ -311,8 +395,7 @@
312396 }
313397
314398 if( strlen( $partname ) < 1 ) {
315 - $this->mainUploadForm( wfMsgHtml( 'minlength1' ) );
316 - return;
 399+ return self::MIN_LENGHT_PARTNAME;
317400 }
318401
319402 /**
@@ -322,8 +405,8 @@
323406 $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $filtered );
324407 $nt = Title::makeTitleSafe( NS_IMAGE, $filtered );
325408 if( is_null( $nt ) ) {
326 - $this->uploadError( wfMsgWikiHtml( 'illegalfilename', htmlspecialchars( $filtered ) ) );
327 - return;
 409+ $resultDetails = array( 'filtered' => $filtered );
 410+ return self::ILLEGAL_FILENAME;
328411 }
329412 $this->mLocalFile = wfLocalFile( $nt );
330413 $this->mDestName = $this->mLocalFile->getName();
@@ -333,26 +416,27 @@
334417 * to modify it by uploading a new revision.
335418 */
336419 if( !$nt->userCan( 'edit' ) ) {
337 - return $this->uploadError( wfMsgWikiHtml( 'protectedpage' ) );
 420+ return self::PROTECTED_PAGE;
338421 }
339422
340423 /**
341424 * In some cases we may forbid overwriting of existing files.
342425 */
343426 $overwrite = $this->checkOverwrite( $this->mDestName );
344 - if( WikiError::isError( $overwrite ) ) {
345 - return $this->uploadError( $overwrite->toString() );
 427+ if( $overwrite !== true ) {
 428+ $resultDetails = array( 'overwrite' => $overwrite );
 429+ return self::OVERWRITE_EXISTING_FILE;
346430 }
347431
348432 /* Don't allow users to override the blacklist (check file extension) */
349433 global $wgStrictFileExtensions;
350434 global $wgFileExtensions, $wgFileBlacklist;
351435 if ($finalExt == '') {
352 - return $this->uploadError( wfMsgExt( 'filetype-missing', array ( 'parseinline' ) ) );
 436+ return self::FILETYPE_MISSING;
353437 } elseif ( $this->checkFileExtensionList( $ext, $wgFileBlacklist ) ||
354438 ($wgStrictFileExtensions && !$this->checkFileExtension( $finalExt, $wgFileExtensions ) ) ) {
355 - return $this->uploadError( wfMsgExt( 'filetype-badtype', array ( 'parseinline' ),
356 - htmlspecialchars( $finalExt ), implode ( ', ', $wgFileExtensions ) ) );
 439+ $resultDetails = array( 'finalExt' => $finalExt );
 440+ return self::FILETYPE_BADTYPE;
357441 }
358442
359443 /**
@@ -366,7 +450,8 @@
367451 $veri = $this->verify( $this->mTempPath, $finalExt );
368452
369453 if( $veri !== true ) { //it's a wiki error...
370 - return $this->uploadError( $veri->toString() );
 454+ $resultDetails = array( 'veri' => $veri );
 455+ return self::VERIFICATION_ERROR;
371456 }
372457
373458 /**
@@ -375,7 +460,8 @@
376461 $error = '';
377462 if( !wfRunHooks( 'UploadVerification',
378463 array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
379 - return $this->uploadError( $error );
 464+ $resultDetails = array( 'error' => $error );
 465+ return self::UPLOAD_VERIFICATION_ERROR;
380466 }
381467 }
382468
@@ -397,7 +483,7 @@
398484 global $wgCheckFileExtensions;
399485 if ( $wgCheckFileExtensions ) {
400486 if ( ! $this->checkFileExtension( $finalExt, $wgFileExtensions ) ) {
401 - $warning .= '<li>'.wfMsgExt( 'filetype-badtype', array ( 'parseinline' ),
 487+ $warning .= '<li>'.wfMsgExt( 'filetype-badtype', array ( 'parseinline' ),
402488 htmlspecialchars( $finalExt ), implode ( ', ', $wgFileExtensions ) ).'</li>';
403489 }
404490 }
@@ -421,7 +507,8 @@
422508 * Stash the file in a temporary location; the user can choose
423509 * to let it through and we'll complete the upload then.
424510 */
425 - return $this->uploadWarning( $warning );
 511+ $resultDetails = array( 'warning' => $warning );
 512+ return self::UPLOAD_WARNING;
426513 }
427514 }
428515
@@ -432,7 +519,7 @@
433520 $pageText = self::getInitialPageText( $this->mComment, $this->mLicense,
434521 $this->mCopyrightStatus, $this->mCopyrightSource );
435522
436 - $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
 523+ $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText,
437524 File::DELETE_SOURCE, $this->mFileProps );
438525 if ( !$status->isGood() ) {
439526 $this->showError( $status->getWikiText() );
@@ -442,9 +529,9 @@
443530 $wgUser->addWatch( $this->mLocalFile->getTitle() );
444531 }
445532 // Success, redirect to description page
446 - $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
447533 $img = null; // @todo: added to avoid passing a ref to null - should this be defined somewhere?
448534 wfRunHooks( 'UploadComplete', array( &$img ) );
 535+ return self::SUCCESS;
449536 }
450537 }
451538
@@ -1402,7 +1489,7 @@
14031490
14041491 if( $error ) {
14051492 $errorText = wfMsg( $error, wfEscapeWikiText( $img->getName() ) );
1406 - return new WikiError( $wgOut->parse( $errorText ) );
 1493+ return $errorText;
14071494 }
14081495
14091496 // Rockin', go ahead and upload

Follow-up revisions

RevisionCommit summaryAuthorDate
r26257Merged revisions 26134-26247 via svnmerge from...david19:06, 30 September 2007

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r26236Synced with the root. This is the version that is being merged with the trunk.yurik05:20, 29 September 2007

Status & tagging log