Rename messages from r90670
[mediawiki.git] / includes / upload / UploadFromUrl.php
blobe8178cdf739253eacc7254ec6daefbdd10c911d8
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 protected $mTempPath;
17 /**
18 * Checks if the user is allowed to use the upload-by-URL feature. If the
19 * user is allowed, pass on permissions checking to the parent.
21 * @param $user User
23 public static function isAllowed( $user ) {
24 if ( !$user->isAllowed( 'upload_by_url' ) )
25 return 'upload_by_url';
26 return parent::isAllowed( $user );
29 /**
30 * Checks if the upload from URL feature is enabled
31 * @return bool
33 public static function isEnabled() {
34 global $wgAllowCopyUploads;
35 return $wgAllowCopyUploads && parent::isEnabled();
38 /**
39 * Entry point for API upload
41 * @param $name string
42 * @param $url string
43 * @param $async mixed Whether the download should be performed
44 * asynchronous. False for synchronous, async or async-leavemessage for
45 * asynchronous download.
47 public function initialize( $name, $url, $async = false ) {
48 global $wgAllowAsyncCopyUploads;
50 $this->mUrl = $url;
51 $this->mAsync = $wgAllowAsyncCopyUploads ? $async : false;
52 if ( $async ) {
53 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
56 $tempPath = $this->mAsync ? null : $this->makeTemporaryFile();
57 # File size and removeTempFile will be filled in later
58 $this->initializePathInfo( $name, $tempPath, 0, false );
61 /**
62 * Entry point for SpecialUpload
63 * @param $request WebRequest object
65 public function initializeFromRequest( &$request ) {
66 $desiredDestName = $request->getText( 'wpDestFile' );
67 if ( !$desiredDestName )
68 $desiredDestName = $request->getText( 'wpUploadFileURL' );
69 return $this->initialize(
70 $desiredDestName,
71 trim( $request->getVal( 'wpUploadFileURL' ) ),
72 false
76 /**
77 * @param $request WebRequest object
79 public static function isValidRequest( $request ) {
80 global $wgUser;
82 $url = $request->getVal( 'wpUploadFileURL' );
83 return !empty( $url )
84 && Http::isValidURI( $url )
85 && $wgUser->isAllowed( 'upload_by_url' );
88 public function getSourceType() { return 'url'; }
90 public function fetchFile() {
91 if ( !Http::isValidURI( $this->mUrl ) ) {
92 return Status::newFatal( 'http-invalid-url' );
95 if ( !$this->mAsync ) {
96 return $this->reallyFetchFile();
98 return Status::newGood();
101 * Create a new temporary file in the URL subdirectory of wfTempDir().
103 * @return string Path to the file
105 protected function makeTemporaryFile() {
106 return tempnam( wfTempDir(), 'URL' );
110 * Callback: save a chunk of the result of a HTTP request to the temporary file
112 * @param $req mixed
113 * @param $buffer string
114 * @return int number of bytes handled
116 public function saveTempFileChunk( $req, $buffer ) {
117 $nbytes = fwrite( $this->mTmpHandle, $buffer );
119 if ( $nbytes == strlen( $buffer ) ) {
120 $this->mFileSize += $nbytes;
121 } else {
122 // Well... that's not good!
123 fclose( $this->mTmpHandle );
124 $this->mTmpHandle = false;
127 return $nbytes;
131 * Download the file, save it to the temporary file and update the file
132 * size and set $mRemoveTempFile to true.
134 protected function reallyFetchFile() {
135 if ( $this->mTempPath === false ) {
136 return Status::newFatal( 'tmp-create-error' );
139 // Note the temporary file should already be created by makeTemporaryFile()
140 $this->mTmpHandle = fopen( $this->mTempPath, 'wb' );
141 if ( !$this->mTmpHandle ) {
142 return Status::newFatal( 'tmp-create-error' );
145 $this->mRemoveTempFile = true;
146 $this->mFileSize = 0;
148 $req = MWHttpRequest::factory( $this->mUrl );
149 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
150 $status = $req->execute();
152 if ( $this->mTmpHandle ) {
153 // File got written ok...
154 fclose( $this->mTmpHandle );
155 $this->mTmpHandle = null;
156 } else {
157 // We encountered a write error during the download...
158 return Status::newFatal( 'tmp-write-error' );
161 if ( !$status->isOk() ) {
162 return $status;
165 return $status;
169 * Wrapper around the parent function in order to defer verifying the
170 * upload until the file really has been fetched.
172 public function verifyUpload() {
173 if ( $this->mAsync ) {
174 return array( 'status' => UploadBase::OK );
176 return parent::verifyUpload();
180 * Wrapper around the parent function in order to defer checking warnings
181 * until the file really has been fetched.
183 public function checkWarnings() {
184 if ( $this->mAsync ) {
185 $this->mIgnoreWarnings = false;
186 return array();
188 return parent::checkWarnings();
192 * Wrapper around the parent function in order to defer checking protection
193 * until we are sure that the file can actually be uploaded
195 public function verifyTitlePermissions( $user ) {
196 if ( $this->mAsync ) {
197 return true;
199 return parent::verifyTitlePermissions( $user );
203 * Wrapper around the parent function in order to defer uploading to the
204 * job queue for asynchronous uploads
206 public function performUpload( $comment, $pageText, $watch, $user ) {
207 if ( $this->mAsync ) {
208 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
210 $status = new Status;
211 $status->error( 'async', $sessionKey );
212 return $status;
215 return parent::performUpload( $comment, $pageText, $watch, $user );
219 * @param $comment
220 * @param $pageText
221 * @param $watch
222 * @param $user User
223 * @return
225 protected function insertJob( $comment, $pageText, $watch, $user ) {
226 $sessionKey = $this->stashSession();
227 $job = new UploadFromUrlJob( $this->getTitle(), array(
228 'url' => $this->mUrl,
229 'comment' => $comment,
230 'pageText' => $pageText,
231 'watch' => $watch,
232 'userName' => $user->getName(),
233 'leaveMessage' => $this->mAsync == 'async-leavemessage',
234 'ignoreWarnings' => $this->mIgnoreWarnings,
235 'sessionId' => session_id(),
236 'sessionKey' => $sessionKey,
237 ) );
238 $job->initializeSessionData();
239 $job->insert();
240 return $sessionKey;