* re r65152 add back async option for uploadByURL API call
authorMark A. Hershberger <mah@users.mediawiki.org>
Sat, 12 Jun 2010 03:58:31 +0000 (03:58 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Sat, 12 Jun 2010 03:58:31 +0000 (03:58 +0000)
* Remove vestigal commented-out code and make sure permission checking works

includes/api/ApiUpload.php
includes/upload/UploadFromUrl.php
maintenance/tests/UploadFromUrlTest.php
maintenance/tests/UploadFromUrlTestSuite.php

index 543b890..6760342 100644 (file)
@@ -92,37 +92,66 @@ class ApiUpload extends ApiBase {
                                }
 
                                $this->mUpload = new UploadFromUrl;
-                               $result = $this->mUpload->initialize( $this->mParams['filename'], $this->mParams['url'],
-                                                                                       $this->mParams['comment'] );
+                               $async = $this->mParams['asyncdownload'] ? 'async' : null;
+
+                               $result = $this->mUpload->initialize( $this->mParams['filename'],
+                                               $this->mParams['url'],
+                                               $this->mParams['comment'],
+                                               $this->mParams['watchlist'],
+                                               $this->mParams['ignorewarnings'],
+                                               $async);
+
+                               $this->checkPermissions( $wgUser );
+                               if ( $async ) {
+                                       $this->getResult()->addValue( null,
+                                                                                                 $this->getModuleName(),
+                                                                                                 array( 'queued' => $result ) );
+                                       return;
+                               }
 
-                               $this->getResult()->addValue( null, $this->getModuleName(), array( 'queued' => $result ) );
-                               return;
+                               $status = $this->mUpload->retrieveFileFromUrl();
+                               if ( !$status->isGood() ) {
+                                       $this->getResult()->addValue( null,
+                                                                                                 $this->getModuleName(),
+                                                                                                 array( 'error' => $status ) );
+                                       return;
+                               }
                        }
                } else {
                        $this->dieUsageMsg( array( 'missingparam', 'filename' ) );
                }
 
+               $this->checkPermissions( $wgUser );
+
                if ( !isset( $this->mUpload ) ) {
                        $this->dieUsage( 'No upload module set', 'nomodule' );
                }
 
+               // Perform the upload
+               $result = $this->performUpload();
+
+               // Cleanup any temporary mess
+               $this->mUpload->cleanupTempFile();
+
+               $this->getResult()->addValue( null, $this->getModuleName(), $result );
+       }
+
+       /**
+        * Checks that the user has permissions to perform this upload.
+        * Dies with usage message on inadequate permissions.
+        * @param $user User The user to check.
+        */
+       protected function checkPermissions( $user ) {
                // Check whether the user has the appropriate permissions to upload anyway
-               $permission = $this->mUpload->isAllowed( $wgUser );
+               $permission = $this->mUpload->isAllowed( $user );
 
                if ( $permission !== true ) {
-                       if ( !$wgUser->isLoggedIn() ) {
+                       if ( !$user->isLoggedIn() ) {
                                $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) );
                        } else {
                                $this->dieUsageMsg( array( 'badaccess-groups' ) );
                        }
                }
-               // Perform the upload
-               $result = $this->performUpload();
-
-               // Cleanup any temporary mess
-               $this->mUpload->cleanupTempFile();
-
-               $this->getResult()->addValue( null, $this->getModuleName(), $result );
        }
 
        /**
@@ -307,6 +336,7 @@ class ApiUpload extends ApiBase {
                        'ignorewarnings' => false,
                        'file' => null,
                        'url' => null,
+                       'asyncdownload' => false,
                        'sessionkey' => null,
                );
                return $params;
@@ -323,6 +353,7 @@ class ApiUpload extends ApiBase {
                        'ignorewarnings' => 'Ignore any warnings',
                        'file' => 'File contents',
                        'url' => 'Url to fetch the file from',
+                       'asyncdownload' => 'Make fetching a URL asyncronous',
                        'sessionkey' => array(
                                'Session key returned by a previous upload that failed due to warnings',
                        ),
index 91a630b..8d6de71 100644 (file)
@@ -35,7 +35,7 @@ class UploadFromUrl extends UploadBase {
         * Entry point for API upload
         * @return bool true on success
         */
