Index: trunk/extensions/SubpageFun/SFun_SubpageInfo.php |
— | — | @@ -190,7 +190,7 @@ |
191 | 191 | * |
192 | 192 | * @return string Result The subpage title of the given page. If page isn't a subpage, the Pages |
193 | 193 | * name (without prefix) will be returned. |
194 | | - */ |
| 194 | + */ |
195 | 195 | static function getSubpageTitle( Title $page ) { |
196 | 196 | $parent = SubpageInfo::getParentPage( $page ); |
197 | 197 | //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 @@ |
9 | 9 | * Support: http://www.mediawiki.org/wiki/Extension_talk:Subpage_Fun |
10 | 10 | * Source code: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/SubpageFun |
11 | 11 | * |
12 | | - * @version: 0.5.1 |
| 12 | + * @version: 0.5.2 |
13 | 13 | * @license: ISC license |
14 | 14 | * @author: Daniel Werner < danweetz@web.de > |
15 | 15 | * |
— | — | @@ -38,17 +38,18 @@ |
39 | 39 | $wgAutoloadClasses['SubpageInfo'] = ExtSubpageFun::getDir() . '/SFun_SubpageInfo.php'; |
40 | 40 | |
41 | 41 | // Register magic words: |
42 | | -$wgHooks['ParserFirstCallInit' ][] = 'ExtSubpageFun::init'; |
43 | | -//$wgHooks['LanguageGetMagic' ][] = 'ExtSubpageFun::onLanguageGetMagic'; |
| 42 | +$wgHooks['ParserFirstCallInit'][] = 'ExtSubpageFun::init'; |
44 | 43 | |
45 | 44 | // register plain variables: |
46 | 45 | $wgHooks['MagicWordwgVariableIDs' ][] = 'ExtSubpageFun::onMagicWordwgVariableIDs'; |
47 | 46 | $wgHooks['ParserGetVariableValueSwitch'][] = 'ExtSubpageFun::onParserGetVariableValueSwitch'; |
48 | 47 | |
| 48 | +// hook up for use with 'Parser Fun' extensions 'THIS' function: |
| 49 | +$wgHooks['GetThisVariableValueSwitch'][] = 'ExtSubpageFun::onGetThisVariableValueSwitch'; |
49 | 50 | |
50 | 51 | class ExtSubpageFun { |
51 | 52 | |
52 | | - const VERSION = '0.5.1'; |
| 53 | + const VERSION = '0.5.2'; |
53 | 54 | |
54 | 55 | const MAG_SUBPAGETITLE = 'subpagetitle'; |
55 | 56 | const MAG_SUBPAGES = 'subpages'; |
— | — | @@ -58,15 +59,15 @@ |
59 | 60 | const MAG_NUMBEROFSUBPAGES = 'numberofsubpages'; |
60 | 61 | const MAG_TOPLEVELPAGE = 'toplevelpage'; |
61 | 62 | |
62 | | - static function init( &$parser ) { |
| 63 | + static function init( Parser &$parser ) { |
63 | 64 | // 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 ); |
71 | 72 | |
72 | 73 | return true; |
73 | 74 | } |
— | — | @@ -101,7 +102,12 @@ |
102 | 103 | |
103 | 104 | foreach ($args as $arg) if( ! is_object( $arg ) ) |
104 | 105 | { |
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 | + ) { |
106 | 112 | $argv[ trim( $match[1] ) ] = trim( $match[2] ); |
107 | 113 | } else { |
108 | 114 | $numargs++; |
— | — | @@ -119,7 +125,19 @@ |
120 | 126 | * |
121 | 127 | * @return Title|null |
122 | 128 | */ |
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 | + |
124 | 142 | if( $title !== null && $title !== '' ) { |
125 | 143 | return Title::newFromText( $title ); |
126 | 144 | } |
— | — | @@ -232,15 +250,15 @@ |
233 | 251 | |
234 | 252 | /*** 'Subpage Fun' parser functions: ***/ |
235 | 253 | |
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() ); |
238 | 256 | if( $t === null ) { |
239 | 257 | return ''; // invalid title given |
240 | 258 | } |
241 | 259 | return wfEscapeWikiText( SubpageInfo::getSubpageTitle( $t ) ); |
242 | 260 | } |
243 | 261 | |
244 | | - static function subpages( &$parser ) { |
| 262 | + static function pf_subpages( &$parser ) { |
245 | 263 | // get all possible arguments: |
246 | 264 | $args = ExtSubpageFun::getFunctionArgsArray( func_get_args() ); |
247 | 265 | |
— | — | @@ -265,7 +283,7 @@ |
266 | 284 | return self::createSiteList( $subpages, $linked, $sep ); |
267 | 285 | } |
268 | 286 | |
269 | | - static function parentpages( &$parser ) { |
| 287 | + static function pf_parentpages( &$parser ) { |
270 | 288 | // get all possible arguments: |
271 | 289 | $args = ExtSubpageFun::getFunctionArgsArray( func_get_args() ); |
272 | 290 | |
— | — | @@ -290,7 +308,7 @@ |
291 | 309 | return self::createSiteList( $parentpages, $linked, $sep ); |
292 | 310 | } |
293 | 311 | |
294 | | - static function siblingpages( &$parser ) { |
| 312 | + static function pf_siblingpages( &$parser ) { |
295 | 313 | //get all possible arguments: |
296 | 314 | $args = ExtSubpageFun::getFunctionArgsArray( func_get_args() ); |
297 | 315 | |
— | — | @@ -314,15 +332,15 @@ |
315 | 333 | return self::createSiteList( $siblingpages, $linked, $sep ); |
316 | 334 | } |
317 | 335 | |
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() ); |
320 | 338 | if( $t === null ) { |
321 | 339 | return ''; // invalid title given |
322 | 340 | } |
323 | 341 | return SubpageInfo::getSubpageLevel( $t ); |
324 | 342 | } |
325 | 343 | |
326 | | - static function numberofsubpages( &$parser ) { |
| 344 | + static function pf_numberofsubpages( &$parser ) { |
327 | 345 | //get all possible arguments: |
328 | 346 | $args = ExtSubpageFun::getFunctionArgsArray( func_get_args() ); |
329 | 347 | |
— | — | @@ -345,8 +363,8 @@ |
346 | 364 | return count( $subpages ); |
347 | 365 | } |
348 | 366 | |
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() ); |
351 | 369 | if( $t === null ) { |
352 | 370 | return ''; // invalid title given |
353 | 371 | } |
— | — | @@ -366,37 +384,65 @@ |
367 | 385 | |
368 | 386 | /**** All the SubpageFunctions for use with MW Variables on the current page ****/ |
369 | 387 | |
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 | + |
371 | 412 | switch( $magicWordId ) { |
372 | 413 | /** SUBPAGETITLE **/ |
373 | 414 | case self::MAG_SUBPAGETITLE: |
374 | | - $ret = self::subpagetitle( $parser ); |
| 415 | + $func = 'pf_subpagetitle'; |
375 | 416 | break; |
376 | 417 | /** SUBPAGES **/ |
377 | 418 | case self::MAG_SUBPAGES: |
378 | | - $ret = self::subpages( $parser ); |
| 419 | + $func = 'pf_subpages'; |
379 | 420 | break; |
380 | 421 | /** PARENTPAGES **/ |
381 | 422 | case self::MAG_PARENTPAGES: |
382 | | - $ret = self::parentpages( $parser ); |
| 423 | + $func = 'pf_parentpages'; |
383 | 424 | break; |
384 | 425 | /** SIBLINGPAGES **/ |
385 | 426 | case self::MAG_SIBLINGPAGES: |
386 | | - $ret = self::siblingpages( $parser ); |
| 427 | + $func = 'pf_siblingpages'; |
387 | 428 | break; |
388 | 429 | /** SUBPAGELEVEL **/ |
389 | 430 | case self::MAG_SUBPAGELEVEL: |
390 | | - $ret = self::subpagelevel( $parser ); |
| 431 | + $func = 'pf_subpagelevel'; |
391 | 432 | break; |
392 | 433 | /** NUMBEROFSUBPAGES **/ |
393 | 434 | case self::MAG_NUMBEROFSUBPAGES: |
394 | | - $ret = self::numberofsubpages( $parser ); |
| 435 | + $func = 'pf_numberofsubpages'; |
395 | 436 | break; |
396 | 437 | /** TOPLEVELPAGE **/ |
397 | 438 | case self::MAG_TOPLEVELPAGE: |
398 | | - $ret = self::toplevelpage( $parser ); |
| 439 | + $func = 'pf_toplevelpage'; |
399 | 440 | break; |
400 | 441 | } |
| 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 | + |
401 | 447 | return true; |
402 | 448 | } |
403 | 449 | |
Index: trunk/extensions/SubpageFun/RELEASE-NOTES |
— | — | @@ -1,6 +1,12 @@ |
2 | 2 | 'Supage Fun' Changelog: |
3 | 3 | ======================= |
4 | 4 | |
| 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 | + |
5 | 11 | * November 8, 2011 - Version 0.5.1: |
6 | 12 | - All functions/variables returning page names in any way are using 'wfEscapeWikiText()' now like other variables like 'PAGENAME' |
7 | 13 | for example. |