X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=thumb.php;h=c38b89c251a657607a21425dc73323de7aa03c87;hb=5c6c93c76dd8d40f0df94d1e7f4d9e573489c7a2;hp=c699bb19ab1cc87bed4e3a95a05f76b86401a1c5;hpb=2375b11953676604a3f45328d004767dfd8e7513;p=lhc%2Fweb%2Fwiklou.git diff --git a/thumb.php b/thumb.php index c699bb19ab..c38b89c251 100644 --- a/thumb.php +++ b/thumb.php @@ -21,6 +21,8 @@ * @ingroup Media */ +use MediaWiki\Logger\LoggerFactory; + define( 'MW_NO_OUTPUT_COMPRESSION', 1 ); require __DIR__ . '/includes/WebStart.php'; @@ -89,7 +91,7 @@ function wfThumbHandle404() { function wfStreamThumb( array $params ) { global $wgVaryOnXFP; - $headers = array(); // HTTP headers to send + $headers = []; // HTTP headers to send $fileName = isset( $params['f'] ) ? $params['f'] : ''; @@ -150,8 +152,8 @@ function wfStreamThumb( array $params ) { } // Check permissions if there are read restrictions - $varyHeader = array(); - if ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) ) { + $varyHeader = []; + if ( !in_array( 'read', User::getGroupPermissions( [ '*' ] ), true ) ) { if ( !$img->getTitle() || !$img->getTitle()->userCan( 'read' ) ) { wfThumbError( 403, 'Access denied. You do not have permission to access ' . 'the source file.' ); @@ -262,7 +264,8 @@ function wfStreamThumb( array $params ) { ); return; } catch ( MWException $e ) { - wfThumbError( 500, $e->getHTML() ); + wfThumbError( 500, $e->getHTML(), 'Exception caught while extracting thumb name', + [ 'exception' => $e ] ); return; } @@ -310,13 +313,14 @@ function wfStreamThumb( array $params ) { $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, [ 'file' => $thumbName, + 'path' => $thumbPath, 'error' => $status->getWikiText( false, false, 'en' ) ] ); } return; } @@ -337,6 +341,7 @@ function wfStreamThumb( array $params ) { // Check for thumbnail generation errors... $msg = wfMessage( 'thumbnail_error' ); $errorCode = 500; + if ( !$thumb ) { $errorMsg = $errorMsg ?: $msg->rawParams( 'File::transform() returned false' )->escaped(); if ( $errorMsg instanceof MessageSpecifier && @@ -346,6 +351,7 @@ function wfStreamThumb( array $params ) { } } elseif ( $thumb->isError() ) { $errorMsg = $thumb->getHtmlMsg(); + $errorCode = $thumb->getHttpStatusCode(); } elseif ( !$thumb->hasFile() ) { $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped(); } elseif ( $thumb->fileIsSource() ) { @@ -356,12 +362,14 @@ function wfStreamThumb( array $params ) { } if ( $errorMsg !== false ) { - wfThumbError( $errorCode, $errorMsg ); + wfThumbError( $errorCode, $errorMsg, null, [ '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, [ + 'file' => $thumbName, 'path' => $thumbPath, + 'error' => $status->getWikiText( false, false, 'en' ) ] ); } } } @@ -389,7 +397,7 @@ function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath // Check if this file keeps failing to render if ( $cache->get( $key ) >= 4 ) { - return array( false, wfMessage( 'thumbnail_image-failure-limit', 4 ) ); + return [ false, wfMessage( 'thumbnail_image-failure-limit', 4 ) ]; } $done = false; @@ -416,24 +424,21 @@ function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath // Thumbnail isn't already there, so create the new thumbnail... try { $work = new PoolCounterWorkViaCallback( $poolCounterType, sha1( $file->getName() ), - array( + [ '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() . '
' . $status->getHTML(); } - ) + ] ); $result = $work->execute(); if ( $result instanceof MediaTransformOutput ) { @@ -453,7 +458,7 @@ function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath $cache->incrWithInit( $key, $cache::TTL_HOUR + mt_rand( 0, 300 ) ); } - return array( $thumb, $errorHtml ); + return [ $thumb, $errorHtml ]; } /** @@ -495,7 +500,7 @@ function wfExtractThumbRequestInfo( $thumbRel ) { return null; // not a valid looking thumbnail request } - $params = array( 'f' => $filename, 'rel404' => $rel ); + $params = [ 'f' => $filename, 'rel404' => $rel ]; if ( $archOrTemp === 'archive/' ) { $params['archived'] = 1; } elseif ( $archOrTemp === 'temp/' ) { @@ -523,7 +528,7 @@ function wfExtractThumbParams( $file, $params ) { unset( $params['thumbName'] ); // Do the hook first for older extensions that rely on it. - if ( !Hooks::run( 'ExtractThumbParameters', array( $thumbname, &$params ) ) ) { + if ( !Hooks::run( 'ExtractThumbParameters', [ $thumbname, &$params ] ) ) { // Check hooks if parameters can be extracted // Hooks return false if they manage to *resolve* the parameters // This hook should be considered deprecated @@ -565,7 +570,6 @@ function wfExtractThumbParams( $file, $params ) { return null; } - /** * Output a thumbnail generation error message * @@ -582,9 +586,12 @@ function wfThumbErrorText( $status, $msgText ) { * * @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 = [] ) { global $wgShowHostnames; header( 'Cache-Control: no-cache' ); @@ -595,6 +602,7 @@ function wfThumbError( $status, $msgHtml ) { HttpStatus::header( 403 ); header( 'Vary: Cookie' ); } else { + LoggerFactory::getInstance( 'thumb' )->error( $msgText ?: $msgHtml, $context ); HttpStatus::header( 500 ); } if ( $wgShowHostnames ) {