Make Special:Redirect work for blocked users and in read-only mode
[lhc/web/wiklou.git] / thumb.php
index 25bca87..bd14e41 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -38,7 +38,7 @@ if ( defined( 'THUMB_HANDLER' ) ) {
 $mediawiki = new MediaWiki();
 $mediawiki->doPostOutputShutdown( 'fast' );
 
-//--------------------------------------------------------------------------
+// --------------------------------------------------------------------------
 
 /**
  * Handle a thumbnail request via thumbnail file URL
@@ -190,6 +190,7 @@ function wfStreamThumb( array $params ) {
                                if ( $targetFile->exists() ) {
                                        $newThumbName = $targetFile->thumbName( $params );
                                        if ( $isOld ) {
+                                               /** @var array $bits */
                                                $newThumbUrl = $targetFile->getArchiveThumbUrl(
                                                        $bits[0] . '!' . $targetFile->getName(), $newThumbName );
                                        } else {
@@ -221,7 +222,7 @@ function wfStreamThumb( array $params ) {
                wfThumbErrorText( 404, "The source file '$fileName' does not exist." );
                return;
        } elseif ( $img->getPath() === false ) {
-               wfThumbErrorText( 500, "The source file '$fileName' is not locally accessible." );
+               wfThumbErrorText( 400, "The source file '$fileName' is not locally accessible." );
                return;
        }
 
@@ -249,11 +250,16 @@ function wfStreamThumb( array $params ) {
        try {
                $thumbName = $img->thumbName( $params );
                if ( !strlen( $thumbName ) ) { // invalid params?
-                       throw new MediaTransformInvalidParametersException( 'Empty return from File::thumbName' );
+                       throw new MediaTransformInvalidParametersException(
+                               'Empty return from File::thumbName'
+                       );
                }
                $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
        } catch ( MediaTransformInvalidParametersException $e ) {
-               wfThumbError( 400, 'The specified thumbnail parameters are not valid: ' . $e->getMessage() );
+               wfThumbError(
+                       400,
+                       'The specified thumbnail parameters are not valid: ' . $e->getMessage()
+               );
                return;
        } catch ( MWException $e ) {
                wfThumbError( 500, $e->getHTML() );
@@ -293,7 +299,8 @@ function wfStreamThumb( array $params ) {
        $dispositionType = isset( $params['download'] ) ? 'attachment' : 'inline';
 
        // Suggest a good name for users downloading this thumbnail
-       $headers[] = "Content-Disposition: {$img->getThumbDisposition( $thumbName, $dispositionType )}";
+       $headers[] =
+               "Content-Disposition: {$img->getThumbDisposition( $thumbName, $dispositionType )}";
 
        if ( count( $varyHeader ) ) {
                $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
@@ -316,29 +323,35 @@ function wfStreamThumb( array $params ) {
 
        $user = RequestContext::getMain()->getUser();
        if ( !wfThumbIsStandard( $img, $params ) && $user->pingLimiter( 'renderfile-nonstandard' ) ) {
-               wfThumbError( 500, wfMessage( 'actionthrottledtext' )->parse() );
+               wfThumbError( 429, wfMessage( 'actionthrottledtext' )->parse() );
                return;
        } elseif ( $user->pingLimiter( 'renderfile' ) ) {
-               wfThumbError( 500, wfMessage( 'actionthrottledtext' )->parse() );
+               wfThumbError( 429, wfMessage( 'actionthrottledtext' )->parse() );
                return;
        }
 
        list( $thumb, $errorMsg ) = wfGenerateThumbnail( $img, $params, $thumbName, $thumbPath );
 
-       /** @var MediaTransformOutput|bool $thumb */
+       /** @var MediaTransformOutput|MediaTransformError|bool $thumb */
 
        // 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 &&
+                       $errorMsg->getKey() === 'thumbnail_image-failure-limit'
+               ) {
+                       $errorCode = 429;
+               }
        } elseif ( $thumb->isError() ) {
                $errorMsg = $thumb->getHtmlMsg();
        } elseif ( !$thumb->hasFile() ) {
                $errorMsg = $msg->rawParams( 'No path supplied in thumbnail object' )->escaped();
        } elseif ( $thumb->fileIsSource() ) {
-               $errorMsg = $msg->
-                       rawParams( 'Image was not scaled, is the requested width bigger than the source?' )->escaped();
+               $errorMsg = $msg
+                       ->rawParams( 'Image was not scaled, is the requested width bigger than the source?' )
+                       ->escaped();
                $errorCode = 400;
        }
 
@@ -412,7 +425,7 @@ function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath
                                'fallback' => function () {
                                        return wfMessage( 'generic-pool-error' )->parse();
                                },
-                               'error' => function ( $status ) {
+                               'error' => function ( Status $status ) {
                                        return $status->getHTML();
                                }
                        )
@@ -427,6 +440,7 @@ function wfGenerateThumbnail( File $file, array $params, $thumbName, $thumbPath
                // Tried to select a page on a non-paged file?
        }
 
+       /** @noinspection PhpUnusedLocalVariableInspection */
        $done = true; // no PHP fatal occured
 
        if ( !$thumb || $thumb->isError() ) {
@@ -497,7 +511,7 @@ function wfExtractThumbRequestInfo( $thumbRel ) {
  */
 function wfExtractThumbParams( $file, $params ) {
        if ( !isset( $params['thumbName'] ) ) {
-               throw new MWException( "No thumbnail name passed to wfExtractThumbParams" );
+               throw new InvalidArgumentException( "No thumbnail name passed to wfExtractThumbParams" );
        }
 
        $thumbname = $params['thumbName'];
@@ -536,7 +550,7 @@ function wfExtractThumbParams( $file, $params ) {
 
        // As a last ditch fallback, use the traditional common parameters
        if ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
-               list( /* all */, $pagefull, $pagenum, $size ) = $matches;
+               list( /* all */, /* pagefull */, $pagenum, $size ) = $matches;
                $params['width'] = $size;
                if ( $pagenum ) {
                        $params['page'] = $pagenum;
@@ -551,11 +565,11 @@ function wfExtractThumbParams( $file, $params ) {
  * Output a thumbnail generation error message
  *
  * @param int $status
- * @param string $msg Plain text (will be html escaped)
+ * @param string $msgText Plain text (will be html escaped)
  * @return void
  */
 function wfThumbErrorText( $status, $msgText ) {
-       return wfThumbError( $status, htmlspecialchars( $msgText ) );
+       wfThumbError( $status, htmlspecialchars( $msgText ) );
 }
 
 /**
@@ -570,10 +584,8 @@ function wfThumbError( $status, $msgHtml ) {
 
        header( 'Cache-Control: no-cache' );
        header( 'Content-Type: text/html; charset=utf-8' );
-       if ( $status == 400 ) {
-               HttpStatus::header( 400 );
-       } elseif ( $status == 404 ) {
-               HttpStatus::header( 404 );
+       if ( $status == 400 || $status == 404 || $status == 429 ) {
+               HttpStatus::header( $status );
        } elseif ( $status == 403 ) {
                HttpStatus::header( 403 );
                header( 'Vary: Cookie' );
@@ -582,7 +594,9 @@ function wfThumbError( $status, $msgHtml ) {
        }
        if ( $wgShowHostnames ) {
                header( 'X-MW-Thumbnail-Renderer: ' . wfHostname() );
-               $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
+               $url = htmlspecialchars(
+                       isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : ''
+               );
                $hostname = htmlspecialchars( wfHostname() );
                $debug = "<!-- $url -->\n<!-- $hostname -->\n";
        } else {