Merge "Make thumb_handler.php use MediaHandler::parseParamString"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 30 Sep 2013 18:11:09 +0000 (18:11 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 30 Sep 2013 18:11:09 +0000 (18:11 +0000)
1  2 
RELEASE-NOTES-1.22
docs/hooks.txt
includes/media/MediaHandler.php
thumb.php

diff --combined RELEASE-NOTES-1.22
@@@ -219,21 -219,6 +219,21 @@@ production
  * Add a variable (wgRedactedFunctionArguments) to redact the values sent as certain function
    parameters from exception stack traces.
  * Added {{REVISIONSIZE}} variable to get the current size of a revision.
 +* Add support for the LESS stylesheet language to ResourceLoader. LESS is a
 +  stylesheet language that compiles into CSS. ResourceLoader file modules may
 +  include LESS style files; ResourceLoader will compile these files into CSS
 +  before sending them to the client.
 +** The $wgResourceLoaderLESSVars configuration variable is an associative array
 +   mapping variable names to string CSS values. These variables are considered
 +   declared for all LESS files. Additional variables may be registered by
 +   adding keys to the array.
 +** $wgResourceLoaderLESSFunctions is an associative array of custom LESS
 +   function names to PHP callables. See <http://leafo.net/lessphp/docs/#custom_functions>
 +   for more details regarding custom functions.
 +** $wgResourceLoaderLESSImportPaths is an array of file system paths. Files
 +   referenced in LESS '@import' statements are looked up here first.
 +* Added meta=filerepoinfo API module for getting information about foreign
 +  image repositories, and related ForeignAPIRepo methods getInfo and getApiUrl.
  
  === Bug fixes in 1.22 ===
  * Disable Special:PasswordReset when $wgEnableEmail is false. Previously one
  * (bug 26811) On DB error pages, server hostnames are now hidden when both
    $wgShowHostnames and $wgShowSQLErrors are false.
  * (bug 6200) line breaks in <blockquote> are handled like they are in <div>
 +* (bug 14931) Default character set now set to 'utf8' when a new MySQL
 +  database is created.
 +* (bug 47191) Fixed "Column 'si_title' cannot be part of FULLTEXT index"
 +  MySQL error when installing using the binary character set option.
  
  === API changes in 1.22 ===
  * (bug 25553) The JSON output formatter now leaves forward slashes unescaped
  * (bug 49090) Token-getting functions will fail when using jsonp callbacks.
  * (bug 52699) action=upload returns normalized file name on warning
    "exists-normalized" instead of filename to be uploaded to.
 +* (bug 53884) action=edit will now return an error when the specified section
 +  does not exist in the page.
  
  === Languages updated in 1.22===
  
@@@ -476,6 -455,8 +476,8 @@@ changes to languages because of Bugzill
    The skins/common/wikiprintable.css file no longer exists. Return value of
    Skin#commonPrintStylesheet is ignored. Please use the 'mediawiki.legacy.commonPrint'
    module instead or base your skin on SkinTemplate.
+ * (bug 49629) The hook ExtractThumbParamaters has been deprecated in favour
+   of media handler overriding MediaHandler::parseParamString.
  
  == Compatibility ==
  
diff --combined docs/hooks.txt
@@@ -1077,6 -1077,7 +1077,7 @@@ change the tables headers
  
  'ExtractThumbParameters': Called when extracting thumbnail parameters from a
  thumbnail file name.
+ DEPRECATED: Media handler should override MediaHandler::parseParamString instead.
  $thumbname: the base name of the thumbnail file
  &$params: the currently extracted params (has source name, temp or archived zone)
  
@@@ -1222,16 -1223,6 +1223,16 @@@ $out: OutputPage object (to check what 
  $user: User whose preferences are being modified.
  &$preferences: Preferences description array, to be fed to an HTMLForm object
  
 +'GetRelativeTimestamp': Pre-emptively override the relative timestamp generated
 +by MWTimestamp::getRelativeTimestamp(). Return false in this hook to use the custom
 +output.
 +&$output: string for the output timestamp
 +&$diff: DateInterval representing the difference between the timestamps
 +$timestamp: MWTimestamp object of the current (user-adjusted) timestamp
 +$relativeTo: MWTimestamp object of the relative (user-adjusted) timestamp
 +$user: User whose preferences are being used to make timestamp
 +$lang: Language that will be used to render the timestamp
 +
  'getUserPermissionsErrors': Add a permissions error when permissions errors are
  checked for. Use instead of userCan for most cases. Return false if the user
  can't do it, and populate $result with the reason in the form of
@@@ -1889,7 -1880,6 +1890,7 @@@ $action : Action being performe
  &$result : Whether or not the action should be prevented
  Change $result and return false to give a definitive answer, otherwise
  the built-in rate limiting checks are used, if enabled.
 +$incrBy: Amount to increment counter by
  
  'PlaceNewSection': Override placement of new sections. Return false and put the
  merged text into $text to override the default behavior.
@@@ -79,13 -79,15 +79,15 @@@ abstract class MediaHandler 
         * Merge a parameter array into a string appropriate for inclusion in filenames
         *
         * @param $params array Array of parameters that have been through normaliseParams.
+        * @return String
         */
        abstract function makeParamString( $params );
  
        /**
         * Parse a param string made with makeParamString back into an array
         *
-        * @param $str string
+        * @param $str string The parameter string without file name (e.g. 122px)
+        * @return Array|Boolean Array of parameters or false on failure.
         */
        abstract function parseParamString( $str );
  
        public static function canRotate() {
                return false;
        }
 +
 +      /**
 +       * On supporting image formats, try to read out the low-level orientation
 +       * of the file and return the angle that the file needs to be rotated to
 +       * be viewed.
 +       *
 +       * This information is only useful when manipulating the original file;
 +       * the width and height we normally work with is logical, and will match
 +       * any produced output views.
 +       *
 +       * For files we don't know, we return 0.
 +       *
 +       * @param $file File
 +       * @return int 0, 90, 180 or 270
 +       */
 +      public function getRotation( $file ) {
 +              return 0;
 +      }
 +
  }
diff --combined thumb.php
+++ b/thumb.php
@@@ -76,7 -76,7 +76,7 @@@ function wfThumbHandle404() 
                return;
        }
  
