MediaWiki  REL1_31
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  $didWork = false;
64  $hidden = $this->hasOption( 'hidden' );
65  $unknown = $this->hasOption( 'unknown' );
66  $bogus = $this->hasOption( 'bogus' );
67 
68  if ( !$hidden && !$unknown && !$bogus ) {
69  $this->output( "Did not select one of --hidden, --unknown or --bogus, exiting\n" );
70  return;
71  }
72 
73  // Remove hidden prefs. Iterate over them to avoid the IN on a large table
74  if ( $hidden ) {
75  if ( !$wgHiddenPrefs ) {
76  $this->output( "No hidden preferences, skipping\n" );
77  }
78  foreach ( $wgHiddenPrefs as $hiddenPref ) {
79  $this->deleteByWhere(
80  $dbw,
81  'Dropping hidden preferences',
82  [ 'up_property' => $hiddenPref ]
83  );
84  }
85  }
86 
87  // Remove unknown preferences. Special-case 'userjs-' as we can't control those names.
88  if ( $unknown ) {
89  $where = [
90  'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ),
91  'up_property NOT IN (' . $dbw->makeList( array_keys( $wgDefaultUserOptions ) ) . ')',
92  ];
93  // Allow extensions to add to the where clause to prevent deletion of their own prefs.
94  Hooks::run( 'DeleteUnknownPreferences', [ &$where, $dbw ] );
95  $this->deleteByWhere( $dbw, 'Dropping unknown preferences', $where );
96  }
97 
98  // Something something phase 3
99  if ( $bogus ) {
100  }
101  }
102 
106  private function deleteByWhere( $dbw, $startMessage, $where ) {
107  $this->output( $startMessage . "...\n" );
108  $total = 0;
109  while ( true ) {
110  $res = $dbw->select(
111  'user_properties',
112  '*', // The table lacks a primary key, so select the whole row
113  $where,
114  __METHOD__,
115  [ 'LIMIT' => $this->mBatchSize ]
116  );
117 
118  $numRows = $res->numRows();
119  $total += $numRows;
120  if ( $res->numRows() <= 0 ) {
121  // All done!
122  $this->output( "DONE! (handled $total entries)\n" );
123  break;
124  }
125 
126  // Progress or something
127  $this->output( "..doing $numRows entries\n" );
128 
129  // Delete our batch, then wait
130  foreach ( $res as $row ) {
131  if ( $this->hasOption( 'dry-run' ) ) {
132  $this->output(
133  " DRY RUN, would drop: " .
134  "[up_user] => '{$row->up_user}' " .
135  "[up_property] => '{$row->up_property}' " .
136  "[up_value] => '{$row->up_value}'\n"
137  );
138  continue;
139  }
140  $this->beginTransaction( $dbw, __METHOD__ );
141  $dbw->delete(
142  'user_properties',
143  [
144  'up_user' => $row->up_user,
145  'up_property' => $row->up_property,
146  'up_value' => $row->up_value,
147  ],
148  __METHOD__
149  );
150  $this->commitTransaction( $dbw, __METHOD__ );
151  }
152  }
153  }
154 }
155 
156 $maintClass = CleanupPreferences::class; // Tells it to run the class
157 require_once RUN_MAINTENANCE_IF_MAIN;
$wgHiddenPrefs
$wgHiddenPrefs
An array of preferences to not show for the user.
Definition: DefaultSettings.php:4923
$wgDefaultUserOptions
$wgDefaultUserOptions
Settings added to this array will override the default globals for the user preferences used by anony...
Definition: DefaultSettings.php:4856
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
CleanupPreferences\deleteByWhere
deleteByWhere( $dbw, $startMessage, $where)
Definition: cleanupPreferences.php:106
$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:37
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:1336
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:219
CleanupPreferences\__construct
__construct()
Default constructor.
Definition: cleanupPreferences.php:35
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
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:156
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1351
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:22
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1309
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:388
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:56
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:240
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:329