106 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
110 $fileName = $params[
'f'] ??
'';
113 if ( isset( $params[
'w'] ) ) {
114 $params[
'width'] = $params[
'w'];
115 unset( $params[
'w'] );
117 if ( isset( $params[
'width'] ) && substr( $params[
'width'], -2 ) ==
'px' ) {
119 $params[
'width'] = substr( $params[
'width'], 0, -2 );
121 if ( isset( $params[
'p'] ) ) {
122 $params[
'page'] = $params[
'p'];
126 $isOld = ( isset( $params[
'archived'] ) && $params[
'archived'] );
127 unset( $params[
'archived'] );
130 $isTemp = ( isset( $params[
'temp'] ) && $params[
'temp'] );
131 unset( $params[
'temp'] );
134 $fileName = strtr( $fileName,
'\\/',
'__' );
135 $localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
139 $repo = $localRepo->getTempRepo();
141 # Temp files are hashed based on the name without the timestamp.
142 # The thumbnails will be hashed based on the entire name however.
143 # @todo fix
this convention to actually be reasonable.
144 $repo->getZonePath(
'public' ) .
'/' . $repo->getTempHashPath( $fileName ) . $fileName
146 } elseif ( $isOld ) {
148 $bits = explode(
'!', $fileName, 2 );
149 if ( count( $bits ) != 2 ) {
158 $img = $localRepo->newFromArchiveName(
$title, $fileName );
160 $img = $localRepo->newFile( $fileName );
171 if ( !in_array(
'read', $permissionManager->getGroupPermissions( [
'*' ] ),
true ) ) {
172 $user = RequestContext::getMain()->getUser();
173 $imgTitle = $img->getTitle();
175 if ( !$imgTitle || !$permissionManager->userCan(
'read', $user, $imgTitle ) ) {
176 wfThumbError( 403,
'Access denied. You do not have permission to access ' .
177 'the source file.' );
180 $headers[] =
'Cache-Control: private';
181 $varyHeader[] =
'Cookie';
185 if ( $img->isDeleted( File::DELETED_FILE ) ) {
191 if ( isset( $params[
'thumbName'] ) ) {
194 if ( $params ==
null ) {
195 wfThumbError( 400,
'The specified thumbnail parameters are not recognized.' );
200 if ( !$img->exists() ) {
201 $redirectedLocation =
false;
206 $possRedirFile = $localRepo->findFile( $img->getName() );
207 if ( $possRedirFile && $possRedirFile->getRedirected() !==
null ) {
208 $redirTarget = $possRedirFile->getName();
209 $targetFile = $localRepo->newFile( Title::makeTitleSafe(
NS_FILE, $redirTarget ) );
210 if ( $targetFile->exists() ) {
211 $newThumbName = $targetFile->thumbName( $params );
214 $newThumbUrl = $targetFile->getArchiveThumbUrl(
215 $bits[0] .
'!' . $targetFile->getName(), $newThumbName );
217 $newThumbUrl = $targetFile->getThumbUrl( $newThumbName );
224 if ( $redirectedLocation ) {
226 $response = RequestContext::getMain()->getRequest()->response();
227 $response->statusHeader( 302 );
228 $response->header(
'Location: ' . $redirectedLocation );
229 $response->header(
'Expires: ' .
230 gmdate(
'D, d M Y H:i:s', time() + 12 * 3600 ) .
' GMT' );
232 $varyHeader[] =
'X-Forwarded-Proto';
234 if ( count( $varyHeader ) ) {
235 $response->header(
'Vary: ' . implode(
', ', $varyHeader ) );
237 $response->header(
'Content-Length: 0' );
244 } elseif ( $img->getPath() ===
false ) {
245 wfThumbErrorText( 400,
"The source file '$fileName' is not locally accessible." );
251 if ( !empty( $_SERVER[
'HTTP_IF_MODIFIED_SINCE'] ) ) {
253 $imsString = preg_replace(
'/;.*$/',
'', $_SERVER[
"HTTP_IF_MODIFIED_SINCE"] );
255 Wikimedia\suppressWarnings();
256 $imsUnix = strtotime( $imsString );
257 Wikimedia\restoreWarnings();
258 if (
wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
259 HttpStatus::header( 304 );
264 $rel404 = $params[
'rel404'] ??
null;
265 unset( $params[
'r'] );
266 unset( $params[
'f'] );
267 unset( $params[
'rel404'] );
271 $thumbName = $img->thumbName( $params );
272 if ( !strlen( $thumbName ) ) {
274 'Empty return from File::thumbName'
277 $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME );
281 'The specified thumbnail parameters are not valid: ' . $e->getMessage()
286 [
'exception' => $e ] );
294 if ( $rel404 !==
null ) {
295 if ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName ) ) {
297 } elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) {
299 $response = RequestContext::getMain()->getRequest()->response();
300 $response->statusHeader( 301 );
301 $response->header(
'Location: ' .
303 $response->header(
'Expires: ' .
304 gmdate(
'D, d M Y H:i:s', time() + 7 * 86400 ) .
' GMT' );
306 $varyHeader[] =
'X-Forwarded-Proto';
308 if ( count( $varyHeader ) ) {
309 $response->header(
'Vary: ' . implode(
', ', $varyHeader ) );
313 wfThumbErrorText( 404,
"The given path of the specified thumbnail is incorrect;
314 expected '" . $img->getThumbRel( $thumbName ) .
"' but got '" .
315 rawurldecode( $rel404 ) .
"'." );
320 $dispositionType = isset( $params[
'download'] ) ?
'attachment' :
'inline';
324 "Content-Disposition: {$img->getThumbDisposition( $thumbName, $dispositionType )}";
326 if ( count( $varyHeader ) ) {
327 $headers[] =
'Vary: ' . implode(
', ', $varyHeader );
331 $thumbPath = $img->getThumbPath( $thumbName );
332 if ( $img->getRepo()->fileExists( $thumbPath ) ) {
333 $starttime = microtime(
true );
334 $status = $img->getRepo()->streamFileWithStatus( $thumbPath, $headers );
335 $streamtime = microtime(
true ) - $starttime;
337 if ( $status->isOK() ) {
338 MediaWikiServices::getInstance()->getStatsdDataFactory()->timing(
339 'media.thumbnail.stream', $streamtime
342 wfThumbError( 500,
'Could not stream the file',
null, [
'file' => $thumbName,
343 'path' => $thumbPath,
'error' => $status->getWikiText(
false,
false,
'en' ) ] );
348 $user = RequestContext::getMain()->getUser();
349 if ( !
wfThumbIsStandard( $img, $params ) && $user->pingLimiter(
'renderfile-nonstandard' ) ) {
352 } elseif ( $user->pingLimiter(
'renderfile' ) ) {
357 $thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
359 if ( strlen( $thumbProxyUrl ) ) {
375 $errorMsg = $errorMsg ?: $msg->rawParams(
'File::transform() returned false' )->escaped();
377 $errorMsg->
getKey() ===
'thumbnail_image-failure-limit'
381 } elseif ( $thumb->isError() ) {
382 $errorMsg = $thumb->getHtmlMsg();
383 $errorCode = $thumb->getHttpStatusCode();
384 } elseif ( !$thumb->hasFile() ) {
385 $errorMsg = $msg->rawParams(
'No path supplied in thumbnail object' )->escaped();
386 } elseif ( $thumb->fileIsSource() ) {
388 ->rawParams(
'Image was not scaled, is the requested width bigger than the source?' )
393 if ( $errorMsg !==
false ) {
394 wfThumbError( $errorCode, $errorMsg,
null, [
'file' => $thumbName,
'path' => $thumbPath ] );
397 $status = $thumb->streamFileWithStatus( $headers );
398 if ( !$status->isOK() ) {
400 'file' => $thumbName,
'path' => $thumbPath,
401 'error' => $status->getWikiText(
false,
false,
'en' ) ] );
457 $cache = ObjectCache::getLocalClusterInstance();
461 $file->getRepo()->getName(),
467 if (
$cache->get( $key ) >= 4 ) {
468 return [
false,
wfMessage(
'thumbnail_image-failure-limit', 4 ) ];
473 register_shutdown_function(
function () use (
$cache, &$done, $key ) {
476 $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
486 if (
$file->isExpensiveToThumbnail() ) {
487 $poolCounterType =
'FileRenderExpensive';
489 $poolCounterType =
'FileRender';
496 'doWork' => function () use (
$file, $params ) {
497 return $file->transform( $params, File::RENDER_NOW );
499 'doCachedWork' =>
function () use (
$file, $params, $thumbPath ) {
502 return $file->getRepo()->fileExists( $thumbPath )
503 ?
$file->transform( $params, File::RENDER_NOW )
506 'error' =>
function (
Status $status ) {
507 return wfMessage(
'generic-pool-error' )->parse() .
'<hr>' . $status->getHTML();
514 } elseif ( is_string( $result ) ) {
515 $errorHtml = $result;
517 }
catch ( Exception $e ) {
524 if ( !$thumb || $thumb->isError() ) {
526 $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
529 return [ $thumb, $errorHtml ];
653function wfThumbError( $status, $msgHtml, $msgText =
null, $context = [] ) {
656 MediaWiki\HeaderCallback::warnIfHeadersSent();
658 if ( headers_sent() ) {
659 LoggerFactory::getInstance(
'thumbnail' )->error(
660 'Error after output had been started. Output may be corrupt or truncated. ' .
661 'Original error: ' . ( $msgText ?: $msgHtml ) .
" (Status $status)",
667 header(
'Cache-Control: no-cache' );
668 header(
'Content-Type: text/html; charset=utf-8' );
669 if ( $status == 400 || $status == 404 || $status == 429 ) {
670 HttpStatus::header( $status );
671 } elseif ( $status == 403 ) {
672 HttpStatus::header( 403 );
673 header(
'Vary: Cookie' );
675 LoggerFactory::getInstance(
'thumbnail' )->error( $msgText ?: $msgHtml, $context );
676 HttpStatus::header( 500 );
679 header(
'X-MW-Thumbnail-Renderer: ' .
wfHostname() );
680 $url = htmlspecialchars(
681 $_SERVER[
'REQUEST_URI'] ??
'',
684 $hostname = htmlspecialchars(
wfHostname(), ENT_NOQUOTES );
685 $debug =
"<!-- $url -->\n<!-- $hostname -->\n";
692<meta charset=
"UTF-8" />
693<title>Error generating thumbnail</title>
696<h1>Error generating thumbnail</h1>
705 header(
'Content-Length: ' . strlen(
$content ) );