-       $params = wfExtractThumbParams( $matches['title'] ); // basic wiki URL param extracting
+       $params = wfExtractThumbRequestInfo( $matches['title'] ); // basic wiki URL param extracting
        if ( $params == null ) {
                wfThumbError( 400, 'The specified thumbnail parameters are not recognized.' );
                return;
  /**
   * Stream a thumbnail specified by parameters
   *
-  * @param $params Array
+  * @param $params Array List of thumbnailing parameters. In addition to parameters
+  *  passed to the MediaHandler, this may also includes the keys:
+  *   f (for filename), archived (if archived file), temp (if temp file),
+  *   w (alias for width), p (alias for page), r (ignored; historical),
+  *   rel404 (path for render on 404 to verify hash path correct),
+  *   thumbName (thumbnail name to potentially extract more parameters from
+  *   e.g. 'lossy-page1-120px-Foo.tiff' would add page, lossy and width
+  *   to the parameters)
   * @return void
   */
  function wfStreamThumb( array $params ) {
        $headers = array(); // HTTP headers to send
  
        $fileName = isset( $params['f'] ) ? $params['f'] : '';
-       unset( $params['f'] );
-       // Backwards compatibility parameters
-       if ( isset( $params['w'] ) ) {
-               $params['width'] = $params['w'];
-               unset( $params['w'] );
-       }
-       if ( isset( $params['p'] ) ) {
-               $params['page'] = $params['p'];
-       }
-       unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER
  
        // Is this a thumb of an archived file?
        $isOld = ( isset( $params['archived'] ) && $params['archived'] );
                $varyHeader[] = 'Cookie';
        }
  
+       // Do rendering parameters extraction from thumbnail name.
+       if ( isset( $params['thumbName'] ) ) {
+               $params = wfExtractThumbParams( $img, $params );
+       }
+       if ( $params == null ) {
+               wfThumbError( 400, 'The specified thumbnail parameters are not recognized.' );
+               return;
+       }
        // Check the source file storage path
        if ( !$img->exists() ) {
                $redirectedLocation = false;
                }
        }
  
