r91065 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91064‎ | r91065 | r91066 >
Date:10:46, 29 June 2011
Author:catrope
Status:resolved (Comments)
Tags:
Comment:
Vector: Add maintenance script to switch a preference for users with the noexperiments preference enabled. This is needed for the upcoming WikiLove deployment
Modified paths:
  • /trunk/extensions/Vector/switchExperimentPrefs.php (added) (history)

Diff [purge]

Index: trunk/extensions/Vector/switchExperimentPrefs.php
@@ -0,0 +1,64 @@
 2+<?php
 3+
 4+$path = '../..';
 5+
 6+if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
 7+ $path = getenv( 'MW_INSTALL_PATH' );
 8+}
 9+
 10+require_once( $path . '/maintenance/Maintenance.php' );
 11+
 12+class SwitchExperimentPrefs extends Maintenance {
 13+ function __construct() {
 14+ parent::__construct();
 15+ $this->addOption( 'pref', 'Preference to set', true, true );
 16+ $this->addOption( 'value', 'Value to set the preference to', true, true );
 17+ $this->mDescription = 'Set a preference for all users that have the vector-noexperiments preference enabled.';
 18+ }
 19+
 20+ function execute() {
 21+ $dbw = wfGetDB( DB_MASTER );
 22+
 23+ $batchSize = 100;
 24+ $total = 0;
 25+ $lastUserID = 0;
 26+ while ( true ) {
 27+ $dbw->begin();
 28+ $res = $dbw->select( 'user_properties', array( 'up_user' ),
 29+ array( 'up_property' => 'vector-noexperiments', "up_user > $lastUserID" ),
 30+ __METHOD__,
 31+ array( 'LIMIT' => $batchSize ) );
 32+ if ( !$res->numRows() ) {
 33+ $dbw->commit();
 34+ break;
 35+ }
 36+ $total += $res->numRows();
 37+
 38+ $ids = array();
 39+ foreach ( $res as $row ) {
 40+ $ids[] = $row->up_user;
 41+ }
 42+ $lastUserID = max( $ids );
 43+
 44+
 45+ foreach ( $ids as $id ) {
 46+ $user = User::newFromId( $id );
 47+ if ( !$user->isLoggedIn() )
 48+ continue;
 49+ $user->setOption( $this->getOption( 'pref' ), $this->getOption( 'value' ) );
 50+ $user->saveSettings();
 51+ }
 52+
 53+ echo "$total\n";
 54+
 55+ wfWaitForSlaves(); // Must be wfWaitForSlaves_masterPos(); on 1.17wmf1
 56+ }
 57+ echo "Done\n";
 58+
 59+ }
 60+}
 61+
 62+$maintClass = 'SwitchExperimentPrefs';
 63+require_once( DO_MAINTENANCE );
 64+
 65+
Property changes on: trunk/extensions/Vector/switchExperimentPrefs.php
___________________________________________________________________
Added: svn:eol-style
166 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r910681.17wmf1: MFT r91065catrope11:06, 29 June 2011
r91163Fixes for r91065 per CR: drop stray begin() call and use RUN_MAINTENANCE_IF_MAINcatrope09:10, 30 June 2011

Comments

#Comment by 😂 (talk | contribs)   16:57, 29 June 2011
  • You're calling begin() on every iteration, but only commit() when there are no more results? Just looks wrong :)
  • Also, use RUN_MAINTENANCE_IF_MAIN instead of DO_MAINTENANCE for good form.
  • Finally: is there a reason you're only changing the preference if the person is logged in? Maybe...couldn't tell immediately.
#Comment by Catrope (talk | contribs)   19:12, 29 June 2011
  • Whoops that begin() is a leftover, I meant to kill all the transaction stuff.
  • Is this a new convention? I copied that from somewhere else (admittedly an unversioned script)
  • Anons don't have prefs
#Comment by 😂 (talk | contribs)   19:15, 29 June 2011
  • Ok
  • Not terribly new...all core scripts use it already
  • isAnon()?

Also to below....yes

#Comment by Catrope (talk | contribs)   19:16, 29 June 2011

isAnon) and !isLoggedIn() mean the same thing.

#Comment by 😂 (talk | contribs)   19:34, 29 June 2011

Now that I look at it further, I'm not entirely sure why the check is needed. How could you get anonymous users from your user_property results?

#Comment by Catrope (talk | contribs)   10:15, 30 June 2011

That's not really expected to happen, no, I'm just being paranoid.

#Comment by Nikerabbit (talk | contribs)   10:17, 30 June 2011

Being paranoid helps sometimes. I had throw MWException( "Should never happen" ) in my extension and it caught a bug in my refactoring :)

#Comment by Catrope (talk | contribs)   19:13, 29 June 2011

Also, does RUN_MAINTENANCE_IF_MAIN work in 1.17wmf1?

Status & tagging log