Update git submodules
[mediawiki.git] / thumb.php
blob2db40526a2ac1b142b15a57817c5d21da9a32247
1 <?php
2 /**
3 * The web entry point for retrieving media thumbnails, created by a MediaHandler
4 * subclass or proxy request if FileRepo::getThumbProxyUrl is configured.
6 * This script may also resize an image on-demand, if it isn't found in the
7 * configured FileBackend storage.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
25 * @ingroup entrypoint
26 * @ingroup Media
29 use MediaWiki\Logger\LoggerFactory;
30 use MediaWiki\MediaWikiServices;
31 use MediaWiki\Profiler\ProfilingContext;
32 use MediaWiki\Request\WebRequest;
33 use MediaWiki\Status\Status;
34 use MediaWiki\Title\Title;
35 use Wikimedia\AtEase\AtEase;
37 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
38 // T241340: thumb.php is included by thumb_handler.php which already defined
39 // MW_ENTRY_POINT to 'thumb_handler'
40 if ( !defined( 'MW_ENTRY_POINT' ) ) {
41 define( 'MW_ENTRY_POINT', 'thumb' );
43 require __DIR__ . '/includes/WebStart.php';
45 wfThumbMain();
47 function wfThumbMain() {
48 global $wgTrivialMimeDetection, $wgRequest;
50 ProfilingContext::singleton()->init( MW_ENTRY_POINT, 'stream' );
52 // Don't use fancy MIME detection, just check the file extension for jpg/gif/png
53 $wgTrivialMimeDetection = true;
55 if ( defined( 'THUMB_HANDLER' ) ) {
56 // Called from thumb_handler.php via 404; extract params from the URI...
57 wfThumbHandle404();
58 } else {
59 // Called directly, use $_GET params
60 wfStreamThumb( $wgRequest->getQueryValuesOnly() );
63 $mediawiki = new MediaWiki();
64 $mediawiki->doPostOutputShutdown();
67 /**
68 * Handle a thumbnail request via thumbnail file URL
70 * @return void
72 function wfThumbHandle404() {
73 global $wgThumbPath;
75 if ( $wgThumbPath ) {
76 $relPath = WebRequest::getRequestPathSuffix( $wgThumbPath );
77 } else {
78 // Determine the request path relative to the thumbnail zone base
79 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
80 $baseUrl = $repo->getZoneUrl( 'thumb' );
81 if ( substr( $baseUrl, 0, 1 ) === '/' ) {
82 $basePath = $baseUrl;
83 } else {
84 $basePath = parse_url( $baseUrl, PHP_URL_PATH );
86 $relPath = WebRequest::getRequestPathSuffix( $basePath );
89 $params = wfExtractThumbRequestInfo( $relPath ); // basic wiki URL param extracting
90 if ( $params == null ) {
91 wfThumbError( 400, 'The specified thumbnail parameters are not recognized.' );
92 return;
95 wfStreamThumb( $params ); // stream the thumbnail
98 /**
99 * Stream a thumbnail specified by parameters
101 * @param array $params List of thumbnailing parameters. In addition to parameters
102 * passed to the MediaHandler, this may also includes the keys:
103 * f (for filename), archived (if archived file), temp (if temp file),
104 * w (alias for width), p (alias for page), r (ignored; historical),
105 * rel404 (path for render on 404 to verify hash path correct),
106 * thumbName (thumbnail name to potentially extract more parameters from
107 * e.g. 'lossy-page1-120px-Foo.tiff' would add page, lossy and width
108 * to the parameters)
109 * @return void
111 function wfStreamThumb( array $params ) {
112 global $wgVaryOnXFP;
114 $headers = []; // HTTP headers to send
116 $fileName = $params['f'] ?? '';
118 // Backwards compatibility parameters
119 if ( isset( $params['w'] ) ) {
120 $params['width'] = $params['w'];
121 unset( $params['w'] );
123 if ( isset( $params['width'] ) && substr( $params['width'], -2 ) == 'px' ) {
124 // strip the px (pixel) suffix, if found
125 $params['width'] = substr( $params['width'], 0, -2 );
127 if ( isset( $params['p'] ) ) {
128 $params['page'] = $params['p'];
131 // Is this a thumb of an archived file?
132 $isOld = ( isset( $params['archived'] ) && $params['archived'] );
133 unset( $params['archived'] ); // handlers don't care
135 // Is this a thumb of a temp file?
136 $isTemp = ( isset( $params['temp'] ) && $params['temp'] );
137 unset( $params['temp'] ); // handlers don't care
139 $services = MediaWikiServices::getInstance();
141 // Some basic input validation
142 $fileName = strtr( $fileName, '\\/', '__' );
143 $localRepo = $services->getRepoGroup()->getLocalRepo();
145 // Actually fetch the image. Method depends on whether it is archived or not.
146 if ( $isTemp ) {
147 $repo = $localRepo->getTempRepo();
148 $img = new UnregisteredLocalFile( null, $repo,
149 # Temp files are hashed based on the name without the timestamp.
150 # The thumbnails will be hashed based on the entire name however.
151 # @todo fix this convention to actually be reasonable.
152 $repo->getZonePath( 'public' ) . '/' . $repo->getTempHashPath( $fileName ) . $fileName
154 } elseif ( $isOld ) {
155 // Format is <timestamp>!<name>
156 $bits = explode( '!', $fileName, 2 );
157 if ( count( $bits ) != 2 ) {
158 wfThumbError( 404, wfMessage( 'badtitletext' )->parse() );
159 return;
161 $title = Title::makeTitleSafe( NS_FILE, $bits[1] );
162 if ( !$title ) {
163 wfThumbError( 404, wfMessage( 'badtitletext' )->parse() );
164 return;
166 $img = $localRepo->newFromArchiveName( $title, $fileName );
167 } else {
168 $img = $localRepo->newFile( $fileName );
171 // Check the source file title
172 if ( !$img ) {
173 wfThumbError( 404, wfMessage( 'badtitletext' )->parse() );
174 return;
177 // Check permissions if there are read restrictions
178 $varyHeader = [];
179 if ( !$services->getGroupPermissionsLookup()->groupHasPermission( '*', 'read' ) ) {
180 $user = RequestContext::getMain()->getUser();
181 $imgTitle = $img->getTitle();
183 if ( !$imgTitle || !$services->getPermissionManager()->userCan( 'read', $user, $imgTitle ) ) {
184 wfThumbError( 403, 'Access denied. You do not have permission to access ' .
185 'the source file.' );
186 return;
188 $headers[] = 'Cache-Control: private';
189 $varyHeader[] = 'Cookie';
192 // Check if the file is hidden
193 if ( $img->isDeleted( File::DELETED_FILE ) ) {
194 wfThumbErrorText( 404, "The source file '$fileName' does not exist." );
195 return;
198 // Do rendering parameters extraction from thumbnail name.
199 if ( isset( $params['thumbName'] ) ) {
200 $params = wfExtractThumbParams( $img, $params );
202 if ( $params == null ) {
203 wfThumbError( 400, 'The specified thumbnail parameters are not recognized.' );
204 return;
207 // Check the source file storage path
208 if ( !$img->exists() ) {
209 $redirectedLocation = false;
210 if ( !$isTemp ) {
211 // Check for file redirect
212 // Since redirects are associated with pages, not versions of files,
213 // we look for the most current version to see if its a redirect.
214 $possRedirFile = $localRepo->findFile( $img->getName() );
215 if ( $possRedirFile && $possRedirFile->getRedirected() !== null ) {
216 $redirTarget = $possRedirFile->getName();
217 $targetFile = $localRepo->newFile( Title::makeTitleSafe( NS_FILE, $redirTarget ) );
218 if ( $targetFile->exists() ) {
219 $newThumbName = $targetFile->thumbName( $params );
220 if ( $isOld ) {
221 /** @var array $bits */
222 $newThumbUrl = $targetFile->getArchiveThumbUrl(
223 $bits[0] . '!' . $targetFile->getName(), $newThumbName );
224 } else {
225 $newThumbUrl = $targetFile->getThumbUrl( $newThumbName );
227 $redirectedLocation = wfExpandUrl( $newThumbUrl, PROTO_CURRENT );
232 if ( $redirectedLocation ) {
233 // File has been moved. Give redirect.
234 $response = RequestContext::getMain()->getRequest()->response();
235 $response->statusHeader( 302 );
236 $response->header( 'Location: ' . $redirectedLocation );
237 $response->header( 'Expires: ' .
238 gmdate( 'D, d M Y H:i:s', time() + 12 * 3600 ) . ' GMT' );
239 if ( $wgVaryOnXFP ) {
240 $varyHeader[] = 'X-Forwarded-Proto';
242 if ( count( $varyHeader ) ) {
243 $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
245 $response->header( 'Content-Length: 0' );
246 return;
249 // If its not a redirect that has a target as a local file, give 404.
250 wfThumbErrorText( 404, "The source file '$fileName' does not exist." );
251 return;
252 } elseif ( $img->getPath() === false ) {
253 wfThumbErrorText( 400, "The source file '$fileName' is not locally accessible." );
254 return;
257 // Check IMS against the source file
258 // This means that clients can keep a cached copy even after it has been deleted on the server
259 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
260 // Fix IE brokenness
261 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
262 // Calculate time
263 AtEase::suppressWarnings();
264 $imsUnix = strtotime( $imsString );
265 AtEase::restoreWarnings();
266 if ( wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
267 HttpStatus::header( 304 );
268 return;
272 $rel404 = $params['rel404'] ?? null;
273 unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER
274 unset( $params['f'] ); // We're done with 'f' parameter.
275 unset( $params['rel404'] ); // moved to $rel404
277 // Get the normalized thumbnail name from the parameters...
278 try {
279 $thumbName = $img->thumbName( $params );
280 if ( !strlen( $thumbName ?? '' ) ) { // invalid params?
281 throw new MediaTransformInvalidParametersException(
282 'Empty return from File::thumbName'
285 $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
286 } catch ( MediaTransformInvalidParametersException $e ) {
287 wfThumbError(
288 400,
289 'The specified thumbnail parameters are not valid: ' . $e->getMessage()
291 return;
292 } catch ( MWException $e ) {
293 wfThumbError( 500, $e->getHTML(), 'Exception caught while extracting thumb name',
294 [ 'exception' => $e ] );
295 return;
298 // For 404 handled thumbnails, we only use the base name of the URI
299 // for the thumb params and the parent directory for the source file name.
300 // Check that the zone relative path matches up so CDN caches won't pick
301 // up thumbs that would not be purged on source file deletion (T36231).
302 if ( $rel404 !== null ) { // thumbnail was handled via 404
303 if ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName ) ) {
304 // Request for the canonical thumbnail name
305 } elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) {
306 // Request for the "long" thumbnail name; redirect to canonical name
307 $response = RequestContext::getMain()->getRequest()->response();
308 $response->statusHeader( 301 );
309 $response->header( 'Location: ' .
310 wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
311 $response->header( 'Expires: ' .
312 gmdate( 'D, d M Y H:i:s', time() + 7 * 86400 ) . ' GMT' );
313 if ( $wgVaryOnXFP ) {
314 $varyHeader[] = 'X-Forwarded-Proto';
316 if ( count( $varyHeader ) ) {
317 $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
319 return;
320 } else {
321 wfThumbErrorText( 404, "The given path of the specified thumbnail is incorrect;
322 expected '" . $img->getThumbRel( $thumbName ) . "' but got '" .
323 rawurldecode( $rel404 ) . "'." );
324 return;
328 $dispositionType = isset( $params['download'] ) ? 'attachment' : 'inline';
330 // Suggest a good name for users downloading this thumbnail
331 $headers[] =
332 'Content-Disposition: ' . $img->getThumbDisposition( $thumbName, $dispositionType );
334 if ( count( $varyHeader ) ) {
335 $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
338 // Stream the file if it exists already...
339 $thumbPath = $img->getThumbPath( $thumbName );
340 if ( $img->getRepo()->fileExists( $thumbPath ) ) {
341 $starttime = microtime( true );
342 $status = $img->getRepo()->streamFileWithStatus( $thumbPath, $headers );
343 $streamtime = microtime( true ) - $starttime;
345 if ( $status->isOK() ) {
346 $services->getStatsdDataFactory()->timing(
347 'media.thumbnail.stream', $streamtime
349 } else {
350 wfThumbError( 500, 'Could not stream the file', null, [ 'file' => $thumbName,
351 'path' => $thumbPath, 'error' => $status->getWikiText( false, false, 'en' ) ] );
353 return;
356 $user = RequestContext::getMain()->getUser();
357 if ( !wfThumbIsStandard( $img, $params ) && $user->pingLimiter( 'renderfile-nonstandard' ) ) {
358 wfThumbError( 429, wfMessage( 'actionthrottledtext' )->parse() );
359 return;
360 } elseif ( $user->pingLimiter( 'renderfile' ) ) {
361 wfThumbError( 429, wfMessage( 'actionthrottledtext' )->parse() );
362 return;
365 $thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
367 if ( strlen( $thumbProxyUrl ?? '' ) ) {
368 wfProxyThumbnailRequest( $img, $thumbName );
369 // No local fallback when in proxy mode
370 return;
371 } else {
372 // Generate the thumbnail locally
373 [ $thumb, $errorMsg ] = wfGenerateThumbnail( $img, $params, $thumbName, $thumbPath );
376 /** @var MediaTransformOutput|MediaTransformError|bool $thumb */
378 // Check for thumbnail generation errors...
379 $msg = wfMessage( 'thumbnail_error' );
380 $errorCode = 500;
382 if ( !$thumb ) {
383 $errorMsg = $errorMsg ?: $msg->rawParams( 'File::transform() returned false' )->escaped();
384 if ( $errorMsg instanceof MessageSpecifier &&
385 $errorMsg->getKey() === 'thumbnail_image-failure-limit'
387 $errorCode = 429;
389 } elseif ( $thumb->isError() ) {
390 $errorMsg = $thumb->getHtmlMsg();
391 $errorCode = $thumb->getHttpStatusCode();
392 } elseif ( !$thumb->hasFile() ) {
393 $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
394 } elseif ( $thumb->fileIsSource() ) {
395 $errorMsg = $msg
396 ->rawParams( 'Image was not scaled, is the requested width bigger than the source?' )
397 ->escaped();
398 $errorCode = 400;
401 if ( $errorMsg !== false ) {
402 wfThumbError( $errorCode, $errorMsg, null, [ 'file' => $thumbName, 'path' => $thumbPath ] );
403 } else {
404 // Stream the file if there were no errors
405 $status = $thumb->streamFileWithStatus( $headers );
406 if ( !$status->isOK() ) {
407 wfThumbError( 500, 'Could not stream the file', null, [
408 'file' => $thumbName, 'path' => $thumbPath,
409 'error' => $status->getWikiText( false, false, 'en' ) ] );
415 * Proxies thumbnail request to a service that handles thumbnailing
417 * @param File $img
418 * @param string $thumbName
420 function wfProxyThumbnailRequest( $img, $thumbName ) {
421 $thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
423 // Instead of generating the thumbnail ourselves, we proxy the request to another service
424 $thumbProxiedUrl = $thumbProxyUrl . $img->getThumbRel( $thumbName );
426 $req = MediaWikiServices::getInstance()->getHttpRequestFactory()->create( $thumbProxiedUrl );
427 $secret = $img->getRepo()->getThumbProxySecret();
429 // Pass a secret key shared with the proxied service if any
430 if ( strlen( $secret ?? '' ) ) {
431 $req->setHeader( 'X-Swift-Secret', $secret );
434 // Send request to proxied service
435 $req->execute();
437 \MediaWiki\Request\HeaderCallback::warnIfHeadersSent();
439 // Simply serve the response from the proxied service as-is
440 header( 'HTTP/1.1 ' . $req->getStatus() );
442 $headers = $req->getResponseHeaders();
444 foreach ( $headers as $key => $values ) {
445 foreach ( $values as $value ) {
446 header( $key . ': ' . $value, false );
450 echo $req->getContent();
454 * Actually try to generate a new thumbnail
456 * @param File $file
457 * @param array $params
458 * @param string $thumbName
459 * @param string $thumbPath
460 * @return array (MediaTransformOutput|bool, string|bool error message HTML)
462 function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath ) {
463 global $wgAttemptFailureEpoch;
465 $cache = ObjectCache::getLocalClusterInstance();
466 $key = $cache->makeKey(
467 'attempt-failures',
468 $wgAttemptFailureEpoch,
469 $file->getRepo()->getName(),
470 $file->getSha1(),
471 md5( $thumbName )
474 // Check if this file keeps failing to render
475 if ( $cache->get( $key ) >= 4 ) {
476 return [ false, wfMessage( 'thumbnail_image-failure-limit', 4 ) ];
479 $done = false;
480 // Record failures on PHP fatals in addition to caching exceptions
481 register_shutdown_function( static function () use ( $cache, &$done, $key ) {
482 if ( !$done ) { // transform() gave a fatal
483 // Randomize TTL to reduce stampedes
484 $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
486 } );
488 $thumb = false;
489 $errorHtml = false;
491 // guard thumbnail rendering with PoolCounter to avoid stampedes
492 // expensive files use a separate PoolCounter config so it is possible
493 // to set up a global limit on them
494 if ( $file->isExpensiveToThumbnail() ) {
495 $poolCounterType = 'FileRenderExpensive';
496 } else {
497 $poolCounterType = 'FileRender';
500 // Thumbnail isn't already there, so create the new thumbnail...
501 try {
502 $work = new PoolCounterWorkViaCallback( $poolCounterType, sha1( $file->getName() ),
504 'doWork' => static function () use ( $file, $params ) {
505 return $file->transform( $params, File::RENDER_NOW );
507 'doCachedWork' => static function () use ( $file, $params, $thumbPath ) {
508 // If the worker that finished made this thumbnail then use it.
509 // Otherwise, it probably made a different thumbnail for this file.
510 return $file->getRepo()->fileExists( $thumbPath )
511 ? $file->transform( $params, File::RENDER_NOW )
512 : false; // retry once more in exclusive mode
514 'error' => static function ( Status $status ) {
515 return wfMessage( 'generic-pool-error' )->parse() . '<hr>' . $status->getHTML();
519 $result = $work->execute();
520 if ( $result instanceof MediaTransformOutput ) {
521 $thumb = $result;
522 } elseif ( is_string( $result ) ) { // error
523 $errorHtml = $result;
525 } catch ( Exception $e ) {
526 // Tried to select a page on a non-paged file?
529 /** @noinspection PhpUnusedLocalVariableInspection */
530 $done = true; // no PHP fatal occurred
532 if ( !$thumb || $thumb->isError() ) {
533 // Randomize TTL to reduce stampedes
534 $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
537 return [ $thumb, $errorHtml ];
541 * Convert pathinfo type parameter, into normal request parameters
543 * So for example, if the request was redirected from
544 * /w/images/thumb/a/ab/Foo.png/120px-Foo.png. The $thumbRel parameter
545 * of this function would be set to "a/ab/Foo.png/120px-Foo.png".
546 * This method is responsible for turning that into an array
547 * with the following keys:
548 * * f => the filename (Foo.png)
549 * * rel404 => the whole thing (a/ab/Foo.png/120px-Foo.png)
550 * * archived => 1 (If the request is for an archived thumb)
551 * * temp => 1 (If the file is in the "temporary" zone)
552 * * thumbName => the thumbnail name, including parameters (120px-Foo.png)
554 * Transform specific parameters are set later via wfExtractThumbParams().
556 * @param string $thumbRel Thumbnail path relative to the thumb zone
557 * @return array|null Associative params array or null
559 function wfExtractThumbRequestInfo( $thumbRel ) {
560 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
562 $hashDirReg = $subdirReg = '';
563 $hashLevels = $repo->getHashLevels();
564 for ( $i = 0; $i < $hashLevels; $i++ ) {
565 $subdirReg .= '[0-9a-f]';
566 $hashDirReg .= "$subdirReg/";
569 // Check if this is a thumbnail of an original in the local file repo
570 if ( preg_match( "!^((archive/)?$hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
571 [ /*all*/, $rel, $archOrTemp, $filename, $thumbname ] = $m;
572 // Check if this is a thumbnail of a temp file in the local file repo
573 } elseif ( preg_match( "!^(temp/)($hashDirReg([^/]*)/([^/]*))$!", $thumbRel, $m ) ) {
574 [ /*all*/, $archOrTemp, $rel, $filename, $thumbname ] = $m;
575 } else {
576 return null; // not a valid looking thumbnail request
579 $params = [ 'f' => $filename, 'rel404' => $rel ];
580 if ( $archOrTemp === 'archive/' ) {
581 $params['archived'] = 1;
582 } elseif ( $archOrTemp === 'temp/' ) {
583 $params['temp'] = 1;
586 $params['thumbName'] = $thumbname;
587 return $params;
591 * Convert a thumbnail name (122px-foo.png) to parameters, using
592 * file handler.
594 * @param File $file File object for file in question
595 * @param array $params Array of parameters so far
596 * @return array|null Parameters array with more parameters, or null
598 function wfExtractThumbParams( $file, $params ) {
599 if ( !isset( $params['thumbName'] ) ) {
600 throw new InvalidArgumentException( "No thumbnail name passed to wfExtractThumbParams" );
603 $thumbname = $params['thumbName'];
604 unset( $params['thumbName'] );
606 // FIXME: Files in the temp zone don't set a MIME type, which means
607 // they don't have a handler. Which means we can't parse the param
608 // string. However, not a big issue as what good is a param string
609 // if you have no handler to make use of the param string and
610 // actually generate the thumbnail.
611 $handler = $file->getHandler();
613 // Based on UploadStash::parseKey
614 $fileNamePos = strrpos( $thumbname, $params['f'] );
615 if ( $fileNamePos === false ) {
616 // Maybe using a short filename? (see FileRepo::nameForThumb)
617 $fileNamePos = strrpos( $thumbname, 'thumbnail' );
620 if ( $handler && $fileNamePos !== false ) {
621 $paramString = substr( $thumbname, 0, $fileNamePos - 1 );
622 $extraParams = $handler->parseParamString( $paramString );
623 if ( $extraParams !== false ) {
624 return $params + $extraParams;
628 // As a last ditch fallback, use the traditional common parameters
629 if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
630 [ /* all */, /* pagefull */, $pagenum, $size ] = $matches;
631 $params['width'] = $size;
632 if ( $pagenum ) {
633 $params['page'] = $pagenum;
635 return $params; // valid thumbnail URL
637 return null;
641 * Output a thumbnail generation error message
643 * @param int $status
644 * @param string $msgText Plain text (will be html escaped)
645 * @return void
647 function wfThumbErrorText( $status, $msgText ) {
648 wfThumbError( $status, htmlspecialchars( $msgText, ENT_NOQUOTES ) );
652 * Output a thumbnail generation error message
654 * @param int $status
655 * @param string $msgHtml HTML
656 * @param string|null $msgText Short error description, for internal logging. Defaults to $msgHtml.
657 * Only used for HTTP 500 errors.
658 * @param array $context Error context, for internal logging. Only used for HTTP 500 errors.
659 * @return void
661 function wfThumbError( $status, $msgHtml, $msgText = null, $context = [] ) {
662 global $wgShowHostnames;
664 \MediaWiki\Request\HeaderCallback::warnIfHeadersSent();
666 if ( headers_sent() ) {
667 LoggerFactory::getInstance( 'thumbnail' )->error(
668 'Error after output had been started. Output may be corrupt or truncated. ' .
669 'Original error: ' . ( $msgText ?: $msgHtml ) . " (Status $status)",
670 $context
672 return;
675 header( 'Cache-Control: no-cache' );
676 header( 'Content-Type: text/html; charset=utf-8' );
677 if ( $status == 400 || $status == 404 || $status == 429 ) {
678 HttpStatus::header( $status );
679 } elseif ( $status == 403 ) {
680 HttpStatus::header( 403 );
681 header( 'Vary: Cookie' );
682 } else {
683 LoggerFactory::getInstance( 'thumbnail' )->error( $msgText ?: $msgHtml, $context );
684 HttpStatus::header( 500 );
686 if ( $wgShowHostnames ) {
687 header( 'X-MW-Thumbnail-Renderer: ' . wfHostname() );
688 $url = htmlspecialchars(
689 $_SERVER['REQUEST_URI'] ?? '',
690 ENT_NOQUOTES
692 $hostname = htmlspecialchars( wfHostname(), ENT_NOQUOTES );
693 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
694 } else {
695 $debug = '';
697 $content = <<<EOT
698 <!DOCTYPE html>
699 <html><head>
700 <meta charset="UTF-8" />
701 <title>Error generating thumbnail</title>
702 </head>
703 <body>
704 <h1>Error generating thumbnail</h1>
706 $msgHtml
707 </p>
708 $debug
709 </body>
710 </html>
712 EOT;
713 header( 'Content-Length: ' . strlen( $content ) );
714 echo $content;