r92055 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92054‎ | r92055 | r92056 >
Date:11:02, 13 July 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
- Added to Gadget::loadStructuredList a param $forcePrefsDescriptionsJson (and to Gadget::newFromDefinition) to properly reload preference descriptions after saving (like what $forceNewText does).
- Fixed a bug in GadgetHooks::userLoadOptions, which wouldn't work for an empty gadget list.
Modified paths:
  • /branches/salvatoreingala/Gadgets/backend/Gadget.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/backend/GadgetHooks.php (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/backend/Gadget.php
@@ -39,9 +39,12 @@
4040 /**
4141 * Creates an instance of this class from definition in MediaWiki:Gadgets-definition
4242 * @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.
4346 * @return Mixed: Instance of Gadget class or false if $definition is invalid
4447 */
45 - public static function newFromDefinition( $definition ) {
 48+ public static function newFromDefinition( $definition, $forcePrefsDescriptionsJson = null ) {
4649 $m = array();
4750 if ( !preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/', $definition, $m ) ) {
4851 return false;
@@ -53,9 +56,7 @@
5457 $gadget->name = trim( str_replace(' ', '_', $m[1] ) );
5558 $gadget->definition = $definition;
5659
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.
6061 $gadget->mTime = wfTimestamp( TS_UNIX );
6162
6263 //Parse gadget options
@@ -94,11 +95,24 @@
9596 }
9697
9798 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 );
103117 $gadget->setPrefsDescription( $prefsDescription );
104118 }
105119 }
@@ -331,14 +345,17 @@
332346 * 'sectionnname2' => array( $gadget3 ) );
333347 * @param $forceNewText String: New text of MediaWiki:gadgets-sdefinition. If specified, will
334348 * 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.
335352 * @return Mixed: Array or false
336353 */
337 - public static function loadStructuredList( $forceNewText = null ) {
 354+ public static function loadStructuredList( $forceNewText = null, $forcePrefsDescriptionsJson = null ) {
338355 global $wgMemc;
339356
340357 static $gadgets = null;
341358
342 - if ( $gadgets !== null && $forceNewText === null ) {
 359+ if ( $gadgets !== null && $forceNewText === null && $forcePrefsDescriptionsJson === null ) {
343360 return $gadgets;
344361 }
345362
@@ -353,7 +370,7 @@
354371 //Check again, loadStructuredList may have been called from UserLoadOptions hook handler;
355372 //in that case, we should just return current value instead of rebuilding the list again.
356373 //TODO: is there a better design?
357 - if ( $gadgets !== null && $forceNewText === null ) {
 374+ if ( $gadgets !== null && $forceNewText === null && $forcePrefsDescriptionsJson === null ) {
358375 wfProfileOut( __METHOD__ );
359376 return $gadgets;
360377 }
@@ -393,7 +410,7 @@
394411 $section = $m[1];
395412 }
396413 else {
397 - $gadget = self::newFromDefinition( $line );
 414+ $gadget = self::newFromDefinition( $line, $forcePrefsDescriptionsJson );
398415 if ( $gadget ) {
399416 $gadgets[$section][$gadget->getName()] = $gadget;
400417 $gadget->category = $section;
Index: branches/salvatoreingala/Gadgets/backend/GadgetHooks.php
@@ -29,12 +29,16 @@
3030 if( $title->getNamespace() == NS_MEDIAWIKI ) {
3131 if ( $title->getText() == 'Gadgets-definition' ) {
3232 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+ }
3943 }
4044 }
4145 }
@@ -218,12 +222,18 @@
219223
220224 wfProfileIn( __METHOD__ );
221225
 226+ $gadgets = Gadget::loadList();
 227+ if ( !$gadgets ) {
 228+ wfProfileOut( __METHOD__ );
 229+ return true;
 230+ }
 231+
222232 //Find out all existing gadget preferences and save them in a map
223233 $preferencesCache = array();
224234 foreach ( $options as $option => $value ) {
225235 $m = array();
226236 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] ) );
228238 $gadgetPrefs = unserialize( $value );
229239 if ( $gadgetPrefs !== false ) {
230240 $preferencesCache[$gadgetName] = $gadgetPrefs;
@@ -237,7 +247,6 @@
238248 }
239249
240250 //Record preferences for each gadget
241 - $gadgets = Gadget::loadList();
242251 foreach ( $gadgets as $gadget ) {
243252 $prefsDescription = $gadget->getPrefsDescription();
244253 if ( $prefsDescription !== null ) {

Status & tagging log