r48730 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48729‎ | r48730 | r48731 >
Date:07:30, 24 March 2009
Author:werdna
Status:deferred
Tags:
Comment:
Store user options in the new user_properties table if and only if they differ from defaults. Includes backwards-compatible code, so a user's preferences are converted on-demand the next time they log in.
Modified paths:
  • /branches/preferences-work/phase3/includes/User.php (modified) (history)

Diff [purge]

Index: branches/preferences-work/phase3/includes/User.php
@@ -109,7 +109,6 @@
110110 'mNewpassword',
111111 'mNewpassTime',
112112 'mEmail',
113 - 'mOptions',
114113 'mTouched',
115114 'mToken',
116115 'mEmailAuthenticated',
@@ -119,6 +118,8 @@
120119 'mEditCount',
121120 // user_group table
122121 'mGroups',
 122+ // user_properties table
 123+ 'mOptions',
123124 );
124125
125126 /**
@@ -195,7 +196,7 @@
196197 /**
197198 * \bool Whether the cache variables have been loaded.
198199 */
199 - var $mDataLoaded, $mAuthLoaded;
 200+ var $mDataLoaded, $mAuthLoaded, $mOptionsLoaded;
200201
201202 /**
202203 * \string Initialization data source if mDataLoaded==false. May be one of:
@@ -311,6 +312,7 @@
312313 function saveToCache() {
313314 $this->load();
314315 $this->loadGroups();
 316+ $this->loadOptions();
315317 if ( $this->isAnon() ) {
316318 // Anonymous users are uncached
317319 return;
@@ -1867,7 +1869,7 @@
18681870 * @see getIntOption()
18691871 */
18701872 function getOption( $oname, $defaultOverride = '' ) {
1871 - $this->load();
 1873+ $this->loadOptions();
18721874
18731875 if ( is_null( $this->mOptions ) ) {
18741876 if($defaultOverride != '') {
@@ -2309,6 +2311,11 @@
23102312 * @private
23112313 */
23122314 function decodeOptions( $str ) {
 2315+ if ($str)
 2316+ $this->mOptionsLoaded = true;
 2317+ else
 2318+ return;
 2319+
23132320 $this->mOptions = array();
23142321 $a = explode( "\n", $str );
23152322 foreach ( $a as $s ) {
@@ -2423,7 +2430,7 @@
24242431 'user_real_name' => $this->mRealName,
24252432 'user_email' => $this->mEmail,
24262433 'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated ),
2427 - 'user_options' => $this->encodeOptions(),
 2434+ 'user_options' => '',
24282435 'user_touched' => $dbw->timestamp($this->mTouched),
24292436 'user_token' => $this->mToken,
24302437 'user_email_token' => $this->mEmailToken,
@@ -2432,6 +2439,9 @@
24332440 'user_id' => $this->mId
24342441 ), __METHOD__
24352442 );
 2443+
 2444+ $this->saveOptions();
 2445+
24362446 wfRunHooks( 'UserSaveSettings', array( $this ) );
24372447 $this->clearSharedCache();
24382448 $this->getUserPage()->invalidateCache();
@@ -3400,5 +3410,49 @@
34013411 $log->addEntry( 'autocreate', $this->getUserPage(), '', array( $this->getId() ) );
34023412 return true;
34033413 }
 3414+
 3415+ protected function loadOptions() {
 3416+ $this->load();
 3417+ if ($this->mOptionsLoaded || !$this->getId() )
 3418+ return;
 3419+
 3420+ // Load from database
 3421+ $dbr = wfGetDB( DB_SLAVE );
 3422+
 3423+ $res = $dbr->select( 'user_properties',
 3424+ '*',
 3425+ array('up_user' => $this->getId()),
 3426+ __METHOD__
 3427+ );
 3428+
 3429+ while( $row = $dbr->fetchObject( $res ) ) {
 3430+ $this->mOptions[$row->up_property] = unserialize( $row->up_value );
 3431+ }
 3432+ }
 3433+
 3434+ protected function saveOptions() {
 3435+ global $wgDefaultUserOptions;
 3436+ $dbw = wfGetDB( DB_MASTER );
 3437+
 3438+ $insert_rows = array();
 3439+
 3440+ foreach( $this->mOptions as $key => $value ) {
 3441+ $ser = serialize($value);
 3442+
 3443+ if ( isset($wgDefaultUserOptions[$key]) &&
 3444+ $value != $wgDefaultUserOptions[$key] ) {
 3445+ $insert_rows[] = array(
 3446+ 'up_user' => $this->getId(),
 3447+ 'up_property' => $key,
 3448+ 'up_value' => $ser,
 3449+ );
 3450+ }
 3451+ }
 3452+
 3453+ $dbw->begin();
 3454+ $dbw->delete( 'user_properties', array( 'up_user' => $this->getId() ), __METHOD__ );
 3455+ $dbw->insert( 'user_properties', $insert_rows, __METHOD__ );
 3456+ $dbw->commit();
 3457+ }
34043458
34053459 }

Status & tagging log