MediaWiki
master
Toggle main menu visibility
cleanupPreferences.php
Go to the documentation of this file.
1
<?php
12
13
// @codeCoverageIgnoreStart
14
require_once __DIR__ .
'/Maintenance.php'
;
15
// @codeCoverageIgnoreEnd
16
17
use
MediaWiki\MainConfigNames
;
18
use
MediaWiki\Maintenance\Maintenance
;
19
use
MediaWiki\User\Options\UserOptionsLookup
;
20
use
MediaWiki\Utils\BatchRowIterator
;
21
use
Wikimedia\Rdbms\IExpression
;
22
use
Wikimedia\Rdbms\IReadableDatabase
;
23
use
Wikimedia\Rdbms\LikeValue
;
24
30
class
CleanupPreferences
extends
Maintenance
{
31
public
function
__construct
() {
32
parent::__construct();
33
$this->addDescription(
'Clean up hidden preferences or removed preferences'
);
34
$this->setBatchSize( 50 );
35
$this->addOption(
'dry-run'
,
'Print debug info instead of actually deleting'
);
36
$this->addOption(
'hidden'
,
'Drop hidden preferences ($wgHiddenPrefs)'
);
37
$this->addOption(
'unknown'
,
38
'Drop unknown preferences (not in $wgDefaultUserOptions or prefixed with "userjs-")'
);
39
}
40
49
public
function
execute
() {
50
$dbr = $this->getReplicaDB();
51
$hidden = $this->hasOption(
'hidden'
);
52
$unknown = $this->hasOption(
'unknown'
);
53
54
if
( !$hidden && !$unknown ) {
55
$this->output(
"Did not select one of --hidden, --unknown, exiting\n"
);
56
return
;
57
}
58
59
// Remove hidden prefs. Iterate over them to avoid the IN on a large table
60
if
( $hidden ) {
61
$hiddenPrefs = $this->getConfig()->get(
MainConfigNames::HiddenPrefs
);
62
if
( !$hiddenPrefs ) {
63
$this->output(
"No hidden preferences, skipping\n"
);
64
}
65
foreach
( $hiddenPrefs as $hiddenPref ) {
66
$this->deleteByWhere(
67
$dbr,
68
'Dropping hidden preferences'
,
69
[
'up_property'
=> $hiddenPref ]
70
);
71
}
72
}
73
74
// Remove unknown preferences. Special-case 'userjs-' as we can't control those names.
75
if
( $unknown ) {
76
$defaultUserOptions = $this->getServiceContainer()->getUserOptionsLookup()->getDefaultOptions(
null
);
77
$where = [
78
$dbr->
expr
(
'up_property'
,
IExpression::NOT_LIKE
,
79
new
LikeValue
(
'userjs-'
, $dbr->
anyString
() ) ),
80
$dbr->
expr
(
'up_property'
,
IExpression::NOT_LIKE
,
81
new
LikeValue
(
UserOptionsLookup::LOCAL_EXCEPTION_SUFFIX
, $dbr->
anyString
() ) ),
82
$dbr->
expr
(
'up_property'
,
'!='
, array_keys( $defaultUserOptions ) ),
83
];
84
// Allow extensions to add to the where clause to prevent deletion of their own prefs.
85
$this->getHookRunner()->onDeleteUnknownPreferences( $where, $dbr );
86
$this->deleteByWhere( $dbr,
'Dropping unknown preferences'
, $where );
87
}
88
}
89
90
private
function
deleteByWhere( IReadableDatabase $dbr,
string
$startMessage, array $where ) {
91
$this->output( $startMessage .
"...\n"
);
92
$dryRun = $this->hasOption(
'dry-run'
);
93
94
$iterator =
new
BatchRowIterator(
95
$dbr,
96
$dbr->
newSelectQueryBuilder
()
97
->from(
'user_properties'
)
98
->select( $dryRun ?
99
[
'up_user'
,
'up_property'
,
'up_value'
] :
100
[
'up_user'
,
'up_property'
] )
101
->where( $where )
102
->caller( __METHOD__ ),
103
[
'up_user'
,
'up_property'
],
104
$this->getBatchSize()
105
);
106
107
$dbw = $this->getPrimaryDB();
108
$total = 0;
109
foreach
( $iterator as $batch ) {
110
$numRows = count( $batch );
111
$total += $numRows;
112
// Progress or something
113
$this->output(
"..doing $numRows entries\n"
);
114
115
// Delete our batch, then wait
116
$deleteWhere = [];
117
foreach
( $batch as $row ) {
118
if
( $dryRun ) {
119
$this->output(
120
" DRY RUN, would drop: "
.
121
"[up_user] => '{$row->up_user}' "
.
122
"[up_property] => '{$row->up_property}' "
.
123
"[up_value] => '{$row->up_value}'\n"
124
);
125
continue
;
126
}
127
$deleteWhere[$row->up_user][$row->up_property] =
true
;
128
}
129
if
( $deleteWhere && !$dryRun ) {
130
$dbw->newDeleteQueryBuilder()
131
->deleteFrom(
'user_properties'
)
132
->where( $dbw->makeWhereFrom2d( $deleteWhere,
'up_user'
,
'up_property'
) )
133
->caller( __METHOD__ )->execute();
134
135
$this->waitForReplication();
136
}
137
}
138
$this->output(
"DONE! (handled $total entries)\n"
);
139
}
140
}
141
142
// @codeCoverageIgnoreStart
143
$maintClass
= CleanupPreferences::class;
// Tells it to run the class
144
require_once RUN_MAINTENANCE_IF_MAIN;
145
// @codeCoverageIgnoreEnd
$maintClass
$maintClass
Definition
addChangeTag.php:56
CleanupPreferences
Maintenance script that removes unused preferences from the database.
Definition
cleanupPreferences.php:30
CleanupPreferences\__construct
__construct()
Definition
cleanupPreferences.php:31
CleanupPreferences\execute
execute()
We will do this in three passes 1) The easiest is to drop the hidden preferences from the database.
Definition
cleanupPreferences.php:49
MediaWiki\MainConfigNames
A class containing constants representing the names of configuration variables.
Definition
MainConfigNames.php:22
MediaWiki\MainConfigNames\HiddenPrefs
const HiddenPrefs
Name constant for the HiddenPrefs setting, for use with Config\get().
Definition
MainConfigNames.php:2689
MediaWiki\User\Options\UserOptionsLookup
Provides access to user options.
Definition
UserOptionsLookup.php:17
MediaWiki\User\Options\UserOptionsLookup\LOCAL_EXCEPTION_SUFFIX
const LOCAL_EXCEPTION_SUFFIX
The suffix appended to preference names for the associated preference that tracks whether they have a...
Definition
UserOptionsLookup.php:29
MediaWiki\Utils\BatchRowIterator
Allows iterating a large number of rows in batches transparently.
Definition
BatchRowIterator.php:26
Wikimedia\Rdbms\LikeValue
Content of like value.
Definition
LikeValue.php:14
Wikimedia\Rdbms\IExpression
Definition
IExpression.php:10
Wikimedia\Rdbms\IExpression\NOT_LIKE
const NOT_LIKE
Definition
IExpression.php:15
Wikimedia\Rdbms\IReadableDatabase
A database connection without write operations.
Definition
IReadableDatabase.php:20
Wikimedia\Rdbms\IReadableDatabase\newSelectQueryBuilder
newSelectQueryBuilder()
Create an empty SelectQueryBuilder which can be used to run queries against this connection.
Wikimedia\Rdbms\IReadableDatabase\expr
expr(string $field, string $op, $value)
See Expression\__construct().
Wikimedia\Rdbms\Platform\ISQLPlatform\anyString
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
cleanupPreferences.php
Generated on
for MediaWiki by
1.18.0