phpcs: More require/include is not a function
[lhc/web/wiklou.git] / maintenance / convertUserOptions.php
1 <?php
2 /**
3 * Convert user options to the new `user_properties` table.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script to convert user options to the new `user_properties` table.
28 *
29 * Do each user sequentially, since accounts can't be deleted
30 *
31 * @ingroup Maintenance
32 */
33 class ConvertUserOptions extends Maintenance {
34
35 private $mConversionCount = 0;
36
37 public function __construct() {
38 parent::__construct();
39 $this->mDescription = "Convert user options from old to new system";
40 }
41
42 public function execute() {
43 $this->output( "...batch conversion of user_options: " );
44 $id = 0;
45 $dbw = wfGetDB( DB_MASTER );
46
47 if ( !$dbw->fieldExists( 'user', 'user_options', __METHOD__ ) ) {
48 $this->output( "nothing to migrate. " );
49 return;
50 }
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' )
57 );
58 $id = $this->convertOptionBatch( $res, $dbw );
59 $dbw->commit( __METHOD__ );
60
61 wfWaitForSlaves();
62
63 if ( $id ) {
64 $this->output( "--Converted to ID $id\n" );
65 }
66 }
67 $this->output( "done. Converted " . $this->mConversionCount . " user records.\n" );
68 }
69
70 /**
71 * @param $res
72 * @param $dbw DatabaseBase
73 * @return null|int
74 */
75 function convertOptionBatch( $res, $dbw ) {
76 $id = null;
77 foreach ( $res as $row ) {
78 $this->mConversionCount++;
79
80 $u = User::newFromRow( $row );
81
82 $u->saveSettings();
83
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__
90 );
91 $id = $row->user_id;
92 }
93
94 return $id;
95 }
96 }
97
98 $maintClass = "ConvertUserOptions";
99 require_once RUN_MAINTENANCE_IF_MAIN;