MediaWiki  REL1_23
convertUserOptions.php
Go to the documentation of this file.
00001 <?php
00024 require_once __DIR__ . '/Maintenance.php';
00025 
00031 class ConvertUserOptions extends Maintenance {
00032 
00033     private $mConversionCount = 0;
00034 
00035     public function __construct() {
00036         parent::__construct();
00037         $this->mDescription = "Convert user options from old to new system";
00038         $this->setBatchSize( 50 );
00039     }
00040 
00041     public function execute() {
00042         $this->output( "...batch conversion of user_options: " );
00043         $id = 0;
00044         $dbw = wfGetDB( DB_MASTER );
00045 
00046         if ( !$dbw->fieldExists( 'user', 'user_options', __METHOD__ ) ) {
00047             $this->output( "nothing to migrate. " );
00048             return;
00049         }
00050         while ( $id !== null ) {
00051             $res = $dbw->select( 'user',
00052                 array( 'user_id', 'user_options' ),
00053                 array(
00054                     'user_id > ' . $dbw->addQuotes( $id ),
00055                     "user_options != " . $dbw->addQuotes( '' ),
00056                 ),
00057                 __METHOD__,
00058                 array(
00059                     'ORDER BY' => 'user_id',
00060                     'LIMIT' => $this->mBatchSize,
00061                 )
00062             );
00063             $id = $this->convertOptionBatch( $res, $dbw );
00064 
00065             wfWaitForSlaves();
00066 
00067             if ( $id ) {
00068                 $this->output( "--Converted to ID $id\n" );
00069             }
00070         }
00071         $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
00072     }
00073 
00079     function convertOptionBatch( $res, $dbw ) {
00080         $id = null;
00081         foreach ( $res as $row ) {
00082             $this->mConversionCount++;
00083             $insertRows = array();
00084             foreach ( explode( "\n", $row->user_options ) as $s ) {
00085                 $m = array();
00086                 if ( !preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
00087                     continue;
00088                 }
00089 
00090                 // MW < 1.16 would save even default values. Filter them out
00091                 // here (as in User) to avoid adding many unnecessary rows.
00092                 $defaultOption = User::getDefaultOption( $m[1] );
00093                 if ( is_null( $defaultOption ) || $m[2] != $defaultOption ) {
00094                     $insertRows[] = array(
00095                         'up_user' => $row->user_id,
00096                         'up_property' => $m[1],
00097                         'up_value' => $m[2],
00098                     );
00099                 }
00100             }
00101 
00102             if ( count( $insertRows ) ) {
00103                 $dbw->insert( 'user_properties', $insertRows, __METHOD__, array( 'IGNORE' ) );
00104             }
00105 
00106             $dbw->update(
00107                 'user',
00108                 array( 'user_options' => '' ),
00109                 array( 'user_id' => $row->user_id ),
00110                 __METHOD__
00111             );
00112             $id = $row->user_id;
00113         }
00114 
00115         return $id;
00116     }
00117 }
00118 
00119 $maintClass = "ConvertUserOptions";
00120 require_once RUN_MAINTENANCE_IF_MAIN;