r103918 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103917‎ | r103918 | r103919 >
Date:16:29, 22 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
Is now supporting 'Parser Fun' extension + parser function functions have 'pf_' prefix now.
Modified paths:
  • /trunk/extensions/SubpageFun/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/SubpageFun/SFun_SubpageInfo.php (modified) (history)
  • /trunk/extensions/SubpageFun/SubpageFun.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SubpageFun/SFun_SubpageInfo.php
@@ -190,7 +190,7 @@
191191 *
192192 * @return string Result The subpage title of the given page. If page isn't a subpage, the Pages
193193 * name (without prefix) will be returned.
194 - */
 194+ */
195195 static function getSubpageTitle( Title $page ) {
196196 $parent = SubpageInfo::getParentPage( $page );
197197 //return the whole subpage name not like SUBPAGENAME only the last part after the last "/":
Index: trunk/extensions/SubpageFun/SubpageFun.php
@@ -8,7 +8,7 @@
99 * Support: http://www.mediawiki.org/wiki/Extension_talk:Subpage_Fun
1010 * Source code: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/SubpageFun
1111 *
12 - * @version: 0.5.1
 12+ * @version: 0.5.2
1313 * @license: ISC license
1414 * @author: Daniel Werner < danweetz@web.de >
1515 *
@@ -38,17 +38,18 @@
3939 $wgAutoloadClasses['SubpageInfo'] = ExtSubpageFun::getDir() . '/SFun_SubpageInfo.php';
4040
4141 // Register magic words:
42 -$wgHooks['ParserFirstCallInit' ][] = 'ExtSubpageFun::init';
43 -//$wgHooks['LanguageGetMagic' ][] = 'ExtSubpageFun::onLanguageGetMagic';
 42+$wgHooks['ParserFirstCallInit'][] = 'ExtSubpageFun::init';
4443
4544 // register plain variables:
4645 $wgHooks['MagicWordwgVariableIDs' ][] = 'ExtSubpageFun::onMagicWordwgVariableIDs';
4746 $wgHooks['ParserGetVariableValueSwitch'][] = 'ExtSubpageFun::onParserGetVariableValueSwitch';
4847
 48+// hook up for use with 'Parser Fun' extensions 'THIS' function:
 49+$wgHooks['GetThisVariableValueSwitch'][] = 'ExtSubpageFun::onGetThisVariableValueSwitch';
4950
5051 class ExtSubpageFun {
5152
52 - const VERSION = '0.5.1';
 53+ const VERSION = '0.5.2';
5354
5455 const MAG_SUBPAGETITLE = 'subpagetitle';
5556 const MAG_SUBPAGES = 'subpages';
@@ -58,15 +59,15 @@
5960 const MAG_NUMBEROFSUBPAGES = 'numberofsubpages';
6061 const MAG_TOPLEVELPAGE = 'toplevelpage';
6162
62 - static function init( &$parser ) {
 63+ static function init( Parser &$parser ) {
6364 // optional SFH_NO_HASH to omit the hash '#' from function names
64 - $parser->setFunctionHook( self::MAG_SUBPAGETITLE, array( __CLASS__, 'subpagetitle'), SFH_NO_HASH );
65 - $parser->setFunctionHook( self::MAG_SUBPAGES, array( __CLASS__, 'subpages'), SFH_NO_HASH );
66 - $parser->setFunctionHook( self::MAG_PARENTPAGES, array( __CLASS__, 'parentpages'), SFH_NO_HASH );
67 - $parser->setFunctionHook( self::MAG_SIBLINGPAGES, array( __CLASS__, 'siblingpages'), SFH_NO_HASH );
68 - $parser->setFunctionHook( self::MAG_SUBPAGELEVEL, array( __CLASS__, 'subpagelevel'), SFH_NO_HASH );
69 - $parser->setFunctionHook( self::MAG_NUMBEROFSUBPAGES, array( __CLASS__, 'numberofsubpages'), SFH_NO_HASH );
70 - $parser->setFunctionHook( self::MAG_TOPLEVELPAGE, array( __CLASS__, 'toplevelpage'), SFH_NO_HASH );
 65+ $parser->setFunctionHook( self::MAG_SUBPAGETITLE, array( __CLASS__, 'pf_subpagetitle' ), SFH_NO_HASH );
 66+ $parser->setFunctionHook( self::MAG_SUBPAGES, array( __CLASS__, 'pf_subpages' ), SFH_NO_HASH );
 67+ $parser->setFunctionHook( self::MAG_PARENTPAGES, array( __CLASS__, 'pf_parentpages' ), SFH_NO_HASH );
 68+ $parser->setFunctionHook( self::MAG_SIBLINGPAGES, array( __CLASS__, 'pf_siblingpages' ), SFH_NO_HASH );
 69+ $parser->setFunctionHook( self::MAG_SUBPAGELEVEL, array( __CLASS__, 'pf_subpagelevel' ), SFH_NO_HASH );
 70+ $parser->setFunctionHook( self::MAG_NUMBEROFSUBPAGES, array( __CLASS__, 'pf_numberofsubpages' ), SFH_NO_HASH );
 71+ $parser->setFunctionHook( self::MAG_TOPLEVELPAGE, array( __CLASS__, 'pf_toplevelpage' ), SFH_NO_HASH );
7172
7273 return true;
7374 }
@@ -101,7 +102,12 @@
102103
103104 foreach ($args as $arg) if( ! is_object( $arg ) )
104105 {
105 - if( preg_match( '/^([^\\n\\r]+?)\\s*=\\s*(.*)$/s', $arg, $match ) ) { // s - include newline. Parameter name is not supposed to have linebreaks
 106+ if( preg_match(
 107+ '/^([^\\n\\r]+?)\\s*=\\s*(.*)$/s', // s - include newline. Parameter name is not supposed to have linebreaks
 108+ $arg,
 109+ $match
 110+ )
 111+ ) {
106112 $argv[ trim( $match[1] ) ] = trim( $match[2] );
107113 } else {
108114 $numargs++;
@@ -119,7 +125,19 @@
120126 *
121127 * @return Title|null
122128 */
123 - private static function newTitleObject ( Parser &$parser, $title = null ) {
 129+ private static function newTitleObject ( Parser &$parser, $title = null ) {
 130+
 131+ if( is_array( $title ) ) {
 132+ /*
 133+ * Instead of one Title, all arguments given to the parser function are given.
 134+ * This is because it makes things more generic to deal with extension 'Parser Fun' support
 135+ * especially for functions only requiring an option title.
 136+ */
 137+ // get all possible arguments:
 138+ $args = ExtSubpageFun::getFunctionArgsArray( $title );
 139+ $title = isset( $args[1] ) ? $args[1] : null;
 140+ }
 141+
124142 if( $title !== null && $title !== '' ) {
125143 return Title::newFromText( $title );
126144 }
@@ -232,15 +250,15 @@
233251
234252 /*** 'Subpage Fun' parser functions: ***/
235253
236 - static function subpagetitle( &$parser, $title = null ) {
237 - $t = self::newTitleObject( $parser, $title );
 254+ static function pf_subpagetitle( &$parser /* , $title = null */ ) {
 255+ $t = self::newTitleObject( $parser, func_get_args() );
238256 if( $t === null ) {
239257 return ''; // invalid title given
240258 }
241259 return wfEscapeWikiText( SubpageInfo::getSubpageTitle( $t ) );
242260 }
243261
244 - static function subpages( &$parser ) {
 262+ static function pf_subpages( &$parser ) {
245263 // get all possible arguments:
246264 $args = ExtSubpageFun::getFunctionArgsArray( func_get_args() );
247265
@@ -265,7 +283,7 @@
266284 return self::createSiteList( $subpages, $linked, $sep );
267285 }
268286
269 - static function parentpages( &$parser ) {
 287+ static function pf_parentpages( &$parser ) {
270288 // get all possible arguments:
271289 $args = ExtSubpageFun::getFunctionArgsArray( func_get_args() );
272290
@@ -290,7 +308,7 @@
291309 return self::createSiteList( $parentpages, $linked, $sep );
292310 }
293311
294 - static function siblingpages( &$parser ) {
 312+ static function pf_siblingpages( &$parser ) {
295313 //get all possible arguments:
296314 $args = ExtSubpageFun::getFunctionArgsArray( func_get_args() );
297315
@@ -314,15 +332,15 @@
315333 return self::createSiteList( $siblingpages, $linked, $sep );
316334 }
317335
318 - static function subpagelevel( &$parser, $title = null) {
319 - $t = self::newTitleObject( $parser, $title );
 336+ static function pf_subpagelevel( &$parser /* , $title = null */ ) {
 337+ $t = self::newTitleObject( $parser, func_get_args() );
320338 if( $t === null ) {
321339 return ''; // invalid title given
322340 }
323341 return SubpageInfo::getSubpageLevel( $t );
324342 }
325343
326 - static function numberofsubpages( &$parser ) {
 344+ static function pf_numberofsubpages( &$parser ) {
327345 //get all possible arguments:
328346 $args = ExtSubpageFun::getFunctionArgsArray( func_get_args() );
329347
@@ -345,8 +363,8 @@
346364 return count( $subpages );
347365 }
348366
349 - static function toplevelpage( &$parser, $title = null) {
350 - $t = self::newTitleObject( $parser, $title );
 367+ static function pf_toplevelpage( &$parser /* , $title = null */ ) {
 368+ $t = self::newTitleObject( $parser, func_get_args() );
351369 if( $t === null ) {
352370 return ''; // invalid title given
353371 }
@@ -366,37 +384,65 @@
367385
368386 /**** All the SubpageFunctions for use with MW Variables on the current page ****/
369387
370 - static function onParserGetVariableValueSwitch( &$parser, &$cache, &$magicWordId, &$ret ) {
 388+ static function onParserGetVariableValueSwitch( Parser &$parser, &$cache, &$magicWordId, &$ret, $frame = null ) {
 389+ return self::variableValueSwitch( $parser, $magicWordId, $ret );
 390+ }
 391+
 392+ /**
 393+ * Make 'Parser Fun' extensions 'THIS' function work with our variables/functions
 394+ */
 395+ static function onGetThisVariableValueSwitch( Parser &$parser, Title $title, &$magicWordId, &$ret, PPFrame $frame, array $args ) {
 396+ $expArgs = array();
 397+ foreach( $args as $arg ) {
 398+ $expArgs[] = trim( $frame->expand( $arg ) );
 399+ }
 400+ $expArgs[] = '1=' . $title->getPrefixedText();
 401+
 402+ return self::variableValueSwitch( $parser, $magicWordId, $ret, $expArgs );
 403+ }
 404+
 405+ /**
 406+ * Where value assigning for normal variables and 'Parser Fun' extensions 'THIS' come together
 407+ */
 408+ private static function variableValueSwitch( Parser &$parser, $magicWordId, &$ret, $args = array() ) {
 409+ // function to call
 410+ $func = null;
 411+
371412 switch( $magicWordId ) {
372413 /** SUBPAGETITLE **/
373414 case self::MAG_SUBPAGETITLE:
374 - $ret = self::subpagetitle( $parser );
 415+ $func = 'pf_subpagetitle';
375416 break;
376417 /** SUBPAGES **/
377418 case self::MAG_SUBPAGES:
378 - $ret = self::subpages( $parser );
 419+ $func = 'pf_subpages';
379420 break;
380421 /** PARENTPAGES **/
381422 case self::MAG_PARENTPAGES:
382 - $ret = self::parentpages( $parser );
 423+ $func = 'pf_parentpages';
383424 break;
384425 /** SIBLINGPAGES **/
385426 case self::MAG_SIBLINGPAGES:
386 - $ret = self::siblingpages( $parser );
 427+ $func = 'pf_siblingpages';
387428 break;
388429 /** SUBPAGELEVEL **/
389430 case self::MAG_SUBPAGELEVEL:
390 - $ret = self::subpagelevel( $parser );
 431+ $func = 'pf_subpagelevel';
391432 break;
392433 /** NUMBEROFSUBPAGES **/
393434 case self::MAG_NUMBEROFSUBPAGES:
394 - $ret = self::numberofsubpages( $parser );
 435+ $func = 'pf_numberofsubpages';
395436 break;
396437 /** TOPLEVELPAGE **/
397438 case self::MAG_TOPLEVELPAGE:
398 - $ret = self::toplevelpage( $parser );
 439+ $func = 'pf_toplevelpage';
399440 break;
400441 }
 442+ if( $func !== null ) {
 443+ $args = array_merge( array( &$parser ), $args ); // $parser as first argument!
 444+ $ret = call_user_func_array( array( __CLASS__, $func ), $args );
 445+ }
 446+
401447 return true;
402448 }
403449
Index: trunk/extensions/SubpageFun/RELEASE-NOTES
@@ -1,6 +1,12 @@
22 'Supage Fun' Changelog:
33 =======================
44
 5+* (trunk), 2011 - Version 0.5.2:
 6+ - Support for 'Parser Fun' extensions 'THIS' function added. With 'Parser Fun' enabled it now is possible to call
 7+ '{{THIS:SUBPAGETITLE}}' which will output the subpage title of the template or page where the phrase is defined on, not the
 8+ page which is actually being rendered by the parser as '{{SUBPAGETITLE}}' would output it.
 9+ - Parser function functions within 'ExtSubpageFun' class now have a 'pf_' prefix.
 10+
511 * November 8, 2011 - Version 0.5.1:
612 - All functions/variables returning page names in any way are using 'wfEscapeWikiText()' now like other variables like 'PAGENAME'
713 for example.

Status & tagging log