MediaWiki REL1_40
cleanupPreferences.php
Go to the documentation of this file.
1<?php
27require_once __DIR__ . '/Maintenance.php';
28
31
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Clean up hidden preferences or removed preferences' );
41 $this->setBatchSize( 50 );
42 $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
43 $this->addOption( 'hidden', 'Drop hidden preferences ($wgHiddenPrefs)' );
44 $this->addOption( 'unknown',
45 'Drop unknown preferences (not in $wgDefaultUserOptions or prefixed with "userjs-")' );
46 }
47
56 public function execute() {
57 $dbr = $this->getDB( DB_REPLICA );
58 $hidden = $this->hasOption( 'hidden' );
59 $unknown = $this->hasOption( 'unknown' );
60
61 if ( !$hidden && !$unknown ) {
62 $this->output( "Did not select one of --hidden, --unknown, exiting\n" );
63 return;
64 }
65
66 // Remove hidden prefs. Iterate over them to avoid the IN on a large table
67 if ( $hidden ) {
68 $hiddenPrefs = $this->getConfig()->get( MainConfigNames::HiddenPrefs );
69 if ( !$hiddenPrefs ) {
70 $this->output( "No hidden preferences, skipping\n" );
71 }
72 foreach ( $hiddenPrefs as $hiddenPref ) {
73 $this->deleteByWhere(
74 $dbr,
75 'Dropping hidden preferences',
76 [ 'up_property' => $hiddenPref ]
77 );
78 }
79 }
80
81 // Remove unknown preferences. Special-case 'userjs-' as we can't control those names.
82 if ( $unknown ) {
83 $defaultUserOptions = MediaWikiServices::getInstance()->getUserOptionsLookup()->getDefaultOptions();
84 $where = [
85 'up_property NOT' . $dbr->buildLike( 'userjs-', $dbr->anyString() ),
86 'up_property NOT IN (' . $dbr->makeList( array_keys( $defaultUserOptions ) ) . ')',
87 ];
88 // Allow extensions to add to the where clause to prevent deletion of their own prefs.
89 $this->getHookRunner()->onDeleteUnknownPreferences( $where, $dbr );
90 $this->deleteByWhere( $dbr, 'Dropping unknown preferences', $where );
91 }
92 }
93
94 private function deleteByWhere( $dbr, $startMessage, $where ) {
95 $this->output( $startMessage . "...\n" );
96 $dryRun = $this->hasOption( 'dry-run' );
97
98 $iterator = new BatchRowIterator(
99 $dbr,
100 'user_properties',
101 [ 'up_user', 'up_property' ],
102 $this->getBatchSize()
103 );
104 if ( $dryRun ) {
105 $iterator->setFetchColumns( [ 'up_user', 'up_property', 'up_value' ] );
106 } else {
107 $iterator->setFetchColumns( [ 'up_user', 'up_property' ] );
108 }
109 $iterator->addConditions( $where );
110 $iterator->setCaller( __METHOD__ );
111
112 $dbw = $this->getDB( DB_PRIMARY );
113 $total = 0;
114 foreach ( $iterator as $batch ) {
115 $numRows = count( $batch );
116 $total += $numRows;
117 // Progress or something
118 $this->output( "..doing $numRows entries\n" );
119
120 // Delete our batch, then wait
121 $deleteWhere = [];
122 foreach ( $batch as $row ) {
123 if ( $dryRun ) {
124 $this->output(
125 " DRY RUN, would drop: " .
126 "[up_user] => '{$row->up_user}' " .
127 "[up_property] => '{$row->up_property}' " .
128 "[up_value] => '{$row->up_value}'\n"
129 );
130 continue;
131 }
132 $deleteWhere[$row->up_user][$row->up_property] = true;
133 }
134 if ( $deleteWhere && !$dryRun ) {
135 $dbw->delete(
136 'user_properties',
137 $dbw->makeWhereFrom2d( $deleteWhere, 'up_user', 'up_property' ),
138 __METHOD__
139 );
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;
getDB()
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.
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.
Service locator for MediaWiki core services.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28