MediaWiki REL1_39
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 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
113 $dbw = $this->getDB( DB_PRIMARY );
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->delete(
137 'user_properties',
138 $dbw->makeWhereFrom2d( $deleteWhere, 'up_user', 'up_property' ),
139 __METHOD__
140 );
141
142 $lbFactory->waitForReplication();
143 }
144 }
145 $this->output( "DONE! (handled $total entries)\n" );
146 }
147}
148
149$maintClass = CleanupPreferences::class; // Tells it to run the class
150require_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.
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