test: helper to skip tests depending on a PHP ext
[mediawiki.git] / maintenance / convertUserOptions.php
blobe2223e1a78d3986b12972d47d5d02a593c912e82
1 <?php
2 /**
3 * Convert user options to the new `user_properties` table.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 require_once( __DIR__ . '/Maintenance.php' );
26 /**
27 * Maintenance script to convert user options to the new `user_properties` table.
29 * Do each user sequentially, since accounts can't be deleted
31 * @ingroup Maintenance
33 class ConvertUserOptions extends Maintenance {
35 private $mConversionCount = 0;
37 public function __construct() {
38 parent::__construct();
39 $this->mDescription = "Convert user options from old to new system";
42 public function execute() {
43 $this->output( "...batch conversion of user_options: " );
44 $id = 0;
45 $dbw = wfGetDB( DB_MASTER );
47 if ( !$dbw->fieldExists( 'user', 'user_options', __METHOD__ ) ) {
48 $this->output( "nothing to migrate. " );
49 return;
51 while ( $id !== null ) {
52 $idCond = 'user_id > ' . $dbw->addQuotes( $id );
53 $optCond = "user_options != " . $dbw->addQuotes( '' ); // For compatibility
54 $res = $dbw->select( 'user', '*',
55 array( $optCond, $idCond ), __METHOD__,
56 array( 'LIMIT' => 50, 'FOR UPDATE' )
58 $id = $this->convertOptionBatch( $res, $dbw );
59 $dbw->commit( __METHOD__ );
61 wfWaitForSlaves();
63 if ( $id ) {
64 $this->output( "--Converted to ID $id\n" );
67 $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
70 /**
71 * @param $res
72 * @param $dbw DatabaseBase
73 * @return null|int
75 function convertOptionBatch( $res, $dbw ) {
76 $id = null;
77 foreach ( $res as $row ) {
78 $this->mConversionCount++;
80 $u = User::newFromRow( $row );
82 $u->saveSettings();
84 // Do this here as saveSettings() doesn't set user_options to '' anymore!
85 $dbw->update(
86 'user',
87 array( 'user_options' => '' ),
88 array( 'user_id' => $row->user_id ),
89 __METHOD__
91 $id = $row->user_id;
94 return $id;
98 $maintClass = "ConvertUserOptions";
99 require_once( RUN_MAINTENANCE_IF_MAIN );