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