From 17551136ff7b1d0bc1b8c600b67b6c1726e000a7 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 31 May 2007 16:01:26 +0000 Subject: [PATCH] *Add BeforeGalleryFindFile, TitleLinkUpdatesAfterCompletion, BeforeParserFetchTemplateAndtitle, BeforeParserMakeImageLinkObj, BeforeParserrenderImageGallery; make parser outputs and output page record images -> timestamps used and templates -> revision ids --- includes/ImageGallery.php | 7 ++++++- includes/LinksUpdate.php | 1 + includes/OutputPage.php | 6 ++++++ includes/Parser.php | 42 ++++++++++++++++++++++++++++++++------- includes/ParserOutput.php | 30 ++++++++++++++++++++++++---- 5 files changed, 74 insertions(+), 12 deletions(-) diff --git a/includes/ImageGallery.php b/includes/ImageGallery.php index 3e19dec276..6045ad682a 100644 --- a/includes/ImageGallery.php +++ b/includes/ImageGallery.php @@ -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. diff --git a/includes/LinksUpdate.php b/includes/LinksUpdate.php index 856c665dd5..3c01da97bb 100644 --- a/includes/LinksUpdate.php +++ b/includes/LinksUpdate.php @@ -76,6 +76,7 @@ class LinksUpdate { } else { $this->doIncrementalUpdate(); } + wfRunHooks( 'TitleLinkUpdatesAfterCompletion', array( &$this->mTitle ) ); } function doIncrementalUpdate() { diff --git a/includes/OutputPage.php b/includes/OutputPage.php index ef1e272769..c86180aa32 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -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 ) ); } diff --git a/includes/Parser.php b/includes/Parser.php index dbd8f0334b..4ad83058f1 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -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; } /** diff --git a/includes/ParserOutput.php b/includes/ParserOutput.php index 03f1819c80..c13c325c30 100644 --- a/includes/ParserOutput.php +++ b/includes/ParserOutput.php @@ -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 } /** -- 2.20.1