r90272 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90271‎ | r90272 | r90273 >
Date:15:15, 17 June 2011
Author:salvatoreingala
Status:deferred (Comments)
Tags:
Comment:
- Moved the gadget preference info to a private member; had to use a hack, though.
- Minor changes.
Modified paths:
  • /branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/Gadgets_body.php (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/Gadgets_body.php
@@ -166,42 +166,35 @@
167167 return true;
168168 }
169169
170 - //Remove gadget-*-config options, since they must not be delivered
171 - //via mw.user.options like other user preferences
172 - $gadgets = Gadget::loadList();
173 -
174 - if ( !$gadgets ) {
175 - return true;
176 - }
177 -
178 - $existingPrefs = array();
 170+ //Find out all existing gadget preferences and save them in a map
 171+ $preferencesCache = array();
179172 foreach ( $options as $option => $value ) {
180173 $m = array();
181174 if ( preg_match( '/gadget-([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)-config/', $option, $m ) ) {
182175 $gadgetName = $m[1];
183 - $existingPrefs[$gadgetName] = $value;
 176+ $preferencesCache[$gadgetName] = FormatJson::decode( $value, true );
184177 unset( $options[$option] );
185178 }
186179 }
187180
 181+ //Record preferences for each gadget
 182+ $gadgets = Gadget::loadList();
