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();
60 $this->mUrl
= trim( $url );
61 $this->comment
= $comment;
62 $this->watchList
= $watchList;
63 $this->ignoreWarnings
= $ignoreWarn;
64 $this->mDesiredDestName
= $title;
72 * Initialize a queued download
75 public function initializeFromJob( $job ) {
76 global $wgTmpDirectory;
78 $this->mUrl
= $job->params
['url'];
79 $this->mTempPath
= tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
80 $this->mDesiredDestName
= $job->title
;
81 $this->comment
= $job->params
['comment'];
82 $this->watchList
= $job->params
['watchlist'];
83 $this->ignoreWarnings
= $job->params
['ignorewarnings'];
88 * Entry point for SpecialUpload
89 * @param $request Object: WebRequest object
91 public function initializeFromRequest( &$request ) {
92 $desiredDestName = $request->getText( 'wpDestFile' );
93 if( !$desiredDestName )
94 $desiredDestName = $request->getText( 'wpUploadFileURL' );
95 return $this->initialize(
97 $request->getVal( 'wpUploadFileURL' ),
98 $request->getVal( 'wpUploadDescription' ),
99 $request->getVal( 'wpWatchThis' ),
100 $request->getVal( 'wpIgnoreWarnings' ),
106 * @param $request Object: WebRequest object
108 public static function isValidRequest( $request ){
111 $url = $request->getVal( 'wpUploadFileURL' );
112 return !empty( $url )
113 && Http
::isValidURI( $url )
114 && $wgUser->isAllowed( 'upload_by_url' );
117 private function saveTempFile( $req ) {
118 $filename = tempnam( wfTempDir(), 'URL' );
119 if ( $filename === false ) {
120 return Status
::newFatal( 'tmp-create-error' );
122 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
123 return Status
::newFatal( 'tmp-write-error' );
126 $this->mTempPath
= $filename;
127 $this->mFileSize
= filesize( $filename );
129 return Status
::newGood();
132 public function retrieveFileFromUrl() {
133 $req = HttpRequest
::factory($this->mUrl
);
134 $status = $req->execute();
136 if( !$status->isOk() ) {
140 $status = $this->saveTempFile( $req );
141 if ( !$status->isGood() ) {
144 $this->mRemoveTempFile
= true;
149 public function doUpload() {
152 $status = $this->retrieveFileFromUrl();
154 if ( $status->isGood() ) {
156 $v = $this->verifyUpload();
157 if( $v['status'] !== UploadBase
::OK
) {
158 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
161 $status = $this->getLocalFile()->upload( $this->mTempPath
, $this->comment
,
162 $this->comment
, File
::DELETE_SOURCE
, $this->mFileProps
, false, $wgUser );
165 if ( $status->isGood() ) {
166 global $wgLocalFileRepo;
167 $file = $this->getLocalFile();
169 $wgUser->leaveUserMessage( wfMsg( 'successfulupload' ),
170 wfMsg( 'upload-success-msg', $file->getDescriptionUrl() ) );
172 $wgUser->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
173 wfMsg( 'upload-failure-msg', $status->getWikiText() ) );