r90322 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90321‎ | r90322 | r90323 >
Date:21:55, 17 June 2011
Author:salvatoreingala
Status:deferred (Comments)
Tags:
Comment:
- Using serialize()/unserialize() to save and retrieve from DB.
- Fixed a bug in preference retrieval.
Modified paths:
  • /branches/salvatoreingala/Gadgets/Gadgets_body.php (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/Gadgets_body.php
@@ -172,7 +172,16 @@
173173 $m = array();
174174 if ( preg_match( '/gadget-([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)-config/', $option, $m ) ) {
175175 $gadgetName = $m[1];
176 - $preferencesCache[$gadgetName] = FormatJson::decode( $value, true );
 176+ wfSuppressWarnings();
 177+ $gadgetPrefs = unserialize( $value );
 178+ wfRestoreWarnings();
 179+ if ( $gadgetPrefs !== false ) {
 180+ $preferencesCache[$gadgetName] = $gadgetPrefs;
 181+ } else {
 182+ //should not happen; just in case
 183+ wfDebug( __METHOD__ . ": couldn't unserialize settings for gadget " .
 184+ "$gadgetName and user {$curUser->getID()}. Ignoring.\n" );
 185+ }
177186 unset( $options[$option] );
178187 }
179188 }
@@ -180,20 +189,19 @@
181190 //Record preferences for each gadget
182191 $gadgets = Gadget::loadList();
183192 foreach ( $gadgets as $gadget ) {
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 );
 193+ $prefsDescription = $gadget->getPrefsDescription();
 194+ if ( $prefsDescription !== null ) {
 195+ if ( isset( $preferencesCache[$gadget->getName()] ) ) {
 196+ $userPrefs = $preferencesCache[$gadget->getName()];
197197 }
 198+
 199+ if ( !isset( $userPrefs ) ) {
 200+ $userPrefs = array(); //no saved prefs (or invalid entry in DB), use defaults
 201+ }
 202+
 203+ Gadget::matchPrefsWithDescription( $prefsDescription, $userPrefs );
 204+
 205+ $gadget->setPrefs( $userPrefs );
198206 }
199207 }
200208
@@ -223,8 +231,8 @@
224232 if ( $gadget->getPrefs() !== null ) {
225233 //TODO: should remove prefs that equal their default
226234
227 - $prefsJson = FormatJson::encode( $gadget->getPrefs() );
228 - $options["gadget-{$gadget->getName()}-config"] = $prefsJson;
 235+ $prefsSerialized = serialize( $gadget->getPrefs() );
 236+ $options["gadget-{$gadget->getName()}-config"] = $prefsSerialized;
229237 }
230238 }
231239

Follow-up revisions

RevisionCommit summaryAuthorDate
r90705Follow up on r90322: removed unserialize() warning suppressionsalvatoreingala09:53, 24 June 2011

Comments

#Comment by 😂 (talk | contribs)   23:31, 17 June 2011

Is there a reason you're suppressing the unserialize error?

#Comment by Salvatore Ingala (talk | contribs)   08:11, 18 June 2011

Since ignoring preferences (thus returning defaults) is a safe way to recover from problems of inconsistent state, I thought a debug message was enough. Maybe I was wrong: I admit that I actually don't have in mind a clear distinction between use-cases of wfDebug-messages and E_NOTICEs.

Status & tagging log