Index: branches/Gadgets-work/Gadgets_body.php |
— | — | @@ -18,81 +18,13 @@ |
19 | 19 | //update cache if MediaWiki:Gadgets-definition was edited |
20 | 20 | $title = $article->mTitle; |
21 | 21 | if( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) { |
22 | | - self::loadStructured( $text ); |
| 22 | + Gadget::loadStructuredList( $text ); |
23 | 23 | } |
24 | 24 | return true; |
25 | 25 | } |
26 | 26 | |
27 | | - private static function load() { |
28 | | - static $gadgets = null; |
29 | | - |
30 | | - if ( $gadgets !== null ) return $gadgets; |
31 | | - |
32 | | - $struct = self::loadStructured(); |
33 | | - if ( !$struct ) { |
34 | | - $gadgets = $struct; |
35 | | - return $gadgets; |
36 | | - } |
37 | | - |
38 | | - $gadgets = array(); |
39 | | - foreach ( $struct as $section => $entries ) { |
40 | | - $gadgets = array_merge( $gadgets, $entries ); |
41 | | - } |
42 | | - |
43 | | - return $gadgets; |
44 | | - } |
45 | | - |
46 | | - public static function loadStructured( $forceNewText = null ) { |
47 | | - global $wgMemc; |
48 | | - |
49 | | - static $gadgets = null; |
50 | | - if ( $gadgets !== null && $forceNewText === null ) return $gadgets; |
51 | | - |
52 | | - $key = wfMemcKey( 'gadgets-definition' ); |
53 | | - |
54 | | - if ( $forceNewText === null ) { |
55 | | - //cached? |
56 | | - $gadgets = $wgMemc->get( $key ); |
57 | | - // TODO: isOutdated() |
58 | | - if ( is_array($gadgets) && next( $gadgets ) instanceof Gadget ) return $gadgets; |
59 | | - |
60 | | - $g = wfMsgForContentNoTrans( "gadgets-definition" ); |
61 | | - if ( wfEmptyMsg( "gadgets-definition", $g ) ) { |
62 | | - $gadgets = false; |
63 | | - return $gadgets; |
64 | | - } |
65 | | - } else { |
66 | | - $g = $forceNewText; |
67 | | - } |
68 | | - |
69 | | - $g = preg_replace( '/<!--.*-->/s', '', $g ); |
70 | | - $g = preg_split( '/(\r\n|\r|\n)+/', $g ); |
71 | | - |
72 | | - $gadgets = array(); |
73 | | - $section = ''; |
74 | | - |
75 | | - foreach ( $g as $line ) { |
76 | | - if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) { |
77 | | - $section = $m[1]; |
78 | | - } |
79 | | - else { |
80 | | - $gadget = Gadget::newFromDefinition( $line ); |
81 | | - if ( $gadget ) { |
82 | | - $gadgets[$section][$gadget->getName()] = $gadget; |
83 | | - } |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - //cache for a while. gets purged automatically when MediaWiki:Gadgets-definition is edited |
88 | | - $wgMemc->set( $key, $gadgets, 60*60*24 ); |
89 | | - $source = $forceNewText !== null ? 'input text' : 'MediaWiki:Gadgets-definition'; |
90 | | - wfDebug( __METHOD__ . ": $source parsed, cache entry $key updated\n"); |
91 | | - |
92 | | - return $gadgets; |
93 | | - } |
94 | | - |
95 | 27 | public static function getPreferences( $user, &$preferences ) { |
96 | | - $gadgets = self::loadStructured(); |
| 28 | + $gadgets = Gadget::loadStructuredList(); |
97 | 29 | if (!$gadgets) return true; |
98 | 30 | |
99 | 31 | $options = array(); |
— | — | @@ -135,7 +67,7 @@ |
136 | 68 | } |
137 | 69 | |
138 | 70 | public static function registerModules( &$resourceLoader ) { |
139 | | - $gadgets = self::load(); |
| 71 | + $gadgets = Gadget::loadList(); |
140 | 72 | if ( !$gadgets ) { |
141 | 73 | return true; |
142 | 74 | } |
— | — | @@ -164,7 +96,7 @@ |
165 | 97 | return true; |
166 | 98 | } |
167 | 99 | |
168 | | - $gadgets = self::Load(); |
| 100 | + $gadgets = Gadget::loadList(); |
169 | 101 | if ( !$gadgets ) return true; |
170 | 102 | |
171 | 103 | $lb = new LinkBatch(); |
— | — | @@ -291,6 +223,74 @@ |
292 | 224 | } |
293 | 225 | return $this->scripts; |
294 | 226 | } |
| 227 | + |
| 228 | + public static function loadList() { |
| 229 | + static $gadgets = null; |
| 230 | + |
| 231 | + if ( $gadgets !== null ) return $gadgets; |
| 232 | + |
| 233 | + $struct = self::loadStructuredList(); |
| 234 | + if ( !$struct ) { |
| 235 | + $gadgets = $struct; |
| 236 | + return $gadgets; |
| 237 | + } |
| 238 | + |
| 239 | + $gadgets = array(); |
| 240 | + foreach ( $struct as $section => $entries ) { |
| 241 | + $gadgets = array_merge( $gadgets, $entries ); |
| 242 | + } |
| 243 | + |
| 244 | + return $gadgets; |
| 245 | + } |
| 246 | + |
| 247 | + public static function loadStructuredList( $forceNewText = null ) { |
| 248 | + global $wgMemc; |
| 249 | + |
| 250 | + static $gadgets = null; |
| 251 | + if ( $gadgets !== null && $forceNewText === null ) return $gadgets; |
| 252 | + |
| 253 | + $key = wfMemcKey( 'gadgets-definition' ); |
| 254 | + |
| 255 | + if ( $forceNewText === null ) { |
| 256 | + //cached? |
| 257 | + $gadgets = $wgMemc->get( $key ); |
| 258 | + // TODO: isOutdated() |
| 259 | + if ( is_array($gadgets) && next( $gadgets ) instanceof Gadget ) return $gadgets; |
| 260 | + |
| 261 | + $g = wfMsgForContentNoTrans( "gadgets-definition" ); |
| 262 | + if ( wfEmptyMsg( "gadgets-definition", $g ) ) { |
| 263 | + $gadgets = false; |
| 264 | + return $gadgets; |
| 265 | + } |
| 266 | + } else { |
| 267 | + $g = $forceNewText; |
| 268 | + } |
| 269 | + |
| 270 | + $g = preg_replace( '/<!--.*-->/s', '', $g ); |
| 271 | + $g = preg_split( '/(\r\n|\r|\n)+/', $g ); |
| 272 | + |
| 273 | + $gadgets = array(); |
| 274 | + $section = ''; |
| 275 | + |
| 276 | + foreach ( $g as $line ) { |
| 277 | + if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) { |
| 278 | + $section = $m[1]; |
| 279 | + } |
| 280 | + else { |
| 281 | + $gadget = Gadget::newFromDefinition( $line ); |
| 282 | + if ( $gadget ) { |
| 283 | + $gadgets[$section][$gadget->getName()] = $gadget; |
| 284 | + } |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + //cache for a while. gets purged automatically when MediaWiki:Gadgets-definition is edited |
| 289 | + $wgMemc->set( $key, $gadgets, 60*60*24 ); |
| 290 | + $source = $forceNewText !== null ? 'input text' : 'MediaWiki:Gadgets-definition'; |
| 291 | + wfDebug( __METHOD__ . ": $source parsed, cache entry $key updated\n"); |
| 292 | + |
| 293 | + return $gadgets; |
| 294 | + } |
295 | 295 | } |
296 | 296 | |
297 | 297 | class GadgetResourceLoaderModule extends ResourceLoaderWikiModule { |