}
$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 );
}
/**
'ignorewarnings' => false,
'file' => null,
'url' => null,
+ 'asyncdownload' => false,
'sessionkey' => null,
);
return $params;
'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',
),
* 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 ) ) {
"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;
+ }
}
/**
$request->getVal( 'wpUploadFileURL' ),
$request->getVal( 'wpUploadDescription' ),
$request->getVal( 'wpWatchThis' ),
- $request->getVal( 'wpIgnoreWarnings' )
+ $request->getVal( 'wpIgnoreWarnings' ),
+ 'async'
);
}
return Status::newGood();
}
- public function doUpload() {
- global $wgUser;
-
+ public function retrieveFileFromUrl() {
$req = HttpRequest::factory($this->mUrl);
$status = $req->execute();
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() ) );
$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 );
'action' => 'upload',
'filename' => 'Test.png',
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
+ 'asyncdownload' => 1,
'token' => $token,
), $data );
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
*/
$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");
}
-}
+ }
'name' => 'local',
'directory' => wfTempDir().'/test-repo',
'url' => 'http://example.com/images',
+ 'deletedDir' => wfTempDir().'/test-repo/delete',
'hashLevels' => 2,
'transformVia404' => false,
);