r55604 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55603‎ | r55604 | r55605 >
Date:17:05, 26 August 2009
Author:btongminh
Status:resolved (Comments)
Tags:
Comment:
* Code style & commenting on upload functions.
* Move isValidURI from UploadFromUrl to HttpFunctions.
* Made some functions in UploadBase static.
Modified paths:
  • /trunk/phase3/docs/upload.txt (modified) (history)
  • /trunk/phase3/includes/HttpFunctions.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadBase.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadFromChunks.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadFromFile.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadFromStash.php (modified) (history)
  • /trunk/phase3/includes/upload/UploadFromUrl.php (modified) (history)

Diff [purge]

Index: trunk/phase3/docs/upload.txt
@@ -1,41 +1,2 @@
2 -Special:Upload:
3 -
4 -wfSpecialUpload
5 - new UploadForm
6 - mUpload = new UploadFrom...
7 - execute()
8 - $wgEnableUploads
9 - isAllowed(upload)
10 - isBlocked()
11 - wfReadOnly()
12 - processUpload()
13 - internalProcessUpload()
14 - wfRunHooks(UploadForm:BeforeProcessing)
15 - mUpload->getTitle()
16 - wfStripIllegalFilenameChars
17 - splitExtensions()
18 - checkFileExtension()
19 - Title::makeTitleSafe
20 - getUserPermissionsErrors(edit; upload; create)
21 - mUpload->verifyUpload()
22 - empty(mFileSize)
23 - getTitle()
24 - checkOverwrite()
25 - fetchFile()
26 - verifyFile()
27 - checkMacBinary()
28 - wfRunHooks(UploadVerification)
29 - if(!ignoreWarning) mUpload->checkWarnings()
30 - getInitialPageText()
31 - mUpload->performUpload()
32 - mLocalFile->upload()
33 - if(isGood() && $watch) addWatch()
34 - if(isGood()) wfRunHooks(UploadComplete)
35 - wfRunHooks(SpecialUploadComplete)
36 -
37 -Changes:
38 - * "Your file will be renamed to $1" check now done on the result of
39 - Title::makeTitleSafe instead of filteredName
40 - * getExistWarning only really does existence checks
41 - * Other stuff forgotten to be documented
42 -
\ No newline at end of file
 2+This document describes how the current uploading system is build up and how
 3+custom backends can be built. (At least someday it will).
Index: trunk/phase3/includes/upload/UploadFromStash.php
@@ -1,8 +1,15 @@
22 <?php
3 -
 3+/**
 4+ * @file
 5+ * @ingroup upload
 6+ *
 7+ * Implements uploading from previously stored file.
 8+ *
 9+ * @author Bryan Tong Minh
 10+ */
 11+
