MediaWiki  master
SpecialUploadStash.php
Go to the documentation of this file.
1 <?php
24 
39  // UploadStash
40  private $stash;
41 
52  private const MAX_SERVE_BYTES = 1048576; // 1MB
53 
54  public function __construct() {
55  parent::__construct( 'UploadStash', 'upload' );
56  }
57 
58  public function doesWrites() {
59  return true;
60  }
61 
68  public function execute( $subPage ) {
70 
71  $this->stash = MediaWikiServices::getInstance()->getRepoGroup()
72  ->getLocalRepo()->getUploadStash( $this->getUser() );
73  $this->checkPermissions();
74 
75  if ( $subPage === null || $subPage === '' ) {
76  $this->showUploads();
77  } else {
78  $this->showUpload( $subPage );
79  }
80  }
81 
89  public function showUpload( $key ) {
90  // prevent callers from doing standard HTML output -- we'll take it from here
91  $this->getOutput()->disable();
92 
93  try {
94  $params = $this->parseKey( $key );
95  if ( $params['type'] === 'thumb' ) {
96  $this->outputThumbFromStash( $params['file'], $params['params'] );
97  } else {
98  $this->outputLocalFile( $params['file'] );
99  }
100  return;
101  } catch ( UploadStashFileNotFoundException $e ) {
102  $code = 404;
103  $message = $e->getMessage();
104  } catch ( UploadStashZeroLengthFileException $e ) {
105  $code = 500;
106  $message = $e->getMessage();
107  } catch ( UploadStashBadPathException $e ) {
108  $code = 500;
109  $message = $e->getMessage();
110  } catch ( SpecialUploadStashTooLargeException $e ) {
111  $code = 500;
112  $message = $e->getMessage();
113  } catch ( Exception $e ) {
114  $code = 500;
115  $message = $e->getMessage();
116  }
117 
118  throw new HttpError( $code, $message );
119  }
120 
130  private function parseKey( $key ) {
131  $type = strtok( $key, '/' );
132 
133  if ( $type !== 'file' && $type !== 'thumb' ) {
134  throw new UploadStashBadPathException(
135  $this->msg( 'uploadstash-bad-path-unknown-type', $type )
136  );
137  }
138  $fileName = strtok( '/' );
139  $thumbPart = strtok( '/' );
140  $file = $this->stash->getFile( $fileName );
141  if ( $type === 'thumb' ) {
142  $srcNamePos = strrpos( $thumbPart, $fileName );
143  if ( $srcNamePos === false || $srcNamePos < 1 ) {
144  throw new UploadStashBadPathException(
145  $this->msg( 'uploadstash-bad-path-unrecognized-thumb-name' )
146  );
147  }
148  $paramString = substr( $thumbPart, 0, $srcNamePos - 1 );
149 
150  $handler = $file->getHandler();
151  if ( $handler ) {
152  $params = $handler->parseParamString( $paramString );
153 
154  return [ 'file' => $file, 'type' => $type, 'params' => $params ];
155  } else {
156  throw new UploadStashBadPathException(
157  $this->msg( 'uploadstash-bad-path-no-handler', $file->getMimeType(), $file->getPath() )
158  );
159  }
160  }
161 
162  return [ 'file' => $file, 'type' => $type ];
163  }
164 
171  private function outputThumbFromStash( $file, $params ) {
172  $flags = 0;
173  // this config option, if it exists, points to a "scaler", as you might find in
174  // the Wikimedia Foundation cluster. See outputRemoteScaledThumb(). This
175  // is part of our horrible NFS-based system, we create a file on a mount
176  // point here, but fetch the scaled file from somewhere else that
177  // happens to share it over NFS.
178  if ( $this->getConfig()->get( 'UploadStashScalerBaseUrl' ) ) {
179  $this->outputRemoteScaledThumb( $file, $params, $flags );
180  } else {
181  $this->outputLocallyScaledThumb( $file, $params, $flags );
182  }
183  }
184 
193  private function outputLocallyScaledThumb( $file, $params, $flags ) {
194  // n.b. this is stupid, we insist on re-transforming the file every time we are invoked. We rely
195  // on HTTP caching to ensure this doesn't happen.
196 
197  $flags |= File::RENDER_NOW;
198 
199  $thumbnailImage = $file->transform( $params, $flags );
200  if ( !$thumbnailImage ) {
202  $this->msg( 'uploadstash-file-not-found-no-thumb' )
203  );
204  }
205 
206  // we should have just generated it locally
207  if ( !$thumbnailImage->getStoragePath() ) {
209  $this->msg( 'uploadstash-file-not-found-no-local-path' )
210  );
211  }
212 
213  // now we should construct a File, so we can get MIME and other such info in a standard way
214  // n.b. MIME type may be different from original (ogx original -> jpeg thumb)
215  $thumbFile = new UnregisteredLocalFile( false,
216  $this->stash->repo, $thumbnailImage->getStoragePath(), false );
217  if ( !$thumbFile ) {
219  $this->msg( 'uploadstash-file-not-found-no-object' )
220  );
221  }
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 
377  public static function tryClearStashedUploads( $formData, $form ) {
378  if ( isset( $formData['Clear'] ) ) {
379  $stash = MediaWikiServices::getInstance()->getRepoGroup()
380  ->getLocalRepo()->getUploadStash( $form->getUser() );
381  wfDebug( 'stash has: ' . print_r( $stash->listFiles(), true ) );
382 
383  if ( !$stash->clear() ) {
384  return Status::newFatal( 'uploadstash-errclear' );
385  }
386  }
387 
388  return Status::newGood();
389  }
390 
395  private function showUploads() {
396  // sets the title, etc.
397  $this->setHeaders();
398  $this->outputHeader();
399 
400  // create the form, which will also be used to execute a callback to process incoming form data
401  // this design is extremely dubious, but supposedly HTMLForm is our standard now?
402 
403  $context = new DerivativeContext( $this->getContext() );
404  $context->setTitle( $this->getPageTitle() ); // Remove subpage
405  $form = HTMLForm::factory( 'ooui', [
406  'Clear' => [
407  'type' => 'hidden',
408  'default' => true,
409  'name' => 'clear',
410  ]
411  ], $context, 'clearStashedUploads' );
412  $form->setSubmitDestructive();
413  $form->setSubmitCallback( [ __CLASS__, 'tryClearStashedUploads' ] );
414  $form->setSubmitTextMsg( 'uploadstash-clear' );
415 
416  $form->prepareForm();
417  $formResult = $form->tryAuthorizedSubmit();
418 
419  // show the files + form, if there are any, or just say there are none
420  $refreshHtml = Html::element( 'a',
421  [ 'href' => $this->getPageTitle()->getLocalURL() ],
422  $this->msg( 'uploadstash-refresh' )->text() );
423  $files = $this->stash->listFiles();
424  if ( $files && count( $files ) ) {
425  sort( $files );
426  $fileListItemsHtml = '';
427  $linkRenderer = $this->getLinkRenderer();
428  foreach ( $files as $file ) {
429  $itemHtml = $linkRenderer->makeKnownLink(
430  $this->getPageTitle( "file/$file" ),
431  $file
432  );
433  try {
434  $fileObj = $this->stash->getFile( $file );
435  $thumb = $fileObj->generateThumbName( $file, [ 'width' => 220 ] );
436  $itemHtml .=
437  $this->msg( 'word-separator' )->escaped() .
438  $this->msg( 'parentheses' )->rawParams(
439  $linkRenderer->makeKnownLink(
440  $this->getPageTitle( "thumb/$file/$thumb" ),
441  $this->msg( 'uploadstash-thumbnail' )->text()
442  )
443  )->escaped();
444  } catch ( Exception $e ) {
445  }
446  $fileListItemsHtml .= Html::rawElement( 'li', [], $itemHtml );
447  }
448  $this->getOutput()->addHTML( Html::rawElement( 'ul', [], $fileListItemsHtml ) );
449  $form->displayForm( $formResult );
450  $this->getOutput()->addHTML( Html::rawElement( 'p', [], $refreshHtml ) );
451  } else {
452  $this->getOutput()->addHTML( Html::rawElement( 'p', [],
453  Html::element( 'span', [], $this->msg( 'uploadstash-nofiles' )->text() )
454  . ' '
455  . $refreshHtml
456  ) );
457  }
458  }
459 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:669
SpecialUploadStash\tryClearStashedUploads
static tryClearStashedUploads( $formData, $form)
Static callback for the HTMLForm in showUploads, to process Note the stash has to be recreated since ...
Definition: SpecialUploadStash.php:377
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:800
wfResetOutputBuffers
wfResetOutputBuffers( $resetGzipEncoding=true)
Clear away any user-level output buffers, discarding contents.
Definition: GlobalFunctions.php:1633
PROTO_CANONICAL
const PROTO_CANONICAL
Definition: Defines.php:212
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
SpecialUploadStash\$stash
$stash
Definition: SpecialUploadStash.php:40
UploadStashBadPathException
Definition: UploadStashBadPathException.php:26
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:716
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
UnlistedSpecialPage
Shortcut to construct a special page which is unlisted by default.
Definition: UnlistedSpecialPage.php:29
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
SpecialPage\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialPage.php:328
$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:902
SpecialUploadStash\execute
execute( $subPage)
Execute page – can output a file directly or show a listing of them.
Definition: SpecialUploadStash.php:68
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:30
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:30
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:62
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:766
SpecialUploadStash
Web access for files temporarily stored by UploadStash.
Definition: SpecialUploadStash.php:38
SpecialUploadStash\parseKey
parseKey( $key)
Parse the key passed to the SpecialPage.
Definition: SpecialUploadStash.php:130
UploadStashZeroLengthFileException
Definition: UploadStashZeroLengthFileException.php:26
SpecialUploadStash\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialUploadStash.php:58
SpecialUploadStash\outputFileHeaders
static outputFileHeaders( $contentType, $size)
Output headers for streaming.
Definition: SpecialUploadStash.php:359
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:551
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:726
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:913
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:689
$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:52
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
UploadStashFileNotFoundException
Definition: UploadStashFileNotFoundException.php:26
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:193
$context
$context
Definition: load.php:43
SpecialUploadStash\showUploads
showUploads()
Default action when we don't have a subpage – just show links to the uploads we have,...
Definition: SpecialUploadStash.php:395
File\RENDER_NOW
const RENDER_NOW
Force rendering in the current process.
Definition: File.php:72
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:912
print
print
Definition: opensearch_desc.php:53
SpecialUploadStash\__construct
__construct()
Definition: SpecialUploadStash.php:54
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
SpecialUploadStash\outputThumbFromStash
outputThumbFromStash( $file, $params)
Get a thumbnail for file, either generated locally or remotely, and stream it out.
Definition: SpecialUploadStash.php:171
SpecialPage\$linkRenderer
MediaWiki Linker LinkRenderer null $linkRenderer
Definition: SpecialPage.php:69
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:231
SpecialUploadStashTooLargeException
Definition: SpecialUploadStashTooLargeException.php:27
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:311
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:636
SpecialUploadStash\showUpload
showUpload( $key)
If file available in stash, cats it out to the client as a simple HTTP response.
Definition: SpecialUploadStash.php:89
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:491
$type
$type
Definition: testCompression.php:52