MediaWiki master
cleanupPreferences.php
Go to the documentation of this file.
1<?php
27require_once __DIR__ . '/Maintenance.php';
28
32
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Clean up hidden preferences or removed preferences' );
42 $this->setBatchSize( 50 );
43 $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
44 $this->addOption( 'hidden', 'Drop hidden preferences ($wgHiddenPrefs)' );
45 $this->addOption( 'unknown',
46 'Drop unknown preferences (not in $wgDefaultUserOptions or prefixed with "userjs-")' );
47 }
48
57 public function execute() {
58 $dbr = $this->getReplicaDB();
59 $hidden = $this->hasOption( 'hidden' );
60 $unknown = $this->hasOption( 'unknown' );
61
62 if ( !$hidden && !$unknown ) {
63 $this->output( "Did not select one of --hidden, --unknown, exiting\n" );
64 return;
65 }
66
67 // Remove hidden prefs. Iterate over them to avoid the IN on a large table
68 if ( $hidden ) {
69 $hiddenPrefs = $this->getConfig()->get( MainConfigNames::HiddenPrefs );
70 if ( !$hiddenPrefs ) {
71 $this->output( "No hidden preferences, skipping\n" );
72 }
73 foreach ( $hiddenPrefs as $hiddenPref ) {
74 $this->deleteByWhere(
75 $dbr,
76 'Dropping hidden preferences',
77 [ 'up_property' => $hiddenPref ]
78 );
79 }
80 }
81
82 // Remove unknown preferences. Special-case 'userjs-' as we can't control those names.
83 if ( $unknown ) {
84 $defaultUserOptions = $this->getServiceContainer()->getUserOptionsLookup()->getDefaultOptions( null );
85 $where = [
86 $dbr->expr( 'up_property', IExpression::NOT_LIKE, new LikeValue( 'userjs-', $dbr->anyString() ) ),
87 $dbr->expr( 'up_property', '!=', array_keys( $defaultUserOptions ) ),
88 ];
89 // Allow extensions to add to the where clause to prevent deletion of their own prefs.
90 $this->getHookRunner()->onDeleteUnknownPreferences( $where, $dbr );
91 $this->deleteByWhere( $dbr, 'Dropping unknown preferences', $where );
92 }
93 }
94
95 private function deleteByWhere( $dbr, $startMessage, $where ) {
96 $this->output( $startMessage . "...\n" );
97 $dryRun = $this->hasOption( 'dry-run' );
98
99 $iterator = new BatchRowIterator(
100 $dbr,
101 'user_properties',
102 [ 'up_user', 'up_property' ],
103 $this->getBatchSize()
104 );
105 if ( $dryRun ) {
106 $iterator->setFetchColumns( [ 'up_user', 'up_property', 'up_value' ] );
107 } else {
108 $iterator->setFetchColumns( [ 'up_user', 'up_property' ] );
109 }
110 $iterator->addConditions( $where );
111 $iterator->setCaller( __METHOD__ );
112
113 $dbw = $this->getPrimaryDB();
114 $total = 0;
115 foreach ( $iterator as $batch ) {
116 $numRows = count( $batch );
117 $total += $numRows;
118 // Progress or something
119 $this->output( "..doing $numRows entries\n" );
120
121 // Delete our batch, then wait
122 $deleteWhere = [];
123 foreach ( $batch as $row ) {
124 if ( $dryRun ) {
125 $this->output(
126 " DRY RUN, would drop: " .
127 "[up_user] => '{$row->up_user}' " .
128 "[up_property] => '{$row->up_property}' " .
129 "[up_value] => '{$row->up_value}'\n"
130 );
131 continue;
132 }
133 $deleteWhere[$row->up_user][$row->up_property] = true;
134 }
135 if ( $deleteWhere && !$dryRun ) {
136 $dbw->newDeleteQueryBuilder()
137 ->deleteFrom( 'user_properties' )
138 ->where( $dbw->makeWhereFrom2d( $deleteWhere, 'up_user', 'up_property' ) )
139 ->caller( __METHOD__ )->execute();
140
141 $this->waitForReplication();
142 }
143 }
144 $this->output( "DONE! (handled $total entries)\n" );
145 }
146}
147
148$maintClass = CleanupPreferences::class; // Tells it to run the class
149require_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.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
getHookRunner()
Get a HookRunner for running core hooks.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
setBatchSize( $s=0)
A class containing constants representing the names of configuration variables.
Content of like value.
Definition LikeValue.php:14