The test "{{Foo|1=bar|2=baz}}" got removed in r96887.
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
1 <?php
2 /**
3 * Implements uploading from a HTTP resource.
4 *
5 * @file
6 * @ingroup upload
7 * @author Bryan Tong Minh
8 * @author Michael Dale
9 */
10
11 class UploadFromUrl extends UploadBase {
12 protected $mAsync, $mUrl;
13 protected $mIgnoreWarnings = true;
14
15 protected $mTempPath;
16
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.
20 *
21 * @param $user User
22 *
23 * @return bool
24 */
25 public static function isAllowed( $user ) {
26 if ( !$user->isAllowed( 'upload_by_url' ) ) {
27 return 'upload_by_url';
28 }
29 return parent::isAllowed( $user );
30 }
31
32 /**
33 * Checks if the upload from URL feature is enabled
34 * @return bool
35 */
36 public static function isEnabled() {
37 global $wgAllowCopyUploads;
38 return $wgAllowCopyUploads && parent::isEnabled();
39 }
40
41 /**
42 * Entry point for API upload
43 *
44 * @param $name string
45 * @param $url string
46 * @param $async mixed Whether the download should be performed
47 * asynchronous. False for synchronous, async or async-leavemessage for
48 * asynchronous download.
49 */
50 public function initialize( $name, $url, $async = false ) {
51 global $wgAllowAsyncCopyUploads;
52
53 $this->mUrl = $url;
54 $this->mAsync = $wgAllowAsyncCopyUploads ? $async : false;
55 if ( $async ) {
56 throw new MWException( 'Asynchronous copy uploads are no longer possible as of r81612.' );
57 }
58
59 $tempPath = $this->mAsync ? null : $this->makeTemporaryFile();
60 # File size and removeTempFile will be filled in later
61 $this->initializePathInfo( $name, $tempPath, 0, false );
62 }
63
64 /**
65 * Entry point for SpecialUpload
66 * @param $request WebRequest object
67 */
68 public function initializeFromRequest( &$request ) {
69 $desiredDestName = $request->getText( 'wpDestFile' );
70 if ( !$desiredDestName )
71 $desiredDestName = $request->getText( 'wpUploadFileURL' );
72 return $this->initialize(
73 $desiredDestName,
74 trim( $request->getVal( 'wpUploadFileURL' ) ),
75 false
76 );
77 }
78
79 /**
80 * @param $request WebRequest object
81 */
82 public static function isValidRequest( $request ) {
83 global $wgUser;
84
85 $url = $request->getVal( 'wpUploadFileURL' );
86 return !empty( $url )
87 && Http::isValidURI( $url )
88 && $wgUser->isAllowed( 'upload_by_url' );
89 }
90
91 public function getSourceType() { return 'url'; }
92
93 public function fetchFile() {
94 if ( !Http::isValidURI( $this->mUrl ) ) {
95 return Status::newFatal( 'http-invalid-url' );
96 }
97
98 if ( !$this->mAsync ) {
99 return $this->reallyFetchFile();
100 }
101 return Status::newGood();
102 }
103 /**
104 * Create a new temporary file in the URL subdirectory of wfTempDir().
105 *
106 * @return string Path to the file
107 */
108 protected function makeTemporaryFile() {
109 return tempnam( wfTempDir(), 'URL' );
110 }
111
112 /**
113 * Callback: save a chunk of the result of a HTTP request to the temporary file
114 *
115 * @param $req mixed
116 * @param $buffer string
117 * @return int number of bytes handled
118 */
119 public function saveTempFileChunk( $req, $buffer ) {
120 $nbytes = fwrite( $this->mTmpHandle, $buffer );
121
122 if ( $nbytes == strlen( $buffer ) ) {
123 $this->mFileSize += $nbytes;
124 } else {
125 // Well... that's not good!
126 fclose( $this->mTmpHandle );
127 $this->mTmpHandle = false;
128 }
129
130 return $nbytes;
131 }
132
133 /**
134 * Download the file, save it to the temporary file and update the file
135 * size and set $mRemoveTempFile to true.
136 */
137 protected function reallyFetchFile() {
138 if ( $this->mTempPath === false ) {
139 return Status::newFatal( 'tmp-create-error' );
140 }
141
142 // Note the temporary file should already be created by makeTemporaryFile()
143 $this->mTmpHandle = fopen( $this->mTempPath, 'wb' );
144 if ( !$this->mTmpHandle ) {
145 return Status::newFatal( 'tmp-create-error' );
146 }
147
148 $this->mRemoveTempFile = true;
149 $this->mFileSize = 0;
150
151 $req = MWHttpRequest::factory( $this->mUrl, array(
152 'followRedirects' => true
153 ) );
154 $req->setCallback( array( $this, 'saveTempFileChunk' ) );
155 $status = $req->execute();
156
157 if ( $this->mTmpHandle ) {
158 // File got written ok...
159 fclose( $this->mTmpHandle );
160 $this->mTmpHandle = null;
161 } else {
162 // We encountered a write error during the download...
163 return Status::newFatal( 'tmp-write-error' );
164 }
165
166 if ( !$status->isOk() ) {
167 return $status;
168 }
169
170 return $status;
171 }
172
173 /**
174 * Wrapper around the parent function in order to defer verifying the
175 * upload until the file really has been fetched.
176 */
177 public function verifyUpload() {
178 if ( $this->mAsync ) {
179 return array( 'status' => UploadBase::OK );
180 }
181 return parent::verifyUpload();
182 }
183
184 /**
185 * Wrapper around the parent function in order to defer checking warnings
186 * until the file really has been fetched.
187 */
188 public function checkWarnings() {
189 if ( $this->mAsync ) {
190 $this->mIgnoreWarnings = false;
191 return array();
192 }
193 return parent::checkWarnings();
194 }
195
196 /**
197 * Wrapper around the parent function in order to defer checking protection
198 * until we are sure that the file can actually be uploaded
199 */
200 public function verifyTitlePermissions( $user ) {
201 if ( $this->mAsync ) {
202 return true;
203 }
204 return parent::verifyTitlePermissions( $user );
205 }
206
207 /**
208 * Wrapper around the parent function in order to defer uploading to the
209 * job queue for asynchronous uploads
210 */
211 public function performUpload( $comment, $pageText, $watch, $user ) {
212 if ( $this->mAsync ) {
213 $sessionKey = $this->insertJob( $comment, $pageText, $watch, $user );
214
215 $status = new Status;
216 $status->error( 'async', $sessionKey );
217 return $status;
218 }
219
220 return parent::performUpload( $comment, $pageText, $watch, $user );
221 }
222
223 /**
224 * @param $comment
225 * @param $pageText
226 * @param $watch
227 * @param $user User
228 * @return
229 */
230 protected function insertJob( $comment, $pageText, $watch, $user ) {
231 $sessionKey = $this->stashSession();
232 $job = new UploadFromUrlJob( $this->getTitle(), array(
233 'url' => $this->mUrl,
234 'comment' => $comment,
235 'pageText' => $pageText,
236 'watch' => $watch,
237 'userName' => $user->getName(),
238 'leaveMessage' => $this->mAsync == 'async-leavemessage',
239 'ignoreWarnings' => $this->mIgnoreWarnings,
240 'sessionId' => session_id(),
241 'sessionKey' => $sessionKey,
242 ) );
243 $job->initializeSessionData();
244 $job->insert();
245 return $sessionKey;
246 }
247
248 }