6 * Implements uploading from a HTTP resource.
8 * @author Bryan Tong Minh
11 class UploadFromUrl
extends UploadBase
{
12 protected $mTempDownloadPath;
13 protected $comment, $watchList, $ignoreWarnings;
16 * Checks if the user is allowed to use the upload-by-URL feature. If the
17 * user is allowed, pass on permissions checking to the parent.
19 public static function isAllowed( $user ) {
20 if ( !$user->isAllowed( 'upload_by_url' ) )
21 return 'upload_by_url';
22 return parent
::isAllowed( $user );
26 * Checks if the upload from URL feature is enabled
29 public static function isEnabled() {
30 global $wgAllowCopyUploads;
31 return $wgAllowCopyUploads && parent
::isEnabled();
35 * Entry point for API upload
36 * @return bool true on success
38 public function initialize( $name, $url, $comment, $watchList = null, $ignoreWarn = null, $async = 'async' ) {
41 if ( !Http
::isValidURI( $url ) ) {
42 return Status
::newFatal( 'http-invalid-url' );
45 'userName' => $wgUser->getName(),
46 'userID' => $wgUser->getID(),
47 'url' => trim( $url ),
48 'timestamp' => wfTimestampNow(),
49 'comment' => $comment,
50 'watchlist' => $watchList,
51 'ignorewarnings' => $ignoreWarn );
53 $title = Title
::newFromText( $name );
55 if ( $async == 'async' ) {
56 $job = new UploadFromUrlJob( $title, $params );
57 return $job->insert();
59 $this->mUrl
= trim( $url );
60 $this->comment
= $comment;
61 $this->watchList
= $watchList;
62 $this->ignoreWarnings
= $ignoreWarn;
63 $this->mDesiredDestName
= $title;
71 * Initialize a queued download
74 public function initializeFromJob( $job ) {
75 global $wgTmpDirectory;
77 $this->mUrl
= $job->params
['url'];
78 $this->mTempPath
= tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
79 $this->mDesiredDestName
= $job->title
;
80 $this->comment
= $job->params
['comment'];
81 $this->watchList
= $job->params
['watchlist'];
82 $this->ignoreWarnings
= $job->params
['ignorewarnings'];
87 * Entry point for SpecialUpload
88 * @param $request Object: WebRequest object
90 public function initializeFromRequest( &$request ) {
91 $desiredDestName = $request->getText( 'wpDestFile' );
92 if ( !$desiredDestName )
93 $desiredDestName = $request->getText( 'wpUploadFileURL' );
94 return $this->initialize(
96 $request->getVal( 'wpUploadFileURL' ),
97 $request->getVal( 'wpUploadDescription' ),
98 $request->getVal( 'wpWatchThis' ),
99 $request->getVal( 'wpIgnoreWarnings' ),
105 * @param $request Object: WebRequest object
107 public static function isValidRequest( $request ) {
110 $url = $request->getVal( 'wpUploadFileURL' );
111 return !empty( $url )
112 && Http
::isValidURI( $url )
113 && $wgUser->isAllowed( 'upload_by_url' );
116 private function saveTempFile( $req ) {
117 $filename = tempnam( wfTempDir(), 'URL' );
118 if ( $filename === false ) {
119 return Status
::newFatal( 'tmp-create-error' );
121 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
122 return Status
::newFatal( 'tmp-write-error' );
125 $this->mTempPath
= $filename;
126 $this->mFileSize
= filesize( $filename );
128 return Status
::newGood();
131 public function retrieveFileFromUrl() {
132 $req = HttpRequest
::factory( $this->mUrl
);
133 $status = $req->execute();
135 if ( !$status->isOk() ) {
139 $status = $this->saveTempFile( $req );
140 if ( !$status->isGood() ) {
143 $this->mRemoveTempFile
= true;
148 public function doUpload() {
151 $status = $this->retrieveFileFromUrl();
153 if ( $status->isGood() ) {
155 $v = $this->verifyUpload();
156 if ( $v['status'] !== UploadBase
::OK
) {
157 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
160 $status = $this->getLocalFile()->upload( $this->mTempPath
, $this->comment
,
161 $this->comment
, File
::DELETE_SOURCE
, $this->mFileProps
, false, $wgUser );
164 if ( $status->isGood() ) {
165 $file = $this->getLocalFile();
167 $wgUser->leaveUserMessage( wfMsg( 'successfulupload' ),
168 wfMsg( 'upload-success-msg', $file->getDescriptionUrl() ) );
170 $wgUser->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
171 wfMsg( 'upload-failure-msg', $status->getWikiText() ) );