412 class UploadFromStash extends UploadBase {
5 -
6 - static function isValidSessionKey( $key, $sessionData ) {
 13+ public static function isValidSessionKey( $key, $sessionData ) {
714 return !empty( $key ) &&
815 is_array( $sessionData ) &&
916 isset( $sessionData[$key] ) &&
@@ -10,7 +17,7 @@
1118 $sessionData[$key]['version'] == self::SESSION_VERSION;
1219 }
1320
14 - static function isValidRequest( $request ) {
 21+ public static function isValidRequest( $request ) {
1522 $sessionData = $request->getSessionData( 'wsUploadData' );
1623 return self::isValidSessionKey(
1724 $request->getInt( 'wpSessionKey' ),
@@ -20,7 +27,7 @@
2128 /*
2229 * some $na vars for uploadBase method compatibility.
2330 */
24 - function initialize( $name, $sessionData, $na, $na2=false ) {
 31+ public function initialize( $name, $sessionData, $na, $na2=false ) {
2532 /**
2633 * Confirming a temporarily stashed upload.
2734 * We don't want path names to be forged, so we keep
@@ -36,7 +43,7 @@
3744 $this->mFileProps = $sessionData['mFileProps'];
3845 }
3946
40 - function initializeFromRequest( &$request ) {
 47+ public function initializeFromRequest( &$request ) {
4148 $sessionKey = $request->getInt( 'wpSessionKey' );
4249 $sessionData = $request->getSessionData('wsUploadData');
4350
@@ -56,7 +63,7 @@
5764 /**
5865 * We're here from "ignore warnings anyway" so return just OK
5966 */
60 - function checkWarnings() {
 67+ public function checkWarnings() {
6168 return array();
6269 }
6370
Index: trunk/phase3/includes/upload/UploadFromUrl.php
@@ -1,5 +1,13 @@
22 <?php
3 -
 3+/**
 4+ * @file
 5+ * @ingroup upload
 6+ *
 7+ * Implements uploading from a HTTP resource.
 8+ *
 9+ * @author Bryan Tong Minh
 10+ * @author Michael Dale
 11+ */
412 class UploadFromUrl extends UploadBase {
513 protected $mTempDownloadPath;
614
@@ -7,9 +15,10 @@
816 protected $dl_mode = Http::SYNC_DOWNLOAD;
917
1018 /**
11 - * Checks if the user is allowed to use the upload-by-URL feature
 19+ * Checks if the user is allowed to use the upload-by-URL feature. If the
 20+ * user is allowed, pass on permissions checking to the parent.
1221 */
13 - static function isAllowed( $user ) {
 22+ public static function isAllowed( $user ) {
1423 if( !$user->isAllowed( 'upload_by_url' ) )
1524 return 'upload_by_url';
1625 return parent::isAllowed( $user );
@@ -18,13 +27,15 @@
1928 /**
2029 * Checks if the upload from URL feature is enabled
2130 */
22 - static function isEnabled() {
 31+ public static function isEnabled() {
2332 global $wgAllowCopyUploads;
2433 return $wgAllowCopyUploads && parent::isEnabled();
2534 }
2635
27 - /* entry point for API upload:: ASYNC_DOWNLOAD (if possible) */
28 - function initialize( $name, $url, $asyncdownload, $na = false ) {
 36+ /**
 37+ * Entry point for API upload:: ASYNC_DOWNLOAD (if possible)
 38+ */
 39+ public function initialize( $name, $url, $asyncdownload, $na = false ) {
2940 global $wgTmpDirectory, $wgPhpCli;
3041
3142 // check for $asyncdownload request:
@@ -36,8 +47,8 @@
3748 }
3849 }
3950
40 - $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
41 - parent::initialize( $name, $local_file, 0, true );
 51+ $localFile = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
 52+ parent::initialize( $name, $localFile, 0, true );
4253
4354 $this->mUrl = trim( $url );
4455 }
@@ -50,7 +61,7 @@
5162 * Entry point for SpecialUpload no ASYNC_DOWNLOAD possible
5263 * @param $request Object: WebRequest object
5364 */
54 - function initializeFromRequest( &$request ) {
 65+ public function initializeFromRequest( &$request ) {
5566
5667 // set dl mode if not set:
5768 if( !$this->dl_mode )
@@ -69,16 +80,16 @@
7081 /**
7182 * Do the real fetching stuff
7283 */
73 - function fetchFile() {
74 - // entry point for SpecialUplaod
75 - if( self::isValidURI( $this->mUrl ) === false ) {
 84+ public function fetchFile() {
 85+ // Entry point for SpecialUpload
 86+ if( Http::isValidURI( $this->mUrl ) === false ) {
7687 return Status::newFatal( 'upload-proto-error' );
7788 }
7889
79 - // now do the actual download to the target file:
 90+ // Now do the actual download to the target file:
8091 $status = Http::doDownload( $this->mUrl, $this->mTempPath, $this->dl_mode );
8192
82 - // update the local filesize var:
 93+ // Update the local filesize var:
8394 $this->mFileSize = filesize( $this->mTempPath );
8495
8596 return $status;
@@ -87,23 +98,13 @@
8899 /**
89100 * @param $request Object: WebRequest object
90101 */
91 - static function isValidRequest( $request ){
 102+ public static function isValidRequest( $request ){
92103 if( !$request->getVal( 'wpUploadFileURL' ) )
93104 return false;
94105 // check that is a valid url:
95 - return self::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
 106+ return Http::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
96107 }
97108
98 - /**
99 - * Checks that the given URI is a valid one
100 - * @param $uri Mixed: URI to check for validity
101 - */
102 - static function isValidURI( $uri ){
103 - return preg_match(
104 - '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
105 - $uri,
106 - $matches
107 - );
108 - }
109109
 110+
110111 }
\ No newline at end of file
Index: trunk/phase3/includes/upload/UploadBase.php
@@ -1,11 +1,24 @@
22 <?php
 3+/**
 4+ * @file
 5+ * @ingroup upload
 6+ *
 7+ * UploadBase and subclasses are the backend of MediaWiki's file uploads.
 8+ * The frontends are formed by ApiUpload and SpecialUpload.
 9+ *
 10+ * See also includes/docs/upload.txt
 11+ *
 12+ * @author Brion Vibber
 13+ * @author Bryan Tong Minh
 14+ * @author Michael Dale
 15+ */
 16+
 17+abstract class UploadBase {
 18+ protected $mTempPath;
 19+ protected $mDesiredDestName, $mDestName, $mRemoveTempFile, $mSourceType;
 20+ protected $mTitle = false, $mTitleError = 0;
 21+ protected $mFilteredName, $mFinalExtension;
322
4 -class UploadBase {
5 - var $mTempPath;
6 - var $mDesiredDestName, $mDestName, $mRemoveTempFile, $mSourceType;
7 - var $mTitle = false, $mTitleError = 0;
8 - var $mFilteredName, $mFinalExtension;
9 -
1023 const SUCCESS = 0;
1124 const OK = 0;
1225 const BEFORE_PROCESSING = 1;
@@ -29,7 +42,7 @@
3043 * Returns true if uploads are enabled.
3144 * Can be override by subclasses.
3245 */
33 - static function isEnabled() {
 46+ public static function isEnabled() {
3447 global $wgEnableUploads;
3548 if ( !$wgEnableUploads )
3649 return false;
@@ -46,7 +59,7 @@
4760 * identifying the missing permission.
4861 * Can be overriden by subclasses.
4962 */
50 - static function isAllowed( $user ) {
 63+ public static function isAllowed( $user ) {
5164 if( !$user->isAllowed( 'upload' ) )
5265 return 'upload';
5366 return true;
@@ -58,21 +71,24 @@
5972 /**
6073 * Create a form of UploadBase depending on wpSourceType and initializes it
6174 */
62 - static function createFromRequest( &$request, $type = null ) {
 75+ public static function createFromRequest( &$request, $type = null ) {
6376 $type = $type ? $type : $request->getVal( 'wpSourceType' );
6477
6578 if( !$type )
6679 return null;
6780
 81+ // Get the upload class
6882 $type = ucfirst( $type );
6983 $className = 'UploadFrom' . $type;
7084 wfDebug( __METHOD__ . ": class name: $className\n" );
7185 if( !in_array( $type, self::$uploadHandlers ) )
7286 return null;
7387
 88+ // Check whether this upload class is enabled
7489 if( !call_user_func( array( $className, 'isEnabled' ) ) )
7590 return null;
7691
 92+ // Check whether the request is valid
7793 if( !call_user_func( array( $className, 'isValidRequest' ), $request ) )
7894 return null;
7995
@@ -85,33 +101,38 @@
86102 /**
87103 * Check whether a request if valid for this handler
88104 */
89 - static function isValidRequest( $request ) {
 105+ public static function isValidRequest( $request ) {
90106 return false;
91107 }
92108
93 - function __construct() {}
 109+ public function __construct() {}
94110
95111 /**
96112 * Do the real variable initialization
97113 */
98 - function initialize( $name, $tempPath, $fileSize, $removeTempFile = false ) {
 114+ public function initialize( $name, $tempPath, $fileSize, $removeTempFile = false ) {
99115 $this->mDesiredDestName = $name;
100116 $this->mTempPath = $tempPath;
101117 $this->mFileSize = $fileSize;
102118 $this->mRemoveTempFile = $removeTempFile;
103119 }
 120+
 121+ /**
 122+ * Initialize from a WebRequest. Override this in a subclass.
 123+ */
 124+ public abstract function initializeFromRequest( &$request );
104125
105126 /**
106127 * Fetch the file. Usually a no-op
107128 */
108 - function fetchFile() {
 129+ public function fetchFile() {
109130 return Status::newGood();
110131 }
111132
112133 /**
113134 * Return the file size
114135 */
115 - function isEmptyFile(){
 136+ public function isEmptyFile(){
116137 return empty( $this->mFileSize );
117138 }
118139
@@ -119,7 +140,7 @@
120141 * Verify whether the upload is sane.
121142 * Returns self::OK or else an array with error information
122143 */
123 - function verifyUpload() {
 144+ public function verifyUpload() {
124145 /**
125146 * If there was no filename or a zero size given, give up quick.
126147 */
@@ -135,8 +156,7 @@
136157 $result['finalExt'] = $this->mFinalExtension;
137158 return $result;
138159 }
139 - $this->mLocalFile = wfLocalFile( $nt );
140 - $this->mDestName = $this->mLocalFile->getName();
 160+ $this->mDestName = $this->getLocalFile()->getName();
141161
142162 /**
143163 * In some cases we may forbid overwriting of existing files.
@@ -171,7 +191,7 @@
172192 /**
173193 * Verifies that it's ok to include the uploaded file
174194 *
175 - * this function seems to intermixes tmpfile and $this->mTempPath .. no idea why this is
 195+ * FIXME: this function seems to intermixes tmpfile and $this->mTempPath .. no idea why this is
176196 *
177197 * @param string $tmpfile the full path of the temporary file to verify
178198 * @return mixed true of the file is verified, a string or array otherwise.
@@ -186,7 +206,7 @@
187207
188208 #check mime type, if desired
189209 global $wgVerifyMimeType;
190 - if ($wgVerifyMimeType) {
 210+ if ( $wgVerifyMimeType ) {
191211 global $wgMimeTypeBlacklist;
192212 if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) )
193213 return array( 'filetype-badmime', $mime );
@@ -205,11 +225,11 @@
206226 }
207227
208228 #check for htmlish code and javascript
209 - if( $this->detectScript( $tmpfile, $mime, $this->mFinalExtension ) ) {
 229+ if( self::detectScript( $tmpfile, $mime, $this->mFinalExtension ) ) {
210230 return 'uploadscripted';
211231 }
212232 if( $this->mFinalExtension == 'svg' || $mime == 'image/svg+xml' ) {
213 - if( $this->detectScriptInSvg( $tmpfile ) ) {
 233+ if( self::detectScriptInSvg( $tmpfile ) ) {
214234 return 'uploadscripted';
215235 }
216236 }
@@ -226,9 +246,13 @@
227247 }
228248
229249 /**
230 - * Check whether the user can edit, upload and create the image
 250+ * Check whether the user can edit, upload and create the image.
 251+ *
 252+ * @param User $user the user to verify the permissions against
 253+ * @return mixed An array as returned by getUserPermissionsErrors or true
 254+ * in case the user has proper permissions.
231255 */
232 - function verifyPermissions( $user ) {
 256+ public function verifyPermissions( $user ) {
233257 /**
234258 * If the image is protected, non-sysop users won't be able
235259 * to modify it by uploading a new revision.
@@ -249,11 +273,14 @@
250274
251275 /**
252276 * Check for non fatal problems with the file
 277+ *
 278+ * @return array Array of warnings
253279 */
254 - function checkWarnings() {
 280+ public function checkWarnings() {
255281 $warning = array();
256282
257 - $filename = $this->mLocalFile->getName();
 283+ $localFile = $this->getLocalFile();
 284+ $filename = $localFile->getName();
258285 $n = strrpos( $filename, '.' );
259286 $partname = $n ? substr( $filename, 0, $n ) : $filename;
260287
@@ -284,14 +311,14 @@
285312 $warning['emptyfile'] = true;
286313
287314
288 - $exists = self::getExistsWarning( $this->mLocalFile );
 315+ $exists = self::getExistsWarning( $localFile );
289316 if( $exists !== false )
290317 $warning['exists'] = $exists;
291318
292319 // Check whether this may be a thumbnail
293320 if( $exists !== false && $exists[0] != 'thumb'
294 - && self::isThumbName( $this->mLocalFile->getName() ) ){
295 - //make the title:
 321+ && self::isThumbName( $filename ) ){
 322+ // Make the title
296323 $nt = $this->getTitle();
297324 $warning['file-thumbnail-no'] = substr( $filename, 0,
298325 strpos( $nt->getText() , '-' ) +1 );
@@ -324,22 +351,25 @@
325352
326353 # If the file existed before and was deleted, warn the user of this
327354 # Don't bother doing so if the file exists now, however
328 - if( $this->mLocalFile->wasDeleted() && !$this->mLocalFile->exists() )
329 - $warning['filewasdeleted'] = $this->mLocalFile->getTitle();
 355+ if( $localFile->wasDeleted() && !$localFile->exists() )
 356+ $warning['filewasdeleted'] = $localFile->getTitle();
330357
331358 return $warning;
332359 }
333360
334361 /**
335 - * Really perform the upload.
 362+ * Really perform the upload. Stores the file in the local repo, watches
 363+ * if necessary and runs the UploadComplete hook.
 364+ *
 365+ * @return mixed Status indicating the whether the upload succeeded.
336366 */
337 - function performUpload( $comment, $pageText, $watch, $user ) {
 367+ public function performUpload( $comment, $pageText, $watch, $user ) {
338368 wfDebug( "\n\n\performUpload: sum:" . $comment . ' c: ' . $pageText . ' w:' . $watch );
339 - $status = $this->mLocalFile->upload( $this->mTempPath, $comment, $pageText,
 369+ $status = $this->getLocalFile()->upload( $this->mTempPath, $comment, $pageText,
340370 File::DELETE_SOURCE, $this->mFileProps, false, $user );
341371
342372 if( $status->isGood() && $watch )
343 - $user->addWatch( $this->mLocalFile->getTitle() );
 373+ $user->addWatch( $this->getLocalFile()->getTitle() );
344374
345375 if( $status->isGood() )
346376 wfRunHooks( 'UploadComplete', array( &$this ) );
@@ -348,9 +378,12 @@
349379 }
350380
351381 /**
352 - * Returns a title or null
 382+ * Returns the title of the file to be uploaded. Sets mTitleError in case
 383+ * the name was illegal.
 384+ *
 385+ * @return Title The title of the file or null in case the name was illegal
353386 */
354 - function getTitle() {
 387+ public function getTitle() {
355388 if ( $this->mTitle !== false )
356389 return $this->mTitle;
357390
@@ -415,7 +448,10 @@
416449 return $this->mTitle = $nt;
417450 }
418451
419 - function getLocalFile() {
 452+ /**
 453+ * Return the local file and initializes if necessary.
 454+ */
 455+ public function getLocalFile() {
420456 if( is_null( $this->mLocalFile ) ) {
421457 $nt = $this->getTitle();
422458 $this->mLocalFile = is_null( $nt ) ? null : wfLocalFile( $nt );
@@ -435,14 +471,20 @@
436472 * @return string - full path the stashed file, or false on failure
437473 * @access private
438474 */
439 - function saveTempUploadedFile( $saveName, $tempName ) {
 475+ protected function saveTempUploadedFile( $saveName, $tempName ) {
440476 $repo = RepoGroup::singleton()->getLocalRepo();
441477 $status = $repo->storeTemp( $saveName, $tempName );
442478 return $status;
443479 }
444480
445 - /* append to a stashed file */
446 - function appendToUploadFile( $srcPath, $toAppendPath ){
 481+ /**
 482+ * Append a file to a stashed file.
 483+ *
 484+ * @param string $srcPath Path to file to append from
 485+ * @param string $toAppendPath Path to file to append to
 486+ * @return Status Status
 487+ */
 488+ public function appendToUploadFile( $srcPath, $toAppendPath ){
447489 $repo = RepoGroup::singleton()->getLocalRepo();
448490 $status = $repo->append( $srcPath, $toAppendPath );
449491 return $status;
@@ -454,21 +496,19 @@
455497 * Returns a key value which will be passed through a form
456498 * to pick up the path info on a later invocation.
457499 *
458 - * @return int
459 - * @access private
 500+ * @return int Session key
460501 */
461 - function stashSession() {
 502+ public function stashSession() {
462503 $status = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
463504 if( !$status->isOK() ) {
464505 # Couldn't save the file.
465506 return false;
466507 }
467 - $mTempPath = $status->value;
468508 if(!isset($_SESSION))
469509 session_start(); // start up the session (might have been previously closed to prevent php session locking)
470510 $key = $this->getSessionKey();
471511 $_SESSION['wsUploadData'][$key] = array(
472 - 'mTempPath' => $mTempPath,
 512+ 'mTempPath' => $status->value,
473513 'mFileSize' => $this->mFileSize,
474514 'mFileProps' => $this->mFileProps,
475515 'version' => self::SESSION_VERSION,
@@ -477,9 +517,9 @@
478518 }
479519
480520 /**
481 - * Pull session key gen from stash in cases where we want to start an upload without much information
 521+ * Generate a random session key from stash in cases where we want to start an upload without much information
482522 */
483 - function getSessionKey(){
 523+ protected function getSessionKey(){
484524 $key = mt_rand( 0, 0x7fffffff );
485525 $_SESSION['wsUploadData'][$key] = array();
486526 return $key;
@@ -489,7 +529,7 @@
490530 * Remove a temporarily kept file stashed by saveTempUploadedFile().
491531 * @return success
492532 */
493 - function unsaveUploadedFile() {
 533+ public function unsaveUploadedFile() {
494534 $repo = RepoGroup::singleton()->getLocalRepo();
495535 $success = $repo->freeTemp( $this->mTempPath );
496536 return $success;
@@ -500,14 +540,14 @@
501541 * on exit to clean up.
502542 * @access private
503543 */
504 - function cleanupTempFile() {
 544+ public function cleanupTempFile() {
505545 if ( $this->mRemoveTempFile && $this->mTempPath && file_exists( $this->mTempPath ) ) {
506546 wfDebug( __METHOD__ . ": Removing temporary file {$this->mTempPath}\n" );
507547 unlink( $this->mTempPath );
508548 }
509549 }
510550
511 - function getTempPath() {
 551+ public function getTempPath() {
512552 return $this->mTempPath;
513553 }
514554
@@ -602,7 +642,7 @@
603643 * @param string $extension The extension of the file
604644 * @return bool true if the file contains something looking like embedded scripts
605645 */
606 - function detectScript( $file, $mime, $extension ) {
 646+ public static function detectScript( $file, $mime, $extension ) {
607647 global $wgAllowTitlesInSVG;
608648
609649 #ugly hack: for text files, always look at the entire file.
@@ -700,7 +740,7 @@
701741 return false;
702742 }
703743
704 - function detectScriptInSvg( $filename ) {
 744+ protected function detectScriptInSvg( $filename ) {
705745 $check = new XmlTypeCheck( $filename, array( $this, 'checkSvgScriptCallback' ) );
706746 return $check->filterMatch;
707747 }
@@ -708,7 +748,7 @@
709749 /**
710750 * @todo Replace this with a whitelist filter!
711751 */
712 - function checkSvgScriptCallback( $element, $attribs ) {
 752+ public function checkSvgScriptCallback( $element, $attribs ) {
713753 $stripped = $this->stripXmlNamespace( $element );
714754
715755 if( $stripped == 'script' ) {
@@ -745,7 +785,7 @@
746786 * or a string containing feedback from the virus scanner if a virus was found.
747787 * If textual feedback is missing but a virus was found, this function returns true.
748788 */
749 - function detectVirus( $file ) {
 789+ public static function detectVirus( $file ) {
750790 global $wgAntivirus, $wgAntivirusSetup, $wgAntivirusRequired, $wgOut;
751791
752792 if ( !$wgAntivirus ) {
@@ -843,7 +883,7 @@
844884 *
845885 * @access private
846886 */
847 - function checkMacBinary() {
 887+ private function checkMacBinary() {
848888 $macbin = new MacBinary( $this->mTempPath );
849889 if( $macbin->isValid() ) {
850890 $dataFile = tempnam( wfTempDir(), 'WikiMacBinary' );
@@ -865,18 +905,19 @@
866906 * Check if there's an overwrite conflict and, if so, if restrictions
867907 * forbid this user from performing the upload.
868908 *
869 - * @return mixed true on success, WikiError on failure
 909+ * @return mixed true on success, error string on failure
870910 * @access private
871911 */
872 - function checkOverwrite() {
 912+ private function checkOverwrite() {
873913 global $wgUser;
874914 // First check whether the local file can be overwritten
875 - if( $this->mLocalFile->exists() )
876 - if( !self::userCanReUpload( $wgUser, $this->mLocalFile ) )
 915+ $file = $this->getLocalFile();
 916+ if( $file->exists() )
 917+ if( !self::userCanReUpload( $wgUser, $file ) )
877918 return 'fileexists-forbidden';
878919
879920 // Check shared conflicts
880 - $file = wfFindFile( $this->mLocalFile->getName() );
 921+ $file = wfFindFile( $file->getName() );
881922 if ( $file && ( !$wgUser->isAllowed( 'reupload' ) ||
882923 !$wgUser->isAllowed( 'reupload-shared' ) ) )
883924 return 'fileexists-shared-forbidden';
@@ -904,6 +945,17 @@
905946 return $user->getId() == $img->getUser( 'id' );
906947 }
907948
 949+ /**
 950+ * Helper function that does various existence checks for a file.
 951+ * The following checks are performed:
 952+ * - The file exists
 953+ * - Article with the same name as the file exists
 954+ * - File exists with normalized extension
 955+ * - The file looks like a thumbnail and the original exists
 956+ *
 957+ * @param File $file The file to check
 958+ * @return mixed False if the file does not exists, else an array
 959+ */
908960 public static function getExistsWarning( $file ) {
909961 if( $file->exists() )
910962 return array( 'exists', $file );
@@ -944,6 +996,9 @@
945997 return false;
946998 }
947999
 1000+ /**
 1001+ * Helper function that checks whether the filename looks like a thumbnail
 1002+ */
9481003 public static function isThumbName( $filename ) {
9491004 $n = strrpos( $filename, '.' );
9501005 $partname = $n ? substr( $filename, 0, $n ) : $filename;
Index: trunk/phase3/includes/upload/UploadFromChunks.php
@@ -1,5 +1,10 @@
22 <?php
33 /**
 4+ * @file
 5+ * @ingroup upload
 6+ *
 7+ * @author Michael Dale
 8+ *
49 * first destination checks are made (if ignorewarnings is not checked) errors / warning is returned.
510 *
611 * we return the uploadUrl
Index: trunk/phase3/includes/upload/UploadFromFile.php
@@ -1,7 +1,15 @@
22 <?php
3 -
 3+/**
 4+ * @file
 5+ * @ingroup upload
 6+ *
 7+ * @author Bryan Tong Minh
 8+ *
 9+ * Implements regular file uploads
 10+ */
411 class UploadFromFile extends UploadBase {
512
 13+
614 function initializeFromRequest( &$request ) {
715 $desiredDestName = $request->getText( 'wpDestFile' );
816 if( !$desiredDestName )
Index: trunk/phase3/includes/HttpFunctions.php
@@ -54,7 +54,7 @@
5555 // check for redirects:
5656 if( isset( $head['Location'] ) && strrpos( $head[0], '302' ) !== false ){
5757 if( $redirectCount < $wgMaxRedirects ){
58 - if( UploadFromUrl::isValidURI( $head['Location'] ) ){
 58+ if( self::isValidURI( $head['Location'] ) ){
5959 return self::doDownload( $head['Location'], $target_file_path, $dl_mode, $redirectCount++ );
6060 } else {
6161 return Status::newFatal( 'upload-proto-error' );
@@ -272,6 +272,18 @@
273273 global $wgVersion;
274274 return "MediaWiki/$wgVersion";
275275 }
 276+
 277+ /**
 278+ * Checks that the given URI is a valid one
 279+ * @param $uri Mixed: URI to check for validity
 280+ */
 281+ public static function isValidURI( $uri ){
 282+ return preg_match(
 283+ '/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
 284+ $uri,
 285+ $matches
 286+ );
 287+ }
276288 }
277289
278290 class HttpRequest {
@@ -322,7 +334,7 @@
323335 */
324336 public function doRequest() {
325337 # Make sure we have a valid url
326 - if( !UploadFromUrl::isValidURI( $this->url ) )
 338+ if( !Http::isValidURI( $this->url ) )
327339 return Status::newFatal('bad-url');
328340
329341 # Use curl if available

Follow-up revisions

RevisionCommit summaryAuthorDate
r55620quick fix for r55604 broken chunk upload class abstractiondale14:30, 27 August 2009

Comments

#Comment by Mdale (talk | contribs)   14:30, 27 August 2009
  • Chunk uploads where broken by abstracting the initializeFromRequest method ... quick fix in r55620

Status & tagging log