Fix bogus variable use in RefreshLinksJob::run()
[mediawiki.git] / maintenance / convertUserOptions.php
blob675d0695d9047789b7621abab9666a7b2a16e023
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 use Wikimedia\Rdbms\ResultWrapper;
27 use Wikimedia\Rdbms\IDatabase;
29 /**
30 * Maintenance script to convert user options to the new `user_properties` table.
32 * @ingroup Maintenance
34 class ConvertUserOptions extends Maintenance {
36 private $mConversionCount = 0;
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Convert user options from old to new system' );
41 $this->setBatchSize( 50 );
44 public function execute() {
45 $this->output( "...batch conversion of user_options: " );
46 $id = 0;
47 $dbw = $this->getDB( DB_MASTER );
49 if ( !$dbw->fieldExists( 'user', 'user_options', __METHOD__ ) ) {
50 $this->output( "nothing to migrate. " );
52 return;
54 while ( $id !== null ) {
55 $res = $dbw->select( 'user',
56 [ 'user_id', 'user_options' ],
58 'user_id > ' . $dbw->addQuotes( $id ),
59 "user_options != " . $dbw->addQuotes( '' ),
61 __METHOD__,
63 'ORDER BY' => 'user_id',
64 'LIMIT' => $this->mBatchSize,
67 $id = $this->convertOptionBatch( $res, $dbw );
69 wfWaitForSlaves();
71 if ( $id ) {
72 $this->output( "--Converted to ID $id\n" );
75 $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
78 /**
79 * @param ResultWrapper $res
80 * @param IDatabase $dbw
81 * @return null|int
83 function convertOptionBatch( $res, $dbw ) {
84 $id = null;
85 foreach ( $res as $row ) {
86 $this->mConversionCount++;
87 $insertRows = [];
88 foreach ( explode( "\n", $row->user_options ) as $s ) {
89 $m = [];
90 if ( !preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
91 continue;
94 // MW < 1.16 would save even default values. Filter them out
95 // here (as in User) to avoid adding many unnecessary rows.
96 $defaultOption = User::getDefaultOption( $m[1] );
97 if ( is_null( $defaultOption ) || $m[2] != $defaultOption ) {
98 $insertRows[] = [
99 'up_user' => $row->user_id,
100 'up_property' => $m[1],
101 'up_value' => $m[2],
106 if ( count( $insertRows ) ) {
107 $dbw->insert( 'user_properties', $insertRows, __METHOD__, [ 'IGNORE' ] );
110 $dbw->update(
111 'user',
112 [ 'user_options' => '' ],
113 [ 'user_id' => $row->user_id ],
114 __METHOD__
116 $id = $row->user_id;
119 return $id;
123 $maintClass = "ConvertUserOptions";
124 require_once RUN_MAINTENANCE_IF_MAIN;