Fixed a typo in r107054
[lhc/web/wiklou.git] / maintenance / convertUserOptions.php
index 1d58919..da6ff9b 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
+ * @file
  * @ingroup Maintenance
  */
 
-require_once( dirname(__FILE__) . '/Maintenance.php' );
+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 
 class ConvertUserOptions extends Maintenance {
 
@@ -30,43 +31,62 @@ class ConvertUserOptions extends Maintenance {
                parent::__construct();
                $this->mDescription = "Convert user options from old to new system";
        }
-       
+
        public function execute() {
-               $this->output( "Beginning batch conversion of user options.\n" );
+               $this->output( "...batch conversion of user_options: " );
                $id = 0;
                $dbw = wfGetDB( DB_MASTER );
 
-               while ($id !== null) {
-                       $idCond = 'user_id>'.$dbw->addQuotes( $id );
-                       $optCond = "user_options!=".$dbw->addQuotes( '' ); // For compatibility
+               if ( !$dbw->fieldExists( 'user', 'user_options', __METHOD__ ) ) {
+                       $this->output( "nothing to migrate. " );
+                       return;
+               }
+               while ( $id !== null ) {
+                       $idCond = 'user_id > ' . $dbw->addQuotes( $id );
+                       $optCond = "user_options != " . $dbw->addQuotes( '' ); // For compatibility
                        $res = $dbw->select( 'user', '*',
-                                       array( $optCond, $idCond ), __METHOD__,
-                                       array( 'LIMIT' => 50, 'FOR UPDATE' ) );
+                               array( $optCond, $idCond ), __METHOD__,
+                               array( 'LIMIT' => 50, 'FOR UPDATE' )
+                       );
                        $id = $this->convertOptionBatch( $res, $dbw );
                        $dbw->commit();
-       
-                       wfWaitForSlaves( 1 );
-       
-                       if ($id)
+
+                       wfWaitForSlaves();
+
+                       if ( $id ) {
                                $this->output( "--Converted to ID $id\n" );
+                       }
                }
-               $this->output( "Conversion done. Converted " . $this->mConversionCount . " user records.\n" );
+               $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
        }
 
+       /**
+        * @param $res
+        * @param $dbw DatabaseBase
+        * @return null|int
+        */
        function convertOptionBatch( $res, $dbw ) {
                $id = null;
-               while ($row = $dbw->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $this->mConversionCount++;
-       
+
                        $u = User::newFromRow( $row );
-       
+
                        $u->saveSettings();
+
+                       // Do this here as saveSettings() doesn't set user_options to '' anymore!
+                       $dbw->update(
+                               'user',
+                               array( 'user_options' => '' ),
+                               array( 'user_id' => $row->user_id ),
+                               __METHOD__
+                       );
                        $id = $row->user_id;
                }
-       
+
                return $id;
        }
 }
 
 $maintClass = "ConvertUserOptions";
-require_once( DO_MAINTENANCE );
+require_once( RUN_MAINTENANCE_IF_MAIN );