Index: branches/salvatoreingala/Gadgets/backend/Gadget.php |
— | — | @@ -39,9 +39,12 @@ |
40 | 40 | /** |
41 | 41 | * Creates an instance of this class from definition in MediaWiki:Gadgets-definition |
42 | 42 | * @param $definition String: Gadget definition |
| 43 | + * @param $forcePrefsDescriptionsJson Array: Array with keys equal to gadget names, and values to their preferences description. |
| 44 | + * If it is specified and a gadget whose name is a key in the array is found, its corresponding value is used instead of |
| 45 | + * loading it from MediaWiki:Gadget-<name>.preferences. |
43 | 46 | * @return Mixed: Instance of Gadget class or false if $definition is invalid |
44 | 47 | */ |
45 | | - public static function newFromDefinition( $definition ) { |
| 48 | + public static function newFromDefinition( $definition, $forcePrefsDescriptionsJson = null ) { |
46 | 49 | $m = array(); |
47 | 50 | if ( !preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/', $definition, $m ) ) { |
48 | 51 | return false; |
— | — | @@ -53,9 +56,7 @@ |
54 | 57 | $gadget->name = trim( str_replace(' ', '_', $m[1] ) ); |
55 | 58 | $gadget->definition = $definition; |
56 | 59 | |
57 | | - //Could be made more precise using per-gadget info; an upper bound suffices. |
58 | | - //Since the list is reloaded every time gadget definitions or gadget preference descriptions |
59 | | - //are changed, 'now' is an upper bound. |
| 60 | + //TODO: make this more precise with gadget-specific info. Untile then, 'now' is an upper bound. |
60 | 61 | $gadget->mTime = wfTimestamp( TS_UNIX ); |
61 | 62 | |
62 | 63 | //Parse gadget options |
— | — | @@ -94,11 +95,24 @@ |
95 | 96 | } |
96 | 97 | |
97 | 98 | if ( $gadget->resourceLoaded ) { |
98 | | - //Retrieve preference descriptions |
99 | | - $prefsDescriptionMsg = "Gadget-{$gadget->name}.preferences"; |
100 | | - $msg = wfMessage( $prefsDescriptionMsg ); |
101 | | - if ( $msg->exists() ) { |
102 | | - $prefsDescription = FormatJson::decode( $msg->plain(), true ); |
| 99 | + if ( $forcePrefsDescriptionsJson === null || |
| 100 | + !is_array( $forcePrefsDescriptionsJson ) || |
| 101 | + !isset( $forcePrefsDescriptionsJson[$gadget->name] ) ) |
| 102 | + { |
| 103 | + //Retrieve preference descriptions |
| 104 | + $prefsDescriptionMsg = "Gadget-{$gadget->name}.preferences"; |
| 105 | + $msg = wfMessage( $prefsDescriptionMsg ); |
| 106 | + if ( $msg->exists() ) { |
| 107 | + wfDebug( __METHOD__ . ": loading description of preferences for gadget {$gadget->name} from $prefsDescriptionMsg.\n" ); |
| 108 | + $prefsDescriptionJson = $msg->plain(); |
| 109 | + } |
| 110 | + } else { |
| 111 | + wfDebug( __METHOD__ . ": loading description of preferences for gadget {$gadget->name} from \$forcePrefsDescriptionsJson.\n" ); |
| 112 | + $prefsDescriptionJson = $forcePrefsDescriptionsJson[$gadget->name]; |
| 113 | + } |
| 114 | + |
| 115 | + if ( isset( $prefsDescriptionJson ) ) { |
| 116 | + $prefsDescription = FormatJson::decode( $prefsDescriptionJson, true ); |
103 | 117 | $gadget->setPrefsDescription( $prefsDescription ); |
104 | 118 | } |
105 | 119 | } |
— | — | @@ -331,14 +345,17 @@ |
332 | 346 | * 'sectionnname2' => array( $gadget3 ) ); |
333 | 347 | * @param $forceNewText String: New text of MediaWiki:gadgets-sdefinition. If specified, will |
334 | 348 | * force a purge of cache and recreation of the gadget list. |
| 349 | + * @param $forcePrefsDescriptionsJson Array: Array with keys equal to gadget names, and values to their preferences description. |
| 350 | + * If it is specified and a gadget whose name is a key in the array is found, its corresponding value is used instead of |
| 351 | + * loading it from MediaWiki:Gadget-<name>.preferences. |
335 | 352 | * @return Mixed: Array or false |
336 | 353 | */ |
337 | | - public static function loadStructuredList( $forceNewText = null ) { |
| 354 | + public static function loadStructuredList( $forceNewText = null, $forcePrefsDescriptionsJson = null ) { |
338 | 355 | global $wgMemc; |
339 | 356 | |
340 | 357 | static $gadgets = null; |
341 | 358 | |
342 | | - if ( $gadgets !== null && $forceNewText === null ) { |
| 359 | + if ( $gadgets !== null && $forceNewText === null && $forcePrefsDescriptionsJson === null ) { |
343 | 360 | return $gadgets; |
344 | 361 | } |
345 | 362 | |
— | — | @@ -353,7 +370,7 @@ |
354 | 371 | //Check again, loadStructuredList may have been called from UserLoadOptions hook handler; |
355 | 372 | //in that case, we should just return current value instead of rebuilding the list again. |
356 | 373 | //TODO: is there a better design? |
357 | | - if ( $gadgets !== null && $forceNewText === null ) { |
| 374 | + if ( $gadgets !== null && $forceNewText === null && $forcePrefsDescriptionsJson === null ) { |
358 | 375 | wfProfileOut( __METHOD__ ); |
359 | 376 | return $gadgets; |
360 | 377 | } |
— | — | @@ -393,7 +410,7 @@ |
394 | 411 | $section = $m[1]; |
395 | 412 | } |
396 | 413 | else { |
397 | | - $gadget = self::newFromDefinition( $line ); |
| 414 | + $gadget = self::newFromDefinition( $line, $forcePrefsDescriptionsJson ); |
398 | 415 | if ( $gadget ) { |
399 | 416 | $gadgets[$section][$gadget->getName()] = $gadget; |
400 | 417 | $gadget->category = $section; |
Index: branches/salvatoreingala/Gadgets/backend/GadgetHooks.php |
— | — | @@ -29,12 +29,16 @@ |
30 | 30 | if( $title->getNamespace() == NS_MEDIAWIKI ) { |
31 | 31 | if ( $title->getText() == 'Gadgets-definition' ) { |
32 | 32 | Gadget::loadStructuredList( $text ); |
33 | | - } elseif ( preg_match( '/Gadget-([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)\.preferences/', $title->getText() ) ) { |
34 | | - $msg = wfMessage( 'Gadgets-definition' ); |
35 | | - if ( $msg->exists() ) { |
36 | | - Gadget::loadStructuredList( $msg->plain() ); |
37 | | - } else { |
38 | | - Gadget::loadStructuredList( '' ); |
| 33 | + } else { |
| 34 | + $m = array(); |
| 35 | + if ( preg_match( '/Gadget-([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)\.preferences/', $title->getText(), $m ) ) { |
| 36 | + $msg = wfMessage( 'Gadgets-definition' ); |
| 37 | + if ( $msg->exists() ) { |
| 38 | + $gadgetName = trim( str_replace(' ', '_', $m[1] ) ); |
| 39 | + Gadget::loadStructuredList( $msg->plain(), array( $gadgetName => $text ) ); |
| 40 | + } else { |
| 41 | + Gadget::loadStructuredList( '' ); |
| 42 | + } |
39 | 43 | } |
40 | 44 | } |
41 | 45 | } |
— | — | @@ -218,12 +222,18 @@ |
219 | 223 | |
220 | 224 | wfProfileIn( __METHOD__ ); |
221 | 225 | |
| 226 | + $gadgets = Gadget::loadList(); |
| 227 | + if ( !$gadgets ) { |
| 228 | + wfProfileOut( __METHOD__ ); |
| 229 | + return true; |
| 230 | + } |
| 231 | + |
222 | 232 | //Find out all existing gadget preferences and save them in a map |
223 | 233 | $preferencesCache = array(); |
224 | 234 | foreach ( $options as $option => $value ) { |
225 | 235 | $m = array(); |
226 | 236 | if ( preg_match( '/gadget-([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)-config/', $option, $m ) ) { |
227 | | - $gadgetName = $m[1]; |
| 237 | + $gadgetName = trim( str_replace(' ', '_', $m[1] ) ); |
228 | 238 | $gadgetPrefs = unserialize( $value ); |
229 | 239 | if ( $gadgetPrefs !== false ) { |
230 | 240 | $preferencesCache[$gadgetName] = $gadgetPrefs; |
— | — | @@ -237,7 +247,6 @@ |
238 | 248 | } |
239 | 249 | |
240 | 250 | //Record preferences for each gadget |
241 | | - $gadgets = Gadget::loadList(); |
242 | 251 | foreach ( $gadgets as $gadget ) { |
243 | 252 | $prefsDescription = $gadget->getPrefsDescription(); |
244 | 253 | if ( $prefsDescription !== null ) { |