Merge "HttpError: Convert line breaks in text message to <br>"
[lhc/web/wiklou.git] / thumb.php
index 12173c5..d39d43b 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -163,7 +163,7 @@ function wfStreamThumb( array $params ) {
 
        // Check if the file is hidden
        if ( $img->isDeleted( File::DELETED_FILE ) ) {
-               wfThumbError( 404, "The source file '$fileName' does not exist." );
+               wfThumbErrorText( 404, "The source file '$fileName' does not exist." );
                return;
        }
 
@@ -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 {
@@ -218,10 +219,10 @@ function wfStreamThumb( array $params ) {
                }
 
                // If its not a redirect that has a target as a local file, give 404.
-               wfThumbError( 404, "The source file '$fileName' does not exist." );
+               wfThumbErrorText( 404, "The source file '$fileName' does not exist." );
                return;
        } elseif ( $img->getPath() === false ) {
-               wfThumbError( 500, "The source file '$fileName' is not locally accessible." );
+               wfThumbErrorText( 400, "The source file '$fileName' is not locally accessible." );
                return;
        }
 
@@ -283,7 +284,7 @@ function wfStreamThumb( array $params ) {
                        }
                        return;
                } else {
-                       wfThumbError( 404, "The given path of the specified thumbnail is incorrect;
+                       wfThumbErrorText( 404, "The given path of the specified thumbnail is incorrect;
                                expected '" . $img->getThumbRel( $thumbName ) . "' but got '" .
                                rawurldecode( $rel404 ) . "'." );
                        return;
@@ -316,22 +317,25 @@ 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() ) {
@@ -412,7 +416,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 +431,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 +502,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 +541,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;
@@ -546,22 +551,32 @@ function wfExtractThumbParams( $file, $params ) {
        return null;
 }
 
+
+/**
+ * Output a thumbnail generation error message
+ *
+ * @param int $status
+ * @param string $msgText Plain text (will be html escaped)
+ * @return void
+ */
+function wfThumbErrorText( $status, $msgText ) {
+       wfThumbError( $status, htmlspecialchars( $msgText ) );
+}
+
 /**
  * Output a thumbnail generation error message
  *
  * @param int $status
- * @param string $msg HTML
+ * @param string $msgHtml HTML
  * @return void
  */
-function wfThumbError( $status, $msg ) {
+function wfThumbError( $status, $msgHtml ) {
        global $wgShowHostnames;
 
        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' );
@@ -585,7 +600,7 @@ function wfThumbError( $status, $msg ) {
 <body>
 <h1>Error generating thumbnail</h1>
 <p>
-$msg
+$msgHtml
 </p>
 $debug
 </body>