r50023 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50022‎ | r50023 | r50024 >
Date:22:20, 28 April 2009
Author:dale
Status:deferred
Tags:
Comment:
more fixes to basic uploading and error handling.
Modified paths:
  • /branches/new-upload/phase3/includes/HttpFunctions.php (modified) (history)
  • /branches/new-upload/phase3/includes/UploadBase.php (modified) (history)
  • /branches/new-upload/phase3/includes/UploadFromChunks.php (modified) (history)
  • /branches/new-upload/phase3/includes/UploadFromStash.php (modified) (history)
  • /branches/new-upload/phase3/includes/UploadFromUrl.php (modified) (history)
  • /branches/new-upload/phase3/includes/filerepo/FSRepo.php (modified) (history)
  • /branches/new-upload/phase3/includes/specials/SpecialUpload.php (modified) (history)

Diff [purge]

Index: branches/new-upload/phase3/includes/filerepo/FSRepo.php
@@ -274,13 +274,13 @@
275275 * @param string $virtualUrl The virtual URL returned by storeTemp
276276 * @return boolean True on success, false on failure
277277 */
278 - function freeTemp( $virtualUrl ) {
279 - $temp = "mwrepo://{$this->name}/temp";
 278+ function freeTemp( $virtualUrl ) {
 279+ $temp = "mwrepo://{$this->name}/temp";
280280 if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
281281 wfDebug( __METHOD__.": Invalid virtual URL\n" );
282282 return false;
283 - }
284 - $path = $this->resolveVirtualUrl( $virtualUrl );
 283+ }
 284+ $path = $this->resolveVirtualUrl( $virtualUrl );
285285 wfSuppressWarnings();
286286 $success = unlink( $path );
287287 wfRestoreWarnings();
Index: branches/new-upload/phase3/includes/UploadFromUrl.php
@@ -52,40 +52,11 @@
5353 //now do the actual download to the shared target:
5454 $status = Http::doDownload ( $this->mUrl, $this->mTempPath, $this->dl_mode);
5555 //update the local filesize var:
56 - $this->mFileSize = filesize($this->mTempPath);
57 -
58 - return $status;
 56+ $this->mFileSize = filesize( $this->mTempPath );
 57+ return $status;
