* @ingroup Media
*/
+use MediaWiki\Logger\LoggerFactory;
+
define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
require __DIR__ . '/includes/WebStart.php';
);
return;
} catch ( MWException $e ) {
- wfThumbError( 500, $e->getHTML() );
+ wfThumbError( 500, $e->getHTML(), 'Exception caught while extracting thumb name',
+ array( 'exception' => $e ) );
return;
}
$thumbPath = $img->getThumbPath( $thumbName );
if ( $img->getRepo()->fileExists( $thumbPath ) ) {
$starttime = microtime( true );
- $success = $img->getRepo()->streamFile( $thumbPath, $headers );
+ $status = $img->getRepo()->streamFileWithStatus( $thumbPath, $headers );
$streamtime = microtime( true ) - $starttime;
- if ( !$success ) {
- wfThumbError( 500, 'Could not stream the file' );
- } else {
+ if ( $status->isOK() ) {
RequestContext::getMain()->getStats()->timing( 'media.thumbnail.stream', $streamtime );
+ } else {
+ wfThumbError( 500, 'Could not stream the file', null, array( 'file' => $thumbName,
+ 'path' => $thumbPath, 'error' => $status->getWikiText() ) );
}
return;
}
}
if ( $errorMsg !== false ) {
- wfThumbError( $errorCode, $errorMsg );
+ wfThumbError( $errorCode, $errorMsg, null, array( 'file' => $thumbName, 'path' => $thumbPath ) );
} else {
// Stream the file if there were no errors
- $success = $thumb->streamFile( $headers );
- if ( !$success ) {
- wfThumbError( 500, 'Could not stream the file' );
+ $status = $thumb->streamFileWithStatus( $headers );
+ if ( !$status->isOK() ) {
+ wfThumbError( 500, 'Could not stream the file', null, array(
+ 'file' => $thumbName, 'path' => $thumbPath, 'error' => $status->getWikiText() ) );
}
}
}
* @return array (MediaTransformOutput|bool, string|bool error message HTML)
*/
function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath ) {
- global $wgMemc, $wgAttemptFailureEpoch;
+ global $wgAttemptFailureEpoch;
- $key = wfMemcKey( 'attempt-failures', $wgAttemptFailureEpoch,
- $file->getRepo()->getName(), $file->getSha1(), md5( $thumbName ) );
+ $cache = ObjectCache::getLocalClusterInstance();
+ $key = $cache->makeKey(
+ 'attempt-failures',
+ $wgAttemptFailureEpoch,
+ $file->getRepo()->getName(),
+ $file->getSha1(),
+ md5( $thumbName )
+ );
// Check if this file keeps failing to render
- if ( $wgMemc->get( $key ) >= 4 ) {
+ if ( $cache->get( $key ) >= 4 ) {
return array( false, wfMessage( 'thumbnail_image-failure-limit', 4 ) );
}
$done = false;
// Record failures on PHP fatals in addition to caching exceptions
- register_shutdown_function( function () use ( &$done, $key ) {
+ register_shutdown_function( function () use ( $cache, &$done, $key ) {
if ( !$done ) { // transform() gave a fatal
- global $wgMemc;
// Randomize TTL to reduce stampedes
- $wgMemc->incrWithInit( $key, 3600 + mt_rand( 0, 300 ) );
+ $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
}
} );
'doWork' => function () use ( $file, $params ) {
return $file->transform( $params, File::RENDER_NOW );
},
- 'getCachedWork' => function () use ( $file, $params, $thumbPath ) {
+ 'doCachedWork' => function () use ( $file, $params, $thumbPath ) {
// If the worker that finished made this thumbnail then use it.
// Otherwise, it probably made a different thumbnail for this file.
return $file->getRepo()->fileExists( $thumbPath )
? $file->transform( $params, File::RENDER_NOW )
: false; // retry once more in exclusive mode
},
- 'fallback' => function () {
- return wfMessage( 'generic-pool-error' )->parse();
- },
'error' => function ( Status $status ) {
- return $status->getHTML();
+ return wfMessage( 'generic-pool-error' )->parse() . '<hr>' . $status->getHTML();
}
)
);
if ( !$thumb || $thumb->isError() ) {
// Randomize TTL to reduce stampedes
- $wgMemc->incrWithInit( $key, 3600 + mt_rand( 0, 300 ) );
+ $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) );
}
return array( $thumb, $errorHtml );
*
* @param int $status
* @param string $msgHtml HTML
+ * @param string $msgText Short error description, for internal logging. Defaults to $msgHtml.
+ * Only used for HTTP 500 errors.
+ * @param array $context Error context, for internal logging. Only used for HTTP 500 errors.
* @return void
*/
-function wfThumbError( $status, $msgHtml ) {
+function wfThumbError( $status, $msgHtml, $msgText = null, $context = array() ) {
global $wgShowHostnames;
header( 'Cache-Control: no-cache' );
HttpStatus::header( 403 );
header( 'Vary: Cookie' );
} else {
+ LoggerFactory::getInstance( 'thumb' )->error( $msgText ?: $msgHtml, $context );
HttpStatus::header( 500 );
}
if ( $wgShowHostnames ) {