* Put parser output file version tracking to core
authorAaron Schulz <aaron@users.mediawiki.org>
Wed, 23 Mar 2011 17:35:40 +0000 (17:35 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Wed, 23 Mar 2011 17:35:40 +0000 (17:35 +0000)
* Added some ParserOutput accessors
* A few cleanups to fetchFile()

includes/ImageGallery.php
includes/OutputPage.php
includes/parser/Parser.php
includes/parser/ParserOutput.php

index 8c13d25..ce4404b 100644 (file)
@@ -257,11 +257,8 @@ class ImageGallery
                                $time = $sha1 = $descQuery = false;
                                wfRunHooks( 'BeforeGalleryFindFile',
                                        array( &$this, &$nt, &$time, &$descQuery, &$sha1 ) );
-                               # Get the file and register it
-                               $img = $this->mParser->fetchFile( $nt, $time, $sha1 );
-                               if ( $img ) {
-                                       $nt = $img->getTitle(); // file title may be different (via hooks)
-                               }
+                               # Fetch and register the file (file title may be different via hooks)
+                               list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $time, $sha1 );
                        } else {
                                $img = false;
                        }
index 60533c5..de5b152 100644 (file)
@@ -123,6 +123,7 @@ class OutputPage {
        var $mInlineMsg = array();
 
        var $mTemplateIds = array();
+       var $mImageTimeKeys = array();
 
        # What level of 'untrustworthiness' is allowed in CSS/JS modules loaded on this page?
        # @see ResourceLoaderModule::$origin
@@ -1308,14 +1309,19 @@ class OutputPage {
                $this->mNoGallery = $parserOutput->getNoGallery();
                $this->mHeadItems = array_merge( $this->mHeadItems, $parserOutput->getHeadItems() );
                $this->addModules( $parserOutput->getModules() );
-               // Versioning...
-               foreach ( (array)$parserOutput->mTemplateIds as $ns => $dbks ) {
+
+               // Template versioning...
+               foreach ( (array)$parserOutput->getTemplateIds() as $ns => $dbks ) {
                        if ( isset( $this->mTemplateIds[$ns] ) ) {
                                $this->mTemplateIds[$ns] = $dbks + $this->mTemplateIds[$ns];
                        } else {
                                $this->mTemplateIds[$ns] = $dbks;
                        }
                }
+               // File versioning...
+               foreach ( (array)$parserOutput->getImageTimeKeys() as $dbk => $data ) {
+                       $this->mImageTimeKeys[$dbk] = $data;
+               }
 
                // Hooks registered in the object
                global $wgParserOutputHooks;
index 4ed6337..1c2d635 100644 (file)
@@ -53,7 +53,7 @@ class Parser {
         * changes in an incompatible way, so the parser cache
         * can automatically discard old data.
         */
-       const VERSION = '1.6.4';
+       const VERSION = '1.6.5';
 
        /**
         * Update this version number when the output of serialiseHalfParsedText()
@@ -1869,7 +1869,6 @@ class Parser {
                                        }
                                        wfProfileOut( __METHOD__."-image" );
                                        continue;
-
                                }
 
                                if ( $ns == NS_CATEGORY ) {
@@ -1914,14 +1913,11 @@ class Parser {
                                wfRunHooks( 'BeforeParserMakeImageLinkObj',
                                        array( &$this, &$nt, &$skip, &$time, &$descQuery, &$sha1 ) );
                                if ( $skip ) {
-                                       $this->mOutput->addImage( $nt->getDBkey() ); // register
+                                       $this->mOutput->addImage( $nt->getDBkey(), null, null ); // register
                                        $link = $sk->link( $nt );
                                } else {
-                                       # Fetch and register the file
-                                       $file = $this->fetchFile( $nt, $time, $sha1 );
-                                       if ( $file ) {
-                                               $nt = $file->getTitle(); // file title may be different (via hooks)
-                                       }
+                                       # Fetch and register the file (file title may be different via hooks)
+                                       list( $file, $nt ) = $this->fetchFileAndTitle( $nt, $time, $sha1 );
                                        $link = $sk->makeMediaLinkFile( $nt, $file, $text );
                                }
                                # Cloak with NOPARSE to avoid replacement in replaceExternalLinks
@@ -3314,6 +3310,8 @@ class Parser {
 
        /**
         * Fetch the unparsed text of a template and register a reference to it.
+        * @param Title $title
+        * @return Array ( string or false, Title )
         */
        function fetchTemplateAndTitle( $title ) {
                $templateCb = $this->mOptions->getTemplateCallback(); # Defaults to Parser::statelessFetchTemplate()
@@ -3328,6 +3326,11 @@ class Parser {
                return array( $text, $finalTitle );
        }
 
+       /**
+        * Fetch the unparsed text of a template and register a reference to it.
+        * @param Title $title
+        * @return mixed string or false
+        */
        function fetchTemplate( $title ) {
                $rv = $this->fetchTemplateAndTitle( $title );
                return $rv[0];
@@ -3398,11 +3401,28 @@ class Parser {
        }
 
        /**
-        * Fetch a file and register a reference to it.
-        * @TODO: register and track file version info too
+        * Fetch a file and its title and register a reference to it.
+        * @param Title $title
+        * @param string $time MW timestamp
+        * @param string $sha1 base 36 SHA-1
+        * @return mixed File or false
         */
        function fetchFile( $title, $time = false, $sha1 = false ) {
-               if ( $sha1 ) { // get by (sha1,timestamp)
+               $res = $this->fetchFileAndTitle( $title, $time, $sha1 );
+               return $res[0];
+       }
+
+       /**
+        * Fetch a file and its title and register a reference to it.
+        * @param Title $title
+        * @param string $time MW timestamp
+        * @param string $sha1 base 36 SHA-1
+        * @return Array ( File or false, Title )
+        */
+       function fetchFileAndTitle( $title, $time = false, $sha1 = false ) {
+               if ( $time === '0' ) {
+                       $file = false; // broken thumbnail forced by hook
+               } elseif ( $sha1 ) { // get by (sha1,timestamp)
                        $file = RepoGroup::singleton()->findFileFromKey( $sha1, array( 'time' => $time ) );
                        if ( $file ) {
                                $title = $file->getTitle(); // file title may not match $title
@@ -3410,8 +3430,12 @@ class Parser {
                } else { // get by (name,timestamp)
                        $file = wfFindFile( $title, array( 'time' => $time ) );
                }
-               $this->mOutput->addImage( $title->getDBkey() );
-               return $file;
+               # Register the file as a dependancy
+               $time = $file ? $file->getTimestamp() : null;
+               $sha1 = $file ? $file->getSha1() : null;
+               $this->mOutput->addImage( $title->getDBkey(), $time, $sha1 );
+
+               return array( $file, $title );  
        }
 
        /**
@@ -4670,14 +4694,11 @@ class Parser {
                wfRunHooks( 'BeforeParserMakeImageLinkObj',
                        array( &$this, &$title, &$skip, &$time, &$descQuery, &$sha1 ) );
                if ( $skip ) {
-                       $this->mOutput->addImage( $title->getDBkey() ); // register
+                       $this->mOutput->addImage( $title->getDBkey(), null, null ); // register
                        return $sk->link( $title );
                }
-               # Fetch and register the file
-               $file = $this->fetchFile( $title, $time, $sha1 );
-               if ( $file ) {
-                       $title = $file->getTitle(); // file title may be different (via hooks)
-               }
+               # Fetch and register the file (file title may be different via hooks)
+               list( $file, $title ) = $this->fetchFileAndTitle( $title, $time, $sha1 );
                # Get parameter map
                $handler = $file ? $file->getHandler() : false;
 
index 401b309..95ae4ad 100644 (file)
@@ -109,6 +109,7 @@ class ParserOutput extends CacheTime {
                $mTemplates = array(),        # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
                $mTemplateIds = array(),      # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
                $mImages = array(),           # DB keys of the images used, in the array key only
+               $mImageTimeKeys = array(),        # DB keys of the images used mapped to sha1 and MW timestamp
                $mExternalLinks = array(),    # External link URLs, in the key only
                $mInterwikiLinks = array(),   # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
                $mNewSection = false,         # Show a new section link?
@@ -174,7 +175,9 @@ class ParserOutput extends CacheTime {
        function getEditSectionTokens()      { return $this->mEditSectionTokens; }
        function &getLinks()                 { return $this->mLinks; }
        function &getTemplates()             { return $this->mTemplates; }
+       function &getTemplateIds()           { return $this->mTemplateIds; }
        function &getImages()                { return $this->mImages; }
+       function &getImageTimeKeys()         { return $this->mImageTimeKeys; }
        function &getExternalLinks()         { return $this->mExternalLinks; }
        function getNoGallery()              { return $this->mNoGallery; }
        function getHeadItems()              { return $this->mHeadItems; }
@@ -256,14 +259,21 @@ class ParserOutput extends CacheTime {
                $this->mLinks[$ns][$dbk] = $id;
        }
 
-       function addImage( $name ) {
+       /**
+        * @param $name string Title dbKey
+        * @param $timestamp string MW timestamp of file creation
+        * @param $sha string base 36 SHA-1 of file
+        * @return void
+        */
+       function addImage( $name, $timestamp, $sha1 ) {
                $this->mImages[$name] = 1;
+               $this->mImageTimeKeys[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
        }
 
        /**
         * @param $title Title
-        * @param  $page_id
-        * @param  $rev_id
+        * @param $page_id
+        * @param $rev_id
         * @return void
         */
        function addTemplate( $title, $page_id, $rev_id ) {