+       // Backwards compatibility parameters
+       if ( isset( $params['w'] ) ) {
+               $params['width'] = $params['w'];
+               unset( $params['w'] );
+       }
+       if ( isset( $params['p'] ) ) {
+               $params['page'] = $params['p'];
+       }
+       unset( $params['r'] ); // ignore 'r' because we unconditionally pass File::RENDER
+       unset( $params['f'] ); // We're done with 'f' parameter.
        // Get the normalized thumbnail name from the parameters...
        try {
                $thumbName = $img->thumbName( $params );
  }
  
  /**
-  * Extract the required params for thumb.php from the thumbnail request URI.
-  * At least 'width' and 'f' should be set if the result is an array.
+  * Convert pathinfo type parameter, into normal request parameters
+  *
+  * So for example, if the request was redirected from
+  * /w/images/thumb/a/ab/Foo.png/120px-Foo.png. The $thumbRel parameter
+  * of this function would be set to "a/ab/Foo.png/120px-Foo.png".
+  * This method is responsible for turning that into an array
+  * with the folowing keys:
+  *  * f => the filename (Foo.png)
+  *  * rel404 => the whole thing (a/ab/Foo.png/120px-Foo.png)
+  *  * archived => 1 (If the request is for an archived thumb)
+  *  * temp => 1 (If the file is in the "temporary" zone)
+  *  * thumbName => the thumbnail name, including parameters (120px-Foo.png)
+  *
+  * Transform specific parameters are set later via wfExtractThumbParams().
   *
   * @param $thumbRel String Thumbnail path relative to the thumb zone
   * @return Array|null associative params array or null
   */
- function wfExtractThumbParams( $thumbRel ) {
+ function wfExtractThumbRequestInfo( $thumbRel ) {
        $repo = RepoGroup::singleton()->getLocalRepo();
  
        $hashDirReg = $subdirReg = '';
                $params['temp'] = 1;
        }
  
-       // Check hooks if parameters can be extracted
-       // Hooks return false if they manage to *resolve* the parameters
+       $params['thumbName'] = $thumbname;
+       return $params;
+ }
+ /**
+  * Convert a thumbnail name (122px-foo.png) to parameters, using
+  * file handler.
+  *
+  * @param File $file File object for file in question.
+  * @param $param Array Array of parameters so far.
+  * @return Array parameters array with more parameters.
+  */
+ function wfExtractThumbParams( $file, $params ) {
+       if ( !isset( $params['thumbName'] ) ) {
+               throw new MWException( "No thumbnail name passed to wfExtractThumbParams" );
+       }
+       $thumbname = $params['thumbName'];
+       unset( $params['thumbName'] );
+       // Do the hook first for older extensions that rely on it.
        if ( !wfRunHooks( 'ExtractThumbParameters', array( $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
+               wfDeprecated( 'ExtractThumbParameters', '1.22' );
                return $params; // valid thumbnail URL (via extension or config)
-       // Check if the parameters can be extracted from the thumbnail name...
-       } elseif ( preg_match( '!^(page(\d*)-)*(\d*)px-[^/]*$!', $thumbname, $matches ) ) {
+       }
+       // FIXME: Files in the temp zone don't set a mime type, which means
+       // they don't have a handler. Which means we can't parse the param
+       // string. However, not a big issue as what good is a param string
+       // if you have no handler to make use of the param string and
+       // actually generate the thumbnail.
+       $handler = $file->getHandler();
+       // Based on UploadStash::parseKey
+       $fileNamePos = strrpos( $thumbname, $params['f'] );
+       if ( $fileNamePos === false ) {
+               // Maybe using a short filename? (see FileRepo::nameForThumb)
+               $fileNamePos = strrpos( $thumbname, 'thumbnail' );
+       }
+       if ( $handler && $fileNamePos !== false ) {
+               $paramString = substr( $thumbname, 0, $fileNamePos - 1 );
+               $extraParams = $handler->parseParamString( $paramString );
+               if ( $handler !== false ) {
+                       return $params + $extraParams;
+               }
+       }
+       // 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;
                $params['width'] = $size;
                if ( $pagenum ) {
                }
                return $params; // valid thumbnail URL
        }
-       return null; // not a valid thumbnail URL
+       return null;
  }
  
  /**
@@@ -387,7 -463,6 +463,7 @@@ function wfThumbError( $status, $msg ) 
                header( 'HTTP/1.1 500 Internal server error' );
        }
        if ( $wgShowHostnames ) {
 +              header( 'X-MW-Thumbnail-Renderer: ' . wfHostname() );
                $url = htmlspecialchars( isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
                $hostname = htmlspecialchars( wfHostname() );
                $debug = "<!-- $url -->\n<!-- $hostname -->\n";