24use Wikimedia\RequestTimeout\TimeoutException;
69 parent::__construct(
'UploadStash',
'upload' );
88 $this->stash = $this->localRepo->getUploadStash( $this->
getUser() );
91 if ( $subPage ===
null || $subPage ===
'' ) {
111 if ( $params[
'type'] ===
'thumb' ) {
119 $message = $e->getMessage();
120 }
catch ( Exception $e ) {
122 $message = $e->getMessage();
138 $type = strtok( $key,
'/' );
140 if (
$type !==
'file' &&
$type !==
'thumb' ) {
142 $this->
msg(
'uploadstash-bad-path-unknown-type',
$type )
145 $fileName = strtok(
'/' );
146 $thumbPart = strtok(
'/' );
147 $file = $this->stash->getFile( $fileName );
148 if (
$type ===
'thumb' ) {
149 $srcNamePos = strrpos( $thumbPart, $fileName );
150 if ( $srcNamePos ===
false || $srcNamePos < 1 ) {
152 $this->
msg(
'uploadstash-bad-path-unrecognized-thumb-name' )
155 $paramString = substr( $thumbPart, 0, $srcNamePos - 1 );
157 $handler =
$file->getHandler();
159 $params = $handler->parseParamString( $paramString );
161 return [
'file' =>
$file,
'type' =>
$type,
'params' => $params ];
164 $this->
msg(
'uploadstash-bad-path-no-handler',
$file->getMimeType(),
$file->getPath() )
185 if (
$file->getRepo()->getThumbProxyUrl()
186 || $this->getConfig()->get(
'UploadStashScalerBaseUrl' )
206 $flags |= File::RENDER_NOW;
208 $thumbnailImage =
$file->transform( $params, $flags );
209 if ( !$thumbnailImage ) {
211 $this->
msg(
'uploadstash-file-not-found-no-thumb' )
216 if ( !$thumbnailImage->getStoragePath() ) {
218 $this->
msg(
'uploadstash-file-not-found-no-local-path' )
225 $this->stash->repo, $thumbnailImage->getStoragePath(),
false );
252 $scalerThumbName =
$file->generateThumbName(
$file->getName(), $params );
256 $thumbProxyUrl =
$file->getRepo()->getThumbProxyUrl();
257 if ( strlen( $thumbProxyUrl ) ) {
258 $scalerThumbUrl = $thumbProxyUrl .
'temp/' .
$file->getUrlRel() .
259 '/' . rawurlencode( $scalerThumbName );
260 $secret =
$file->getRepo()->getThumbProxySecret();
265 $scalerBaseUrl = $this->
getConfig()->get(
'UploadStashScalerBaseUrl' );
267 if ( preg_match(
'/^\/\//', $scalerBaseUrl ) ) {
274 $scalerThumbUrl = $scalerBaseUrl .
'/' .
$file->getUrlRel() .
275 '/' . rawurlencode( $scalerThumbName );
285 $req = $this->httpRequestFactory->create( $scalerThumbUrl, $httpOptions, __METHOD__ );
288 if ( strlen( $secret ) ) {
289 $req->setHeader(
'X-Swift-Secret', $secret );
292 $status = $req->execute();
293 if ( !$status->isOK() ) {
294 $errors = $status->getErrorsArray();
297 'uploadstash-file-not-found-no-remote-thumb',
298 print_r( $errors, 1 ),
303 $contentType = $req->getResponseHeader(
"content-type" );
304 if ( !$contentType ) {
306 $this->
msg(
'uploadstash-file-not-found-missing-content-type' )
322 if (
$file->getSize() > self::MAX_SERVE_BYTES ) {
324 $this->
msg(
'uploadstash-file-too-large', self::MAX_SERVE_BYTES )
328 $file->getRepo()->streamFileWithStatus(
$file->getPath(),
329 [
'Content-Transfer-Encoding: binary',
330 'Expires: Sun, 17-Jan-2038 19:14:07 GMT' ]
343 if ( $size > self::MAX_SERVE_BYTES ) {
345 $this->
msg(
'uploadstash-file-too-large', self::MAX_SERVE_BYTES )
364 header(
"Content-Type: $contentType",
true );
365 header(
'Content-Transfer-Encoding: binary',
true );
366 header(
'Expires: Sun, 17-Jan-2038 19:14:07 GMT',
true );
368 header(
'Cache-Control: private' );
369 header(
"Content-Length: $size",
true );
386 $form = HTMLForm::factory(
'ooui', [
392 ], $context,
'clearStashedUploads' );
393 $form->setSubmitDestructive();
394 $form->setSubmitCallback(
function ( $formData, $form ) {
395 if ( isset( $formData[
'Clear'] ) ) {
396 wfDebug(
'stash has: ' . print_r( $this->stash->listFiles(),
true ) );
398 if ( !$this->stash->clear() ) {
399 return Status::newFatal(
'uploadstash-errclear' );
403 return Status::newGood();
405 $form->setSubmitTextMsg(
'uploadstash-clear' );
407 $form->prepareForm();
408 $formResult = $form->tryAuthorizedSubmit();
414 $this->
msg(
'uploadstash-refresh' )->text()
416 $files = $this->stash->listFiles();
417 if ( $files && count( $files ) ) {
419 $fileListItemsHtml =
'';
420 foreach ( $files as
$file ) {
426 $fileObj = $this->stash->getFile(
$file );
427 $thumb = $fileObj->generateThumbName(
$file, [
'width' => 220 ] );
429 $this->
msg(
'word-separator' )->escaped() .
430 $this->
msg(
'parentheses' )->rawParams(
432 $this->getPageTitle(
"thumb/$file/$thumb" ),
433 $this->msg(
'uploadstash-thumbnail' )->text()
436 }
catch ( TimeoutException $e ) {
438 }
catch ( Exception $e ) {
439 MWExceptionHandler::logException( $e );
441 $fileListItemsHtml .= Html::rawElement(
'li', [], $itemHtml );
443 $this->
getOutput()->addHTML( Html::rawElement(
'ul', [], $fileListItemsHtml ) );
444 $form->displayForm( $formResult );
445 $this->
getOutput()->addHTML( Html::rawElement(
'p', [], $refreshHtml ) );
447 $this->
getOutput()->addHTML( Html::rawElement(
'p', [],
448 Html::element(
'span', [], $this->
msg(
'uploadstash-nofiles' )->text() )
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
wfResetOutputBuffers( $resetGzipEncoding=true)
Clear away any user-level output buffers, discarding contents.
An IContextSource implementation which will inherit context from another source but allow individual ...
Implements some public methods and some protected utility functions which are required by multiple ch...
Show an error that looks like an HTTP server error.
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Prioritized list of file repositories.
getLocalRepo()
Get the local repository, i.e.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
getContext()
Gets the context this SpecialPage is executed in.
LinkRenderer null $linkRenderer
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getPageTitle( $subpage=false)
Get a self-referential title object.
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Web access for files temporarily stored by UploadStash.
static outputFileHeaders( $contentType, $size)
Output headers for streaming.
outputLocalFile(File $file)
Output HTTP response for file Side effect: writes HTTP response to STDOUT.
outputThumbFromStash( $file, $params)
Get a thumbnail for file, either generated locally or remotely, and stream it out.
__construct(RepoGroup $repoGroup, HttpRequestFactory $httpRequestFactory)
showUploads()
Default action when we don't have a subpage – just show links to the uploads we have,...
parseKey( $key)
Parse the key passed to the SpecialPage.
showUpload( $key)
If file available in stash, cats it out to the client as a simple HTTP response.
outputRemoteScaledThumb( $file, $params, $flags)
Scale a file with a remote "scaler", as exists on the Wikimedia Foundation cluster,...
const MAX_SERVE_BYTES
Since we are directly writing the file to STDOUT, we should not be reading in really big files and se...
execute( $subPage)
Execute page – can output a file directly or show a listing of them.
doesWrites()
Indicates whether this special page may perform database writes.
outputLocallyScaledThumb( $file, $params, $flags)
Scale a file (probably with a locally installed imagemagick, or similar) and output it to STDOUT.
HttpRequestFactory $httpRequestFactory
outputContents( $content, $contentType)
Output HTTP response of raw content Side effect: writes HTTP response to STDOUT.
Shortcut to construct a special page which is unlisted by default.
A file object referring to either a standalone local file, or a file in a local repository with no da...
UploadStash is intended to accomplish a few things:
while(( $__line=Maintenance::readconsole()) !==false) print
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.