3 * Clean up user preferences from the database.
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
21 * @author TyA <tya.wiki@gmail.com>
22 * @author Chad <chad@wikimedia.org>
23 * @see https://phabricator.wikimedia.org/T32976
24 * @ingroup Maintenance
27 require_once __DIR__
. '/Maintenance.php';
30 * Maintenance script that removes bogus preferences from the database.
32 * @ingroup Maintenance
34 class CleanupPreferences
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addDescription(
38 'Clean up hidden preferences, removed preferences, and normalizes values'
40 $this->setBatchSize( 50 );
41 $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
42 $this->addOption( 'hidden', 'Drop hidden preferences ($wgHiddenPrefs)' );
43 $this->addOption( 'unknown',
44 'Drop unknown preferences (not in $wgDefaultUserOptions or prefixed with "userjs-")' );
45 // TODO: actually implement this
46 // $this->addOption( 'bogus', 'Drop preferences that have invalid/unaccepted values' );
50 * We will do this in three passes
51 * 1) The easiest is to drop the hidden preferences from the database. We
52 * don't actually want them
53 * 2) Drop preference keys that we don't know about. They could've been
54 * removed from core, provided by a now-disabled extension, or the result
55 * of a bug. We don't want them.
56 * 3) TODO: Normalize accepted preference values. This is the biggest part of the work.
57 * For each preference we know about, iterate over it and if it's got a
58 * limited set of accepted values (so it's not text, basically), make sure
59 * all values are in that range. Drop ones that aren't.
61 public function execute() {
62 global $wgHiddenPrefs, $wgDefaultUserOptions;
64 $dbw = $this->getDB( DB_MASTER
);
65 $hidden = $this->hasOption( 'hidden' );
66 $unknown = $this->hasOption( 'unknown' );
67 $bogus = $this->hasOption( 'bogus' );
69 if ( !$hidden && !$unknown && !$bogus ) {
70 $this->output( "Did not select one of --hidden, --unknown or --bogus, exiting\n" );
74 // Remove hidden prefs. Iterate over them to avoid the IN on a large table
76 if ( !$wgHiddenPrefs ) {
77 $this->output( "No hidden preferences, skipping\n" );
79 foreach ( $wgHiddenPrefs as $hiddenPref ) {
82 'Dropping hidden preferences',
83 [ 'up_property' => $hiddenPref ]
88 // Remove unknown preferences. Special-case 'userjs-' as we can't control those names.
91 'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ),
92 'up_property NOT IN (' . $dbw->makeList( array_keys( $wgDefaultUserOptions ) ) . ')',
94 // Allow extensions to add to the where clause to prevent deletion of their own prefs.
95 Hooks
::run( 'DeleteUnknownPreferences', [ &$where, $dbw ] );
96 $this->deleteByWhere( $dbw, 'Dropping unknown preferences', $where );
99 // Something something phase 3
104 private function deleteByWhere( $dbw, $startMessage, $where ) {
105 $this->output( $startMessage . "...\n" );
110 '*', // The table lacks a primary key, so select the whole row
113 [ 'LIMIT' => $this->mBatchSize
]
116 $numRows = $res->numRows();
118 if ( $res->numRows() <= 0 ) {
119 $this->output( "DONE! (handled $total entries)\n" );
123 // Progress or something
124 $this->output( "..doing $numRows entries\n" );
126 // Delete our batch, then wait
127 foreach ( $res as $row ) {
128 if ( $this->hasOption( 'dry-run' ) ) {
130 " DRY RUN, would drop: " .
131 "[up_user] => '{$row->up_user}' " .
132 "[up_property] => '{$row->up_property}' " .
133 "[up_value] => '{$row->up_value}'\n"
137 $this->beginTransaction( $dbw, __METHOD__
);
141 'up_user' => $row->up_user
,
142 'up_property' => $row->up_property
,
143 'up_value' => $row->up_value
,
147 $this->commitTransaction( $dbw, __METHOD__
);
153 $maintClass = CleanupPreferences
::class; // Tells it to run the class
154 require_once RUN_MAINTENANCE_IF_MAIN
;