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