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'] );
134 $services = MediaWikiServices::getInstance();
137 $fileName = strtr( $fileName,
'\\/',
'__' );
138 $localRepo = $services->getRepoGroup()->getLocalRepo();
142 $repo = $localRepo->getTempRepo();
144 # Temp files are hashed based on the name without the timestamp.
145 # The thumbnails will be hashed based on the entire name however.
146 # @todo fix
this convention to actually be reasonable.
147 $repo->getZonePath(
'public' ) .
'/' . $repo->getTempHashPath( $fileName ) . $fileName
149 } elseif ( $isOld ) {
151 $bits = explode(
'!', $fileName, 2 );
152 if ( count( $bits ) != 2 ) {
161 $img = $localRepo->newFromArchiveName(
$title, $fileName );
163 $img = $localRepo->newFile( $fileName );
174 if ( !$services->getGroupPermissionsLookup()->groupHasPermission(
'*',
'read' ) ) {
175 $user = RequestContext::getMain()->getUser();
176 $imgTitle = $img->getTitle();
178 if ( !$imgTitle || !$services->getPermissionManager()->userCan(
'read', $user, $imgTitle ) ) {
179 wfThumbError( 403,
'Access denied. You do not have permission to access ' .
180 'the source file.' );
183 $headers[] =
'Cache-Control: private';
184 $varyHeader[] =
'Cookie';
188 if ( $img->isDeleted( File::DELETED_FILE ) ) {
194 if ( isset( $params[
'thumbName'] ) ) {
197 if ( $params ==
null ) {
198 wfThumbError( 400,
'The specified thumbnail parameters are not recognized.' );
203 if ( !$img->exists() ) {
204 $redirectedLocation =
false;
209 $possRedirFile = $localRepo->findFile( $img->getName() );
210 if ( $possRedirFile && $possRedirFile->getRedirected() !==
null ) {
211 $redirTarget = $possRedirFile->getName();
212 $targetFile = $localRepo->newFile( Title::makeTitleSafe(
NS_FILE, $redirTarget ) );
213 if ( $targetFile->exists() ) {
214 $newThumbName = $targetFile->thumbName( $params );
217 $newThumbUrl = $targetFile->getArchiveThumbUrl(
218 $bits[0] .
'!' . $targetFile->getName(), $newThumbName );
220 $newThumbUrl = $targetFile->getThumbUrl( $newThumbName );
227 if ( $redirectedLocation ) {
229 $response = RequestContext::getMain()->getRequest()->response();
230 $response->statusHeader( 302 );
231 $response->header(
'Location: ' . $redirectedLocation );
232 $response->header(
'Expires: ' .
233 gmdate(
'D, d M Y H:i:s', time() + 12 * 3600 ) .
' GMT' );
235 $varyHeader[] =
'X-Forwarded-Proto';
237 if ( count( $varyHeader ) ) {
238 $response->header(
'Vary: ' . implode(
', ', $varyHeader ) );
240 $response->header(
'Content-Length: 0' );
247 } elseif ( $img->getPath() ===
false ) {
248 wfThumbErrorText( 400,
"The source file '$fileName' is not locally accessible." );
254 if ( !empty( $_SERVER[
'HTTP_IF_MODIFIED_SINCE'] ) ) {
256 $imsString = preg_replace(
'/;.*$/',
'', $_SERVER[
"HTTP_IF_MODIFIED_SINCE"] );
258 AtEase::suppressWarnings();
259 $imsUnix = strtotime( $imsString );
260 AtEase::restoreWarnings();
261 if (
wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
262 HttpStatus::header( 304 );
267 $rel404 = $params[
'rel404'] ??
null;
268 unset( $params[
'r'] );
269 unset( $params[
'f'] );
270 unset( $params[
'rel404'] );
274 $thumbName = $img->thumbName( $params );
275 if ( !strlen( $thumbName ??
'' ) ) {
277 'Empty return from File::thumbName'
280 $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME );
284 'The specified thumbnail parameters are not valid: ' . $e->getMessage()
289 [
'exception' => $e ] );
297 if ( $rel404 !==
null ) {
298 if ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName ) ) {
300 } elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) {
302 $response = RequestContext::getMain()->getRequest()->response();
303 $response->statusHeader( 301 );
304 $response->header(
'Location: ' .
306 $response->header(
'Expires: ' .
307 gmdate(
'D, d M Y H:i:s', time() + 7 * 86400 ) .
' GMT' );
309 $varyHeader[] =
'X-Forwarded-Proto';
311 if ( count( $varyHeader ) ) {
312 $response->header(
'Vary: ' . implode(
', ', $varyHeader ) );
316 wfThumbErrorText( 404,
"The given path of the specified thumbnail is incorrect;
317 expected '" . $img->getThumbRel( $thumbName ) .
"' but got '" .
318 rawurldecode( $rel404 ) .
"'." );
323 $dispositionType = isset( $params[
'download'] ) ?
'attachment' :
'inline';
327 'Content-Disposition: ' . $img->getThumbDisposition( $thumbName, $dispositionType );
329 if ( count( $varyHeader ) ) {
330 $headers[] =
'Vary: ' . implode(
', ', $varyHeader );
334 $thumbPath = $img->getThumbPath( $thumbName );
335 if ( $img->getRepo()->fileExists( $thumbPath ) ) {
336 $starttime = microtime(
true );
337 $status = $img->getRepo()->streamFileWithStatus( $thumbPath, $headers );
338 $streamtime = microtime(
true ) - $starttime;
340 if ( $status->isOK() ) {
341 $services->getStatsdDataFactory()->timing(
342 'media.thumbnail.stream', $streamtime
345 wfThumbError( 500,
'Could not stream the file',
null, [
'file' => $thumbName,
346 'path' => $thumbPath,
'error' => $status->getWikiText(
false,
false,
'en' ) ] );
351 $user = RequestContext::getMain()->getUser();
352 if ( !
wfThumbIsStandard( $img, $params ) && $user->pingLimiter(
'renderfile-nonstandard' ) ) {
355 } elseif ( $user->pingLimiter(
'renderfile' ) ) {
360 $thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
362 if ( strlen( $thumbProxyUrl ??
'' ) ) {
378 $errorMsg = $errorMsg ?: $msg->rawParams(
'File::transform() returned false' )->escaped();
380 $errorMsg->
getKey() ===
'thumbnail_image-failure-limit'
384 } elseif ( $thumb->isError() ) {
385 $errorMsg = $thumb->getHtmlMsg();
386 $errorCode = $thumb->getHttpStatusCode();
387 } elseif ( !$thumb->hasFile() ) {
388 $errorMsg = $msg->rawParams(
'No path supplied in thumbnail object' )->escaped();
389 } elseif ( $thumb->fileIsSource() ) {
391 ->rawParams(
'Image was not scaled, is the requested width bigger than the source?' )
396 if ( $errorMsg !==
false ) {
397 wfThumbError( $errorCode, $errorMsg,
null, [
'file' => $thumbName,
'path' => $thumbPath ] );
400 $status = $thumb->streamFileWithStatus( $headers );
401 if ( !$status->isOK() ) {
403 'file' => $thumbName,
'path' => $thumbPath,
404 'error' => $status->getWikiText(
false,
false,
'en' ) ] );
460 $cache = ObjectCache::getLocalClusterInstance();
461 $key = $cache->makeKey(
464 $file->getRepo()->getName(),
470 if ( $cache->get( $key ) >= 4 ) {
471 return [
false,
wfMessage(
'thumbnail_image-failure-limit', 4 ) ];
476 register_shutdown_function(
static function () use ( $cache, &$done, $key ) {
479 $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
489 if (
$file->isExpensiveToThumbnail() ) {
490 $poolCounterType =
'FileRenderExpensive';
492 $poolCounterType =
'FileRender';
499 'doWork' =>
static function () use (
$file, $params ) {
500 return $file->transform( $params, File::RENDER_NOW );
502 'doCachedWork' =>
static function () use (
$file, $params, $thumbPath ) {
505 return $file->getRepo()->fileExists( $thumbPath )
506 ?
$file->transform( $params, File::RENDER_NOW )
509 'error' =>
static function (
Status $status ) {
510 return wfMessage(
'generic-pool-error' )->parse() .
'<hr>' . $status->getHTML();
517 } elseif ( is_string( $result ) ) {
518 $errorHtml = $result;
520 }
catch ( Exception $e ) {
527 if ( !$thumb || $thumb->isError() ) {
529 $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
532 return [ $thumb, $errorHtml ];
656function wfThumbError( $status, $msgHtml, $msgText =
null, $context = [] ) {
659 \MediaWiki\Request\HeaderCallback::warnIfHeadersSent();
661 if ( headers_sent() ) {
662 LoggerFactory::getInstance(
'thumbnail' )->error(
663 'Error after output had been started. Output may be corrupt or truncated. ' .
664 'Original error: ' . ( $msgText ?: $msgHtml ) .
" (Status $status)",
670 header(
'Cache-Control: no-cache' );
671 header(
'Content-Type: text/html; charset=utf-8' );
672 if ( $status == 400 || $status == 404 || $status == 429 ) {
673 HttpStatus::header( $status );
674 } elseif ( $status == 403 ) {
675 HttpStatus::header( 403 );
676 header(
'Vary: Cookie' );
678 LoggerFactory::getInstance(
'thumbnail' )->error( $msgText ?: $msgHtml, $context );
679 HttpStatus::header( 500 );
682 header(
'X-MW-Thumbnail-Renderer: ' .
wfHostname() );
683 $url = htmlspecialchars(
684 $_SERVER[
'REQUEST_URI'] ??
'',
687 $hostname = htmlspecialchars(
wfHostname(), ENT_NOQUOTES );
688 $debug =
"<!-- $url -->\n<!-- $hostname -->\n";
695<meta charset=
"UTF-8" />
696<title>Error generating thumbnail</title>
699<h1>Error generating thumbnail</h1>
708 header(
'Content-Length: ' . strlen(
$content ) );