5958
60 - /*
61 - $res = $this->curlCopy();
62 - if( $res !== true ) {
63 - return array(
64 - 'status' => self::BEFORE_PROCESSING,
65 - 'error' => $res,
66 - );
67 - }*/
68 -
6959 }
7060
71 -
72 - /**
73 - * Callback function for CURL-based web transfer
74 - * Write data to file unless we've passed the length limit;
75 - * if so, abort immediately.
76 - * @access private
77 -
78 - function uploadCurlCallback( $ch, $data ) {
79 - global $wgMaxUploadSize;
80 - $length = strlen( $data );
81 - $this->mFileSize += $length;
82 - if( $this->mFileSize > $wgMaxUploadSize ) {
83 - return 0;
84 - }
85 - fwrite( $this->mCurlDestHandle, $data );
86 - return $length;
87 - }
88 -*/
89 - //this can be deprecated in favor of http_request2 functions
9061 static function isValidRequest( $request ){
9162 if( !$request->getVal('wpUploadFileURL') )
9263 return false;
Index: branches/new-upload/phase3/includes/UploadFromStash.php
@@ -1,6 +1,6 @@
22 <?php
33 class UploadFromStash extends UploadBase {
4 - static function isValidSessionKey( $key, $sessionData ) {
 4+ static function isValidSessionKey( $key, $sessionData ) {
55 return !empty( $key ) &&
66 is_array( $sessionData ) &&
77 isset( $sessionData[$key] ) &&
@@ -8,7 +8,7 @@
99 $sessionData[$key]['version'] == self::SESSION_VERSION
1010 ;
1111 }
12 - static function isValidRequest( $request ) {
 12+ static function isValidRequest(& $request ) {
1313 $sessionData = $request->getSessionData('wsUploadData');
1414 return self::isValidSessionKey(
1515 $request->getInt( 'wpSessionKey' ),
@@ -21,7 +21,7 @@
2222 * We don't want path names to be forged, so we keep
2323 * them in the session on the server and just give
2424 * an opaque key to the user agent.
25 - */
 25+ */
2626 parent::initialize( $name,
2727 $sessionData['mTempPath'],
2828 $sessionData['mFileSize'],
Index: branches/new-upload/phase3/includes/UploadFromChunks.php
@@ -19,7 +19,7 @@
2020 const DONE = 3;
2121
2222 function initializeFromParams( &$param , &$request) {
23 - $this->initFromSessionKey( $param['chunksessionkey'] );
 23+ $this->initFromSessionKey( $param['chunksessionkey'], $request );
2424 //set the chunk mode:
2525 if( !$this->mSessionKey && !$param['done'] ){
2626 //session key not set init the chunk upload system:
@@ -43,19 +43,22 @@
4444 return $this->status;
4545 }
4646
47 - function initFromSessionKey( $sessionKey ){
 47+ function initFromSessionKey( $sessionKey, $request ){
4848 if( !$sessionKey || empty( $sessionKey ) ){
4949 return false;
5050 }
5151 $this->mSessionKey = $sessionKey;
52 - if( isset( $_SESSION['wsUploadData'][$this->mSessionKey]['version'] ) &&
53 - $_SESSION['wsUploadData'][$this->mSessionKey]['version'] == self::SESSION_VERSION ) {
 52+ //load the sessionData array:
 53+ $sessionData = $request->getSessionData('wsUploadData');
 54+
 55+ if( isset( $sessionData[$this->mSessionKey]['version'] ) &&
 56+ $sessionData[$this->mSessionKey]['version'] == self::SESSION_VERSION ) {
5457 //update the local object from the session
55 - $this->mComment = $_SESSION[ 'wsUploadData' ][ $this->mSessionKey ][ 'mComment' ];
56 - $this->mWatch = $_SESSION[ 'wsUploadData' ][ $this->mSessionKey ][ 'mWatch' ];
57 - $this->mFilteredName = $_SESSION[ 'wsUploadData' ][ $this->mSessionKey ][ 'mFilteredName' ];
58 - $this->mTempAppendPath = $_SESSION[ 'wsUploadData' ][ $this->mSessionKey ][ 'mTempAppendPath' ];
59 - $this->mDesiredDestName = $_SESSION[ 'wsUploadData' ][ $this->mSessionKey ][ 'mDesiredDestName' ];
 58+ $this->mComment = $sessionData[ $this->mSessionKey ][ 'mComment' ];
 59+ $this->mWatch = $sessionData[ $this->mSessionKey ][ 'mWatch' ];
 60+ $this->mFilteredName = $sessionData[ $this->mSessionKey ][ 'mFilteredName' ];
 61+ $this->mTempAppendPath = $sessionData[ $this->mSessionKey ][ 'mTempAppendPath' ];
 62+ $this->mDesiredDestName = $sessionData[ $this->mSessionKey ][ 'mDesiredDestName' ];
6063 }else{
6164 $this->status = Array( 'error'=> 'missing session data');
6265 return false;
Index: branches/new-upload/phase3/includes/HttpFunctions.php
@@ -27,8 +27,7 @@
2828 return $req->request();
2929 }
3030 public static function doDownload( $url, $target_file_path , $dl_mode = self::SYNC_DOWNLOAD ){
31 - global $wgPhpCliPath, $wgMaxUploadSize;
32 - print "doDownload:$target_file_path";
 31+ global $wgPhpCliPath, $wgMaxUploadSize;
3332 //do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize to large no need to download it
3433 $head = get_headers($url, 1);
3534 if(isset($head['Content-Length']) && $head['Content-Length'] > $wgMaxUploadSize){
Index: branches/new-upload/phase3/includes/specials/SpecialUpload.php
@@ -18,7 +18,7 @@
1919 * implements Special:Upload
2020 * @ingroup SpecialPage
2121 */
22 -class UploadForm {
 22+class UploadForm extends SpecialPage {
2323 /**#@+
2424 * @access private
2525 */
@@ -41,7 +41,7 @@
4242 * Get data POSTed through the form and assign them to the object
4343 * @param $request Data posted.
4444 */
45 - function __construct( &$request ) {
 45+ function __construct( &$request ) {
4646 // Guess the desired name from the filename if not provided
4747 $this->mDesiredDestName = $request->getText( 'wpDestFile' );
4848 if( !$this->mDesiredDestName )
@@ -72,7 +72,8 @@
7373 $this->mReUpload = $request->getCheck( 'wpReUpload' );
7474
7575 $this->mAction = $request->getVal( 'action' );
76 - $this->mUpload = UploadBase::createFromRequest( $request );
 76+
 77+ $this->mUpload = UploadBase::createFromRequest( $request );
7778 }
7879
7980
@@ -80,8 +81,8 @@
8182 * Start doing stuff
8283 * @access public
8384 */
84 - function execute() {
85 - global $wgUser, $wgOut;
 85+ function execute() {
 86+ global $wgUser, $wgOut;
8687
8788 # Check uploading enabled
8889 if( !UploadBase::isEnabled() ) {
@@ -114,9 +115,9 @@
115116 $wgOut->readOnlyPage();
116117 return;
117118 }
118 - if( $this->mReUpload ) {
119 - // User did not choose to ignore warnings
120 - if( !$this->mUpload->unsaveUploadedFile() ) {
 119+ if( $this->mReUpload ) {
 120+ // User choose to cancel upload
 121+ if( !$this->mUpload->unsaveUploadedFile() ) {
121122 return;
122123 }
123124 # Because it is probably checked and shouldn't be
@@ -484,16 +485,13 @@
485486 global $wgOut, $wgUser;
486487 global $wgUseCopyrightUpload;
487488
488 - $sessionData = $this->mUpload->stashSession();
 489+ $this->mSessionKey = $this->mUpload->stashSession();
489490
490491 if( $sessionData === false ) {
491492 # Couldn't save file; an error has been displayed so let's go.
492493 return;
493 - }
 494+ }
494495
495 - $this->mSessionKey = mt_rand( 0, 0x7fffffff );
496 - $_SESSION['wsUploadData'][$this->mSessionKey] = $sessionData;
497 -
498496 $sk = $wgUser->getSkin();
499497
500498 $wgOut->addHTML( '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" );
Index: branches/new-upload/phase3/includes/UploadBase.php
@@ -57,9 +57,11 @@
5858 * Create a form of UploadBase depending on wpSourceType and initializes it
5959 */
6060 static function createFromRequest( &$request, $type = null ) {
61 - $type = $type ? $type : $request->getVal( 'wpSourceType' );
 61+ $type = $type ? $type : $request->getVal( 'wpSourceType' );
 62+
6263 if( !$type )
6364 return null;
 65+
6466 $type = ucfirst($type);
6567 $className = 'UploadFrom'.$type;
6668 if( !in_array( $type, self::$uploadHandlers ) )
@@ -67,11 +69,12 @@
6870
6971 if( !call_user_func( array( $className, 'isEnabled' ) ) )
7072 return null;
71 -
 73+
7274 if( !call_user_func( array( $className, 'isValidRequest' ), $request ) )
7375 return null;
74 -
 76+
7577 $handler = new $className;
 78+
7679 $handler->initializeFromRequest( $request );
7780 return $handler;
7881 }
@@ -98,7 +101,7 @@
99102 * Fetch the file. Usually a no-op
100103 */
101104 function fetchFile() {
102 - return self::OK;
 105+ return Status::newGood();
103106 }
104107 //return the file size
105108 function isEmptyFile(){
@@ -280,9 +283,12 @@
281284
282285 // Check whether this may be a thumbnail
283286 if( $exists !== false && $exists[0] != 'thumb'
284 - && self::isThumbName( $this->mLocalFile->getName() ) )
 287+ && self::isThumbName( $this->mLocalFile->getName() ) ){
 288+ //make the title:
 289+ $nt = $this->getTitle();
285290 $warning['file-thumbnail-no'] = substr( $filename , 0,
286291 strpos( $nt->getText() , '-' ) +1 );
 292+ }
287293
288294 // Check dupes against existing files
289295 $hash = File::sha1Base36( $this->mTempPath );
@@ -444,16 +450,16 @@
445451 * @access private
446452 */
447453 function stashSession() {
448 - $stash = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
449 -
450 - if( !$stash ) {
 454+ $status = $this->saveTempUploadedFile( $this->mDestName, $this->mTempPath );
 455+ if( !$status->isOK() ) {
451456 # Couldn't save the file.
452457 return false;
453458 }
 459+ $mTempPath = $status->value;
454460
455461 $key = $this->getSessionKey ();
456462 $_SESSION['wsUploadData'][$key] = array(
457 - 'mTempPath' => $stash,
 463+ 'mTempPath' => $mTempPath,
458464 'mFileSize' => $this->mFileSize,
459465 'mSrcName' => $this->mSrcName,
460466 'mFileProps' => $this->mFileProps,
@@ -472,9 +478,10 @@
473479 * Remove a temporarily kept file stashed by saveTempUploadedFile().
474480 * @return success
475481 */
476 - function unsaveUploadedFile() {
477 - $repo = RepoGroup::singleton()->getLocalRepo();
478 - $success = $repo->freeTemp( $this->mTempPath );
 482+ function unsaveUploadedFile() {
 483+ $repo = RepoGroup::singleton()->getLocalRepo();
 484+ print "free temp: {$this->mTempPath}\n";
 485+ $success = $repo->freeTemp( $this->mTempPath );
479486 return $success;
480487 }
481488
@@ -483,7 +490,7 @@
484491 * on exit to clean up.
485492 * @access private
486493 */
487 - function cleanupTempFile() {
 494+ function cleanupTempFile() {
488495 if ( $this->mRemoveTempFile && $this->mTempPath && file_exists( $this->mTempPath ) ) {
489496 wfDebug( __METHOD__.": Removing temporary file {$this->mTempPath}\n" );
490497 unlink( $this->mTempPath );

Status & tagging log