r55729 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55728‎ | r55729 | r55730 >
Date:20:53, 1 September 2009
Author:aaron
Status:ok (Comments)
Tags:
Comment:
Added a script to move users from one group to another
Modified paths:
  • /trunk/phase3/maintenance/migrateUserGroup.php (added) (history)

Diff [purge]

Index: trunk/phase3/maintenance/migrateUserGroup.php
@@ -0,0 +1,71 @@
 2+<?php
 3+/**
 4+ * Re-assign users from an old group to a new one
 5+ *
 6+ * This program is free software; you can redistribute it and/or modify
 7+ * it under the terms of the GNU General Public License as published by
 8+ * the Free Software Foundation; either version 2 of the License, or
 9+ * (at your option) any later version.
 10+ *
 11+ * This program is distributed in the hope that it will be useful,
 12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14+ * GNU General Public License for more details.
 15+ *
 16+ * You should have received a copy of the GNU General Public License along
 17+ * with this program; if not, write to the Free Software Foundation, Inc.,
 18+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 19+ * http://www.gnu.org/copyleft/gpl.html
 20+ *
 21+ * @ingroup Maintenance
 22+ */
 23+
 24+require_once( dirname(__FILE__) . '/Maintenance.php' );
 25+
 26+class MigrateUserGroup extends Maintenance {
 27+ public function __construct() {
 28+ parent::__construct();
 29+ $this->mDescription = "Re-assign users from an old group to a new one";
 30+ $this->addArg( 'oldgroup', 'Old user group key', true );
 31+ $this->addArg( 'newgroup', 'New user group key', true );
 32+ $this->setBatchSize( 200 );
 33+ }
 34+
 35+ public function execute() {
 36+ $count = 0;
 37+ $oldGroup = $this->getArg( 0 );
 38+ $newGroup = $this->getArg( 1 );
 39+ $dbr = wfGetDB( DB_SLAVE );
 40+ $start = $dbr->selectField( 'user_groups', 'MIN(ug_user)',
 41+ array('ug_group' => $oldGroup), __FUNCTION__ );
 42+ $end = $dbr->selectField( 'user_groups', 'MAX(ug_user)',
 43+ array('ug_group' => $oldGroup), __FUNCTION__ );
 44+ if( $start === null ) {
 45+ $this->error( "Nothing to do - no users in the '$oldGroup' group", true );
 46+ }
 47+ # Do remaining chunk
 48+ $end += $this->mBatchSize - 1;
 49+ $blockStart = $start;
 50+ $blockEnd = $start + $this->mBatchSize - 1;
 51+ // Migrate users over in batches...
 52+ $dbw = wfGetDB( DB_MASTER );
 53+ while( $blockEnd <= $end ) {
 54+ $this->output( "Doing users $blockStart to $blockEnd\n" );
 55+ $dbw->begin();
 56+ $dbw->update( 'user_groups',
 57+ array('ug_group' => $newGroup),
 58+ array('ug_group' => $oldGroup,
 59+ "ug_user BETWEEN $blockStart AND $blockEnd" )
 60+ );
 61+ $count += $dbw->affectedRows();
 62+ $dbw->commit();
 63+ $blockStart += $this->mBatchSize;
 64+ $blockEnd += $this->mBatchSize;
 65+ wfWaitForSlaves( 5 );
 66+ }
 67+ $this->output( "Done! $count user(s) in group '$oldGroup' are now in '$newGroup' instead.\n" );
 68+ }
 69+}
 70+
 71+$maintClass = "MigrateUserGroup";
 72+require_once( DO_MAINTENANCE );

Comments

#Comment by Simetrical (talk | contribs)   21:19, 7 September 2009

Why don't you just use LIMIT for batching?

Status & tagging log