MediaWiki  master
cleanupPreferences.php
Go to the documentation of this file.
1 <?php
27 require_once __DIR__ . '/Maintenance.php';
28 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Clean up hidden preferences or removed preferences' );
38  $this->setBatchSize( 50 );
39  $this->addOption( 'dry-run', 'Print debug info instead of actually deleting' );
40  $this->addOption( 'hidden', 'Drop hidden preferences ($wgHiddenPrefs)' );
41  $this->addOption( 'unknown',
42  'Drop unknown preferences (not in $wgDefaultUserOptions or prefixed with "userjs-")' );
43  }
44 
53  public function execute() {
54  $dbw = $this->getDB( DB_PRIMARY );
55  $hidden = $this->hasOption( 'hidden' );
56  $unknown = $this->hasOption( 'unknown' );
57 
58  if ( !$hidden && !$unknown ) {
59  $this->output( "Did not select one of --hidden, --unknown, exiting\n" );
60  return;
61  }
62 
63  // Remove hidden prefs. Iterate over them to avoid the IN on a large table
64  if ( $hidden ) {
65  $hiddenPrefs = $this->getConfig()->get( 'HiddenPrefs' );
66  if ( !$hiddenPrefs ) {
67  $this->output( "No hidden preferences, skipping\n" );
68  }
69  foreach ( $hiddenPrefs as $hiddenPref ) {
70  $this->deleteByWhere(
71  $dbw,
72  'Dropping hidden preferences',
73  [ 'up_property' => $hiddenPref ]
74  );
75  }
76  }
77 
78  // Remove unknown preferences. Special-case 'userjs-' as we can't control those names.
79  if ( $unknown ) {
80  $defaultUserOptions = $this->getConfig()->get( 'DefaultUserOptions' );
81  $where = [
82  'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ),
83  'up_property NOT IN (' . $dbw->makeList( array_keys( $defaultUserOptions ) ) . ')',
84  ];
85  // Allow extensions to add to the where clause to prevent deletion of their own prefs.
86  $this->getHookRunner()->onDeleteUnknownPreferences( $where, $dbw );
87  $this->deleteByWhere( $dbw, 'Dropping unknown preferences', $where );
88  }
89  }
90 
91  private function deleteByWhere( $dbw, $startMessage, $where ) {
92  $this->output( $startMessage . "...\n" );
93  $total = 0;
94  while ( true ) {
95  $res = $dbw->select(
96  'user_properties',
97  '*', // The table lacks a primary key, so select the whole row
98  $where,
99  __METHOD__,
100  [ 'LIMIT' => $this->mBatchSize ]
101  );
102 
103  $numRows = $res->numRows();
104  $total += $numRows;
105  if ( $res->numRows() <= 0 ) {
106  $this->output( "DONE! (handled $total entries)\n" );
107  break;
108  }
109 
110  // Progress or something
111  $this->output( "..doing $numRows entries\n" );
112 
113  // Delete our batch, then wait
114  foreach ( $res as $row ) {
115  if ( $this->hasOption( 'dry-run' ) ) {
116  $this->output(
117  " DRY RUN, would drop: " .
118  "[up_user] => '{$row->up_user}' " .
119  "[up_property] => '{$row->up_property}' " .
120  "[up_value] => '{$row->up_value}'\n"
121  );
122  continue;
123  }
124  $this->beginTransaction( $dbw, __METHOD__ );
125  $dbw->delete(
126  'user_properties',
127  [
128  'up_user' => $row->up_user,
129  'up_property' => $row->up_property,
130  'up_value' => $row->up_value,
131  ],
132  __METHOD__
133  );
134  $this->commitTransaction( $dbw, __METHOD__ );
135  }
136  }
137  }
138 }
139 
140 $maintClass = CleanupPreferences::class; // Tells it to run the class
141 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
CleanupPreferences\deleteByWhere
deleteByWhere( $dbw, $startMessage, $where)
Definition: cleanupPreferences.php:91
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$res
$res
Definition: testCompression.php:57
CleanupPreferences
Maintenance script that removes unused preferences from the database.
Definition: cleanupPreferences.php:34
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
Definition: Maintenance.php:1406
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:598
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
CleanupPreferences\__construct
__construct()
Default constructor.
Definition: cleanupPreferences.php:35
CleanupPreferences\execute
execute()
We will do this in three passes 1) The easiest is to drop the hidden preferences from the database.
Definition: cleanupPreferences.php:53
$maintClass
$maintClass
Definition: cleanupPreferences.php:140
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Maintenance\getHookRunner
getHookRunner()
Get a HookRunner for running core hooks.
Definition: Maintenance.php:1636
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1421
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1375
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition: Maintenance.php:375