| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use Exception; |
| 24 |
use File; |
| 25 |
use HttpError; |
| 26 |
use LocalRepo; |
| 27 |
use MediaWiki\Html\Html; |
| 28 |
use MediaWiki\HTMLForm\HTMLForm; |
| 29 |
use MediaWiki\Http\HttpRequestFactory; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\Pager\UploadStashPager; |
| 32 |
use MediaWiki\SpecialPage\UnlistedSpecialPage; |
| 33 |
use MediaWiki\Status\Status; |
| 34 |
use MediaWiki\Utils\UrlUtils; |
| 35 |
use RepoGroup; |
| 36 |
use SpecialUploadStashTooLargeException; |
| 37 |
use UnregisteredLocalFile; |
| 38 |
use UploadStash; |
| 39 |
use UploadStashBadPathException; |
| 40 |
use UploadStashFileNotFoundException; |
| 41 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
class SpecialUploadStash extends UnlistedSpecialPage { |
| 57 |
|
| 58 |
private $stash; |
| 59 |
|
| 60 |
private LocalRepo $localRepo; |
| 61 |
private HttpRequestFactory $httpRequestFactory; |
| 62 |
private UrlUtils $urlUtils; |
| 63 |
private IConnectionProvider $dbProvider; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private const MAX_SERVE_BYTES = 1_048_576; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function __construct( |
| 84 |
RepoGroup $repoGroup, |
| 85 |
HttpRequestFactory $httpRequestFactory, |
| 86 |
UrlUtils $urlUtils, |
| 87 |
IConnectionProvider $dbProvider |
| 88 |
) { |
| 89 |
parent::__construct( 'UploadStash', 'upload' ); |
| 90 |
$this->localRepo = $repoGroup->getLocalRepo(); |
| 91 |
$this->httpRequestFactory = $httpRequestFactory; |
| 92 |
$this->urlUtils = $urlUtils; |
| 93 |
$this->dbProvider = $dbProvider; |
| 94 |
} |
| 95 |
|
| 96 |
public function doesWrites() { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function execute( $subPage ) { |
| 107 |
$this->useTransactionalTimeLimit(); |
| 108 |
|
| 109 |
|
| 110 |
$this->stash = $this->localRepo->getUploadStash( $this->getUser() ); |
| 111 |
$this->checkPermissions(); |
| 112 |
|
| 113 |
if ( $subPage === null || $subPage === '' ) { |
| 114 |
$this->showUploads(); |
| 115 |
} else { |
| 116 |
$this->showUpload( $subPage ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function showUpload( $key ) { |
| 128 |
|
| 129 |
$this->getOutput()->disable(); |
| 130 |
|
| 131 |
try { |
| 132 |
$params = $this->parseKey( $key ); |
| 133 |
if ( $params['type'] === 'thumb' ) { |
| 134 |
$this->outputThumbFromStash( $params['file'], $params['params'] ); |
| 135 |
} else { |
| 136 |
$this->outputLocalFile( $params['file'] ); |
| 137 |
} |
| 138 |
return; |
| 139 |
} catch ( UploadStashFileNotFoundException $e ) { |
| 140 |
$code = 404; |
| 141 |
$message = $e->getMessage(); |
| 142 |
} catch ( Exception $e ) { |
| 143 |
$code = 500; |
| 144 |
$message = $e->getMessage(); |
| 145 |
} |
| 146 |
|
| 147 |
throw new HttpError( $code, $message ); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private function parseKey( $key ) { |
| 160 |
$type = strtok( $key, '/' ); |
| 161 |
|
| 162 |
if ( $type !== 'file' && $type !== 'thumb' ) { |
| 163 |
throw new UploadStashBadPathException( |
| 164 |
$this->msg( 'uploadstash-bad-path-unknown-type', $type ) |
| 165 |
); |
| 166 |
} |
| 167 |
$fileName = strtok( '/' ); |
| 168 |
$thumbPart = strtok( '/' ); |
| 169 |
$file = $this->stash->getFile( $fileName ); |
| 170 |
if ( $type === 'thumb' ) { |
| 171 |
$srcNamePos = strrpos( $thumbPart, $fileName ); |
| 172 |
if ( $srcNamePos === false || $srcNamePos < 1 ) { |
| 173 |
throw new UploadStashBadPathException( |
| 174 |
$this->msg( 'uploadstash-bad-path-unrecognized-thumb-name' ) |
| 175 |
); |
| 176 |
} |
| 177 |
$paramString = substr( $thumbPart, 0, $srcNamePos - 1 ); |
| 178 |
|
| 179 |
$handler = $file->getHandler(); |
| 180 |
if ( $handler ) { |
| 181 |
$params = $handler->parseParamString( $paramString ); |
| 182 |
if ( $params === false ) { |
| 183 |
|
| 184 |
$params = []; |
| 185 |
} |
| 186 |
|
| 187 |
return [ 'file' => $file, 'type' => $type, 'params' => $params ]; |
| 188 |
} else { |
| 189 |
throw new UploadStashBadPathException( |
| 190 |
$this->msg( 'uploadstash-bad-path-no-handler', $file->getMimeType(), $file->getPath() ) |
| 191 |
); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return [ 'file' => $file, 'type' => $type ]; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private function outputThumbFromStash( $file, $params ) { |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if ( $file->getRepo()->getThumbProxyUrl() |
| 211 |
|| $this->getConfig()->get( MainConfigNames::UploadStashScalerBaseUrl ) |
| 212 |
) { |
| 213 |
$this->outputRemoteScaledThumb( $file, $params ); |
| 214 |
} else { |
| 215 |
$this->outputLocallyScaledThumb( $file, $params ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
private function outputLocallyScaledThumb( $file, $params ) { |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
$thumbnailImage = $file->transform( $params, File::RENDER_NOW ); |
| 230 |
if ( !$thumbnailImage ) { |
| 231 |
throw new UploadStashFileNotFoundException( |
| 232 |
$this->msg( 'uploadstash-file-not-found-no-thumb' ) |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
if ( !$thumbnailImage->getStoragePath() ) { |
| 238 |
throw new UploadStashFileNotFoundException( |
| 239 |
$this->msg( 'uploadstash-file-not-found-no-local-path' ) |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
$thumbFile = new UnregisteredLocalFile( false, |
| 246 |
$this->stash->repo, $thumbnailImage->getStoragePath(), false ); |
| 247 |
|
| 248 |
$this->outputLocalFile( $thumbFile ); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
private function outputRemoteScaledThumb( $file, $params ) { |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
$scalerThumbName = $file->generateThumbName( $file->getName(), $params ); |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
$thumbProxyUrl = $file->getRepo()->getThumbProxyUrl(); |
| 276 |
if ( strlen( $thumbProxyUrl ) ) { |
| 277 |
$scalerThumbUrl = $thumbProxyUrl . 'temp/' . $file->getUrlRel() . |
| 278 |
'/' . rawurlencode( $scalerThumbName ); |
| 279 |
$secret = $file->getRepo()->getThumbProxySecret(); |
| 280 |
} else { |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
$scalerBaseUrl = $this->getConfig()->get( MainConfigNames::UploadStashScalerBaseUrl ); |
| 285 |
|
| 286 |
if ( preg_match( '/^\/\//', $scalerBaseUrl ) ) { |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
$scalerBaseUrl = $this->urlUtils->expand( $scalerBaseUrl, PROTO_CANONICAL ); |
| 291 |
} |
| 292 |
|
| 293 |
$scalerThumbUrl = $scalerBaseUrl . '/' . $file->getUrlRel() . |
| 294 |
'/' . rawurlencode( $scalerThumbName ); |
| 295 |
$secret = false; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
$httpOptions = [ |
| 301 |
'method' => 'GET', |
| 302 |
'timeout' => 5 |
| 303 |
]; |
| 304 |
$req = $this->httpRequestFactory->create( $scalerThumbUrl, $httpOptions, __METHOD__ ); |
| 305 |
|
| 306 |
|
| 307 |
if ( strlen( $secret ) ) { |
| 308 |
$req->setHeader( 'X-Swift-Secret', $secret ); |
| 309 |
} |
| 310 |
|
| 311 |
$status = $req->execute(); |
| 312 |
if ( !$status->isOK() ) { |
| 313 |
throw new UploadStashFileNotFoundException( |
| 314 |
$this->msg( |
| 315 |
'uploadstash-file-not-found-no-remote-thumb', |
| 316 |
$status->getMessage(), |
| 317 |
$scalerThumbUrl |
| 318 |
) |
| 319 |
); |
| 320 |
} |
| 321 |
$contentType = $req->getResponseHeader( "content-type" ); |
| 322 |
if ( !$contentType ) { |
| 323 |
throw new UploadStashFileNotFoundException( |
| 324 |
$this->msg( 'uploadstash-file-not-found-missing-content-type' ) |
| 325 |
); |
| 326 |
} |
| 327 |
|
| 328 |
$this->outputContents( $req->getContent(), $contentType ); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
private function outputLocalFile( File $file ) { |
| 340 |
if ( $file->getSize() > self::MAX_SERVE_BYTES ) { |
| 341 |
throw new SpecialUploadStashTooLargeException( |
| 342 |
$this->msg( 'uploadstash-file-too-large', self::MAX_SERVE_BYTES ) |
| 343 |
); |
| 344 |
} |
| 345 |
|
| 346 |
$file->getRepo()->streamFileWithStatus( $file->getPath(), |
| 347 |
[ 'Content-Transfer-Encoding: binary', |
| 348 |
'Expires: Sun, 17-Jan-2038 19:14:07 GMT' ] |
| 349 |
); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
private function outputContents( $content, $contentType ) { |
| 360 |
$size = strlen( $content ); |
| 361 |
if ( $size > self::MAX_SERVE_BYTES ) { |
| 362 |
throw new SpecialUploadStashTooLargeException( |
| 363 |
$this->msg( 'uploadstash-file-too-large', self::MAX_SERVE_BYTES ) |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
wfResetOutputBuffers(); |
| 368 |
self::outputFileHeaders( $contentType, $size ); |
| 369 |
print $content; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
private static function outputFileHeaders( $contentType, $size ) { |
| 382 |
header( "Content-Type: $contentType", true ); |
| 383 |
header( 'Content-Transfer-Encoding: binary', true ); |
| 384 |
header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true ); |
| 385 |
|
| 386 |
header( 'Cache-Control: private' ); |
| 387 |
header( "Content-Length: $size", true ); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
private function showUploads() { |
| 395 |
|
| 396 |
$this->setHeaders(); |
| 397 |
$this->outputHeader(); |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
$form = HTMLForm::factory( 'ooui', [ |
| 403 |
'Clear' => [ |
| 404 |
'type' => 'hidden', |
| 405 |
'default' => true, |
| 406 |
'name' => 'clear', |
| 407 |
] |
| 408 |
], $this->getContext(), 'clearStashedUploads' ); |
| 409 |
$form->setTitle( $this->getPageTitle() ); |
| 410 |
$form->setSubmitDestructive(); |
| 411 |
$form->setSubmitCallback( function ( $formData, $form ) { |
| 412 |
if ( isset( $formData['Clear'] ) ) { |
| 413 |
wfDebug( 'stash has: ' . print_r( $this->stash->listFiles(), true ) ); |
| 414 |
|
| 415 |
if ( !$this->stash->clear() ) { |
| 416 |
return Status::newFatal( 'uploadstash-errclear' ); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
return Status::newGood(); |
| 421 |
} ); |
| 422 |
$form->setSubmitTextMsg( 'uploadstash-clear' ); |
| 423 |
|
| 424 |
$form->prepareForm(); |
| 425 |
$formResult = $form->tryAuthorizedSubmit(); |
| 426 |
|
| 427 |
|
| 428 |
$linkRenderer = $this->getLinkRenderer(); |
| 429 |
$refreshHtml = $linkRenderer->makeKnownLink( |
| 430 |
$this->getPageTitle(), |
| 431 |
$this->msg( 'uploadstash-refresh' )->text() |
| 432 |
); |
| 433 |
$pager = new UploadStashPager( |
| 434 |
$this->getContext(), |
| 435 |
$linkRenderer, |
| 436 |
$this->dbProvider, |
| 437 |
$this->stash, |
| 438 |
$this->localRepo |
| 439 |
); |
| 440 |
if ( $pager->getNumRows() ) { |
| 441 |
$pager->getForm(); |
| 442 |
$this->getOutput()->addParserOutputContent( $pager->getFullOutput() ); |
| 443 |
$form->displayForm( $formResult ); |
| 444 |
$this->getOutput()->addHTML( Html::rawElement( 'p', [], $refreshHtml ) ); |
| 445 |
} else { |
| 446 |
$this->getOutput()->addHTML( Html::rawElement( 'p', [], |
| 447 |
Html::element( 'span', [], $this->msg( 'uploadstash-nofiles' )->text() ) |
| 448 |
. ' ' |
| 449 |
. $refreshHtml |
| 450 |
) ); |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
class_alias( SpecialUploadStash::class, 'SpecialUploadStash' ); |