From: Mark A. Hershberger Date: Sat, 12 Jun 2010 03:58:31 +0000 (+0000) Subject: * re r65152 add back async option for uploadByURL API call X-Git-Tag: 1.31.0-rc.0~36531 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22suivi_revisions%22%29%20.%20%22?a=commitdiff_plain;h=e519859e492d3390f6edacf7214e2ad6863c4095;p=lhc%2Fweb%2Fwiklou.git * re r65152 add back async option for uploadByURL API call * Remove vestigal commented-out code and make sure permission checking works --- diff --git a/includes/api/ApiUpload.php b/includes/api/ApiUpload.php index 543b890d35..6760342a7c 100644 --- a/includes/api/ApiUpload.php +++ b/includes/api/ApiUpload.php @@ -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', ), diff --git a/includes/upload/UploadFromUrl.php b/includes/upload/UploadFromUrl.php index 91a630bd11..8d6de71603 100644 --- a/includes/upload/UploadFromUrl.php +++ b/includes/upload/UploadFromUrl.php @@ -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() ) ); diff --git a/maintenance/tests/UploadFromUrlTest.php b/maintenance/tests/UploadFromUrlTest.php index 9294fa9613..1e181543a8 100644 --- a/maintenance/tests/UploadFromUrlTest.php +++ b/maintenance/tests/UploadFromUrlTest.php @@ -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"); } -} + } diff --git a/maintenance/tests/UploadFromUrlTestSuite.php b/maintenance/tests/UploadFromUrlTestSuite.php index 314be76395..e9ddd9625b 100644 --- a/maintenance/tests/UploadFromUrlTestSuite.php +++ b/maintenance/tests/UploadFromUrlTestSuite.php @@ -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, );