MediaWiki  REL1_22
convertUserOptions.php
Go to the documentation of this file.
00001 <?php
00024 require_once __DIR__ . '/Maintenance.php';
00025 
00033 class ConvertUserOptions extends Maintenance {
00034 
00035     private $mConversionCount = 0;
00036 
00037     public function __construct() {
00038         parent::__construct();
00039         $this->mDescription = "Convert user options from old to new system";
00040     }
00041 
00042     public function execute() {
00043         $this->output( "...batch conversion of user_options: " );
00044         $id = 0;
00045         $dbw = wfGetDB( DB_MASTER );
00046 
00047         if ( !$dbw->fieldExists( 'user', 'user_options', __METHOD__ ) ) {
00048             $this->output( "nothing to migrate. " );
00049             return;
00050         }
00051         while ( $id !== null ) {
00052             $idCond = 'user_id > ' . $dbw->addQuotes( $id );
00053             $optCond = "user_options != " . $dbw->addQuotes( '' ); // For compatibility
00054             $res = $dbw->select( 'user', '*',
00055                 array( $optCond, $idCond ), __METHOD__,
00056                 array( 'LIMIT' => 50, 'FOR UPDATE' )
00057             );
00058             $id = $this->convertOptionBatch( $res, $dbw );
00059             $dbw->commit( __METHOD__ );
00060 
00061             wfWaitForSlaves();
00062 
00063             if ( $id ) {
00064                 $this->output( "--Converted to ID $id\n" );
00065             }
00066         }
00067         $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
00068     }
00069 
00075     function convertOptionBatch( $res, $dbw ) {
00076         $id = null;
00077         foreach ( $res as $row ) {
00078             $this->mConversionCount++;
00079 
00080             $u = User::newFromRow( $row );
00081 
00082             $u->saveSettings();
00083 
00084             // Do this here as saveSettings() doesn't set user_options to '' anymore!
00085             $dbw->update(
00086                 'user',
00087                 array( 'user_options' => '' ),
00088                 array( 'user_id' => $row->user_id ),
00089                 __METHOD__
00090             );
00091             $id = $row->user_id;
00092         }
00093 
00094         return $id;
00095     }
00096 }
00097 
00098 $maintClass = "ConvertUserOptions";
00099 require_once RUN_MAINTENANCE_IF_MAIN;