MediaWiki  REL1_24
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 
00049             return;
00050         }
00051         while ( $id !== null ) {
00052             $res = $dbw->select( 'user',
00053                 array( 'user_id', 'user_options' ),
00054                 array(
00055                     'user_id > ' . $dbw->addQuotes( $id ),
00056                     "user_options != " . $dbw->addQuotes( '' ),
00057                 ),
00058                 __METHOD__,
00059                 array(
00060                     'ORDER BY' => 'user_id',
00061                     'LIMIT' => $this->mBatchSize,
00062                 )
00063             );
00064             $id = $this->convertOptionBatch( $res, $dbw );
00065 
00066             wfWaitForSlaves();
00067 
00068             if ( $id ) {
00069                 $this->output( "--Converted to ID $id\n" );
00070             }
00071         }
00072         $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
00073     }
00074 
00080     function convertOptionBatch( $res, $dbw ) {
00081         $id = null;
00082         foreach ( $res as $row ) {
00083             $this->mConversionCount++;
00084             $insertRows = array();
00085             foreach ( explode( "\n", $row->user_options ) as $s ) {
00086                 $m = array();
00087                 if ( !preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
00088                     continue;
00089                 }
00090 
00091                 // MW < 1.16 would save even default values. Filter them out
00092                 // here (as in User) to avoid adding many unnecessary rows.
00093                 $defaultOption = User::getDefaultOption( $m[1] );
00094                 if ( is_null( $defaultOption ) || $m[2] != $defaultOption ) {
00095                     $insertRows[] = array(
00096                         'up_user' => $row->user_id,
00097                         'up_property' => $m[1],
00098                         'up_value' => $m[2],
00099                     );
00100                 }
00101             }
00102 
00103             if ( count( $insertRows ) ) {
00104                 $dbw->insert( 'user_properties', $insertRows, __METHOD__, array( 'IGNORE' ) );
00105             }
00106 
00107             $dbw->update(
00108                 'user',
00109                 array( 'user_options' => '' ),
00110                 array( 'user_id' => $row->user_id ),
00111                 __METHOD__
00112             );
00113             $id = $row->user_id;
00114         }
00115 
00116         return $id;
00117     }
00118 }
00119 
00120 $maintClass = "ConvertUserOptions";
00121 require_once RUN_MAINTENANCE_IF_MAIN;