r49575 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r49574 | r49575 (on ViewVC) | r49576 >
Date:21:46, 16 April 2009
Author:catrope
Status:reverted (Comments)
Tags:
Comment:(bug 6092) Add parser function equivalents of {{REVISIONID}}, {{REVISIONTIMESTAMP}} (and friends) and {{REVISIONUSER}} magic words. These parser functions are marked as expensive and cache their results
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/parser/CoreParserFunctions.php
===================================================================
--- trunk/phase3/includes/parser/CoreParserFunctions.php	(revision 49574)
+++ trunk/phase3/includes/parser/CoreParserFunctions.php	(revision 49575)
@@ -66,6 +66,13 @@
 		$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 @@
 			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-
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 49574)
+++ trunk/phase3/RELEASE-NOTES	(revision 49575)
@@ -165,6 +165,8 @@
   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.

Follow-up revisions

RevisionCommit summaryAuthorDate
r51424Reverted r49575, {{revisionid:...}} due to unresolved CR comments and the genera...tstarling17:09, 3 June 2009

Comments

#Comment by Melancholie (Talk | contribs)   05:31, 17 April 2009

Is there now a 38.107.191.108 magic word + parser function, or does the magic word just get "imported"? See https://bugzilla.wikimedia.org/show_bug.cgi?id=6092#c23

#Comment by Catrope (Talk | contribs)   09:39, 17 April 2009

See the bug, comment 24.

#Comment by Happy-melon (Talk | contribs)   13:25, 20 April 2009

What have I got to do with it?? :D

#Comment by Melancholie (Talk | contribs)   05:32, 17 April 2009

{{REVISIONUSER}} (<nowiki> ;-)

#Comment by Spidern (Talk | contribs)   03:06, 21 April 2009

Oh 38.107.191.108, you silly person you.

#Comment by Zzyzx11 (Talk | contribs)   03:12, 21 April 2009

How in the world did I get into this conversation?

#Comment by Zzyzx11 (Talk | contribs)   03:24, 21 April 2009

I mean 38.107.191.108 is way better than me... especially when that user saved the entire Wikipedia on 20091122042542 :D

#Comment by Voice of All (Talk | contribs)   07:02, 21 April 2009
#Comment by Brion VIBBER (Talk | contribs)   00:31, 29 April 2009

There's a lot of code duplication here; I'd recommend refactoring the code a bit so it's easier to maintain.

#Comment by Kronoxt (Talk | contribs)   01:19, 15 May 2009

Are you talking about me?? Hoe I got implicated in that... anyways.

#Comment by Kronoxt (Talk | contribs)   02:12, 15 May 2009

&EDIT : **How I got involved in that

#Comment by Shinjiman (Talk | contribs)   15:29, 1 June 2009

Me too, how I got involved in that?

#Comment by Splarka (Talk | contribs)   08:19, 30 May 2009

Hey, Someone should make sure this can't be used as Template:CURRENTUSER by giving an invalid/bad/deleted/hidden title.

#Comment by Capmo (Talk | contribs)   07:08, 4 June 2009

Hey, it took me 20min to understand why you were all talking about me, lol! Especially 38.107.191.108, you naughty boy!

#Comment by Pinky (Talk | contribs)   01:27, 16 June 2009

test ~~~

Status & tagging log

Views
Toolbox