MediaWiki REL1_31
cleanupPreferences.php
Go to the documentation of this file.
1<?php
27require_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
157require_once RUN_MAINTENANCE_IF_MAIN;
$wgHiddenPrefs
An array of preferences to not show for the user.
$wgDefaultUserOptions
Settings added to this array will override the default globals for the user preferences used by anony...
Maintenance script that removes bogus preferences from the database.
deleteByWhere( $dbw, $startMessage, $where)
__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...
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
hasOption( $name)
Checks to see if a particular param exists.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
setBatchSize( $s=0)
Set the batch size.
$res
Definition database.txt:21
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
require_once RUN_MAINTENANCE_IF_MAIN
const DB_MASTER
Definition defines.php:29