14require_once __DIR__ .
'/Maintenance.php';
31 parent::__construct();
32 $this->
addDescription(
'Clean up hidden preferences or removed preferences' );
34 $this->
addOption(
'dry-run',
'Print debug info instead of actually deleting' );
35 $this->
addOption(
'hidden',
'Drop hidden preferences ($wgHiddenPrefs)' );
37 'Drop unknown preferences (not in $wgDefaultUserOptions or prefixed with "userjs-")' );
53 if ( !$hidden && !$unknown ) {
54 $this->
output(
"Did not select one of --hidden, --unknown, exiting\n" );
60 $hiddenPrefs = $this->
getConfig()->get( MainConfigNames::HiddenPrefs );
61 if ( !$hiddenPrefs ) {
62 $this->
output(
"No hidden preferences, skipping\n" );
64 foreach ( $hiddenPrefs as $hiddenPref ) {
67 'Dropping hidden preferences',
68 [
'up_property' => $hiddenPref ]
75 $defaultUserOptions = $this->
getServiceContainer()->getUserOptionsLookup()->getDefaultOptions(
null );
77 $dbr->
expr(
'up_property', IExpression::NOT_LIKE,
79 $dbr->
expr(
'up_property', IExpression::NOT_LIKE,
81 $dbr->
expr(
'up_property',
'!=', array_keys( $defaultUserOptions ) ),
84 $this->
getHookRunner()->onDeleteUnknownPreferences( $where, $dbr );
85 $this->deleteByWhere( $dbr,
'Dropping unknown preferences', $where );
89 private function deleteByWhere(
IReadableDatabase $dbr,
string $startMessage, array $where ) {
90 $this->
output( $startMessage .
"...\n" );
96 ->from(
'user_properties' )
98 [
'up_user',
'up_property',
'up_value' ] :
99 [
'up_user',
'up_property' ] )
101 ->caller( __METHOD__ ),
102 [
'up_user',
'up_property' ],
108 foreach ( $iterator as $batch ) {
109 $numRows = count( $batch );
112 $this->
output(
"..doing $numRows entries\n" );
116 foreach ( $batch as $row ) {
119 " DRY RUN, would drop: " .
120 "[up_user] => '{$row->up_user}' " .
121 "[up_property] => '{$row->up_property}' " .
122 "[up_value] => '{$row->up_value}'\n"
126 $deleteWhere[$row->up_user][$row->up_property] =
true;
128 if ( $deleteWhere && !$dryRun ) {
129 $dbw->newDeleteQueryBuilder()
130 ->deleteFrom(
'user_properties' )
131 ->where( $dbw->makeWhereFrom2d( $deleteWhere,
'up_user',
'up_property' ) )
132 ->caller( __METHOD__ )->execute();
137 $this->
output(
"DONE! (handled $total entries)\n" );
143require_once RUN_MAINTENANCE_IF_MAIN;
Allows iterating a large number of rows in batches transparently.
Maintenance script that removes unused preferences from the database.
__construct()
Default constructor.
execute()
We will do this in three passes 1) The easiest is to drop the hidden preferences from the database.
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getReplicaDB(string|false $virtualDomain=false)
getHookRunner()
Get a HookRunner for running core hooks.
getServiceContainer()
Returns the main service container.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.