Index: trunk/phase3/includes/SpecialUpload.php |
— | — | @@ -19,6 +19,20 @@ |
20 | 20 | * @addtogroup SpecialPage |
21 | 21 | */ |
22 | 22 | 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 | + |
23 | 37 | /**#@+ |
24 | 38 | * @access private |
25 | 39 | */ |
— | — | @@ -245,7 +259,7 @@ |
246 | 260 | } |
247 | 261 | $this->mainUploadForm(); |
248 | 262 | } else if( 'submit' == $this->mAction || $this->mUploadClicked ) { |
249 | | - $this->processUpload(); |
| 263 | + $this->processUpload(); |
250 | 264 | } else { |
251 | 265 | $this->mainUploadForm(); |
252 | 266 | } |
— | — | @@ -253,34 +267,104 @@ |
254 | 268 | $this->cleanupTempFile(); |
255 | 269 | } |
256 | 270 | |
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; |
258 | 287 | |
| 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 | + |
259 | 342 | /** |
260 | 343 | * Really do the upload |
261 | 344 | * Checks are made in SpecialUpload::execute() |
| 345 | + * |
| 346 | + * @param array $resultDetails contains result-specific dict of additional values |
| 347 | + * |
262 | 348 | * @access private |
263 | 349 | */ |
264 | | - function processUpload() { |
265 | | - global $wgUser, $wgOut; |
| 350 | + function internalProcessUpload( &$resultDetails ) { |
| 351 | + global $wgUser; |
266 | 352 | |
267 | 353 | if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) ) |
268 | 354 | { |
269 | 355 | wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file." ); |
270 | | - return false; |
| 356 | + return self::BEFORE_PROCESSING; |
271 | 357 | } |
272 | 358 | |
273 | 359 | /* Check for PHP error if any, requires php 4.2 or newer */ |
274 | 360 | if( $this->mCurlError == 1/*UPLOAD_ERR_INI_SIZE*/ ) { |
275 | | - $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) ); |
276 | | - return; |
| 361 | + return self::LARGE_FILE_SERVER; |
277 | 362 | } |
278 | 363 | |
279 | 364 | /** |
280 | 365 | * If there was no filename or a zero size given, give up quick. |
281 | 366 | */ |
282 | 367 | if( trim( $this->mSrcName ) == '' || empty( $this->mFileSize ) ) { |
283 | | - $this->mainUploadForm( wfMsgHtml( 'emptyfile' ) ); |
284 | | - return; |
| 368 | + return self::EMPTY_FILE; |
285 | 369 | } |
286 | 370 | |
287 | 371 | # Chop off any directories in the given filename |
— | — | @@ -311,8 +395,7 @@ |
312 | 396 | } |
313 | 397 | |
314 | 398 | if( strlen( $partname ) < 1 ) { |
315 | | - $this->mainUploadForm( wfMsgHtml( 'minlength1' ) ); |
316 | | - return; |
| 399 | + return self::MIN_LENGHT_PARTNAME; |
317 | 400 | } |
318 | 401 | |
319 | 402 | /** |
— | — | @@ -322,8 +405,8 @@ |
323 | 406 | $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $filtered ); |
324 | 407 | $nt = Title::makeTitleSafe( NS_IMAGE, $filtered ); |
325 | 408 | if( is_null( $nt ) ) { |
326 | | - $this->uploadError( wfMsgWikiHtml( 'illegalfilename', htmlspecialchars( $filtered ) ) ); |
327 | | - return; |
| 409 | + $resultDetails = array( 'filtered' => $filtered ); |
| 410 | + return self::ILLEGAL_FILENAME; |
328 | 411 | } |
329 | 412 | $this->mLocalFile = wfLocalFile( $nt ); |
330 | 413 | $this->mDestName = $this->mLocalFile->getName(); |
— | — | @@ -333,26 +416,27 @@ |
334 | 417 | * to modify it by uploading a new revision. |
335 | 418 | */ |
336 | 419 | if( !$nt->userCan( 'edit' ) ) { |
337 | | - return $this->uploadError( wfMsgWikiHtml( 'protectedpage' ) ); |
| 420 | + return self::PROTECTED_PAGE; |
338 | 421 | } |
339 | 422 | |
340 | 423 | /** |
341 | 424 | * In some cases we may forbid overwriting of existing files. |
342 | 425 | */ |
343 | 426 | $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; |
346 | 430 | } |
347 | 431 | |
348 | 432 | /* Don't allow users to override the blacklist (check file extension) */ |
349 | 433 | global $wgStrictFileExtensions; |
350 | 434 | global $wgFileExtensions, $wgFileBlacklist; |
351 | 435 | if ($finalExt == '') { |
352 | | - return $this->uploadError( wfMsgExt( 'filetype-missing', array ( 'parseinline' ) ) ); |
| 436 | + return self::FILETYPE_MISSING; |
353 | 437 | } elseif ( $this->checkFileExtensionList( $ext, $wgFileBlacklist ) || |
354 | 438 | ($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; |
357 | 441 | } |
358 | 442 | |
359 | 443 | /** |
— | — | @@ -366,7 +450,8 @@ |
367 | 451 | $veri = $this->verify( $this->mTempPath, $finalExt ); |
368 | 452 | |
369 | 453 | if( $veri !== true ) { //it's a wiki error... |
370 | | - return $this->uploadError( $veri->toString() ); |
| 454 | + $resultDetails = array( 'veri' => $veri ); |
| 455 | + return self::VERIFICATION_ERROR; |
371 | 456 | } |
372 | 457 | |
373 | 458 | /** |
— | — | @@ -375,7 +460,8 @@ |
376 | 461 | $error = ''; |
377 | 462 | if( !wfRunHooks( 'UploadVerification', |
378 | 463 | array( $this->mDestName, $this->mTempPath, &$error ) ) ) { |
379 | | - return $this->uploadError( $error ); |
| 464 | + $resultDetails = array( 'error' => $error ); |
| 465 | + return self::UPLOAD_VERIFICATION_ERROR; |
380 | 466 | } |
381 | 467 | } |
382 | 468 | |
— | — | @@ -397,7 +483,7 @@ |
398 | 484 | global $wgCheckFileExtensions; |
399 | 485 | if ( $wgCheckFileExtensions ) { |
400 | 486 | if ( ! $this->checkFileExtension( $finalExt, $wgFileExtensions ) ) { |
401 | | - $warning .= '<li>'.wfMsgExt( 'filetype-badtype', array ( 'parseinline' ), |
| 487 | + $warning .= '<li>'.wfMsgExt( 'filetype-badtype', array ( 'parseinline' ), |
402 | 488 | htmlspecialchars( $finalExt ), implode ( ', ', $wgFileExtensions ) ).'</li>'; |
403 | 489 | } |
404 | 490 | } |
— | — | @@ -421,7 +507,8 @@ |
422 | 508 | * Stash the file in a temporary location; the user can choose |
423 | 509 | * to let it through and we'll complete the upload then. |
424 | 510 | */ |
425 | | - return $this->uploadWarning( $warning ); |
| 511 | + $resultDetails = array( 'warning' => $warning ); |
| 512 | + return self::UPLOAD_WARNING; |
426 | 513 | } |
427 | 514 | } |
428 | 515 | |
— | — | @@ -432,7 +519,7 @@ |
433 | 520 | $pageText = self::getInitialPageText( $this->mComment, $this->mLicense, |
434 | 521 | $this->mCopyrightStatus, $this->mCopyrightSource ); |
435 | 522 | |
436 | | - $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText, |
| 523 | + $status = $this->mLocalFile->upload( $this->mTempPath, $this->mComment, $pageText, |
437 | 524 | File::DELETE_SOURCE, $this->mFileProps ); |
438 | 525 | if ( !$status->isGood() ) { |
439 | 526 | $this->showError( $status->getWikiText() ); |
— | — | @@ -442,9 +529,9 @@ |
443 | 530 | $wgUser->addWatch( $this->mLocalFile->getTitle() ); |
444 | 531 | } |
445 | 532 | // Success, redirect to description page |
446 | | - $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() ); |
447 | 533 | $img = null; // @todo: added to avoid passing a ref to null - should this be defined somewhere? |
448 | 534 | wfRunHooks( 'UploadComplete', array( &$img ) ); |
| 535 | + return self::SUCCESS; |
449 | 536 | } |
450 | 537 | } |
451 | 538 | |
— | — | @@ -1402,7 +1489,7 @@ |
1403 | 1490 | |
1404 | 1491 | if( $error ) { |
1405 | 1492 | $errorText = wfMsg( $error, wfEscapeWikiText( $img->getName() ) ); |
1406 | | - return new WikiError( $wgOut->parse( $errorText ) ); |
| 1493 | + return $errorText; |
1407 | 1494 | } |
1408 | 1495 | |
1409 | 1496 | // Rockin', go ahead and upload |