r25825 - Code Review

From MediaWiki.org

Jump to: navigation, search
Repository:MediaWiki
Revision:r25824 | r25825 (on ViewVC) | r25826 >
Date:10:23, 13 September 2007
Author:daniel
Status:ok
Tags:
Comment:support parser-function syntax
Modified paths:

Diff [purge]

Index: trunk/extensions/CategoryTree/CategoryTree.php
===================================================================
--- trunk/extensions/CategoryTree/CategoryTree.php	(revision 25824)
+++ trunk/extensions/CategoryTree/CategoryTree.php	(revision 25825)
@@ -81,7 +81,7 @@
 $wgHooks['OutputPageParserOutput'][] = 'efCategoryTreeParserOutput';
 $wgHooks['LoadAllMessages'][] = 'efInjectCategoryTreeMessages';
 $wgHooks['ArticleFromTitle'][] = 'efCategoryTreeArticleFromTitle';
-
+$wgHooks['LanguageGetMagic'][] = 'efCategoryTreeGetMagic';
 /**
  * register Ajax function
  */
@@ -99,10 +99,27 @@
 		return;
 	}
 
-	if ( $wgCategoryTreeAllowTag ) $wgParser->setHook( 'categorytree' , 'efCategoryTreeParserHook' );
+	if ( $wgCategoryTreeAllowTag ) {
+		$wgParser->setHook( 'categorytree' , 'efCategoryTreeParserHook' );
+		$wgParser->setFunctionHook( 'categorytree' , 'efCategoryTreeParserFunction' );
+	}
 }
 
 /**
+* Hook magic word
+*/
+function efCategoryTreeGetMagic( &$magicWords, $langCode ) {
+	global $wgUseAjax, $wgCategoryTreeAllowTag;
+
+	if ( $wgUseAjax && $wgCategoryTreeAllowTag ) {
+		//XXX: should we allow a local alias?
+		$magicWords['categorytree'] = array( 0, 'categorytree' );
+	}
+
+	return true;
+}
+
+/**
  * Entry point for Ajax, registered in $wgAjaxExportList.
  * This loads CategoryTreeFunctions.php and calls CategoryTree::ajax()
  */
@@ -164,6 +181,38 @@
 }
 
 /**
+ * Entry point for the {{#categorytree}} tag parser function.
+ * This is a wrapper around efCategoryTreeParserHook
+ */
+function efCategoryTreeParserFunction( &$parser ) {
+	$params = func_get_args();
+	array_shift( $params ); //first is &$parser, strip it
+
+	//first user-supplied parameter must be category name
+	if ( !$params ) return ''; //no category specified, return nothing
+	$cat = array_shift( $params );
+
+	//build associative arguments from flat parameter list
+	$argv = array();
+	foreach ( $params as $p ) {
+		if ( preg_match('/^\s*(\S.*?)\s*=\s*(.*?)\s*$/', $p, $m) ) {
+			$k = $m[1];
+			$v = $m[2];
+		}
+		else {
+			$k = trim($p);
+			$v = true;
+		}
+
+		$argv[$k] = $v;
+	}
+
+	//now handle just like a <categorytree> tag
+	$html = efCategoryTreeParserHook( $cat, $argv, $parser );
+	return array( $html, 'noargs' => true, 'noparse' => true ); //XXX: isHTML would be right logically, but it causes extra blank lines
+}
+
+/**
  * Entry point for the <categorytree> tag parser hook.
  * This loads CategoryTreeFunctions.php and calls CategoryTree::getTag()
  */
Index: trunk/extensions/CategoryTree/README
===================================================================
--- trunk/extensions/CategoryTree/README	(revision 25824)
+++ trunk/extensions/CategoryTree/README	(revision 25825)
@@ -48,9 +48,9 @@
 parsing the HTML of category pages.
 
 The custom tag is called <categorytree>. For example, if you put
-<categorytree>Foo</categorytree> on a wiki page, it will show the contents
-of category Foo as a dynamic tree on that page. The tag accepts the following
-attributes, using a HTML-like syntax:
+<categorytree mode="pages">Foo</categorytree> on a wiki page, it will show
+the contents of category Foo as a dynamic tree on that page. The tag accepts
+the following attributes, using a HTML-like syntax:
 
 * hideroot - set this to "on" to hide the "root" node of the tree, i.e.
              the mention of category Foo from the example.
@@ -64,7 +64,11 @@
          
 * style - can be used to specify any CSS styles you would like for the
           tree.
-          
+
+Alternatively to the <categorytree> tag, parser-function syntax can also be
+used, e.g {{#categorytree:Foo|hideroot=yes}}. This syntax allows the use of
+magic variables, templates and template parameters for the category name.
+
 The special page is called Special:CategoryTree; there you can enter the
 name of a category and then browse it's content. The CategoryTree
 extension also adds a tab for this special page to every category page.
Views
Toolbox