3 * Implements Special:UploadStash.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
26 * Web access for files temporarily stored by UploadStash.
28 * For example -- files that were uploaded with the UploadWizard extension are stored temporarily
29 * before committing them to the db. But we want to see their thumbnails and get other information
32 * Since this is based on the user's session, in effect this creates a private temporary file area.
33 * However, the URLs for the files cannot be shared.
35 class SpecialUploadStash
extends UnlistedSpecialPage
{
39 // Since we are directly writing the file to STDOUT,
40 // we should not be reading in really big files and serving them out.
42 // We also don't want people using this as a file drop, even if they
45 // This service is really for thumbnails and other such previews while
47 const MAX_SERVE_BYTES
= 1048576; // 1MB
49 public function __construct() {
50 parent
::__construct( 'UploadStash', 'upload' );
52 $this->stash
= RepoGroup
::singleton()->getLocalRepo()->getUploadStash( $this->getUser() );
53 } catch ( UploadStashNotAvailableException
$e ) {
58 * Execute page -- can output a file directly or show a listing of them.
60 * @param string $subPage Subpage, e.g. in
61 * http://example.com/wiki/Special:UploadStash/foo.jpg, the "foo.jpg" part
62 * @return bool Success
64 public function execute( $subPage ) {
65 $this->checkPermissions();
67 if ( $subPage === null ||
$subPage === '' ) {
68 return $this->showUploads();
71 return $this->showUpload( $subPage );
75 * If file available in stash, cats it out to the client as a simple HTTP response.
76 * n.b. Most sanity checking done in UploadStashLocalFile, so this is straightforward.
78 * @param string $key The key of a particular requested file
82 public function showUpload( $key ) {
83 // prevent callers from doing standard HTML output -- we'll take it from here
84 $this->getOutput()->disable();
87 $params = $this->parseKey( $key );
88 if ( $params['type'] === 'thumb' ) {
89 return $this->outputThumbFromStash( $params['file'], $params['params'] );
91 return $this->outputLocalFile( $params['file'] );
93 } catch ( UploadStashFileNotFoundException
$e ) {
95 $message = $e->getMessage();
96 } catch ( UploadStashZeroLengthFileException
$e ) {
98 $message = $e->getMessage();
99 } catch ( UploadStashBadPathException
$e ) {
101 $message = $e->getMessage();
102 } catch ( SpecialUploadStashTooLargeException
$e ) {
104 $message = 'Cannot serve a file larger than ' . self
::MAX_SERVE_BYTES
.
105 ' bytes. ' . $e->getMessage();
106 } catch ( Exception
$e ) {
108 $message = $e->getMessage();
111 throw new HttpError( $code, $message );
115 * Parse the key passed to the SpecialPage. Returns an array containing
116 * the associated file object, the type ('file' or 'thumb') and if
117 * application the transform parameters
120 * @throws UploadStashBadPathException
123 private function parseKey( $key ) {
124 $type = strtok( $key, '/' );
126 if ( $type !== 'file' && $type !== 'thumb' ) {
127 throw new UploadStashBadPathException( "Unknown type '$type'" );
129 $fileName = strtok( '/' );
130 $thumbPart = strtok( '/' );
131 $file = $this->stash
->getFile( $fileName );
132 if ( $type === 'thumb' ) {
133 $srcNamePos = strrpos( $thumbPart, $fileName );
134 if ( $srcNamePos === false ||
$srcNamePos < 1 ) {
135 throw new UploadStashBadPathException( 'Unrecognized thumb name' );
137 $paramString = substr( $thumbPart, 0, $srcNamePos - 1 );
139 $handler = $file->getHandler();
141 $params = $handler->parseParamString( $paramString );
143 return array( 'file' => $file, 'type' => $type, 'params' => $params );
145 throw new UploadStashBadPathException( 'No handler found for ' .
146 "mime {$file->getMimeType()} of file {$file->getPath()}" );
150 return array( 'file' => $file, 'type' => $type );
154 * Get a thumbnail for file, either generated locally or remotely, and stream it out
157 * @param array $params
159 * @return bool Success
161 private function outputThumbFromStash( $file, $params ) {
163 // this config option, if it exists, points to a "scaler", as you might find in
164 // the Wikimedia Foundation cluster. See outputRemoteScaledThumb(). This
165 // is part of our horrible NFS-based system, we create a file on a mount
166 // point here, but fetch the scaled file from somewhere else that
167 // happens to share it over NFS.
168 if ( $this->getConfig()->get( 'UploadStashScalerBaseUrl' ) ) {
169 $this->outputRemoteScaledThumb( $file, $params, $flags );
171 $this->outputLocallyScaledThumb( $file, $params, $flags );
176 * Scale a file (probably with a locally installed imagemagick, or similar)
177 * and output it to STDOUT.
179 * @param array $params Scaling parameters ( e.g. array( width => '50' ) );
180 * @param int $flags Scaling flags ( see File:: constants )
181 * @throws MWException|UploadStashFileNotFoundException
182 * @return bool Success
184 private function outputLocallyScaledThumb( $file, $params, $flags ) {
185 // n.b. this is stupid, we insist on re-transforming the file every time we are invoked. We rely
186 // on HTTP caching to ensure this doesn't happen.
188 $flags |
= File
::RENDER_NOW
;
190 $thumbnailImage = $file->transform( $params, $flags );
191 if ( !$thumbnailImage ) {
192 throw new MWException( 'Could not obtain thumbnail' );
195 // we should have just generated it locally
196 if ( !$thumbnailImage->getStoragePath() ) {
197 throw new UploadStashFileNotFoundException( "no local path for scaled item" );
200 // now we should construct a File, so we can get MIME and other such info in a standard way
201 // n.b. MIME type may be different from original (ogx original -> jpeg thumb)
202 $thumbFile = new UnregisteredLocalFile( false,
203 $this->stash
->repo
, $thumbnailImage->getStoragePath(), false );
205 throw new UploadStashFileNotFoundException( "couldn't create local file object for thumbnail" );
208 return $this->outputLocalFile( $thumbFile );
212 * Scale a file with a remote "scaler", as exists on the Wikimedia Foundation
213 * cluster, and output it to STDOUT.
214 * Note: Unlike the usual thumbnail process, the web client never sees the
215 * cluster URL; we do the whole HTTP transaction to the scaler ourselves
216 * and cat the results out.
217 * Note: We rely on NFS to have propagated the file contents to the scaler.
218 * However, we do not rely on the thumbnail being created in NFS and then
219 * propagated back to our filesystem. Instead we take the results of the
220 * HTTP request instead.
221 * Note: No caching is being done here, although we are instructing the
222 * client to cache it forever.
225 * @param array $params Scaling parameters ( e.g. array( width => '50' ) );
226 * @param int $flags Scaling flags ( see File:: constants )
227 * @throws MWException
228 * @return bool Success
230 private function outputRemoteScaledThumb( $file, $params, $flags ) {
231 // This option probably looks something like
232 // 'http://upload.wikimedia.org/wikipedia/test/thumb/temp'. Do not use
234 $scalerBaseUrl = $this->getConfig()->get( 'UploadStashScalerBaseUrl' );
236 if ( preg_match( '/^\/\//', $scalerBaseUrl ) ) {
237 // this is apparently a protocol-relative URL, which makes no sense in this context,
238 // since this is used for communication that's internal to the application.
240 $scalerBaseUrl = wfExpandUrl( $scalerBaseUrl, PROTO_CANONICAL
);
243 // We need to use generateThumbName() instead of thumbName(), because
244 // the suffix needs to match the file name for the remote thumbnailer
246 $scalerThumbName = $file->generateThumbName( $file->getName(), $params );
247 $scalerThumbUrl = $scalerBaseUrl . '/' . $file->getUrlRel() .
248 '/' . rawurlencode( $scalerThumbName );
250 // make a curl call to the scaler to create a thumbnail
251 $httpOptions = array(
253 'timeout' => 'default'
255 $req = MWHttpRequest
::factory( $scalerThumbUrl, $httpOptions );
256 $status = $req->execute();
257 if ( !$status->isOK() ) {
258 $errors = $status->getErrorsArray();
259 $errorStr = "Fetching thumbnail failed: " . print_r( $errors, 1 );
260 $errorStr .= "\nurl = $scalerThumbUrl\n";
261 throw new MWException( $errorStr );
263 $contentType = $req->getResponseHeader( "content-type" );
264 if ( !$contentType ) {
265 throw new MWException( "Missing content-type header" );
268 return $this->outputContents( $req->getContent(), $contentType );
272 * Output HTTP response for file
273 * Side effect: writes HTTP response to STDOUT.
275 * @param File $file File object with a local path (e.g. UnregisteredLocalFile,
276 * LocalFile. Oddly these don't share an ancestor!)
277 * @throws SpecialUploadStashTooLargeException
280 private function outputLocalFile( File
$file ) {
281 if ( $file->getSize() > self
::MAX_SERVE_BYTES
) {
282 throw new SpecialUploadStashTooLargeException();
285 return $file->getRepo()->streamFile( $file->getPath(),
286 array( 'Content-Transfer-Encoding: binary',
287 'Expires: Sun, 17-Jan-2038 19:14:07 GMT' )
292 * Output HTTP response of raw content
293 * Side effect: writes HTTP response to STDOUT.
294 * @param string $content Content
295 * @param string $contentType MIME type
296 * @throws SpecialUploadStashTooLargeException
299 private function outputContents( $content, $contentType ) {
300 $size = strlen( $content );
301 if ( $size > self
::MAX_SERVE_BYTES
) {
302 throw new SpecialUploadStashTooLargeException();
304 self
::outputFileHeaders( $contentType, $size );
311 * Output headers for streaming
312 * @todo Unsure about encoding as binary; if we received from HTTP perhaps
313 * we should use that encoding, concatenated with semicolon to `$contentType` as it
315 * Side effect: preps PHP to write headers to STDOUT.
316 * @param string $contentType String suitable for content-type header
317 * @param string $size Length in bytes
319 private static function outputFileHeaders( $contentType, $size ) {
320 header( "Content-Type: $contentType", true );
321 header( 'Content-Transfer-Encoding: binary', true );
322 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true );
323 // Bug 53032 - It shouldn't be a problem here, but let's be safe and not cache
324 header( 'Cache-Control: private' );
325 header( "Content-Length: $size", true );
329 * Static callback for the HTMLForm in showUploads, to process
330 * Note the stash has to be recreated since this is being called in a static context.
331 * This works, because there really is only one stash per logged-in user, despite appearances.
333 * @param array $formData
334 * @param HTMLForm $form
337 public static function tryClearStashedUploads( $formData, $form ) {
338 if ( isset( $formData['Clear'] ) ) {
339 $stash = RepoGroup
::singleton()->getLocalRepo()->getUploadStash( $form->getUser() );
340 wfDebug( 'stash has: ' . print_r( $stash->listFiles(), true ) . "\n" );
342 if ( !$stash->clear() ) {
343 return Status
::newFatal( 'uploadstash-errclear' );
347 return Status
::newGood();
351 * Default action when we don't have a subpage -- just show links to the uploads we have,
352 * Also show a button to clear stashed files
355 private function showUploads() {
356 // sets the title, etc.
358 $this->outputHeader();
360 // create the form, which will also be used to execute a callback to process incoming form data
361 // this design is extremely dubious, but supposedly HTMLForm is our standard now?
363 $context = new DerivativeContext( $this->getContext() );
364 $context->setTitle( $this->getPageTitle() ); // Remove subpage
365 $form = new HTMLForm( array(
371 ), $context, 'clearStashedUploads' );
372 $form->setSubmitCallback( array( __CLASS__
, 'tryClearStashedUploads' ) );
373 $form->setSubmitTextMsg( 'uploadstash-clear' );
375 $form->prepareForm();
376 $formResult = $form->tryAuthorizedSubmit();
378 // show the files + form, if there are any, or just say there are none
379 $refreshHtml = Html
::element( 'a',
380 array( 'href' => $this->getPageTitle()->getLocalURL() ),
381 $this->msg( 'uploadstash-refresh' )->text() );
382 $files = $this->stash
->listFiles();
383 if ( $files && count( $files ) ) {
385 $fileListItemsHtml = '';
386 foreach ( $files as $file ) {
387 // TODO: Use Linker::link or even construct the list in plain wikitext
388 $fileListItemsHtml .= Html
::rawElement( 'li', array(),
389 Html
::element( 'a', array( 'href' =>
390 $this->getPageTitle( "file/$file" )->getLocalURL() ), $file )
393 $this->getOutput()->addHtml( Html
::rawElement( 'ul', array(), $fileListItemsHtml ) );
394 $form->displayForm( $formResult );
395 $this->getOutput()->addHtml( Html
::rawElement( 'p', array(), $refreshHtml ) );
397 $this->getOutput()->addHtml( Html
::rawElement( 'p', array(),
398 Html
::element( 'span', array(), $this->msg( 'uploadstash-nofiles' )->text() )
408 class SpecialUploadStashTooLargeException
extends MWException
{