-       public function initialize( $name, $url, $comment, $watchList, $ignoreWarn ) {
+       public function initialize( $name, $url, $comment, $watchList = null, $ignoreWarn = null, $async = 'async') {
                global $wgUser;
 
                if( !Http::isValidURI( $url ) ) {
@@ -51,25 +51,21 @@ class UploadFromUrl extends UploadBase {
                        "ignorewarnings" => $ignoreWarn);
 
                $title = Title::newFromText( $name );
-               /* // Check whether the user has the appropriate permissions to upload anyway */
-               /* $permission = $this->isAllowed( $wgUser ); */
 
-               /* if ( $permission !== true ) { */
-               /*      if ( !$wgUser->isLoggedIn() ) { */
-               /*              return Status::newFatal( 'uploadnologintext' ); */
-               /*      } else { */
-               /*              return Status::newFatal( 'badaccess-groups' ); */
-               /*      } */
-               /* } */
-
-               /* $permErrors = $this->verifyPermissions( $wgUser ); */
-               /* if ( $permErrors !== true ) { */
-               /*      return Status::newFatal( 'badaccess-groups' ); */
-               /* } */
-
-
-               $job = new UploadFromUrlJob( $title, $params );
-               return $job->insert();
+               if ( $async == 'async' ) {
+                       $job = new UploadFromUrlJob( $title, $params );
+                       return $job->insert();
+               }
+               else {
+                       $this->mUrl = trim( $url );
+                       $this->comment = $comment;
+                       $this->watchList = $watchList;
+                       $this->ignoreWarnings = $ignoreWarn;
+                       $this->mDesiredDestName = $title;
+                       $this->getTitle();
+
+                       return true;
+               }
        }
 
        /**
@@ -101,7 +97,8 @@ class UploadFromUrl extends UploadBase {
                        $request->getVal( 'wpUploadFileURL' ),
                        $request->getVal( 'wpUploadDescription' ),
                        $request->getVal( 'wpWatchThis' ),
-                       $request->getVal( 'wpIgnoreWarnings' )
+                       $request->getVal( 'wpIgnoreWarnings' ),
+                       'async'
                );
        }
 
@@ -132,9 +129,7 @@ class UploadFromUrl extends UploadBase {
                return Status::newGood();
        }
 
-       public function doUpload() {
-               global $wgUser;
-
+       public function retrieveFileFromUrl() {
                $req = HttpRequest::factory($this->mUrl);
                $status = $req->execute();
 
@@ -146,29 +141,33 @@ class UploadFromUrl extends UploadBase {
                if ( !$status->isGood() ) {
                        return $status;
                }
-
                $this->mRemoveTempFile = true;
 
-               $v = $this->verifyUpload();
-               if( $v['status'] !== UploadBase::OK ) {
-                       return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
-               }
+               return $status;
+       }
 
-               // This has to come from API
-               /* $warnings = $this->checkForWarnings(); */
-               /* if( isset($warnings) ) return $warnings; */
+       public function doUpload() {
+               global $wgUser;
 
-               $file = $this->getLocalFile();
-               // This comes from ApiBase
-               /* $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() ); */
+               $status = $this->retrieveFileFromUrl();
 
-               $status = $this->getLocalFile()->upload( $this->mTempPath, $this->comment,
-                       $this->comment, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
+               if ( $status->isGood() ) {
+
+                       $v = $this->verifyUpload();
+                       if( $v['status'] !== UploadBase::OK ) {
+                               return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
+                       }
+
+                       $status = $this->getLocalFile()->upload( $this->mTempPath, $this->comment,
+                               $this->comment, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
+               }
 
                if ( $status->isGood() ) {
-                       $url = $this->getLocalFile()->getDescriptionUrl();
+                       global $wgLocalFileRepo;
+                       $file = $this->getLocalFile();
+
                        $wgUser->leaveUserMessage( wfMsg( 'successfulupload' ),
-                               wfMsg( 'upload-success-msg', $url ) );
+                               wfMsg( 'upload-success-msg', $file->getDescriptionUrl() ) );
                } else {
                        $wgUser->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
                                wfMsg( 'upload-failure-msg', $status->getWikiText() ) );
index 9294fa9..1e18154 100644 (file)
@@ -132,6 +132,7 @@ class UploadFromUrlTest extends ApiSetup {
                $data = $this->doApiRequest( array(
                        'action' => 'upload',
                        'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
+                       'asyncdownload' => 1,
                        'filename' => 'Test.png',
                        'token' => $token,
                ), $data );
@@ -159,6 +160,7 @@ class UploadFromUrlTest extends ApiSetup {
                        'action' => 'upload',
                        'filename' => 'Test.png',
                        'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
+                       'asyncdownload' => 1,
                        'token' => $token,
                ), $data );
 
@@ -172,6 +174,36 @@ class UploadFromUrlTest extends ApiSetup {
                return $data;
        }
 
+       /**
+        * @depends testLogin
+        */
+       function testSyncDownload( $data ) {
+               global $wgUser;
+               $data[2]['wsEditToken'] = $data[2]['wsToken'];
+               $token = md5( $data[2]['wsToken'] ) . EDIT_TOKEN_SUFFIX;
+
+               $job = Job::pop();
+               $this->assertFalse( $job );
+
+               self::deleteFile( 'Test.png' );
+
+               $wgUser->addGroup( 'users' );
+               $data = $this->doApiRequest( array(
+                       'action' => 'upload',
+                       'filename' => 'Test.png',
+                       'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
+                       'ignorewarnings' => true,
+                       'token' => $token,
+               ), $data );
+
+               $job = Job::pop();
+               $this->assertFalse( $job );
+
+               $this->assertEquals( 'Success', $data[0]['upload']['result'] );
+
+               return $data;
+       }
+
        /**
         * @depends testDoDownload
         */
@@ -179,5 +211,27 @@ class UploadFromUrlTest extends ApiSetup {
                $t = Title::newFromText( "Test.png", NS_FILE );
 
                $this->assertTrue( $t->exists() );
+
+               self::deleteFile( 'Test.png' );
+        }
+
+       /**
+        *
+        */
+       function deleteFile( $name ) {
+
+               $t = Title::newFromText( $name, NS_FILE );
+               $this->assertTrue($t->exists(), "File '$name' exists");
+
+               if ( $t->exists() ) {
+                       $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
+                       $empty = "";
+                       $status = FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
+                       $a = new Article ( $t );
+                       $a->doDeleteArticle( "testing" );
+               }
+               $t = Title::newFromText( $name, NS_FILE );
+
+               $this->assertFalse($t->exists(), "File '$name' was deleted");
        }
-}
+ }
index 314be76..e9ddd96 100644 (file)
@@ -32,6 +32,7 @@ class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite
                        'name' => 'local',
                        'directory' => wfTempDir().'/test-repo',
                        'url' => 'http://example.com/images',
+                       'deletedDir' => wfTempDir().'/test-repo/delete',
                        'hashLevels' => 2,
                        'transformVia404' => false,
                );