Unbreak creating extension tables from the web installer
[lhc/web/wiklou.git] / maintenance / cleanupPreferences.php
1 <?php
2 /**
3 * Clean up user preferences from the database.
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 * @author TyA <tya.wiki@gmail.com>
22 * @author Chad <chad@wikimedia.org>
23 * @see https://phabricator.wikimedia.org/T32976
24 * @ingroup Maintenance
25 */
26
27 require_once __DIR__ . '/Maintenance.php';
28
29 /**
30 * Maintenance script that removes bogus preferences from the database.
31 *
32 * @ingroup Maintenance
33 */
34 class CleanupPreferences extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->mDescription = 'Clean up hidden preferences, removed preferences, and normalizes values';
38 $this->setBatchSize( 50 );
39 $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
40 $this->addOption( 'hidden', 'Drop hidden preferences ($wgHiddenPrefs)' );
41 $this->addOption( 'unknown',
42 'Drop unknown preferences (not in $wgDefaultUserOptions or a gadget or userjs)' );
43 // TODO: actually implement this
44 // $this->addOption( 'bogus', 'Drop preferences that have invalid/unaccepted values' );
45 }
46
47 /**
48 * We will do this in three passes
49 * 1) The easiest is to drop the hidden preferences from the database. We
50 * don't actually want them
51 * 2) Drop preference keys that we don't know about. They could've been
52 * removed from core, provided by a now-disabled extension, or the result
53 * of a bug. We don't want them.
54 * 3) TODO: Normalize accepted preference values. This is the biggest part of the work.
55 * For each preference we know about, iterate over it and if it's got a
56 * limited set of accepted values (so it's not text, basically), make sure
57 * all values are in that range. Drop ones that aren't.
58 */
59 public function execute() {
60 global $wgHiddenPrefs, $wgDefaultUserOptions;
61
62 $dbw = $this->getDB( DB_MASTER );
63 $didWork = false;
64 $hidden = $this->hasOption( 'hidden' );
65 $unknown = $this->hasOption( 'unknown' );
66 $bogus = $this->hasOption( 'bogus' );
67
68 if ( !$hidden && !$unknown && !$bogus ) {
69 $this->output( "Did not select one of --hidden, --unknown or --bogus, exiting\n" );
70 return;
71 }
72
73 // Remove hidden prefs. Iterate over them to avoid the IN on a large table
74 if ( $hidden ) {
75 if ( !$wgHiddenPrefs ) {
76 $this->output( "No hidden preferences, skipping\n" );
77 }
78 foreach ( $wgHiddenPrefs as $hiddenPref ) {
79 $this->deleteByWhere(
80 $dbw,
81 'Dropping hidden preferences',
82 [ 'up_property' => $hiddenPref ]
83 );
84 }
85 }
86
87 // Remove unknown preferences. Special-case gadget- and userjs- as we can't
88 // control those names.
89 if ( $unknown ) {
90 $where = [
91 'up_property NOT' . $dbw->buildLike( 'gadget-', $dbw->anyString() ),
92 'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ),
93 'up_property NOT IN (' . $dbw->makeList( array_keys( $wgDefaultUserOptions ) ) . ')',
94 ];
95 // Allow extensions to add to the where clause to prevent deletion of their own prefs.
96 Hooks::run( 'DeleteUnknownPreferences', [ &$where, $dbw ] );
97 $this->deleteByWhere( $dbw, 'Dropping unknown preferences', $where );
98 }
99
100 // Something something phase 3
101 if ( $bogus ) {
102 }
103 }
104
105 /**
106 *
107 */
108 private function deleteByWhere( $dbw, $startMessage, $where ) {
109 $this->output( $startMessage . "...\n" );
110 $total = 0;
111 while ( true ) {
112 $res = $dbw->select(
113 'user_properties',
114 '*', // The table lacks a primary key, so select the whole row
115 $where,
116 __METHOD__,
117 [ 'LIMIT' => $this->mBatchSize ]
118 );
119
120 $numRows = $res->numRows();
121 $total += $numRows;
122 if ( $res->numRows() <= 0 ) {
123 // All done!
124 $this->output( "DONE! (handled $total entries)\n" );
125 break;
126 }
127
128 // Progress or something
129 $this->output( "..doing $numRows entries\n" );
130
131 // Delete our batch, then wait
132 foreach ( $res as $row ) {
133 if ( $this->hasOption( 'dry-run' ) ) {
134 $this->output(
135 " DRY RUN, would drop: " .
136 "[up_user] => '{$row->up_user}' " .
137 "[up_property] => '{$row->up_property}' " .
138 "[up_value] => '{$row->up_value}'\n"
139 );
140 continue;
141 }
142 $this->beginTransaction( $dbw, __METHOD__ );
143 $dbw->delete(
144 'user_properties',
145 [
146 'up_user' => $row->up_user,
147 'up_property' => $row->up_property,
148 'up_value' => $row->up_value,
149 ],
150 __METHOD__
151 );
152 $this->commitTransaction( $dbw, __METHOD__ );
153 }
154 }
155 }
156 }
157
158 $maintClass = CleanupPreferences::class; // Tells it to run the class
159 require_once RUN_MAINTENANCE_IF_MAIN;