188183 foreach ( $gadgets as $gadget ) {
189 - $prefsDescription = $gadget->getPrefsDescription();
190 -
191 - if ( $prefsDescription === null ) {
192 - continue;
 184+ if ( isset( $preferencesCache[$gadget->getName()] ) ) {
 185+ if ( $gadget->getPrefsDescription() !== null ) {
 186+ if ( isset( $preferencesCache[$gadget->getName()] ) ) {
 187+ $userPrefs = $preferencesCache[$gadget->getName()];
 188+ }
 189+
 190+ if ( !isset( $userPrefs ) ) {
 191+ $userPrefs = array(); //no saved prefs (or invalid JSON), use defaults
 192+ }
 193+
 194+ Gadget::matchPrefsWithDescription( $gadget->getPrefsDescription(), $userPrefs );
 195+
 196+ $gadget->setPrefs( $userPrefs );
 197+ }
193198 }
194 -
195 - if ( isset( $existingPrefs[$gadget->getName()] ) ) {
196 - $userPrefs = FormatJson::decode( $existingPrefs[$gadget->getName()], true );
197 - }
198 -
199 - if ( !isset( $userPrefs ) ) {
200 - $userPrefs = array(); //no saved prefs, use defaults
201 - }
202 -
203 - Gadget::matchPrefsWithDescription( $prefsDescription, $userPrefs );
204 -
205 - $gadget->setPrefs( $userPrefs );
206199 }
207200
208201 return true;
@@ -291,15 +284,10 @@
292285 $resourceLoaded = false,
293286 $requiredRights = array(),
294287 $onByDefault = false,
295 - $category;
 288+ $category,
 289+ $preferences = null;
296290
297291
298 - //Map from gadget names to preferences of current user.
299 - //This is needed because gadget preferences are retrieved and saved
300 - //in UserLoadOptions and UserSaveOptions hooks handlers instead than
301 - //in Gadget class constructor.
302 - private static $preferencesCache = array();
303 -
304292 //Syntax specifications of preference description language
305293 private static $prefsDescriptionSpecifications = array(
306294 'boolean' => array(
@@ -736,6 +724,7 @@
737725 return true; // empty array
738726 }
739727
 728+
740729 /**
741730 * Loads list of gadgets and returns it as associative array of sections with gadgets
742731 * e.g. array( 'sectionnname1' => array( $gadget1, $gadget2),
@@ -748,14 +737,30 @@
749738 global $wgMemc;
750739
751740 static $gadgets = null;
752 - if ( $gadgets !== null && $forceNewText === null ) return $gadgets;
 741+
 742+ if ( $gadgets !== null && $forceNewText === null ) {
 743+ return $gadgets;
 744+ }
753745
754746 wfProfileIn( __METHOD__ );
 747+
 748+ $user = RequestContext::getMain()->getUser();
 749+ if ( $user->isLoggedIn() ) {
 750+ //Force loading user options
 751+ //HACK: this may lead to loadStructuredList being recursively called.
 752+ $user->getOptions();
 753+
 754+ //Check again, loadStructuredList may have been called from UserLoadOptions hook handler;
 755+ //in that case, we should just return current value instead of rebuilding the list again.
 756+ //TODO: is there a better design?
 757+ if ( $gadgets !== null && $forceNewText === null ) {
 758+ wfProfileOut( __METHOD__ );
 759+ return $gadgets;
 760+ }
 761+ }
 762+
755763 $key = wfMemcKey( 'gadgets-definition', self::GADGET_CLASS_VERSION );
756764
757 - //Force loading user options
758 - RequestContext::getMain()->getUser()->getOptions();
759 -
760765 if ( $forceNewText === null ) {
761766 //cached?
762767 $gadgets = $wgMemc->get( $key );
@@ -905,7 +910,7 @@
906911 //Check if a preference is valid, according to description
907912 //NOTE: we pass both $prefs and $prefName (instead of just $prefs[$prefName])
908913 // to allow checking for null.
909 - private static function checkSinglePref( &$prefDescription, &$prefs, $prefName ) {
 914+ private static function checkSinglePref( $prefDescription, $prefs, $prefName ) {
910915
911916 //isset( $prefs[$prefName] ) would return false for null values
912917 if ( !array_key_exists( $prefName, $prefs ) ) {
@@ -1034,8 +1039,7 @@
10351040 * @return Mixed the array of preferences if they have been set, null otherwise.
10361041 */
10371042 public function getPrefs() {
1038 - $prefs = self::$preferencesCache[$this->getName()];
1039 - return self::$preferencesCache[$this->getName()];
 1043+ return $this->preferences;
10401044 }
10411045
10421046 /**
@@ -1065,9 +1069,9 @@
10661070 if ( !self::checkPrefsAgainstDescription( $prefsDescription, $prefs ) ) {
10671071 return false; //validation failed
10681072 }
1069 -
1070 - self::$preferencesCache[$this->getName()] = $prefs;
10711073
 1074+ $this->preferences = $prefs;
 1075+
10721076 if ( $savePrefs ) {
10731077 $user = RequestContext::getMain()->getUser();
10741078 $user->saveSettings();
@@ -1115,7 +1119,6 @@
11161120 }
11171121
11181122 public function getScript( ResourceLoaderContext $context ) {
1119 -
11201123 $prefs = $this->gadget->getPrefs();
11211124
11221125 //Enclose gadget's code in a closure, with "this" bound to the
Index: branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php
@@ -32,6 +32,7 @@
3333 }
3434
3535 $gadgetName = $params['gadget'];
 36+
3637 $gadgets = Gadget::loadList();
3738 $gadget = $gadgets && isset( $gadgets[$gadgetName] ) ? $gadgets[$gadgetName] : null;
3839
@@ -47,6 +48,10 @@
4849
4950 $userPrefs = $gadget->getPrefs();
5051
 52+ if ( $userPrefs === null ) {
 53+ throw new MWException( __METHOD__ . ': $userPrefs should not be null.' );
 54+ }
 55+
5156 //Add user preferences to preference description
5257 foreach ( $userPrefs as $pref => $value ) {
5358 $prefsDescription['fields'][$pref]['value'] = $value;

Follow-up revisions

RevisionCommit summaryAuthorDate
r90283Using dieUsage instead of MWException. Fixes r90272.salvatoreingala16:06, 17 June 2011

Comments

#Comment by Reedy (talk | contribs)   15:16, 17 June 2011

Don't just through exceptions in the API, use dieUsage/dieUsageMsg

That way it will fail somewhat gracefully

#Comment by Salvatore Ingala (talk | contribs)   15:20, 17 June 2011

I thought an exception was better because that's a condition that should never happen (even if params are wrong); that is, if it happens, that's a bug elsewhere. Should I use dieUsage for these cases?

Status & tagging log