107 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
111 $fileName = $params[
'f'] ??
'';
114 if ( isset( $params[
'w'] ) ) {
115 $params[
'width'] = $params[
'w'];
116 unset( $params[
'w'] );
118 if ( isset( $params[
'width'] ) && substr( $params[
'width'], -2 ) ==
'px' ) {
120 $params[
'width'] = substr( $params[
'width'], 0, -2 );
122 if ( isset( $params[
'p'] ) ) {
123 $params[
'page'] = $params[
'p'];
127 $isOld = ( isset( $params[
'archived'] ) && $params[
'archived'] );
128 unset( $params[
'archived'] );
131 $isTemp = ( isset( $params[
'temp'] ) && $params[
'temp'] );
132 unset( $params[
'temp'] );
135 $fileName = strtr( $fileName,
'\\/',
'__' );
136 $localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
140 $repo = $localRepo->getTempRepo();
142 # Temp files are hashed based on the name without the timestamp.
143 # The thumbnails will be hashed based on the entire name however.
144 # @todo fix
this convention to actually be reasonable.
145 $repo->getZonePath(
'public' ) .
'/' . $repo->getTempHashPath( $fileName ) . $fileName
147 } elseif ( $isOld ) {
149 $bits = explode(
'!', $fileName, 2 );
150 if ( count( $bits ) != 2 ) {
159 $img = $localRepo->newFromArchiveName(
$title, $fileName );
161 $img = $localRepo->newFile( $fileName );
172 if ( !in_array(
'read', $permissionManager->getGroupPermissions( [
'*' ] ),
true ) ) {
173 $user = RequestContext::getMain()->getUser();
174 $imgTitle = $img->getTitle();
176 if ( !$imgTitle || !$permissionManager->userCan(
'read', $user, $imgTitle ) ) {
177 wfThumbError( 403,
'Access denied. You do not have permission to access ' .
178 'the source file.' );
181 $headers[] =
'Cache-Control: private';
182 $varyHeader[] =
'Cookie';
186 if ( $img->isDeleted( File::DELETED_FILE ) ) {
192 if ( isset( $params[
'thumbName'] ) ) {
195 if ( $params ==
null ) {
196 wfThumbError( 400,
'The specified thumbnail parameters are not recognized.' );
201 if ( !$img->exists() ) {
202 $redirectedLocation =
false;
207 $possRedirFile = $localRepo->findFile( $img->getName() );
208 if ( $possRedirFile && $possRedirFile->getRedirected() !==
null ) {
209 $redirTarget = $possRedirFile->getName();
210 $targetFile = $localRepo->newFile( Title::makeTitleSafe(
NS_FILE, $redirTarget ) );
211 if ( $targetFile->exists() ) {
212 $newThumbName = $targetFile->thumbName( $params );
215 $newThumbUrl = $targetFile->getArchiveThumbUrl(
216 $bits[0] .
'!' . $targetFile->getName(), $newThumbName );
218 $newThumbUrl = $targetFile->getThumbUrl( $newThumbName );
225 if ( $redirectedLocation ) {
227 $response = RequestContext::getMain()->getRequest()->response();
228 $response->statusHeader( 302 );
229 $response->header(
'Location: ' . $redirectedLocation );
230 $response->header(
'Expires: ' .
231 gmdate(
'D, d M Y H:i:s', time() + 12 * 3600 ) .
' GMT' );
233 $varyHeader[] =
'X-Forwarded-Proto';
235 if ( count( $varyHeader ) ) {
236 $response->header(
'Vary: ' . implode(
', ', $varyHeader ) );
238 $response->header(
'Content-Length: 0' );
245 } elseif ( $img->getPath() ===
false ) {
246 wfThumbErrorText( 400,
"The source file '$fileName' is not locally accessible." );
252 if ( !empty( $_SERVER[
'HTTP_IF_MODIFIED_SINCE'] ) ) {
254 $imsString = preg_replace(
'/;.*$/',
'', $_SERVER[
"HTTP_IF_MODIFIED_SINCE"] );
256 AtEase::suppressWarnings();
257 $imsUnix = strtotime( $imsString );
258 AtEase::restoreWarnings();
259 if (
wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
260 HttpStatus::header( 304 );
265 $rel404 = $params[
'rel404'] ??
null;
266 unset( $params[
'r'] );
267 unset( $params[
'f'] );
268 unset( $params[
'rel404'] );
272 $thumbName = $img->thumbName( $params );
273 if ( !strlen( $thumbName ) ) {
275 'Empty return from File::thumbName'
278 $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME );
282 'The specified thumbnail parameters are not valid: ' . $e->getMessage()
287 [
'exception' => $e ] );
295 if ( $rel404 !==
null ) {
296 if ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName ) ) {
298 } elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) {
300 $response = RequestContext::getMain()->getRequest()->response();
301 $response->statusHeader( 301 );
302 $response->header(
'Location: ' .
304 $response->header(
'Expires: ' .
305 gmdate(
'D, d M Y H:i:s', time() + 7 * 86400 ) .
' GMT' );
307 $varyHeader[] =
'X-Forwarded-Proto';
309 if ( count( $varyHeader ) ) {
310 $response->header(
'Vary: ' . implode(
', ', $varyHeader ) );
314 wfThumbErrorText( 404,
"The given path of the specified thumbnail is incorrect;
315 expected '" . $img->getThumbRel( $thumbName ) .
"' but got '" .
316 rawurldecode( $rel404 ) .
"'." );
321 $dispositionType = isset( $params[
'download'] ) ?
'attachment' :
'inline';
325 'Content-Disposition: ' . $img->getThumbDisposition( $thumbName, $dispositionType );
327 if ( count( $varyHeader ) ) {
328 $headers[] =
'Vary: ' . implode(
', ', $varyHeader );
332 $thumbPath = $img->getThumbPath( $thumbName );
333 if ( $img->getRepo()->fileExists( $thumbPath ) ) {
334 $starttime = microtime(
true );
335 $status = $img->getRepo()->streamFileWithStatus( $thumbPath, $headers );
336 $streamtime = microtime(
true ) - $starttime;
338 if ( $status->isOK() ) {
339 MediaWikiServices::getInstance()->getStatsdDataFactory()->timing(
340 'media.thumbnail.stream', $streamtime
343 wfThumbError( 500,
'Could not stream the file',
null, [
'file' => $thumbName,
344 'path' => $thumbPath,
'error' => $status->getWikiText(
false,
false,
'en' ) ] );
349 $user = RequestContext::getMain()->getUser();
350 if ( !
wfThumbIsStandard( $img, $params ) && $user->pingLimiter(
'renderfile-nonstandard' ) ) {
353 } elseif ( $user->pingLimiter(
'renderfile' ) ) {
358 $thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
360 if ( strlen( $thumbProxyUrl ) ) {
376 $errorMsg = $errorMsg ?: $msg->rawParams(
'File::transform() returned false' )->escaped();
378 $errorMsg->
getKey() ===
'thumbnail_image-failure-limit'
382 } elseif ( $thumb->isError() ) {
383 $errorMsg = $thumb->getHtmlMsg();
384 $errorCode = $thumb->getHttpStatusCode();
385 } elseif ( !$thumb->hasFile() ) {
386 $errorMsg = $msg->rawParams(
'No path supplied in thumbnail object' )->escaped();
387 } elseif ( $thumb->fileIsSource() ) {
389 ->rawParams(
'Image was not scaled, is the requested width bigger than the source?' )
394 if ( $errorMsg !==
false ) {
395 wfThumbError( $errorCode, $errorMsg,
null, [
'file' => $thumbName,
'path' => $thumbPath ] );
398 $status = $thumb->streamFileWithStatus( $headers );
399 if ( !$status->isOK() ) {
401 'file' => $thumbName,
'path' => $thumbPath,
402 'error' => $status->getWikiText(
false,
false,
'en' ) ] );
458 $cache = ObjectCache::getLocalClusterInstance();
462 $file->getRepo()->getName(),
468 if (
$cache->get( $key ) >= 4 ) {
469 return [
false,
wfMessage(
'thumbnail_image-failure-limit', 4 ) ];
474 register_shutdown_function(
static function () use (
$cache, &$done, $key ) {
477 $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
487 if (
$file->isExpensiveToThumbnail() ) {
488 $poolCounterType =
'FileRenderExpensive';
490 $poolCounterType =
'FileRender';
497 'doWork' =>
static function () use (
$file, $params ) {
498 return $file->transform( $params, File::RENDER_NOW );
500 'doCachedWork' =>
static function () use (
$file, $params, $thumbPath ) {
503 return $file->getRepo()->fileExists( $thumbPath )
504 ?
$file->transform( $params, File::RENDER_NOW )
507 'error' =>
static function (
Status $status ) {
508 return wfMessage(
'generic-pool-error' )->parse() .
'<hr>' . $status->getHTML();
515 } elseif ( is_string( $result ) ) {
516 $errorHtml = $result;
518 }
catch ( Exception $e ) {
525 if ( !$thumb || $thumb->isError() ) {
527 $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
530 return [ $thumb, $errorHtml ];
654function wfThumbError( $status, $msgHtml, $msgText =
null, $context = [] ) {
657 MediaWiki\HeaderCallback::warnIfHeadersSent();
659 if ( headers_sent() ) {
660 LoggerFactory::getInstance(
'thumbnail' )->error(
661 'Error after output had been started. Output may be corrupt or truncated. ' .
662 'Original error: ' . ( $msgText ?: $msgHtml ) .
" (Status $status)",
668 header(
'Cache-Control: no-cache' );
669 header(
'Content-Type: text/html; charset=utf-8' );
670 if ( $status == 400 || $status == 404 || $status == 429 ) {
671 HttpStatus::header( $status );
672 } elseif ( $status == 403 ) {
673 HttpStatus::header( 403 );
674 header(
'Vary: Cookie' );
676 LoggerFactory::getInstance(
'thumbnail' )->error( $msgText ?: $msgHtml, $context );
677 HttpStatus::header( 500 );
680 header(
'X-MW-Thumbnail-Renderer: ' .
wfHostname() );
681 $url = htmlspecialchars(
682 $_SERVER[
'REQUEST_URI'] ??
'',
685 $hostname = htmlspecialchars(
wfHostname(), ENT_NOQUOTES );
686 $debug =
"<!-- $url -->\n<!-- $hostname -->\n";
693<meta charset=
"UTF-8" />
694<title>Error generating thumbnail</title>
697<h1>Error generating thumbnail</h1>
706 header(
'Content-Length: ' . strlen(
$content ) );