Index: trunk/extensions/Multilang/Multilang.php |
— | — | @@ -8,28 +8,18 @@ |
9 | 9 | * @author Rob Church <robchur@gmail.com> |
10 | 10 | */ |
11 | 11 | |
12 | | -if( defined( 'MEDIAWIKI' ) ) { |
| 12 | +if( !defined( 'MEDIAWIKI' ) ) { |
| 13 | + die; |
| 14 | +} |
13 | 15 | |
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 | +); |
20 | 22 | |
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'; |
34 | 24 | |
35 | | -} |
36 | | - |
| 25 | +$wgHooks['ParserClearState'][] = 'Multilang::clearState'; |
| 26 | +$wgHooks['ParserFirstCallInit'][] = 'Multilang::onParserFirstCallInit'; |
Index: trunk/extensions/Multilang/Multilang.class.php |
— | — | @@ -6,12 +6,12 @@ |
7 | 7 | * Alternative text blocks |
8 | 8 | * Index is the language code to which it corresponds |
9 | 9 | */ |
10 | | - private $text = array(); |
| 10 | + private static $text = array(); |
11 | 11 | |
12 | 12 | /** |
13 | 13 | * Fallback language |
14 | 14 | */ |
15 | | - private $fallback = ''; |
| 15 | + private static $fallback = ''; |
16 | 16 | |
17 | 17 | /** |
18 | 18 | * Register a new alternative text block |
— | — | @@ -22,11 +22,11 @@ |
23 | 23 | * @param $parser Parent parser |
24 | 24 | * @return string |
25 | 25 | */ |
26 | | - public function languageBlock( $text, $args, &$parser ) { |
| 26 | + public static function languageBlock( $text, $args, $parser ) { |
27 | 27 | if( isset( $args['lang'] ) ) { |
28 | 28 | $lang = strtolower( $args['lang'] ); |
29 | | - $this->text[$lang] = $text; |
30 | | - $this->updateFallback( $lang ); |
| 29 | + self::$text[$lang] = $text; |
| 30 | + self::updateFallback( $lang ); |
31 | 31 | } else { |
32 | 32 | # Disaster! We *have* to know the language code, otherwise |
33 | 33 | # we have no idea when to show the text in question |
— | — | @@ -43,11 +43,11 @@ |
44 | 44 | * @param $parser Parent parser |
45 | 45 | * @return string |
46 | 46 | */ |
47 | | - public function outputBlock( $text, $args, &$parser ) { |
| 47 | + public static function outputBlock( $text, $args, $parser ) { |
48 | 48 | global $wgLang; |
49 | 49 | # Cache is varied according to interface language... |
50 | 50 | $lang = $wgLang->getCode(); |
51 | | - $text = $this->getText( $lang ); |
| 51 | + $text = self::getText( $lang ); |
52 | 52 | $output = $parser->parse( $text, $parser->getTitle(), $parser->getOptions(), true, false ); |
53 | 53 | return $output->getText(); |
54 | 54 | } |
— | — | @@ -59,8 +59,8 @@ |
60 | 60 | * @param $lang Language code |
61 | 61 | * @return string |
62 | 62 | */ |
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(); |
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
— | — | @@ -68,8 +68,8 @@ |
69 | 69 | * |
70 | 70 | * @return string |
71 | 71 | */ |
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] : ''; |
74 | 74 | } |
75 | 75 | |
76 | 76 | /** |
— | — | @@ -78,19 +78,28 @@ |
79 | 79 | * |
80 | 80 | * @param $lang Language code |
81 | 81 | */ |
82 | | - private function updateFallback( $lang ) { |
| 82 | + private static function updateFallback( $lang ) { |
83 | 83 | global $wgContLang; |
84 | | - if( $this->fallback == '' || $lang == $wgContLang->getCode() ) { |
85 | | - $this->fallback = $lang; |
| 84 | + if( self::$fallback == '' || $lang == $wgContLang->getCode() ) { |
| 85 | + self::$fallback = $lang; |
86 | 86 | } |
87 | 87 | } |
88 | | - |
| 88 | + |
89 | 89 | /** |
| 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 | + /** |
90 | 99 | * Clear all internal state information |
91 | 100 | */ |
92 | | - public function clearState() { |
93 | | - $this->text = array(); |
94 | | - $this->fallback = ''; |
| 101 | + public static function clearState() { |
| 102 | + self::$text = array(); |
| 103 | + self::$fallback = ''; |
95 | 104 | return true; |
96 | 105 | } |
97 | 106 | |