r93852 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93851‎ | r93852 | r93853 >
Date:20:28, 3 August 2011
Author:ialex
Status:deferred
Tags:
Comment:
Use the ParserFirstCallInit hook instead of an extension function to register parser hooks
Modified paths:
  • /trunk/extensions/Multilang/Multilang.class.php (modified) (history)
  • /trunk/extensions/Multilang/Multilang.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Multilang/Multilang.php
@@ -8,28 +8,18 @@
99 * @author Rob Church <robchur@gmail.com>
1010 */
1111
12 -if( defined( 'MEDIAWIKI' ) ) {
 12+if( !defined( 'MEDIAWIKI' ) ) {
 13+ die;
 14+}
1315
14 - $wgExtensionCredits['parserhook'][] = array(
15 - 'path' => __FILE__,
16 - 'name' => 'Multilang',
17 - 'author' => '',
18 - 'url' => 'http://www.mediawiki.org/wiki/Extension:Multilang',
19 - );
 16+$wgExtensionCredits['parserhook'][] = array(
 17+ 'path' => __FILE__,
 18+ 'name' => 'Multilang',
 19+ 'author' => '',
 20+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Multilang',
 21+);
2022
21 - $wgAutoloadClasses['Multilang'] = dirname( __FILE__ ) . '/Multilang.class.php';
22 - $wgExtensionFunctions[] = 'efMultilang';
23 -
24 - function efMultilang() {
25 - global $wgMultilang, $wgParser, $wgHooks;
26 - # Use of a StubObject means we can have a single, persistent instance
27 - # that will remember what's going on between parse runs, and we can
28 - # defer initialisation until we need to call a hook function
29 - $wgMultilang = new StubObject( 'wgMultilang', 'Multilang' );
30 - $wgParser->setHook( 'language', array( &$wgMultilang, 'languageBlock' ) );
31 - $wgParser->setHook( 'multilang', array( &$wgMultilang, 'outputBlock' ) );
32 - $wgHooks['ParserClearState'][] = array( &$wgMultilang, 'clearState' );
33 - }
 23+$wgAutoloadClasses['Multilang'] = dirname( __FILE__ ) . '/Multilang.class.php';
3424
35 -}
36 -
 25+$wgHooks['ParserClearState'][] = 'Multilang::clearState';
 26+$wgHooks['ParserFirstCallInit'][] = 'Multilang::onParserFirstCallInit';
Index: trunk/extensions/Multilang/Multilang.class.php
@@ -6,12 +6,12 @@
77 * Alternative text blocks
88 * Index is the language code to which it corresponds
99 */
10 - private $text = array();
 10+ private static $text = array();
1111
1212 /**
1313 * Fallback language
1414 */
15 - private $fallback = '';
 15+ private static $fallback = '';
1616
1717 /**
1818 * Register a new alternative text block
@@ -22,11 +22,11 @@
2323 * @param $parser Parent parser
2424 * @return string
2525 */
26 - public function languageBlock( $text, $args, &$parser ) {
 26+ public static function languageBlock( $text, $args, $parser ) {
2727 if( isset( $args['lang'] ) ) {
2828 $lang = strtolower( $args['lang'] );
29 - $this->text[$lang] = $text;
30 - $this->updateFallback( $lang );
 29+ self::$text[$lang] = $text;
 30+ self::updateFallback( $lang );
3131 } else {
3232 # Disaster! We *have* to know the language code, otherwise
3333 # we have no idea when to show the text in question
@@ -43,11 +43,11 @@
4444 * @param $parser Parent parser
4545 * @return string
4646 */
47 - public function outputBlock( $text, $args, &$parser ) {
 47+ public static function outputBlock( $text, $args, $parser ) {
4848 global $wgLang;
4949 # Cache is varied according to interface language...
5050 $lang = $wgLang->getCode();
51 - $text = $this->getText( $lang );
 51+ $text = self::getText( $lang );
5252 $output = $parser->parse( $text, $parser->getTitle(), $parser->getOptions(), true, false );
5353 return $output->getText();
5454 }
@@ -59,8 +59,8 @@
6060 * @param $lang Language code
6161 * @return string
6262 */
63 - private function getText( $lang ) {
64 - return isset( $this->text[$lang] ) ? $this->text[$lang] : $this->getFallback();
 63+ private static function getText( $lang ) {
 64+ return isset( self::$text[$lang] ) ? self::$text[$lang] : self::getFallback();
6565 }
6666
6767 /**
@@ -68,8 +68,8 @@
6969 *
7070 * @return string
7171 */
72 - private function getFallback() {
73 - return isset( $this->text[$this->fallback] ) ? $this->text[$this->fallback] : '';
 72+ private static function getFallback() {
 73+ return isset( self::$text[self::$fallback] ) ? self::$text[self::$fallback] : '';
7474 }
7575
7676 /**
@@ -78,19 +78,28 @@
7979 *
8080 * @param $lang Language code
8181 */
82 - private function updateFallback( $lang ) {
 82+ private static function updateFallback( $lang ) {
8383 global $wgContLang;
84 - if( $this->fallback == '' || $lang == $wgContLang->getCode() ) {
85 - $this->fallback = $lang;
 84+ if( self::$fallback == '' || $lang == $wgContLang->getCode() ) {
 85+ self::$fallback = $lang;
8686 }
8787 }
88 -
 88+
8989 /**
 90+ * Set hooks on a Parser instance
 91+ */
 92+ public static function onParserFirstCallInit( $parser ) {
 93+ $parser->setHook( 'language', array( __CLASS__, 'languageBlock' ) );
 94+ $parser->setHook( 'multilang', array( __CLASS__, 'outputBlock' ) );
 95+ return true;
 96+ }
 97+
 98+ /**
9099 * Clear all internal state information
91100 */
92 - public function clearState() {
93 - $this->text = array();
94 - $this->fallback = '';
 101+ public static function clearState() {
 102+ self::$text = array();
 103+ self::$fallback = '';
95104 return true;
96105 }
97106

Status & tagging log