*Add BeforeGalleryFindFile, TitleLinkUpdatesAfterCompletion, BeforeParserFetchTemplat...
authorAaron Schulz <aaron@users.mediawiki.org>
Thu, 31 May 2007 16:01:26 +0000 (16:01 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Thu, 31 May 2007 16:01:26 +0000 (16:01 +0000)
includes/ImageGallery.php
includes/LinksUpdate.php
includes/OutputPage.php
includes/Parser.php
includes/ParserOutput.php

index 3e19dec..6045ad6 100644 (file)
@@ -17,6 +17,7 @@ class ImageGallery
        var $mImages, $mShowBytes, $mShowFilename;
        var $mCaption = false;
        var $mSkin = false;
+       var $mRevisionId = 0;
 
        /**
         * Is the gallery on a wiki page (i.e. not a special page)
@@ -201,8 +202,12 @@ class ImageGallery
                foreach ( $this->mImages as $pair ) {
                        $nt = $pair[0];
                        $text = $pair[1];
+                       
+                       # Give extensions a chance to select the file revision for us
+                       $time = false;
+                       wfRunHooks( 'BeforeGalleryFindFile', array( &$this, &$nt, &$time ) );
 
-                       $img = wfFindFile( $nt );
+                       $img = wfFindFile( $nt, $time );
 
                        if( $nt->getNamespace() != NS_IMAGE || !$img ) {
                                # We're dealing with a non-image, spit out the name and be done with it.
index 856c665..3c01da9 100644 (file)
@@ -76,6 +76,7 @@ class LinksUpdate {
                } else {
                        $this->doIncrementalUpdate();
                }
+               wfRunHooks( 'TitleLinkUpdatesAfterCompletion', array( &$this->mTitle ) );
        }
 
        function doIncrementalUpdate() {
index ef1e272..c86180a 100644 (file)
@@ -54,6 +54,8 @@ class OutputPage {
                $this->mETag = false;
                $this->mRevisionId = null;
                $this->mNewSectionLink = false;
+               $this->mTemplateIds = array();
+               $this->mImageTimestamps = array();
        }
        
        public function redirect( $url, $responsecode = '302' ) {
@@ -384,6 +386,10 @@ class OutputPage {
                }
                $this->mNoGallery = $parserOutput->getNoGallery();
                $this->mHeadItems = array_merge( $this->mHeadItems, (array)$parserOutput->mHeadItems );
+               // Versioning...
+               $this->mTemplateIds += (array)$parserOutput->mTemplateIds;
+               $this->mImageTimestamps += (array)$parserOutput->mImageTimestamps;
+               
                wfRunHooks( 'OutputPageParserOutput', array( &$this, $parserOutput ) );
        }
 
index dbd8f03..4ad8305 100644 (file)
@@ -400,12 +400,15 @@ class Parser
         * Expand templates and variables in the text, producing valid, static wikitext.
         * Also removes comments.
         */
-       function preprocess( $text, $title, $options ) {
+       function preprocess( $text, $title, $options, $revid = null ) {
                wfProfileIn( __METHOD__ );
                $this->clearState();
                $this->setOutputType( OT_PREPROCESS );
                $this->mOptions = $options;
                $this->mTitle = $title;
+               if( $revid !== null ) {
+                       $this->mRevisionId = $revid;
+               }
                wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$this->mStripState ) );
                $text = $this->strip( $text, $this->mStripState );
                wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState ) );
