MediaWiki  master
SpecialUploadStash.php
Go to the documentation of this file.
1 <?php
38  private $stash;
39 
41  private $localRepo;
42 
53  private const MAX_SERVE_BYTES = 1048576; // 1MB
54 
58  public function __construct( RepoGroup $repoGroup ) {
59  parent::__construct( 'UploadStash', 'upload' );
60  $this->localRepo = $repoGroup->getLocalRepo();
61  }
62 
63  public function doesWrites() {
64  return true;
65  }
66 
73  public function execute( $subPage ) {
75 
76  // This is not set in constructor, because the context with the user is not safe to be set
77  $this->stash = $this->localRepo->getUploadStash( $this->getUser() );
78  $this->checkPermissions();
79 
80  if ( $subPage === null || $subPage === '' ) {
81  $this->showUploads();
82  } else {
83  $this->showUpload( $subPage );
84  }
85  }
86 
94  public function showUpload( $key ) {
95  // prevent callers from doing standard HTML output -- we'll take it from here
96  $this->getOutput()->disable();
97 
98  try {
99  $params = $this->parseKey( $key );
100  if ( $params['type'] === 'thumb' ) {
101  $this->outputThumbFromStash( $params['file'], $params['params'] );
102  } else {
103  $this->outputLocalFile( $params['file'] );
104  }
105  return;
106  } catch ( UploadStashFileNotFoundException $e ) {
107  $code = 404;
108  $message = $e->getMessage();
109  } catch ( UploadStashZeroLengthFileException $e ) {
110  $code = 500;
111  $message = $e->getMessage();
112  } catch ( UploadStashBadPathException $e ) {
113  $code = 500;
114  $message = $e->getMessage();
115  } catch ( SpecialUploadStashTooLargeException $e ) {
116  $code = 500;
117  $message = $e->getMessage();
118  } catch ( Exception $e ) {
119  $code = 500;
120  $message = $e->getMessage();
121  }
122 
123  throw new HttpError( $code, $message );
124  }
125 
135  private function parseKey( $key ) {
136  $type = strtok( $key, '/' );
137 
138  if ( $type !== 'file' && $type !== 'thumb' ) {
139  throw new UploadStashBadPathException(
140  $this->msg( 'uploadstash-bad-path-unknown-type', $type )
141  );
142  }
143  $fileName = strtok( '/' );
144  $thumbPart = strtok( '/' );
145  $file = $this->stash->getFile( $fileName );
146  if ( $type === 'thumb' ) {
147  $srcNamePos = strrpos( $thumbPart, $fileName );
148  if ( $srcNamePos === false || $srcNamePos < 1 ) {
149  throw new UploadStashBadPathException(
150  $this->msg( 'uploadstash-bad-path-unrecognized-thumb-name' )
151  );
152  }
153  $paramString = substr( $thumbPart, 0, $srcNamePos - 1 );
154 
155  $handler = $file->getHandler();
156  if ( $handler ) {
157  $params = $handler->parseParamString( $paramString );
158 
159  return [ 'file' => $file, 'type' => $type, 'params' => $params ];
160  } else {
161  throw new UploadStashBadPathException(
162  $this->msg( 'uploadstash-bad-path-no-handler', $file->getMimeType(), $file->getPath() )
163  );
164  }
165  }
166 
167  return [ 'file' => $file, 'type' => $type ];
168  }
169 
176  private function outputThumbFromStash( $file, $params ) {
177  $flags = 0;
178  // this config option, if it exists, points to a "scaler", as you might find in
179  // the Wikimedia Foundation cluster. See outputRemoteScaledThumb(). This
180  // is part of our horrible NFS-based system, we create a file on a mount
181  // point here, but fetch the scaled file from somewhere else that
182  // happens to share it over NFS.
183  if ( $this->getConfig()->get( 'UploadStashScalerBaseUrl' ) ) {
184  $this->outputRemoteScaledThumb( $file, $params, $flags );
185  } else {
186  $this->outputLocallyScaledThumb( $file, $params, $flags );
187  }
188  }
189 
198  private function outputLocallyScaledThumb( $file, $params, $flags ) {
199  // n.b. this is stupid, we insist on re-transforming the file every time we are invoked. We rely
200  // on HTTP caching to ensure this doesn't happen.
201 
202  $flags |= File::RENDER_NOW;
203 
204  $thumbnailImage = $file->transform( $params, $flags );
205  if ( !$thumbnailImage ) {
207  $this->msg( 'uploadstash-file-not-found-no-thumb' )
208  );
209  }
210 
211  // we should have just generated it locally
212  if ( !$thumbnailImage->getStoragePath() ) {
214  $this->msg( 'uploadstash-file-not-found-no-local-path' )
215  );
216  }
217 
218  // now we should construct a File, so we can get MIME and other such info in a standard way
219  // n.b. MIME type may be different from original (ogx original -> jpeg thumb)
220  $thumbFile = new UnregisteredLocalFile( false,
221  $this->stash->repo, $thumbnailImage->getStoragePath(), false );
222 
223  $this->outputLocalFile( $thumbFile );
224  }
225 
244  private function outputRemoteScaledThumb( $file, $params, $flags ) {
245  // This option probably looks something like
246  // '//upload.wikimedia.org/wikipedia/test/thumb/temp'. Do not use
247  // trailing slash.
248  $scalerBaseUrl = $this->getConfig()->get( 'UploadStashScalerBaseUrl' );
249 
250  if ( preg_match( '/^\/\//', $scalerBaseUrl ) ) {
251  // this is apparently a protocol-relative URL, which makes no sense in this context,
252  // since this is used for communication that's internal to the application.
253  // default to http.
254  $scalerBaseUrl = wfExpandUrl( $scalerBaseUrl, PROTO_CANONICAL );
255  }
256 
257  // We need to use generateThumbName() instead of thumbName(), because
258  // the suffix needs to match the file name for the remote thumbnailer
259  // to work
260  $scalerThumbName = $file->generateThumbName( $file->getName(), $params );
261  $scalerThumbUrl = $scalerBaseUrl . '/' . $file->getUrlRel() .
262  '/' . rawurlencode( $scalerThumbName );
263 
264  // If a thumb proxy is set up for the repo, we favor that, as that will
265  // keep the request internal
266  $thumbProxyUrl = $file->getRepo()->getThumbProxyUrl();
267 
268  if ( strlen( $thumbProxyUrl ) ) {
269  $scalerThumbUrl = $thumbProxyUrl . 'temp/' . $file->getUrlRel() .
270  '/' . rawurlencode( $scalerThumbName );
271  }
272 
273  // make an http request based on wgUploadStashScalerBaseUrl to lazy-create
274  // a thumbnail
275  $httpOptions = [
276  'method' => 'GET',
277  'timeout' => 5 // T90599 attempt to time out cleanly
278  ];
279  $req = MWHttpRequest::factory( $scalerThumbUrl, $httpOptions, __METHOD__ );
280 
281  $secret = $file->getRepo()->getThumbProxySecret();
282 
283  // Pass a secret key shared with the proxied service if any
284  if ( strlen( $secret ) ) {
285  $req->setHeader( 'X-Swift-Secret', $secret );
286  }
287 
288  $status = $req->execute();
289  if ( !$status->isOK() ) {
290  $errors = $status->getErrorsArray();
292  $this->msg(
293  'uploadstash-file-not-found-no-remote-thumb',
294  print_r( $errors, 1 ),
295  $scalerThumbUrl
296  )
297  );
298  }
299  $contentType = $req->getResponseHeader( "content-type" );
300  if ( !$contentType ) {
302  $this->msg( 'uploadstash-file-not-found-missing-content-type' )
303  );
304  }
305 
306  $this->outputContents( $req->getContent(), $contentType );
307  }
308 
317  private function outputLocalFile( File $file ) {
318  if ( $file->getSize() > self::MAX_SERVE_BYTES ) {
320  $this->msg( 'uploadstash-file-too-large', self::MAX_SERVE_BYTES )
321  );
322  }
323 
324  $file->getRepo()->streamFileWithStatus( $file->getPath(),
325  [ 'Content-Transfer-Encoding: binary',
326  'Expires: Sun, 17-Jan-2038 19:14:07 GMT' ]
327  );
328  }
329 
337  private function outputContents( $content, $contentType ) {
338  $size = strlen( $content );
339  if ( $size > self::MAX_SERVE_BYTES ) {
341  $this->msg( 'uploadstash-file-too-large', self::MAX_SERVE_BYTES )
342  );
343  }
344  // Cancel output buffering and gzipping if set
346  self::outputFileHeaders( $contentType, $size );
347  print $content;
348  }
349 
359  private static function outputFileHeaders( $contentType, $size ) {
360  header( "Content-Type: $contentType", true );
361  header( 'Content-Transfer-Encoding: binary', true );
362  header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true );
363  // T55032 - It shouldn't be a problem here, but let's be safe and not cache
364  header( 'Cache-Control: private' );
365  header( "Content-Length: $size", true );
366  }
367 
372  private function showUploads() {
373  // sets the title, etc.
374  $this->setHeaders();
375  $this->outputHeader();
376 
377  // create the form, which will also be used to execute a callback to process incoming form data
378  // this design is extremely dubious, but supposedly HTMLForm is our standard now?
379 
380  $context = new DerivativeContext( $this->getContext() );
381  $context->setTitle( $this->getPageTitle() ); // Remove subpage
382  $form = HTMLForm::factory( 'ooui', [
383  'Clear' => [
384  'type' => 'hidden',
385  'default' => true,
386  'name' => 'clear',
387  ]
388  ], $context, 'clearStashedUploads' );
389  $form->setSubmitDestructive();
390  $form->setSubmitCallback( function ( $formData, $form ) {
391  if ( isset( $formData['Clear'] ) ) {
392  wfDebug( 'stash has: ' . print_r( $this->stash->listFiles(), true ) );
393 
394  if ( !$this->stash->clear() ) {
395  return Status::newFatal( 'uploadstash-errclear' );
396  }
397  }
398 
399  return Status::newGood();
400  } );
401  $form->setSubmitTextMsg( 'uploadstash-clear' );
402 
403  $form->prepareForm();
404  $formResult = $form->tryAuthorizedSubmit();
405 
406  // show the files + form, if there are any, or just say there are none
407  $refreshHtml = Html::element( 'a',
408  [ 'href' => $this->getPageTitle()->getLocalURL() ],
409  $this->msg( 'uploadstash-refresh' )->text() );
410  $files = $this->stash->listFiles();
411  if ( $files && count( $files ) ) {
412  sort( $files );
413  $fileListItemsHtml = '';
414  $linkRenderer = $this->getLinkRenderer();
415  foreach ( $files as $file ) {
416  $itemHtml = $linkRenderer->makeKnownLink(
417  $this->getPageTitle( "file/$file" ),
418  $file
419  );
420  try {
421  $fileObj = $this->stash->getFile( $file );
422  $thumb = $fileObj->generateThumbName( $file, [ 'width' => 220 ] );
423  $itemHtml .=
424  $this->msg( 'word-separator' )->escaped() .
425  $this->msg( 'parentheses' )->rawParams(
426  $linkRenderer->makeKnownLink(
427  $this->getPageTitle( "thumb/$file/$thumb" ),
428  $this->msg( 'uploadstash-thumbnail' )->text()
429  )
430  )->escaped();
431  } catch ( Exception $e ) {
432  }
433  $fileListItemsHtml .= Html::rawElement( 'li', [], $itemHtml );
434  }
435  $this->getOutput()->addHTML( Html::rawElement( 'ul', [], $fileListItemsHtml ) );
436  $form->displayForm( $formResult );
437  $this->getOutput()->addHTML( Html::rawElement( 'p', [], $refreshHtml ) );
438  } else {
439  $this->getOutput()->addHTML( Html::rawElement( 'p', [],
440  Html::element( 'span', [], $this->msg( 'uploadstash-nofiles' )->text() )
441  . ' '
442  . $refreshHtml
443  ) );
444  }
445  }
446 }
SpecialPage\$linkRenderer
LinkRenderer null $linkRenderer
Definition: SpecialPage.php:75
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:701
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:831
wfResetOutputBuffers
wfResetOutputBuffers( $resetGzipEncoding=true)
Clear away any user-level output buffers, discarding contents.
Definition: GlobalFunctions.php:1642
SpecialUploadStash\$localRepo
LocalRepo $localRepo
Definition: SpecialUploadStash.php:41
PROTO_CANONICAL
const PROTO_CANONICAL
Definition: Defines.php:212
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
UploadStashBadPathException
@newable
Definition: UploadStashBadPathException.php:27
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:747
UnlistedSpecialPage
Shortcut to construct a special page which is unlisted by default.
Definition: UnlistedSpecialPage.php:31
UnregisteredLocalFile
A file object referring to either a standalone local file, or a file in a local repository with no da...
Definition: UnregisteredLocalFile.php:36
UploadStash
UploadStash is intended to accomplish a few things:
Definition: UploadStash.php:53
SpecialPage\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialPage.php:347
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
SpecialUploadStash\outputRemoteScaledThumb
outputRemoteScaledThumb( $file, $params, $flags)
Scale a file with a remote "scaler", as exists on the Wikimedia Foundation cluster,...
Definition: SpecialUploadStash.php:244
SpecialPage\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: SpecialPage.php:938
SpecialUploadStash\execute
execute( $subPage)
Execute page – can output a file directly or show a listing of them.
Definition: SpecialUploadStash.php:73
SpecialUploadStash\outputLocalFile
outputLocalFile(File $file)
Output HTTP response for file Side effect: writes HTTP response to STDOUT.
Definition: SpecialUploadStash.php:317
HttpError
Show an error that looks like an HTTP server error.
Definition: HttpError.php:32
SpecialUploadStash\$stash
UploadStash null $stash
Definition: SpecialUploadStash.php:38
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:31
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:63
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:797
SpecialUploadStash
Web access for files temporarily stored by UploadStash.
Definition: SpecialUploadStash.php:36
SpecialUploadStash\parseKey
parseKey( $key)
Parse the key passed to the SpecialPage.
Definition: SpecialUploadStash.php:135
UploadStashZeroLengthFileException
@newable
Definition: UploadStashZeroLengthFileException.php:27
SpecialUploadStash\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialUploadStash.php:63
SpecialUploadStash\outputFileHeaders
static outputFileHeaders( $contentType, $size)
Output headers for streaming.
Definition: SpecialUploadStash.php:359
SpecialUploadStash\__construct
__construct(RepoGroup $repoGroup)
Definition: SpecialUploadStash.php:58
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!...
Definition: SpecialPage.php:575
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:757
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:910
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:721
$content
$content
Definition: router.php:76
SpecialUploadStash\MAX_SERVE_BYTES
const MAX_SERVE_BYTES
Since we are directly writing the file to STDOUT, we should not be reading in really big files and se...
Definition: SpecialUploadStash.php:53
RepoGroup\getLocalRepo
getLocalRepo()
Get the local repository, i.e.
Definition: RepoGroup.php:357
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
UploadStashFileNotFoundException
@newable
Definition: UploadStashFileNotFoundException.php:27
SpecialUploadStash\outputLocallyScaledThumb
outputLocallyScaledThumb( $file, $params, $flags)
Scale a file (probably with a locally installed imagemagick, or similar) and output it to STDOUT.
Definition: SpecialUploadStash.php:198
SpecialUploadStash\showUploads
showUploads()
Default action when we don't have a subpage – just show links to the uploads we have,...
Definition: SpecialUploadStash.php:372
File\RENDER_NOW
const RENDER_NOW
Force rendering in the current process.
Definition: File.php:73
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:948
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:31
SpecialUploadStash\outputThumbFromStash
outputThumbFromStash( $file, $params)
Get a thumbnail for file, either generated locally or remotely, and stream it out.
Definition: SpecialUploadStash.php:176
SpecialUploadStash\outputContents
outputContents( $content, $contentType)
Output HTTP response of raw content Side effect: writes HTTP response to STDOUT.
Definition: SpecialUploadStash.php:337
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:234
SpecialUploadStashTooLargeException
@newable
Definition: SpecialUploadStashTooLargeException.php:28
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:316
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:666
SpecialUploadStash\showUpload
showUpload( $key)
If file available in stash, cats it out to the client as a simple HTTP response.
Definition: SpecialUploadStash.php:94
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:37
MWHttpRequest\factory
static factory( $url, array $options=null, $caller=__METHOD__)
Generate a new request object.
Definition: MWHttpRequest.php:195
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:490
$type
$type
Definition: testCompression.php:52