thumb.php now handles short and long thumbnail name formats when possible.
authorAaron <aschulz@wikimedia.org>
Tue, 4 Sep 2012 16:57:44 +0000 (09:57 -0700)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 12 Sep 2012 21:17:09 +0000 (21:17 +0000)
Change-Id: I33932ac0e0294dc13332dce9d4ab00a75d9cdcba

includes/filerepo/file/File.php
thumb.php

index 3b0ea14..557609d 100644 (file)
@@ -68,6 +68,9 @@ abstract class File {
        const FOR_THIS_USER = 2;
        const RAW = 3;
 
+       // Options for File::thumbName()
+       const THUMB_FULL_NAME = 1;
+
        /**
         * Some member variables can be lazy-initialised using __get(). The
         * initialisation function for these variables is always a function named
@@ -759,15 +762,18 @@ abstract class File {
        }
 
        /**
-        * Return the file name of a thumbnail with the specified parameters
+        * Return the file name of a thumbnail with the specified parameters.
+        * Use File::THUMB_FULL_NAME to always get a name like "<params>-<source>".
+        * Otherwise, the format may be "<params>-<source>" or "<params>-thumbnail.<ext>".
         *
         * @param $params Array: handler-specific parameters
-        * @private -ish
-        *
+        * @param $flags integer Bitfield that supports THUMB_* constants
         * @return string
         */
-       function thumbName( $params ) {
-               $name = $this->repo ? $this->repo->nameForThumb( $this->getName() ) : $this->getName();
+       public function thumbName( $params, $flags = 0 ) {
+               $name = ( $this->repo && !( $flags & self::THUMB_FULL_NAME ) )
+                       ? $this->repo->nameForThumb( $this->getName() )
+                       : $this->getName();
                return $this->generateThumbName( $name, $params );
        }
 
@@ -779,7 +785,7 @@ abstract class File {
         *
         * @return string
         */
-       function generateThumbName( $name, $params ) {
+       public function generateThumbName( $name, $params ) {
                if ( !$this->getHandler() ) {
                        return null;
                }
index 8fc868d..9a2d00b 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -217,16 +217,26 @@ function wfStreamThumb( array $params ) {
 
        // 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
-                       && urldecode( $params['rel404'] ) !== $img->getThumbRel( $thumbName ) )
-               {
-                       wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
-                       wfProfileOut( __METHOD__ );
-                       return;
+               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' );
+                       } else {
+                               wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
+                               wfProfileOut( __METHOD__ );
+                               return;
+                       }
                }
                $thumbPath = $img->getThumbPath( $thumbName );
                if ( $img->getRepo()->fileExists( $thumbPath ) ) {