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(
38  'Clean up hidden preferences, removed preferences, and normalizes values'
39  );
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  // TODO: actually implement this
46  // $this->addOption( 'bogus', 'Drop preferences that have invalid/unaccepted values' );
47  }
48 
61  public function execute() {
63 
64  $dbw = $this->getDB( DB_MASTER );
65  $hidden = $this->hasOption( 'hidden' );
66  $unknown = $this->hasOption( 'unknown' );
67  $bogus = $this->hasOption( 'bogus' );
68 
69  if ( !$hidden && !$unknown && !$bogus ) {
70  $this->output( "Did not select one of --hidden, --unknown or --bogus, exiting\n" );
71  return;
72  }
73 
74  // Remove hidden prefs. Iterate over them to avoid the IN on a large table
75  if ( $hidden ) {
76  if ( !$wgHiddenPrefs ) {
77  $this->output( "No hidden preferences, skipping\n" );
78  }
79  foreach ( $wgHiddenPrefs as $hiddenPref ) {
80  $this->deleteByWhere(
81  $dbw,
82  'Dropping hidden preferences',
83  [ 'up_property' => $hiddenPref ]
84  );
85  }
86  }
87 
88  // Remove unknown preferences. Special-case 'userjs-' as we can't control those names.
89  if ( $unknown ) {
90  $where = [
91  'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ),
92  'up_property NOT IN (' . $dbw->makeList( array_keys( $wgDefaultUserOptions ) ) . ')',
93  ];
94  // Allow extensions to add to the where clause to prevent deletion of their own prefs.
95  Hooks::run( 'DeleteUnknownPreferences', [ &$where, $dbw ] );
96  $this->deleteByWhere( $dbw, 'Dropping unknown preferences', $where );
97  }
98 
99  // Something something phase 3
100  if ( $bogus ) {
101  }
102  }
103 
104  private function deleteByWhere( $dbw, $startMessage, $where ) {
105  $this->output( $startMessage . "...\n" );
106  $total = 0;
107  while ( true ) {
108  $res = $dbw->select(
109  'user_properties',
110  '*', // The table lacks a primary key, so select the whole row
111  $where,
112  __METHOD__,
113  [ 'LIMIT' => $this->mBatchSize ]
114  );
115 
116  $numRows = $res->numRows();
117  $total += $numRows;
118  if ( $res->numRows() <= 0 ) {
119  $this->output( "DONE! (handled $total entries)\n" );
120  break;
121  }
122 
123  // Progress or something
124  $this->output( "..doing $numRows entries\n" );
125 
126  // Delete our batch, then wait
127  foreach ( $res as $row ) {
128  if ( $this->hasOption( 'dry-run' ) ) {
129  $this->output(
130  " DRY RUN, would drop: " .
131  "[up_user] => '{$row->up_user}' " .
132  "[up_property] => '{$row->up_property}' " .
133  "[up_value] => '{$row->up_value}'\n"
134  );
135  continue;
136  }
137  $this->beginTransaction( $dbw, __METHOD__ );
138  $dbw->delete(
139  'user_properties',
140  [
141  'up_user' => $row->up_user,
142  'up_property' => $row->up_property,
143  'up_value' => $row->up_value,
144  ],
145  __METHOD__
146  );
147  $this->commitTransaction( $dbw, __METHOD__ );
148  }
149  }
150  }
151 }
152 
153 $maintClass = CleanupPreferences::class; // Tells it to run the class
154 require_once RUN_MAINTENANCE_IF_MAIN;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
$wgDefaultUserOptions
Settings added to this array will override the default globals for the user preferences used by anony...
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
setBatchSize( $s=0)
Set the batch size.
$wgHiddenPrefs
An array of preferences to not show for the user.
hasOption( $name)
Checks to see if a particular option exists.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
const DB_MASTER
Definition: defines.php:26
execute()
We will do this in three passes 1) The easiest is to drop the hidden preferences from the database...
$res
Definition: database.txt:21
addDescription( $text)
Set the description text.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
deleteByWhere( $dbw, $startMessage, $where)
output( $out, $channel=null)
Throw some output to the user.
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
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
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
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Maintenance script that removes bogus preferences from the database.
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.