MediaWiki  1.33.0
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->mDescription = 'Clean up hidden preferences, removed preferences, and normalizes values';
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  // TODO: actually implement this
44  // $this->addOption( 'bogus', 'Drop preferences that have invalid/unaccepted values' );
45  }
46 
59  public function execute() {
61 
62  $dbw = $this->getDB( DB_MASTER );
63  $hidden = $this->hasOption( 'hidden' );
64  $unknown = $this->hasOption( 'unknown' );
65  $bogus = $this->hasOption( 'bogus' );
66 
67  if ( !$hidden && !$unknown && !$bogus ) {
68  $this->output( "Did not select one of --hidden, --unknown or --bogus, exiting\n" );
69  return;
70  }
71 
72  // Remove hidden prefs. Iterate over them to avoid the IN on a large table
73  if ( $hidden ) {
74  if ( !$wgHiddenPrefs ) {
75  $this->output( "No hidden preferences, skipping\n" );
76  }
77  foreach ( $wgHiddenPrefs as $hiddenPref ) {
78  $this->deleteByWhere(
79  $dbw,
80  'Dropping hidden preferences',
81  [ 'up_property' => $hiddenPref ]
82  );
83  }
84  }
85 
86  // Remove unknown preferences. Special-case 'userjs-' as we can't control those names.
87  if ( $unknown ) {
88  $where = [
89  'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ),
90  'up_property NOT IN (' . $dbw->makeList( array_keys( $wgDefaultUserOptions ) ) . ')',
91  ];
92  // Allow extensions to add to the where clause to prevent deletion of their own prefs.
93  Hooks::run( 'DeleteUnknownPreferences', [ &$where, $dbw ] );
94  $this->deleteByWhere( $dbw, 'Dropping unknown preferences', $where );
95  }
96 
97  // Something something phase 3
98  if ( $bogus ) {
99  }
100  }
101 
102  private function deleteByWhere( $dbw, $startMessage, $where ) {
103  $this->output( $startMessage . "...\n" );
104  $total = 0;
105  while ( true ) {
106  $res = $dbw->select(
107  'user_properties',
108  '*', // The table lacks a primary key, so select the whole row
109  $where,
110  __METHOD__,
111  [ 'LIMIT' => $this->mBatchSize ]
112  );
113 
114  $numRows = $res->numRows();
115  $total += $numRows;
116  if ( $res->numRows() <= 0 ) {
117  $this->output( "DONE! (handled $total entries)\n" );
118  break;
119  }
120 
121  // Progress or something
122  $this->output( "..doing $numRows entries\n" );
123 
124  // Delete our batch, then wait
125  foreach ( $res as $row ) {
126  if ( $this->hasOption( 'dry-run' ) ) {
127  $this->output(
128  " DRY RUN, would drop: " .
129  "[up_user] => '{$row->up_user}' " .
130  "[up_property] => '{$row->up_property}' " .
131  "[up_value] => '{$row->up_value}'\n"
132  );
133  continue;
134  }
135  $this->beginTransaction( $dbw, __METHOD__ );
136  $dbw->delete(
137  'user_properties',
138  [
139  'up_user' => $row->up_user,
140  'up_property' => $row->up_property,
141  'up_value' => $row->up_value,
142  ],
143  __METHOD__
144  );
145  $this->commitTransaction( $dbw, __METHOD__ );
146  }
147  }
148  }
149 }
150 
151 $maintClass = CleanupPreferences::class; // Tells it to run the class
152 require_once RUN_MAINTENANCE_IF_MAIN;
$wgHiddenPrefs
$wgHiddenPrefs
An array of preferences to not show for the user.
Definition: DefaultSettings.php:4893
$wgDefaultUserOptions
$wgDefaultUserOptions
Settings added to this array will override the default globals for the user preferences used by anony...
Definition: DefaultSettings.php:4825
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
CleanupPreferences\deleteByWhere
deleteByWhere( $dbw, $startMessage, $where)
Definition: cleanupPreferences.php:102
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
CleanupPreferences
Maintenance script that removes bogus preferences from the database.
Definition: cleanupPreferences.php:34
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1399
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:248
CleanupPreferences\__construct
__construct()
Default constructor.
Definition: cleanupPreferences.php:35
DB_MASTER
const DB_MASTER
Definition: defines.php:26
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:59
$maintClass
$maintClass
Definition: cleanupPreferences.php:151
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1414
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1373
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:269
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:375