@@ -3264,13 +3267,25 @@ class Parser
         * Fetch the unparsed text of a template and register a reference to it.
         */
        function fetchTemplateAndtitle( $title ) {
-               $text = false;
+               $text = $skip = false;
                $finalTitle = $title;
                // Loop to fetch the article, with up to 1 redirect
                for ( $i = 0; $i < 2 && is_object( $title ); $i++ ) {
-                       $rev = Revision::newFromTitle( $title );
-                       $this->mOutput->addTemplate( $title, $title->getArticleID() );
-                       if ( $rev ) {
+                       # Give extensions a chance to select the revision instead
+                       $id = false; // Assume current
+                       wfRunHooks( 'BeforeParserFetchTemplateAndtitle', array( &$this, &$title, &$skip, &$id ) );
+                       
+                       if( $skip ) {
+                               $text = false;
+                               $this->mOutput->addTemplate( $title, $title->getArticleID(), 0 );
+                               break;
+                       }
+                       $rev = $id ? Revision::newFromId( $id ) : Revision::newFromTitle( $title );
+                       $rev_id = $rev ? $rev->getId() : 0;
+                       
+                       $this->mOutput->addTemplate( $title, $title->getArticleID(), $rev_id );
+                       
+                       if( $rev ) {
                                $text = $rev->getText();
                        } elseif( $title->getNamespace() == NS_MEDIAWIKI ) {
                                global $wgLang;
@@ -4124,7 +4139,7 @@ class Parser
                                }
 
                                // process categories, check if a category exists in some variant
-                               foreach( $categories as $category){
+                               foreach( $categories as $category ){
                                        $variants = $wgContLang->convertLinkToAllVariants($category);
                                        foreach($variants as $variant){
                                                if($variant != $category){
@@ -4346,6 +4361,7 @@ class Parser
                $ig->setShowFilename( false );
                $ig->setParsing();
                $ig->useSkin( $this->mOptions->getSkin() );
+               $ig->mRevisionId = $this->mRevisionId;
 
                if( isset( $params['caption'] ) ) {
                        $caption = $params['caption'];
@@ -4362,6 +4378,8 @@ class Parser
                if( isset( $params['heights'] ) ) {
                        $ig->setHeights( $params['heights'] );
                }
+               
+               wfRunHooks( 'BeforeParserrenderImageGallery', array( &$this, &$ig ) );
 
                $lines = explode( "\n", $text );
                foreach ( $lines as $line ) {
@@ -4510,8 +4528,18 @@ class Parser
                $alt = $this->mStripState->unstripBoth( $alt );
                $alt = Sanitizer::stripAllTags( $alt );
 
+               # Give extensions a chance to select the file revision for us
+               $skip = $time = false;
+               wfRunHooks( 'BeforeParserMakeImageLinkObj', array( &$this, &$nt, &$skip, &$time ) );
+
                # Linker does the rest
-               return $sk->makeImageLinkObj( $nt, $caption, $alt, $align, $params, $framed, $thumb, $manual_thumb, $valign );
+               if( $skip ) {
+                       $link = $sk->makeLinkObj( $nt );
+               } else {
+                       $link = $sk->makeImageLinkObj( $nt, $caption, $alt, $align, $params, $framed, $thumb, $manual_thumb, $valign, $time );
+               }
+               
+               return $link;
        }
 
        /**
index 03f1819..c13c325 100644 (file)
@@ -14,7 +14,9 @@ class ParserOutput
                $mTitleText,        # title text of the chosen language variant
                $mLinks,            # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
                $mTemplates,        # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
+               $mTemplateIds,      # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
                $mImages,           # DB keys of the images used, in the array key only
+               $mImageTimestamps,  # Map of DBK to rev ID for the template references. ID=zero for broken.
                $mExternalLinks,    # External link URLs, in the key only
                $mHTMLtitle,        # Display HTML title
                $mSubtitle,         # Additional subtitle
@@ -41,10 +43,12 @@ class ParserOutput
                $this->mNewSection = false;
                $this->mNoGallery = false;
                $this->mHeadItems = array();
+               $this->mTemplateIds = array();
+               $this->mImageTimestamps = array();
        }
 
        function getText()                   { return $this->mText; }
-       function &getLanguageLinks()          { return $this->mLanguageLinks; }
+       function &getLanguageLinks()         { return $this->mLanguageLinks; }
        function getCategoryLinks()          { return array_keys( $this->mCategories ); }
        function &getCategories()            { return $this->mCategories; }
        function getCacheTime()              { return $this->mCacheTime; }
@@ -66,7 +70,6 @@ class ParserOutput
        function setSubtitle( $st )          { return wfSetVar( $this->mSubtitle, $st ); }
 
        function addCategory( $c, $sort )    { $this->mCategories[$c] = $sort; }
-       function addImage( $name )           { $this->mImages[$name] = 1; }
        function addLanguageLink( $t )       { $this->mLanguageLinks[] = $t; }
        function addExternalLink( $url )     { $this->mExternalLinks[$url] = 1; }
 
@@ -88,14 +91,33 @@ class ParserOutput
                }
                $this->mLinks[$ns][$dbk] = $id;
        }
+       
+       function addImage( $name, $timestamp=NULL ) {
+               if( isset($this->mImages[$name]) ) 
+                       return; // No repeated pointless DB calls!
+               $this->mImages[$name] = 1;
+               if( is_null($timestamp) ) {
+                       wfProfileIn( __METHOD__ );
+                       $dbr = wfGetDB(DB_SLAVE);
+                       $timestamp = $dbr->selectField('image', 'img_timestamp',
+                               array('img_name' => $name),
+                               __METHOD__ );
+               }
+               $timestamp = $timestamp ? $timestamp : 0;
+               $this->mImageTimestamps[$name] = $timestamp; // For versioning
+       }
 
-       function addTemplate( $title, $id ) {
+       function addTemplate( $title, $page_id, $rev_id ) {
                $ns = $title->getNamespace();
                $dbk = $title->getDBkey();
                if ( !isset( $this->mTemplates[$ns] ) ) {
                        $this->mTemplates[$ns] = array();
                }
-               $this->mTemplates[$ns][$dbk] = $id;
+               $this->mTemplates[$ns][$dbk] = $page_id;
+               if ( !isset( $this->mTemplateIds[$ns] ) ) {
+                       $this->mTemplateIds[$ns] = array();
+               }
+               $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
        }
 
        /**