r46630 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r46629 | r46630 (on ViewVC) | r46631 >
Date:01:35, 31 January 2009
Author:mrzman
Status:ok (Comments)
Tags:
Comment:Allow the {{NAMESPACE}}, {{TALKSPACE}}, and {{SUBJECTSPACE}} magic words (and their urlencoding versions) to be used as parser functions to return the desired namespace for a given title.
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/parser/CoreParserFunctions.php
===================================================================
--- trunk/phase3/includes/parser/CoreParserFunctions.php	(revision 46629)
+++ trunk/phase3/includes/parser/CoreParserFunctions.php	(revision 46630)
@@ -47,6 +47,12 @@
 		$parser->setFunctionHook( 'pagesincategory',  array( __CLASS__, 'pagesincategory'  ), SFH_NO_HASH );
 		$parser->setFunctionHook( 'pagesize',         array( __CLASS__, 'pagesize'         ), SFH_NO_HASH );
 		$parser->setFunctionHook( 'protectionlevel',  array( __CLASS__, 'protectionlevel'  ), SFH_NO_HASH );
+		$parser->setFunctionHook( 'namespace',        array( __CLASS__, 'namespace'        ), SFH_NO_HASH );
+		$parser->setFunctionHook( 'namespacee',       array( __CLASS__, 'namespacee'       ), SFH_NO_HASH );
+		$parser->setFunctionHook( 'talkspace',        array( __CLASS__, 'talkspace'        ), SFH_NO_HASH );
+		$parser->setFunctionHook( 'talkspacee',       array( __CLASS__, 'talkspacee'       ), SFH_NO_HASH );
+		$parser->setFunctionHook( 'subjectspace',     array( __CLASS__, 'subjectspace'     ), SFH_NO_HASH );
+		$parser->setFunctionHook( 'subjectspacee',    array( __CLASS__, 'subjectspacee'    ), SFH_NO_HASH );
 		$parser->setFunctionHook( 'tag',              array( __CLASS__, 'tagObj'           ), SFH_OBJECT_ARGS );
 
 		if ( $wgAllowDisplayTitle ) {
@@ -248,6 +254,48 @@
 		return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
 	} 
 
+	
+	/*
+	* Given a title, return the namespace name that would be given by the
+	* corresponding magic word 
+	*/
+	static function namespace( $parser, $title = null ) {
+		$t = Title::newFromText( $title );
+		if ( is_null($t) )
+			return '';
+		return str_replace( '_', ' ', $t->getNsText() );
+	}
+	static function namespacee( $parser, $title = null ) {
+		$t = Title::newFromText( $title );
+		if ( is_null($t) )
+			return '';
+		return wfUrlencode( $t->getNsText() );
+	}
+	static function talkspace( $parser, $title = null ) {
+		$t = Title::newFromText( $title );
+		if ( is_null($t) || !$t->canTalk() )
+			return '';
+		return str_replace( '_', ' ', $t->getTalkNsText() );
+	}
+	static function talkspacee( $parser, $title = null ) {
+		$t = Title::newFromText( $title );
+		if ( is_null($t) || !$t->canTalk() )
+			return '';
+		return wfUrlencode( $t->getTalkNsText() );
+	}
+	static function subjectspace( $parser, $title = null ) {
+		$t = Title::newFromText( $title );
+		if ( is_null($t) )
+			return '';
+		return str_replace( '_', ' ', $t->getSubjectNsText() );
+	}
+	static function subjectspacee( $parser, $title = null ) {
+		$t = Title::newFromText( $title );
+		if ( is_null($t) )
+			return '';
+		return wfUrlencode( $t->getSubjectNsText() );
+	}
+	
 	/**
 	 * Return the number of pages in the given category, or 0 if it's nonexis-
 	 * tent.  This is an expensive parser function and can't be called too many
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 46629)
+++ trunk/phase3/RELEASE-NOTES	(revision 46630)
@@ -73,6 +73,8 @@
   "mw-confirmemail-pending"
 * Local redirects to foreign images are now displayed on the ImagePage when
   viewing on the local wiki.
+* The {{NAMESPACE}}, {{TALKSPACE}}, and {{SUBJECTSPACE}} magic words can now be
+  used as parser functions to return the desired namespace for a given title.
 
 === Bug fixes in 1.15 ===
 * (bug 16968) Special:Upload no longer throws useless warnings.

Follow-up revisions

RevisionCommit summaryAuthorDate
r46662(bug 8249) Followup to r46630, add parser function versions of the various PAGEN...mrzman22:25, 31 January 2009

Comments

#Comment by Simetrical (Talk | contribs)   23:45, 31 January 2009

I see no limitations here on the number of queries this could cause per page. Shouldn't this be limited like #ifexist and pagesincategory?

#Comment by Simetrical (Talk | contribs)   23:46, 31 January 2009

Wait, sorry, this shouldn't cause queries, of course. They never need be unstubbed.

#Comment by Werdna (Talk | contribs)   23:38, 11 February 2009

Don't these parser functions need to be localised and added to MagicWords.php?

See r46822 for an example of this...

#Comment by Mr.Z-man (Talk | contribs)   00:12, 12 February 2009

they're already there as magic words

#Comment by Werdna (Talk | contribs)   00:15, 12 February 2009

Oh, right.

Status & tagging log

  • 02:17, 18 February 2009 Brion VIBBER (Talk | contribs) changed the status of r46630 [removed: new added: ok]
  • 00:15, 12 February 2009 Werdna (Talk | contribs) changed the status of r46630 [removed: fixme added: new]
  • 23:38, 11 February 2009 Werdna (Talk | contribs) changed the status of r46630 [removed: new added: fixme]
  • 23:46, 31 January 2009 Simetrical (Talk | contribs) changed the status of r46630 [removed: fixme added: new]
  • 23:45, 31 January 2009 Simetrical (Talk | contribs) changed the status of r46630 [removed: new added: fixme]
Views
Toolbox