(bug 6092) Add parser function equivalents of {{REVISIONID}}, {{REVISIONTIMESTAMP...
authorRoan Kattouw <catrope@users.mediawiki.org>
Thu, 16 Apr 2009 21:46:49 +0000 (21:46 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Thu, 16 Apr 2009 21:46:49 +0000 (21:46 +0000)
RELEASE-NOTES
includes/parser/CoreParserFunctions.php

index 44c8a48..7e6c866 100644 (file)
@@ -165,6 +165,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
   to allow extensions to update caches in similar way as MediaWiki invalidates
   a cached MonoBook sidebar
 * Special:AllPages: Move hardcoded styles from code to CSS
+* (bug 6092) Add parser function equivalents of {{REVISIONID}},
+  {{REVISIONTIMESTAMP}}  (and friends) and {{REVISIONUSER}} magic words
 
 === Bug fixes in 1.15 ===
 * (bug 16968) Special:Upload no longer throws useless warnings.
index fb50b46..8f528ec 100644 (file)
@@ -66,6 +66,13 @@ class CoreParserFunctions {
                $parser->setFunctionHook( 'talkpagenamee',    array( __CLASS__, 'talkpagenamee'    ), SFH_NO_HASH );
                $parser->setFunctionHook( 'subjectpagename',  array( __CLASS__, 'subjectpagename'  ), SFH_NO_HASH );
                $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionid',       array( __CLASS__, 'revisionid'       ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisiontimestamp',array( __CLASS__, 'revisiontimestamp'), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionday',      array( __CLASS__, 'revisionday'      ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionday2',     array( __CLASS__, 'revisionday2'     ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionmonth',    array( __CLASS__, 'revisionmonth'    ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionyear',     array( __CLASS__, 'revisionyear'     ), SFH_NO_HASH );
+               $parser->setFunctionHook( 'revisionuser',     array( __CLASS__, 'revisionuser'     ), SFH_NO_HASH );
                $parser->setFunctionHook( 'tag',              array( __CLASS__, 'tagObj'           ), SFH_OBJECT_ARGS );
                $parser->setFunctionHook( 'formatdate',           array( __CLASS__, 'formatDate'           ) );
                $parser->setFunctionHook( 'groupconvert',         array( __CLASS__, 'groupconvert'         ), SFH_NO_HASH );
@@ -411,6 +418,90 @@ class CoreParserFunctions {
                        return '';
                return $t->getSubjectPage()->getPrefixedUrl();
        }
+       /*
+        * Functions to get revision informations, corresponding to the magic words
+        * of the same names
+        */
+       static function revisionid( $parser, $title = null ) {
+               static $cache = array ();
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) )
+                       return '';
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       // Let the edit saving system know we should parse the page
+                       // *after* a revision ID has been assigned.
+                       $parser->mOutput->setFlag( 'vary-revision' );
+                       wfDebug( __METHOD__ . ": {{REVISIONID}} used, setting vary-revision...\n" );
+                       return $parser->getRevisionId();
+               }
+               if ( isset( $cache[$t->getPrefixedText()] ) )
+                       return $cache[$t->getPrefixedText()];
+               elseif ( $parser->incrementExpensiveFunctionCount() ) {
+                       $a = new Article( $t );
+                       return $cache[$t->getPrefixedText()] = $a->getRevIdFetched();
+               }
+               return '';
+       }
+       static function revisiontimestamp( $parser, $title = null ) {
+               static $cache = array ();
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) )
+                       return '';
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       // Let the edit saving system know we should parse the page
+                       // *after* a revision ID has been assigned. This is for null edits.
+                       $parser->mOutput->setFlag( 'vary-revision' );
+                       wfDebug( __METHOD__ . ": {{REVISIONTIMESTAMP}} or related parser function used, setting vary-revision...\n" );
+                       return $parser->getRevisionTimestamp();
+               }
+               if ( isset( $cache[$t->getPrefixedText()] ) )
+                       return $cache[$t->getPrefixedText()];
+               elseif ( $parser->incrementExpensiveFunctionCount() ) {
+                       $a = new Article( $t );
+                       return $cache[$t->getPrefixedText()] = $a->getTimestamp();
+               }
+               return '';
+       }
+       static function revisionday( $parser, $title = null ) {
+               $timestamp = self::revisiontimestamp( $parser, $title );
+               if ( $timestamp == '' ) return '';
+               return intval( substr( $timestamp, 6, 2 ) );
+       }
+       static function revisionday2( $parser, $title = null ) {
+               $timestamp = self::revisiontimestamp( $parser, $title );
+               if ( $timestamp == '' ) return '';
+               return substr( $timestamp, 6, 2 );
+       }
+       static function revisionmonth( $parser, $title = null ) {
+               $timestamp = self::revisiontimestamp( $parser, $title );
+               if ( $timestamp == '' ) return '';
+               return intval( substr( $timestamp, 4, 2 ) );
+       }
+       static function revisionyear( $parser, $title = null ) {
+               $timestamp = self::revisiontimestamp( $parser, $title );
+               if ( $timestamp == '' ) return '';
+               return substr( $timestamp, 0, 4 );
+       }
+       static function revisionuser( $parser, $title = null ) {
+               static $cache = array();
+               $t = Title::newFromText( $title );
+               if ( is_null( $t ) )
+                       return '';
+               if ( $t->equals( $parser->getTitle() ) ) {
+                       // Let the edit saving system know we should parse the page
+                       // *after* a revision ID has been assigned. This is for null edits.
+                       $parser->mOutput->setFlag( 'vary-revision' );
+                       wfDebug( __METHOD__ . ": {{REVISIONUSER}} used, setting vary-revision...\n" );
+                       return $parser->getRevisionUser();
+               }
+               if ( isset( $cache[$t->getPrefixedText()] ) )
+                       return $cache[$t->getPrefixedText()];
+               elseif ( $parser->incrementExpensiveFunctionCount() ) {
+                       $a = new Article( $t );
+                       return $cache[$t->getPrefixedText()] = $a->getUserText();
+               }
+               return '';
+       }
        
        /**
         * Return the number of pages in the given category, or 0 if it's nonexis-