Move <warnings> and <query-continue> to result's top and optimize
[lhc/web/wiklou.git] / thumb.php
index 15fbf07..ede30e6 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -35,7 +35,7 @@ if ( defined( 'THUMB_HANDLER' ) ) {
        // Called from thumb_handler.php via 404; extract params from the URI...
        wfThumbHandle404();
 } else {
-       // Called directly, use $_REQUEST params
+       // Called directly, use $_GET params
        wfThumbHandleRequest();
 }
 
@@ -50,8 +50,8 @@ wfLogProfilingData();
  */
 function wfThumbHandleRequest() {
        $params = get_magic_quotes_gpc()
-               ? array_map( 'stripslashes', $_REQUEST )
-               : $_REQUEST;
+               ? array_map( 'stripslashes', $_GET )
+               : $_GET;
 
        wfStreamThumb( $params ); // stream the thumbnail
 }
@@ -91,6 +91,7 @@ function wfThumbHandle404() {
  */
 function wfStreamThumb( array $params ) {
        global $wgVaryOnXFP;
+
        wfProfileIn( __METHOD__ );
 
        $headers = array(); // HTTP headers to send
@@ -169,13 +170,11 @@ function wfStreamThumb( array $params ) {
 
        // Check the source file storage path
        if ( !$img->exists() ) {
-               wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
+               wfThumbError( 404, "The source file '$fileName' does not exist." );
                wfProfileOut( __METHOD__ );
                return;
-       }
-       $sourcePath = $img->getPath();
-       if ( $sourcePath === false ) {
-               wfThumbError( 500, 'The source file is not locally accessible.' );
+       } elseif ( $img->getPath() === false ) {
+               wfThumbError( 500, "The source file '$fileName' is not locally accessible." );
                wfProfileOut( __METHOD__ );
                return;
        }
@@ -189,74 +188,75 @@ function wfStreamThumb( array $params ) {
                wfSuppressWarnings();
                $imsUnix = strtotime( $imsString );
                wfRestoreWarnings();
-               $sourceTsUnix = wfTimestamp( TS_UNIX, $img->getTimestamp() );
-               if ( $sourceTsUnix <= $imsUnix ) {
+               if ( wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
                        header( 'HTTP/1.1 304 Not Modified' );
                        wfProfileOut( __METHOD__ );
                        return;
                }
        }
 
-       $thumbName = $img->thumbName( $params );
-       if ( !strlen( $thumbName ) ) { // invalid params?
-               wfThumbError( 400, 'The specified thumbnail parameters are not valid.' );
+       // Get the normalized thumbnail name from the parameters...
+       try {
+               $thumbName = $img->thumbName( $params );
+               if ( !strlen( $thumbName ) ) { // invalid params?
+                       wfThumbError( 400, 'The specified thumbnail parameters are not valid.' );
+                       wfProfileOut( __METHOD__ );
+                       return;
+               }
+               $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
+       } catch ( MWException $e ) {
+               wfThumbError( 500, $e->getHTML() );
                wfProfileOut( __METHOD__ );
                return;
        }
 
-       $disposition = $img->getThumbDisposition( $thumbName );
-       $headers[] = "Content-Disposition: $disposition";
-
-       // Stream the file if it exists already...
-       try {
-               $thumbName2 = $img->thumbName( $params, File::THUMB_FULL_NAME ); // b/c; "long" style
-               // For 404 handled thumbnails, we only use the the base name of the URI
-               // for the thumb params and the parent directory for the source file name.
-               // Check that the zone relative path matches up so squid caches won't pick
-               // up thumbs that would not be purged on source file deletion (bug 34231).
-               if ( isset( $params['rel404'] ) ) { // thumbnail was handled via 404
-                       if ( urldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName ) ) {
-                               // Request for the canonical thumbnail name
-                       } elseif ( urldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName2 ) ) {
-                               // Request for the "long" thumbnail name; redirect to canonical name
-                               $response = RequestContext::getMain()->getRequest()->response();
-                               $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) );
-                               $response->header( 'Location: ' . wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
-                               $response->header( 'Expires: ' .
-                                       gmdate( 'D, d M Y H:i:s', time() + 7*86400 ) . ' GMT' );
-                               if ( $wgVaryOnXFP ) {
-                                       $varyHeader[] = 'X-Forwarded-Proto';
-                               }
-                               if ( count( $varyHeader ) ) {
-                                       $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
-                               }
-                               wfProfileOut( __METHOD__ );
-                               return;
-                       } else {
-                               wfThumbError( 404, 'The given path of the specified thumbnail is incorrect.' );
-                               wfProfileOut( __METHOD__ );
-                               return;
+       // For 404 handled thumbnails, we only use the the base name of the URI
+       // for the thumb params and the parent directory for the source file name.
+       // Check that the zone relative path matches up so squid caches won't pick
+       // up thumbs that would not be purged on source file deletion (bug 34231).
+       if ( isset( $params['rel404'] ) ) { // thumbnail was handled via 404
+               if ( rawurldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName ) ) {
+                       // Request for the canonical thumbnail name
+               } elseif ( rawurldecode( $params['rel404'] ) === $img->getThumbRel( $thumbName2 ) ) {
+                       // Request for the "long" thumbnail name; redirect to canonical name
+                       $response = RequestContext::getMain()->getRequest()->response();
+                       $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) );
+                       $response->header( 'Location: ' .
+                               wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
+                       $response->header( 'Expires: ' .
+                               gmdate( 'D, d M Y H:i:s', time() + 7 * 86400 ) . ' GMT' );
+                       if ( $wgVaryOnXFP ) {
+                               $varyHeader[] = 'X-Forwarded-Proto';
                        }
-               }
-               $thumbPath = $img->getThumbPath( $thumbName );
-               if ( $img->getRepo()->fileExists( $thumbPath ) ) {
                        if ( count( $varyHeader ) ) {
-                               $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
+                               $response->header( 'Vary: ' . implode( ', ', $varyHeader ) );
                        }
-                       $img->getRepo()->streamFile( $thumbPath, $headers );
+                       wfProfileOut( __METHOD__ );
+                       return;
+               } else {
+                       wfThumbError( 404, "The given path of the specified thumbnail is incorrect;
+                               expected '" . $img->getThumbRel( $thumbName ) . "' but got '" .
+                               rawurldecode( $params['rel404'] ) . "'." );
                        wfProfileOut( __METHOD__ );
                        return;
                }
-       } catch ( MWException $e ) {
-               wfThumbError( 500, $e->getHTML() );
-               wfProfileOut( __METHOD__ );
-               return;
        }
 
+       // Suggest a good name for users downloading this thumbnail
+       $headers[] = "Content-Disposition: {$img->getThumbDisposition( $thumbName )}";
+
        if ( count( $varyHeader ) ) {
                $headers[] = 'Vary: ' . implode( ', ', $varyHeader );
        }
 
+       // Stream the file if it exists already...
+       $thumbPath = $img->getThumbPath( $thumbName );
+       if ( $img->getRepo()->fileExists( $thumbPath ) ) {
+               $img->getRepo()->streamFile( $thumbPath, $headers );
+               wfProfileOut( __METHOD__ );
+               return;
+       }
+
        // Thumbnail isn't already there, so create the new thumbnail...
        try {
                $thumb = $img->transform( $params, File::RENDER_NOW );
@@ -364,7 +364,7 @@ function wfThumbError( $status, $msg ) {
                $hostname = htmlspecialchars( wfHostname() );
                $debug = "<!-- $url -->\n<!-- $hostname -->\n";
        } else {
-               $debug = "";
+               $debug = '';
        }
        echo <<<EOT
 <html><head><title>Error generating thumbnail</title></head>