r84179 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84178‎ | r84179 | r84180 >
Date:16:45, 17 March 2011
Author:ialex
Status:deferred
Tags:
Comment:
* Use ParserFirstCallInit hook to register parser functions
* Use the same magic word name for parser functions and variable, no need to have separate ones
* Use $out->getTitle() instead of $wgTitle
* Removed useless switch() in LanguageGetMagic()
Modified paths:
  • /trunk/extensions/BookManager/BookManager.body.php (modified) (history)
  • /trunk/extensions/BookManager/BookManager.php (modified) (history)

Diff [purge]

Index: trunk/extensions/BookManager/BookManager.body.php
@@ -3,65 +3,55 @@
44 class BookManagerFunctions {
55 const VERSION = "0.1.6 ";
66
7 - static function register( ) {
8 - global $wgParser;
9 -
 7+ static function register( $parser ) {
108 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
119 # instead of {{#int:...}})
12 - $wgParser->setFunctionHook( 'prevpagename', array( __CLASS__, 'prevpagename' ), SFH_NO_HASH );
13 - $wgParser->setFunctionHook( 'prevpagenamee', array( __CLASS__, 'prevpagenamee' ), SFH_NO_HASH );
14 - $wgParser->setFunctionHook( 'nextpagename', array( __CLASS__, 'nextpagename' ), SFH_NO_HASH );
15 - $wgParser->setFunctionHook( 'nextpagenamee', array( __CLASS__, 'nextpagenamee' ), SFH_NO_HASH );
16 - $wgParser->setFunctionHook( 'rootpagename', array( __CLASS__, 'rootpagename' ), SFH_NO_HASH );
17 - $wgParser->setFunctionHook( 'rootpagenamee', array( __CLASS__, 'rootpagenamee' ), SFH_NO_HASH );
18 - $wgParser->setFunctionHook( 'chaptername', array( __CLASS__, 'chaptername' ), SFH_NO_HASH );
19 - $wgParser->setFunctionHook( 'chapternamee', array( __CLASS__, 'chapternamee' ), SFH_NO_HASH );
 10+ $parser->setFunctionHook( 'prevpagename', array( __CLASS__, 'prevpagename' ), SFH_NO_HASH );
 11+ $parser->setFunctionHook( 'prevpagenamee', array( __CLASS__, 'prevpagenamee' ), SFH_NO_HASH );
 12+ $parser->setFunctionHook( 'nextpagename', array( __CLASS__, 'nextpagename' ), SFH_NO_HASH );
 13+ $parser->setFunctionHook( 'nextpagenamee', array( __CLASS__, 'nextpagenamee' ), SFH_NO_HASH );
 14+ $parser->setFunctionHook( 'rootpagename', array( __CLASS__, 'rootpagename' ), SFH_NO_HASH );
 15+ $parser->setFunctionHook( 'rootpagenamee', array( __CLASS__, 'rootpagenamee' ), SFH_NO_HASH );
 16+ $parser->setFunctionHook( 'chaptername', array( __CLASS__, 'chaptername' ), SFH_NO_HASH );
 17+ $parser->setFunctionHook( 'chapternamee', array( __CLASS__, 'chapternamee' ), SFH_NO_HASH );
 18+
 19+ return true;
2020 }
2121 /**** All the BookManager functions to declare magicword id ****/
2222 static function DeclareVarIds( &$aCustomVariableIds ) {
2323 # aCustomVariableIds is where MediaWiki wants to store its
2424 # list of custom variable ids. We oblige by adding ours:
25 - $aCustomVariableIds[] = MAG_PREVPAGENAME;
26 - $aCustomVariableIds[] = MAG_PREVPAGENAMEE;
27 - $aCustomVariableIds[] = MAG_NEXTPAGENAME;
28 - $aCustomVariableIds[] = MAG_NEXTPAGENAMEE;
29 - $aCustomVariableIds[] = MAG_ROOTPAGENAME;
30 - $aCustomVariableIds[] = MAG_ROOTPAGENAMEE;
31 - $aCustomVariableIds[] = MAG_CHAPTERNAME;
32 - $aCustomVariableIds[] = MAG_CHAPTERNAMEE;
 25+ $aCustomVariableIds[] = 'prevpagename';
 26+ $aCustomVariableIds[] = 'prevpagenamee';
 27+ $aCustomVariableIds[] = 'nextpagename';
 28+ $aCustomVariableIds[] = 'nextpagenamee';
 29+ $aCustomVariableIds[] = 'rootpagename';
 30+ $aCustomVariableIds[] = 'rootpagenamee';
 31+ $aCustomVariableIds[] = 'chaptername';
 32+ $aCustomVariableIds[] = 'chapternamee';
3333 return true;
3434 }
35 - /**** All the BookManager functions to declare magicwords ****/
 35+ /**** All the BookManager functions to declare magicwords ****/
3636 static function LanguageGetMagic( &$magicWords, $langCode = "en" ) {
37 - switch ( $langCode ) {
38 - default:
39 - # PREVPAGENAME
40 - $magicWords[MAG_PREVPAGENAME] = array ( 0, 'PREVPAGENAME' );
41 - $magicWords['prevpagename'] = $magicWords[MAG_PREVPAGENAME];
42 - # PREVPAGENAME
43 - $magicWords[MAG_PREVPAGENAMEE] = array ( 0, 'PREVPAGENAMEE' );
44 - $magicWords['prevpagenamee'] = $magicWords[MAG_PREVPAGENAMEE];
45 - # NEXTPAGENAME
46 - $magicWords[MAG_NEXTPAGENAME] = array ( 0, 'NEXTPAGENAME' );
47 - $magicWords['nextpagename'] = $magicWords[MAG_NEXTPAGENAME];
48 - # NEXTPAGENAMEE
49 - $magicWords[MAG_NEXTPAGENAMEE] = array ( 0, 'NEXTPAGENAMEE' );
50 - $magicWords['nextpagenamee'] = $magicWords[MAG_NEXTPAGENAMEE];
51 - # ROOTPAGENAME
52 - $magicWords[MAG_ROOTPAGENAME] = array ( 0, 'ROOTPAGENAME' , 'BOOKNAME' );
53 - $magicWords['rootpagename'] = $magicWords[MAG_ROOTPAGENAME];
54 - # ROOTPAGENAMEE
55 - $magicWords[MAG_ROOTPAGENAMEE] = array ( 0, 'ROOTPAGENAMEE' , 'BOOKNAMEE' );
56 - $magicWords['rootpagenamee'] = $magicWords[MAG_ROOTPAGENAMEE];
57 - # CHAPTERNAME
58 - $magicWords[MAG_CHAPTERNAME] = array ( 0, 'CHAPTERNAME' );
59 - $magicWords['chaptername'] = $magicWords[MAG_CHAPTERNAME];
60 - # CHAPTERNAMEE
61 - $magicWords[MAG_CHAPTERNAMEE] = array ( 0, 'CHAPTERNAMEE' );
62 - $magicWords['chapternamee'] = $magicWords[MAG_CHAPTERNAMEE];
63 - }
 37+ # PREVPAGENAME
 38+ $magicWords['prevpagename'] = array ( 0, 'PREVPAGENAME' );
 39+ # PREVPAGENAME
 40+ $magicWords['prevpagenamee'] = array ( 0, 'PREVPAGENAMEE' );
 41+ # NEXTPAGENAME
 42+ $magicWords['nextpagename'] = array ( 0, 'NEXTPAGENAME' );
 43+ # NEXTPAGENAMEE
 44+ $magicWords['nextpagenamee'] = array ( 0, 'NEXTPAGENAMEE' );
 45+ # ROOTPAGENAME
 46+ $magicWords['rootpagename'] = array ( 0, 'ROOTPAGENAME' , 'BOOKNAME' );
 47+ # ROOTPAGENAMEE
 48+ $magicWords['rootpagenamee'] = array ( 0, 'ROOTPAGENAMEE' , 'BOOKNAMEE' );
 49+ # CHAPTERNAME
 50+ $magicWords['chaptername'] = array ( 0, 'CHAPTERNAME' );
 51+ # CHAPTERNAMEE
 52+ $magicWords['chapternamee'] = array ( 0, 'CHAPTERNAMEE' );
 53+
6454 return true;
65 - }
 55+ }
6656 /**** All the BookManager private functions ****/
6757
6858
@@ -257,28 +247,28 @@
258248
259249 static function AssignAValue( &$parser, &$cache, &$magicWordId, &$ret ) {
260250 switch( $magicWordId ) {
261 - case MAG_PREVPAGENAME:
 251+ case 'prevpagename':
262252 $ret = BookManagerFunctions::prevpagename( $parser );
263253 return true;
264 - case MAG_PREVPAGENAMEE:
 254+ case 'prevpagenamee':
265255 $ret = BookManagerFunctions::prevpagenamee( $parser );
266256 return true;
267 - case MAG_NEXTPAGENAME:
 257+ case 'nextpagename':
268258 $ret = BookManagerFunctions::nextpagename( $parser );
269259 return true;
270 - case MAG_NEXTPAGENAMEE:
 260+ case 'nextpagenamee':
271261 $ret = BookManagerFunctions::nextpagenamee( $parser );
272262 return true;
273 - case MAG_ROOTPAGENAME:
 263+ case 'rootpagename':
274264 $ret = BookManagerFunctions::rootpagename( $parser );
275265 return true;
276 - case MAG_ROOTPAGENAMEE:
 266+ case 'rootpagenamee':
277267 $ret = BookManagerFunctions::rootpagenamee( $parser );
278268 return true;
279 - case MAG_CHAPTERNAME:
 269+ case 'chaptername':
280270 $ret = BookManagerFunctions::chaptername( $parser );
281271 return true;
282 - case MAG_CHAPTERNAMEE:
 272+ case 'chapternamee':
283273 $ret = BookManagerFunctions::chapternamee( $parser );
284274 return true;
285275 }
@@ -293,12 +283,12 @@
294284 */
295285
296286 static function addText( &$out, &$text ) {
297 - global $wgTitle, $wgParser, $wgBookManagerNamespaces, $wgBookManagerNavbar;
298 - $ns = $wgTitle->getNamespace();
 287+ global $wgParser, $wgBookManagerNamespaces, $wgBookManagerNavbar;
 288+ $ns = $out->getTitle()->getNamespace();
299289 $opt = array(
300290 'parseinline',
301291 );
302 - $currenttitletext = $wgTitle->getText();
 292+ $currenttitletext = $out->getTitle()->getText();
303293 $prev = self::pageText( $wgParser, $currenttitletext, - 1 );
304294 $base = Title::newFromText($currenttitletext)->getBaseText();
305295 $next = self::pageText( $wgParser, $currenttitletext, + 1 );
Index: trunk/extensions/BookManager/BookManager.php
@@ -63,17 +63,8 @@
6464 $wgCommunityCollectionNamespace = NS_PROJECT;
6565
6666 /**** Register magic words ****/
67 -$wgExtensionFunctions[] = 'BookManagerFunctions::register';
 67+$wgHooks['ParserFirstCallInit'][] = 'BookManagerFunctions::register';
6868
69 -define( 'MAG_PREVPAGENAME', 'magicbookmanagercustomvar0' );
70 -define( 'MAG_PREVPAGENAMEE', 'magicbookmanagercustomvar1' );
71 -define( 'MAG_NEXTPAGENAME', 'magicbookmanagercustomvar2' );
72 -define( 'MAG_NEXTPAGENAMEE', 'magicbookmanagercustomvar3' );
73 -define( 'MAG_ROOTPAGENAME', 'magicbookmanagercustomvar4' );
74 -define( 'MAG_ROOTPAGENAMEE', 'magicbookmanagercustomvar5' );
75 -define( 'MAG_CHAPTERNAME', 'magicbookmanagercustomvar6' );
76 -define( 'MAG_CHAPTERNAMEE', 'magicbookmanagercustomvar7' );
77 -
7869 $wgHooks['LanguageGetMagic'][] = 'BookManagerFunctions::LanguageGetMagic';
7970
8071 $wgHooks['MagicWordwgVariableIDs'][] = 'BookManagerFunctions::DeclareVarIds';

Status & tagging log