Index: branches/ApiEdit_Vodafone/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,9 @@ |
246 | 260 | } |
247 | 261 | $this->mainUploadForm(); |
248 | 262 | } else if( 'submit' == $this->mAction || $this->mUploadClicked ) { |
249 | | - $this->processUpload(); |
| 263 | + $details = null; |
| 264 | + $retval = $this->processUpload( $details ); |
| 265 | + $this->processRest($retval, $details); |
250 | 266 | } else { |
251 | 267 | $this->mainUploadForm(); |
252 | 268 | } |
— | — | @@ -253,34 +269,107 @@ |
254 | 270 | $this->cleanupTempFile(); |
255 | 271 | } |
256 | 272 | |
| 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 | + |
257 | 344 | /* -------------------------------------------------------------- */ |
258 | 345 | |
259 | 346 | /** |
260 | 347 | * Really do the upload |
261 | 348 | * Checks are made in SpecialUpload::execute() |
| 349 | + * |
| 350 | + * @param array $resultDetails contains result-specific dict of additional values |
| 351 | + * |
262 | 352 | * @access private |
263 | 353 | */ |
264 | | - function processUpload() { |
| 354 | + function processUpload( &$resultDetails="" ) { |
265 | 355 | global $wgUser, $wgOut; |
266 | 356 | |
267 | 357 | if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) ) |
268 | 358 | { |
269 | 359 | wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file." ); |
270 | | - return false; |
| 360 | + return self::BEFORE_PROCESSING; |
| 361 | + |
271 | 362 | } |
272 | 363 | |
273 | 364 | /* Check for PHP error if any, requires php 4.2 or newer */ |
274 | 365 | if( $this->mCurlError == 1/*UPLOAD_ERR_INI_SIZE*/ ) { |
275 | | - $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) ); |
276 | | - return; |
| 366 | + return self::LARGE_FILE_SERVER; |
277 | 367 | } |
278 | 368 | |
279 | 369 | /** |
280 | 370 | * If there was no filename or a zero size given, give up quick. |
281 | 371 | */ |
282 | 372 | if( trim( $this->mSrcName ) == '' || empty( $this->mFileSize ) ) { |
283 | | - $this->mainUploadForm( wfMsgHtml( 'emptyfile' ) ); |
284 | | - return; |
| 373 | + return self::EMPTY_FILE; |
285 | 374 | } |
286 | 375 | |
287 | 376 | # Chop off any directories in the given filename |
— | — | @@ -309,10 +398,10 @@ |
310 | 399 | for( $i = 0; $i < count( $ext ) - 1; $i++ ) |
311 | 400 | $partname .= '.' . $ext[$i]; |
312 | 401 | } |
| 402 | + |
313 | 403 | |
314 | 404 | if( strlen( $partname ) < 1 ) { |
315 | | - $this->mainUploadForm( wfMsgHtml( 'minlength1' ) ); |
316 | | - return; |
| 405 | + return self::MIN_LENGHT_PARTNAME; |
317 | 406 | } |
318 | 407 | |
319 | 408 | /** |
— | — | @@ -321,9 +410,10 @@ |
322 | 411 | */ |
323 | 412 | $filtered = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $filtered ); |
324 | 413 | $nt = Title::makeTitleSafe( NS_IMAGE, $filtered ); |
| 414 | + |
325 | 415 | if( is_null( $nt ) ) { |
326 | | - $this->uploadError( wfMsgWikiHtml( 'illegalfilename', htmlspecialchars( $filtered ) ) ); |
327 | | - return; |
| 416 | + $resultDetails = array( 'filtered' => $filtered ); |
| 417 | + return self::ILLEGAL_FILENAME; |
328 | 418 | } |
329 | 419 | $this->mLocalFile = wfLocalFile( $nt ); |
330 | 420 | $this->mDestName = $this->mLocalFile->getName(); |
— | — | @@ -333,26 +423,30 @@ |
334 | 424 | * to modify it by uploading a new revision. |
335 | 425 | */ |
336 | 426 | if( !$nt->userCan( 'edit' ) ) { |
337 | | - return $this->uploadError( wfMsgWikiHtml( 'protectedpage' ) ); |
| 427 | + return self::PROTECTED_PAGE; |
338 | 428 | } |
339 | 429 | |
340 | 430 | /** |
341 | 431 | * In some cases we may forbid overwriting of existing files. |
342 | 432 | */ |
343 | 433 | $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; |
346 | 438 | } |
347 | 439 | |
348 | 440 | /* Don't allow users to override the blacklist (check file extension) */ |
349 | 441 | global $wgStrictFileExtensions; |
350 | 442 | global $wgFileExtensions, $wgFileBlacklist; |
| 443 | + |
351 | 444 | if ($finalExt == '') { |
352 | | - return $this->uploadError( wfMsgExt( 'filetype-missing', array ( 'parseinline' ) ) ); |
| 445 | + return self::FILETYPE_MISSING; |
| 446 | + |
353 | 447 | } elseif ( $this->checkFileExtensionList( $ext, $wgFileBlacklist ) || |
354 | 448 | ($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; |
357 | 451 | } |
358 | 452 | |
359 | 453 | /** |
— | — | @@ -366,7 +460,8 @@ |
367 | 461 | $veri = $this->verify( $this->mTempPath, $finalExt ); |
368 | 462 | |
369 | 463 | if( $veri !== true ) { //it's a wiki error... |
370 | | - return $this->uploadError( $veri->toString() ); |
| 464 | + $resultDetails = array( 'veri' => $veri ); |
| 465 | + return self::VERIFICATION_ERROR; |
371 | 466 | } |
372 | 467 | |
373 | 468 | /** |
— | — | @@ -375,7 +470,8 @@ |
376 | 471 | $error = ''; |
377 | 472 | if( !wfRunHooks( 'UploadVerification', |
378 | 473 | array( $this->mDestName, $this->mTempPath, &$error ) ) ) { |
379 | | - return $this->uploadError( $error ); |
| 474 | + $resultDetails = array( 'error' => $error ); |
| 475 | + return self::UPLOAD_VERIFICATION_ERROR; |
380 | 476 | } |
381 | 477 | } |
382 | 478 | |
— | — | @@ -385,7 +481,6 @@ |
386 | 482 | */ |
387 | 483 | if ( ! $this->mIgnoreWarning ) { |
388 | 484 | $warning = ''; |
389 | | - |
390 | 485 | global $wgCapitalLinks; |
391 | 486 | if( $wgCapitalLinks ) { |
392 | 487 | $filtered = ucfirst( $filtered ); |
— | — | @@ -416,12 +511,14 @@ |
417 | 512 | if ( !$this->mDestWarningAck ) { |
418 | 513 | $warning .= self::getExistsWarning( $this->mLocalFile ); |
419 | 514 | } |
| 515 | + |
420 | 516 | if( $warning != '' ) { |
421 | 517 | /** |
422 | 518 | * Stash the file in a temporary location; the user can choose |
423 | 519 | * to let it through and we'll complete the upload then. |
424 | 520 | */ |
425 | | - return $this->uploadWarning( $warning ); |
| 521 | + $resultDetails = array( 'warning' => $warning ); |
| 522 | + return self::UPLOAD_WARNING; |
426 | 523 | } |
427 | 524 | } |
428 | 525 | |
— | — | @@ -442,9 +539,9 @@ |
443 | 540 | $wgUser->addWatch( $this->mLocalFile->getTitle() ); |
444 | 541 | } |
445 | 542 | // Success, redirect to description page |
446 | | - $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() ); |
447 | 543 | $img = null; // @todo: added to avoid passing a ref to null - should this be defined somewhere? |
448 | 544 | wfRunHooks( 'UploadComplete', array( &$img ) ); |
| 545 | + return self::SUCCESS; |
449 | 546 | } |
450 | 547 | } |
451 | 548 | |
— | — | @@ -544,6 +641,7 @@ |
545 | 642 | $llink = $sk->makeKnownLinkObj( $ltitle, wfMsgHtml( 'deletionlog' ), |
546 | 643 | 'type=delete&page=' . $file->getTitle()->getPrefixedUrl() ); |
547 | 644 | $warning .= '<li>' . wfMsgWikiHtml( 'filewasdeleted', $llink ) . '</li>'; |
| 645 | + |
548 | 646 | } |
549 | 647 | return $warning; |
550 | 648 | } |
— | — | @@ -758,7 +856,7 @@ |
759 | 857 | wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" ); |
760 | 858 | return false; |
761 | 859 | } |
762 | | - |
| 860 | + |
763 | 861 | if( $this->mDesiredDestName && $wgUser->isAllowed( 'deletedhistory' ) ) { |
764 | 862 | $title = Title::makeTitleSafe( NS_IMAGE, $this->mDesiredDestName ); |
765 | 863 | if( $title instanceof Title && ( $count = $title->isDeleted() ) > 0 ) { |
— | — | @@ -1009,7 +1107,6 @@ |
1010 | 1108 | #check mime type, if desired |
1011 | 1109 | global $wgVerifyMimeType; |
1012 | 1110 | if ($wgVerifyMimeType) { |
1013 | | - |
1014 | 1111 | wfDebug ( "\n\nmime: <$mime> extension: <$extension>\n\n"); |
1015 | 1112 | #check mime type against file extension |
1016 | 1113 | if( !$this->verifyExtension( $mime, $extension ) ) { |
— | — | @@ -1020,6 +1117,7 @@ |
1021 | 1118 | global $wgMimeTypeBlacklist; |
1022 | 1119 | if( isset($wgMimeTypeBlacklist) && !is_null($wgMimeTypeBlacklist) |
1023 | 1120 | && $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) ) { |
| 1121 | + |
1024 | 1122 | return new WikiErrorMsg( 'filetype-badmime', htmlspecialchars( $mime ) ); |
1025 | 1123 | } |
1026 | 1124 | } |
— | — | @@ -1038,6 +1136,7 @@ |
1039 | 1137 | } |
1040 | 1138 | |
1041 | 1139 | wfDebug( __METHOD__.": all clear; passing.\n" ); |
| 1140 | + |
1042 | 1141 | return true; |
1043 | 1142 | } |
1044 | 1143 | |
— | — | @@ -1343,7 +1442,7 @@ |
1344 | 1443 | |
1345 | 1444 | if( $error ) { |
1346 | 1445 | $errorText = wfMsg( $error, wfEscapeWikiText( $img->getName() ) ); |
1347 | | - return new WikiError( $wgOut->parse( $errorText ) ); |
| 1446 | + return $errorText; |
1348 | 1447 | } |
1349 | 1448 | |
1350 | 1449 | // Rockin', go ahead and upload |
— | — | @@ -1411,3 +1510,5 @@ |
1412 | 1511 | return $pageText; |
1413 | 1512 | } |
1414 | 1513 | } |
| 1514 | + |
| 1515 | + |