Fixed mistake in r73686 where I wrapped CSS in a JavaScript conditional.
[mediawiki.git] / includes / upload / UploadFromUrl.php
blob42b382750f5b61fa7d4139718336dee59f058b9b
1 <?php
2 /**
3 * Implements uploading from a HTTP resource.
5 * @file
6 * @ingroup upload
7 * @author Bryan Tong Minh
8 * @author Michael Dale
9 */
11 class UploadFromUrl extends UploadBase {
12 protected $mAsync, $mUrl;
13 protected $mIgnoreWarnings = true;
15 /**
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 );
25 /**
26 * Checks if the upload from URL feature is enabled
27 * @return bool
29 public static function isEnabled() {
30 global $wgAllowCopyUploads;
31 return $wgAllowCopyUploads && parent::isEnabled();
34 /**
35 * Entry point for API upload
37 * @param $name string
38 * @param $url string
39 * @param $async mixed Whether the download should be performed
40 * asynchronous. False for synchronous, async or async-leavemessage for
41 * asynchronous download.
43 public function initialize( $name, $url, $async = false ) {
44 global $wgAllowAsyncCopyUploads;
46 $this->mUrl = $url;
47 $this->mAsync = $wgAllowAsyncCopyUploads ? $async : false;
49 $tempPath = $this->mAsync ? null : $this->makeTemporaryFile();
50 # File size and removeTempFile will be filled in later
51 $this->initializePathInfo( $name, $tempPath, 0, false );
54 /**
55 * Entry point for SpecialUpload
56 * @param $request Object: WebRequest object
58 public function initializeFromRequest( &$request ) {
59 $desiredDestName = $request->getText( 'wpDestFile' );
60 if ( !$desiredDestName )
61 $desiredDestName = $request->getText( 'wpUploadFileURL' );
62 return $this->initialize(
63 $desiredDestName,
64 $request->getVal( 'wpUploadFileURL' ),
65 false
69 /**
70 * @param $request Object: WebRequest object
72 public static function isValidRequest( $request ) {
73 global $wgUser;
75 $url = $request->getVal( 'wpUploadFileURL' );
76 return !empty( $url )
77 && Http::isValidURI( $url )
78 && $wgUser->isAllowed( 'upload_by_url' );
82 public function fetchFile() {
83 if ( !Http::isValidURI( $this->mUrl ) ) {
84 return Status::newFatal( 'http-invalid-url' );
87 if ( !$this->mAsync ) {
88 return $this->reallyFetchFile();
90 return Status::newGood();
92 /**
93 * Create a new temporary file in the URL subdirectory of wfTempDir().
95 * @return string Path to the file
97 protected function makeTemporaryFile() {
98 return tempnam( wfTempDir(), 'URL' );
101 * Save the result of a HTTP request to the temporary file
103 * @param $req HttpRequest
104 * @return Status
106 private function saveTempFile( $req ) {
107 if ( $this->mTempPath === false ) {
108 return Status::newFatal( 'tmp-create-error' );
110 if ( file_put_contents( $this->mTempPath, $req->getContent() ) === false ) {
111 return Status::newFatal( 'tmp-write-error' );
114 $this->mFileSize = filesize( $this->mTempPath );
116 return Status::newGood();
119 * Download the file, save it to the temporary file and update the file
120 * size and set $mRemoveTempFile to true.
122 protected function reallyFetchFile() {
123 $req = HttpRequest::factory( $this->mUrl );
124 $status = $req->execute();
126 if ( !$status->isOk() ) {
127 return $status;
130 $status = $this->saveTempFile( $req );
131 if ( !$status->isGood() ) {
132 return $status;
134 $this->mRemoveTempFile = true;
136 return $status;
140 * Wrapper around the parent function in order to defer verifying the
141 * upload until the file really has been fetched.
143 public function verifyUpload() {
144 if ( $this->mAsync ) {
145 return array( 'status' => self::OK );
147 return parent::verifyUpload();
151 * Wrapper around the parent function in order to defer checking warnings
152 * until the file really has been fetched.
154 public function checkWarnings() {
155 if ( $this->mAsync ) {
156 $this->mIgnoreWarnings = false;
157 return array();
159 return parent::checkWarnings();
163 * Wrapper around the parent function in order to defer checking protection
164 * until we are sure that the file can actually be uploaded
166 public function verifyPermissions( $user ) {
167 if ( $this->mAsync ) {
168 return true;
170 return parent::verifyPermissions( $user );
174 * Wrapper around the parent function in order to defer uploading to the
175 * job queue for asynchronous uploads
177 public function performUpload( $comment, $pageText, $watch, $user ) {
178 if ( $this->mAsync ) {
179 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
181 $status = new Status;
182 $status->error( 'async', $sessionKey );
183 return $status;
186 return parent::performUpload( $comment, $pageText, $watch, $user );
190 protected function insertJob( $comment, $pageText, $watch, $user ) {
191 $sessionKey = $this->getSessionKey();
192 $job = new UploadFromUrlJob( $this->getTitle(), array(
193 'url' => $this->mUrl,
194 'comment' => $comment,
195 'pageText' => $pageText,
196 'watch' => $watch,
197 'userName' => $user->getName(),
198 'leaveMessage' => $this->mAsync == 'async-leavemessage',
199 'ignoreWarnings' => $this->mIgnoreWarnings,
200 'sessionId' => session_id(),
201 'sessionKey' => $sessionKey,
202 ) );
203 $job->initializeSessionData();
204 $job->insert();
205 return $sessionKey;