r50501 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50500‎ | r50501 | r50502 >
Date:23:30, 11 May 2009
Author:dale
Status:deferred (Comments)
Tags:
Comment:
* supports redirects as configured by $wgMaxRedirects
* better & earlier error reporting for uploadFromURL
* some new http error msgs
Modified paths:
  • /branches/new-upload/phase3/includes/DefaultSettings.php (modified) (history)
  • /branches/new-upload/phase3/includes/HttpFunctions.php (modified) (history)
  • /branches/new-upload/phase3/includes/UploadFromUrl.php (modified) (history)
  • /branches/new-upload/phase3/includes/api/ApiUpload.php (modified) (history)
  • /branches/new-upload/phase3/languages/messages/MessagesEn.php (modified) (history)

Diff [purge]

Index: branches/new-upload/phase3/includes/UploadFromUrl.php
@@ -51,7 +51,7 @@
5252 */
5353 function fetchFile( ) {
5454 //entry point for SpecialUplaod
55 - if( stripos($this->mUrl, 'http://') !== 0 && stripos($this->mUrl, 'ftp://') !== 0 ) {
 55+ if( self::isValidURI($this->mUrl) === false) {
5656 return Status::newFatal('upload-proto-error');
5757 }
5858 //print "fetchFile:: $this->dl_mode";
@@ -69,7 +69,10 @@
7070 if( !$request->getVal('wpUploadFileURL') )
7171 return false;
7272 //check that is a valid url:
 73+ return self::isValidURI( $request->getVal('wpUploadFileURL') );
 74+ }
 75+ static function isValidURI( $uri ){
7376 return preg_match('/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
74 - $request->getVal('wpUploadFileURL'), $matches);
 77+ $uri, $matches);
7578 }
7679 }
\ No newline at end of file
Index: branches/new-upload/phase3/includes/api/ApiUpload.php
@@ -124,8 +124,8 @@
125125 $this->mUpload->initialize( $this->mParams['filename'], $this->mParams['url']);
126126
127127 $status = $this->mUpload->fetchFile();
128 - if( !$status->isOK() ){
129 - $this->dieUsage( 'fetchfilerror', $status->getWikiText());
 128+ if( !$status->isOK() ){
 129+ return $this->dieUsage( 'fetchfilerror', $status->getWikiText());
130130 }
131131 //check if we doing a async request set session info and return the upload_session_key)
132132 if( $this->mUpload->isAsync() ){
Index: branches/new-upload/phase3/includes/DefaultSettings.php
@@ -436,6 +436,7 @@
437437 $wgMaxUploadSize = 1024*1024*100; # 100MB
438438
439439
 440+
440441 /**
441442 * Point the upload navigation link to an external URL
442443 * Useful if you want to use a shared repository by default
Index: branches/new-upload/phase3/includes/HttpFunctions.php
@@ -9,8 +9,9 @@
1010 const SYNC_DOWNLOAD = 1; //syncronys upload (in a single request)
1111 const ASYNC_DOWNLOAD = 2; //asynchronous upload we should spawn out another process and monitor progress if possible)
1212
13 - var $body = '';
 13+ var $body = '';
1414
 15+ static $redirectcount=0;
1516 /**
1617 * Simple wrapper for Http::request( 'GET' )
1718 */
@@ -27,13 +28,33 @@
2829 $req = new HttpRequest( $url, $opts );
2930 return $req->doRequest();
3031 }
31 - public static function doDownload( $url, $target_file_path , $dl_mode = self::SYNC_DOWNLOAD ){
32 - global $wgPhpCliPath, $wgMaxUploadSize;
33 - //do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize to large no need to download it
 32+ public static function doDownload( $url, $target_file_path , $dl_mode = self::SYNC_DOWNLOAD , $redirectCount=0){
 33+ global $wgPhpCliPath, $wgMaxUploadSize, $wgMaxRedirects;
 34+ //do a quick check to HEAD to insure the file size is not > $wgMaxUploadSize
3435 $head = get_headers($url, 1);
3536
 37+ //check for non-valid result:
 38+
 39+ wfDebug("\n head: " . print_r($head, true). "\n");
 40+
3641 //check for redirects:
 42+ if( isset( $head['Location'] ) && strrpos($head[0], '302')!==false ){
 43+ if($redirectCount < $wgMaxRedirects){
 44+ if( UploadFromUrl::isValidURI( $head['Location'] )){
 45+ return self::doDownload ( $head['Location'], $target_file_path , $dl_mode, $redirectCount++);
 46+ }else{
 47+ return Status::newFatal('upload-proto-error');
 48+ }
 49+ }else{
 50+ return Status::newFatal('upload-too-many-redirects');
 51+ }
 52+ }
 53+ //we did not get a 200 ok response:
 54+ if( strrpos($head[0], '200 OK') === false){
 55+ return Status::newFatal( 'upload-http-error', htmlspecialchars($head[0]) );
 56+ }
3757
 58+
3859 $content_length = (isset($head['Content-Length']))?$head['Content-Length']:null;
3960 if($content_length){
4061 if($content_length > $wgMaxUploadSize){
@@ -133,18 +154,19 @@
134155 //run the actual request .. (this can take some time)
135156 wfDebug("do Request: " . $sd['url'] . ' tf: ' . $sd['target_file_path'] );
136157 $status = $req->doRequest();
137 - wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n");
 158+ //wfDebug("done with req status is: ". $status->isOK(). ' '.$status->getWikiText(). "\n");
138159
139160 //start up the session again:
140161 if( session_start() === false){
141162 wfDebug( __METHOD__ . ' ERROR:: Could not start session');
142163 }
143164 //grab the updated session data pointer
144 - $sd =& $_SESSION[ 'wsDownload' ][$upload_session_key];
145 -
 165+ $sd =& $_SESSION[ 'wsDownload' ][$upload_session_key];
146166 //if error update status:
147167 if( !$status->isOK() ){
148 - $sd['error'] = $status->getWikiText();
 168+ $sd['apiUploadResult']= ApiFormatJson::getJsonEncode(
 169+ array( 'error' => $status->getWikiText() )
 170+ );
149171 }
150172 //if status oky process upload using fauxReq to api:
151173 if( $status->isOK() ){
Index: branches/new-upload/phase3/languages/messages/MessagesEn.php
@@ -1930,7 +1930,12 @@
19311931 'upload-misc-error-text' => 'An unknown error occurred during the upload.
19321932 Please verify that the URL is valid and accessible and try again.
19331933 If the problem persists, contact an [[Special:ListUsers/sysop|administrator]].',
 1934+'upload-too-many-redirects' => 'The URL contained too many redirects',
 1935+'upload-unknown-size' => 'Unknown size',
19341936
 1937+//idealy we map out all the http errors and translations else just call this with the http resposne:
 1938+'upload-http-error' => "An http error occured : $1 ",
 1939+
19351940 # Some likely curl errors. More could be added from <http://curl.haxx.se/libcurl/c/libcurl-errors.html>
19361941 'upload-curl-error6' => 'Could not reach URL',
19371942 'upload-curl-error6-text' => 'The URL provided could not be reached.

Comments

#Comment by Catrope (talk | contribs)   09:11, 12 May 2009

Is the misspelling of 'fetchfileerror' as 'fetchfilerror' intentional?

#Comment by Mdale (talk | contribs)   15:00, 12 May 2009

nope, thanks for catching that.

Status & tagging log