3 * Implements Special:UploadStash
5 * Web access for files temporarily stored by UploadStash.
7 * For example -- files that were uploaded with the UploadWizard extension are stored temporarily
8 * before committing them to the db. But we want to see their thumbnails and get other information
11 * Since this is based on the user's session, in effect this creates a private temporary file area.
12 * However, the URLs for the files cannot be shared.
15 * @ingroup SpecialPage
19 class SpecialUploadStash
extends UnlistedSpecialPage
{
23 // Since we are directly writing the file to STDOUT,
24 // we should not be reading in really big files and serving them out.
26 // We also don't want people using this as a file drop, even if they
29 // This service is really for thumbnails and other such previews while
31 const MAX_SERVE_BYTES
= 1048576; // 1MB
33 public function __construct() {
34 parent
::__construct( 'UploadStash', 'upload' );
36 $this->stash
= RepoGroup
::singleton()->getLocalRepo()->getUploadStash();
37 } catch ( UploadStashNotAvailableException
$e ) {
43 * Execute page -- can output a file directly or show a listing of them.
45 * @param $subPage String: subpage, e.g. in http://example.com/wiki/Special:UploadStash/foo.jpg, the "foo.jpg" part
46 * @return Boolean: success
48 public function execute( $subPage ) {
51 if ( !$this->userCanExecute( $wgUser ) ) {
52 $this->displayRestrictionError();
56 if ( !isset( $subPage ) ||
$subPage === '' ) {
57 return $this->showUploads();
60 return $this->showUpload( $subPage );
65 * If file available in stash, cats it out to the client as a simple HTTP response.
66 * n.b. Most sanity checking done in UploadStashLocalFile, so this is straightforward.
68 * @param $key String: the key of a particular requested file
70 public function showUpload( $key ) {
73 // prevent callers from doing standard HTML output -- we'll take it from here
77 $params = $this->parseKey( $key );
78 if ( $params['type'] === 'thumb' ) {
79 return $this->outputThumbFromStash( $params['file'], $params['params'] );
81 return $this->outputLocalFile( $params['file'] );
83 } catch( UploadStashFileNotFoundException
$e ) {
85 $message = $e->getMessage();
86 } catch( UploadStashZeroLengthFileException
$e ) {
88 $message = $e->getMessage();
89 } catch( UploadStashBadPathException
$e ) {
91 $message = $e->getMessage();
92 } catch( SpecialUploadStashTooLargeException
$e ) {
94 $message = 'Cannot serve a file larger than ' . self
::MAX_SERVE_BYTES
. ' bytes. ' . $e->getMessage();
95 } catch( Exception
$e ) {
97 $message = $e->getMessage();
100 wfHttpError( $code, HttpStatus
::getMessage( $code ), $message );
105 * Parse the key passed to the SpecialPage. Returns an array containing
106 * the associated file object, the type ('file' or 'thumb') and if
107 * application the transform parameters
112 private function parseKey( $key ) {
113 $type = strtok( $key, '/' );
115 if ( $type !== 'file' && $type !== 'thumb' ) {
116 throw new UploadStashBadPathException( "Unknown type '$type'" );
118 $fileName = strtok( '/' );
119 $thumbPart = strtok( '/' );
120 $file = $this->stash
->getFile( $fileName );
121 if ( $type === 'thumb' ) {
122 $srcNamePos = strrpos( $thumbPart, $fileName );
123 if ( $srcNamePos === false ||
$srcNamePos < 1 ) {
124 throw new UploadStashBadPathException( 'Unrecognized thumb name' );
126 $paramString = substr( $thumbPart, 0, $srcNamePos - 1 );
128 $handler = $file->getHandler();
129 $params = $handler->parseParamString( $paramString );
130 return array( 'file' => $file, 'type' => $type, 'params' => $params );
133 return array( 'file' => $file, 'type' => $type );
137 * Get a thumbnail for file, either generated locally or remotely, and stream it out
140 * @param $params array
142 * @return boolean success
144 private function outputThumbFromStash( $file, $params ) {
146 // this global, if it exists, points to a "scaler", as you might find in the Wikimedia Foundation cluster. See outputRemoteScaledThumb()
147 // this is part of our horrible NFS-based system, we create a file on a mount point here, but fetch the scaled file from somewhere else that
148 // happens to share it over NFS
149 global $wgUploadStashScalerBaseUrl;
152 if ( $wgUploadStashScalerBaseUrl ) {
153 $this->outputRemoteScaledThumb( $file, $params, $flags );
155 $this->outputLocallyScaledThumb( $file, $params, $flags );
160 * Scale a file (probably with a locally installed imagemagick, or similar) and output it to STDOUT.
161 * @param $file: File object
162 * @param $params: scaling parameters ( e.g. array( width => '50' ) );
163 * @param $flags: scaling flags ( see File:: constants )
164 * @throws MWException
165 * @return boolean success
167 private function outputLocallyScaledThumb( $file, $params, $flags ) {
169 // n.b. this is stupid, we insist on re-transforming the file every time we are invoked. We rely
170 // on HTTP caching to ensure this doesn't happen.
172 $flags |
= File
::RENDER_NOW
;
174 $thumbnailImage = $file->transform( $params, $flags );
175 if ( !$thumbnailImage ) {
176 throw new MWException( 'Could not obtain thumbnail' );
179 // we should have just generated it locally
180 if ( ! $thumbnailImage->getPath() ) {
181 throw new UploadStashFileNotFoundException( "no local path for scaled item" );
184 // now we should construct a File, so we can get mime and other such info in a standard way
185 // n.b. mimetype may be different from original (ogx original -> jpeg thumb)
186 $thumbFile = new UnregisteredLocalFile( false, $this->stash
->repo
, $thumbnailImage->getPath(), false );
187 if ( ! $thumbFile ) {
188 throw new UploadStashFileNotFoundException( "couldn't create local file object for thumbnail" );
191 return $this->outputLocalFile( $thumbFile );
196 * Scale a file with a remote "scaler", as exists on the Wikimedia Foundation cluster, and output it to STDOUT.
197 * Note: unlike the usual thumbnail process, the web client never sees the cluster URL; we do the whole HTTP transaction to the scaler ourselves
198 * and cat the results out.
199 * Note: We rely on NFS to have propagated the file contents to the scaler. However, we do not rely on the thumbnail being created in NFS and then
200 * propagated back to our filesystem. Instead we take the results of the HTTP request instead.
201 * Note: no caching is being done here, although we are instructing the client to cache it forever.
202 * @param $file: File object
203 * @param $params: scaling parameters ( e.g. array( width => '50' ) );
204 * @param $flags: scaling flags ( see File:: constants )
205 * @throws MWException
206 * @return boolean success
208 private function outputRemoteScaledThumb( $file, $params, $flags ) {
210 // this global probably looks something like 'http://upload.wikimedia.org/wikipedia/test/thumb/temp'
211 // do not use trailing slash
212 global $wgUploadStashScalerBaseUrl;
213 $scalerBaseUrl = $wgUploadStashScalerBaseUrl;
215 if( preg_match( '/^\/\//', $scalerBaseUrl ) ) {
216 // this is apparently a protocol-relative URL, which makes no sense in this context,
217 // since this is used for communication that's internal to the application.
219 $scalerBaseUrl = 'http:' . $scalerBaseUrl;
222 // We need to use generateThumbName() instead of thumbName(), because
223 // the suffix needs to match the file name for the remote thumbnailer
225 $scalerThumbName = $file->generateThumbName( $file->getName(), $params );
226 $scalerThumbUrl = $scalerBaseUrl . '/' . $file->getUrlRel() .
227 '/' . rawurlencode( $scalerThumbName );
229 // make a curl call to the scaler to create a thumbnail
230 $httpOptions = array(
232 'timeout' => 'default'
234 $req = MWHttpRequest
::factory( $scalerThumbUrl, $httpOptions );
235 $status = $req->execute();
236 if ( ! $status->isOK() ) {
237 $errors = $status->getErrorsArray();
238 $errorStr = "Fetching thumbnail failed: " . print_r( $errors, 1 );
239 $errorStr .= "\nurl = $scalerThumbUrl\n";
240 throw new MWException( $errorStr );
242 $contentType = $req->getResponseHeader( "content-type" );
243 if ( ! $contentType ) {
244 throw new MWException( "Missing content-type header" );
246 return $this->outputContents( $req->getContent(), $contentType );
250 * Output HTTP response for file
251 * Side effect: writes HTTP response to STDOUT.
252 * XXX could use wfStreamfile (in includes/Streamfile.php), but for consistency with outputContents() doing it this way.
253 * XXX is mimeType really enough, or do we need encoding for full Content-Type header?
255 * @param $file File object with a local path (e.g. UnregisteredLocalFile, LocalFile. Oddly these don't share an ancestor!)
257 private function outputLocalFile( $file ) {
258 if ( $file->getSize() > self
::MAX_SERVE_BYTES
) {
259 throw new SpecialUploadStashTooLargeException();
261 self
::outputFileHeaders( $file->getMimeType(), $file->getSize() );
262 readfile( $file->getPath() );
267 * Output HTTP response of raw content
268 * Side effect: writes HTTP response to STDOUT.
269 * @param String $content: content
270 * @param String $mimeType: mime type
272 private function outputContents( $content, $contentType ) {
273 $size = strlen( $content );
274 if ( $size > self
::MAX_SERVE_BYTES
) {
275 throw new SpecialUploadStashTooLargeException();
277 self
::outputFileHeaders( $contentType, $size );
283 * Output headers for streaming
284 * XXX unsure about encoding as binary; if we received from HTTP perhaps we should use that encoding, concatted with semicolon to mimeType as it usually is.
285 * Side effect: preps PHP to write headers to STDOUT.
286 * @param String $contentType : string suitable for content-type header
287 * @param String $size: length in bytes
289 private static function outputFileHeaders( $contentType, $size ) {
290 header( "Content-Type: $contentType", true );
291 header( 'Content-Transfer-Encoding: binary', true );
292 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true );
293 header( "Content-Length: $size", true );
297 * Static callback for the HTMLForm in showUploads, to process
298 * Note the stash has to be recreated since this is being called in a static context.
299 * This works, because there really is only one stash per logged-in user, despite appearances.
303 public static function tryClearStashedUploads( $formData ) {
304 if ( isset( $formData['Clear'] ) ) {
305 $stash = RepoGroup
::singleton()->getLocalRepo()->getUploadStash();
306 wfDebug( "stash has: " . print_r( $stash->listFiles(), true ) );
307 if ( ! $stash->clear() ) {
308 return Status
::newFatal( 'uploadstash-errclear' );
311 return Status
::newGood();
315 * Default action when we don't have a subpage -- just show links to the uploads we have,
316 * Also show a button to clear stashed files
317 * @param Status : $status - the result of processRequest
319 private function showUploads( $status = null ) {
321 if ( $status === null ) {
322 $status = Status
::newGood();
325 // sets the title, etc.
327 $this->outputHeader();
329 // create the form, which will also be used to execute a callback to process incoming form data
330 // this design is extremely dubious, but supposedly HTMLForm is our standard now?
332 $form = new HTMLForm( array(
338 ), 'clearStashedUploads' );
339 $form->setSubmitCallback( array( __CLASS__
, 'tryClearStashedUploads' ) );
340 $form->setTitle( $this->getTitle() );
341 $form->setSubmitText( wfMsg( 'uploadstash-clear' ) );
343 $form->prepareForm();
344 $formResult = $form->tryAuthorizedSubmit();
346 // show the files + form, if there are any, or just say there are none
347 $refreshHtml = Html
::element( 'a',
348 array( 'href' => $this->getTitle()->getLocalURL() ),
349 wfMsg( 'uploadstash-refresh' ) );
350 $files = $this->stash
->listFiles();
351 if ( $files && count( $files ) ) {
353 $fileListItemsHtml = '';
354 foreach ( $files as $file ) {
355 // TODO: Use Linker::link or even construct the list in plain wikitext
356 $fileListItemsHtml .= Html
::rawElement( 'li', array(),
357 Html
::element( 'a', array( 'href' =>
358 $this->getTitle( "file/$file" )->getLocalURL() ), $file )
361 $wgOut->addHtml( Html
::rawElement( 'ul', array(), $fileListItemsHtml ) );
362 $form->displayForm( $formResult );
363 $wgOut->addHtml( Html
::rawElement( 'p', array(), $refreshHtml ) );
365 $wgOut->addHtml( Html
::rawElement( 'p', array(),
366 Html
::element( 'span', array(), wfMsg( 'uploadstash-nofiles' ) )
376 class SpecialUploadStashTooLargeException
extends MWException
{};