MediaWiki REL1_37
DeleteLocalPasswords.php
Go to the documentation of this file.
1<?php
27
28require_once __DIR__ . '/../Maintenance.php';
29
46 protected $user;
47
49 protected $total;
50
51 public function __construct() {
52 parent::__construct();
53 $this->addDescription( "Deletes local password for users." );
54 $this->setBatchSize( 1000 );
55
56 $this->addOption( 'user', 'If specified, only checks the given user', false, true );
57 $this->addOption( 'delete', 'Really delete. To prevent accidents, you must provide this flag.' );
58 $this->addOption( 'prefix', "Instead of deleting, make passwords invalid by prefixing with "
59 . "':null:'. Make sure PasswordConfig has a 'null' entry. This is meant for testing before "
60 . "hard delete." );
61 $this->addOption( 'unprefix', 'Instead of deleting, undo the effect of --prefix.' );
62 }
63
64 protected function initialize() {
65 if (
66 $this->hasOption( 'delete' ) + $this->hasOption( 'prefix' )
67 + $this->hasOption( 'unprefix' ) !== 1
68 ) {
69 $this->fatalError( "Exactly one of the 'delete', 'prefix', 'unprefix' options must be used\n" );
70 }
71 if ( $this->hasOption( 'prefix' ) || $this->hasOption( 'unprefix' ) ) {
72 $passwordHashTypes = MediaWikiServices::getInstance()->getPasswordFactory()->getTypes();
73 if (
74 !isset( $passwordHashTypes['null'] )
75 || $passwordHashTypes['null']['class'] !== InvalidPassword::class
76 ) {
77 $this->fatalError(
78<<<'ERROR'
79'null' password entry missing. To use password prefixing, add
80 $wgPasswordConfig['null'] = [ 'class' => InvalidPassword::class ];
81to your configuration (and remove once the passwords were deleted).
82ERROR
83 );
84 }
85 }
86
87 $user = $this->getOption( 'user', false );
88 if ( $user !== false ) {
89 $userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
90 $this->user = $userNameUtils->getCanonical( $user );
91 if ( $this->user === false ) {
92 $this->fatalError( "Invalid user name\n" );
93 }
94 }
95 }
96
97 public function execute() {
98 $this->initialize();
99
100 foreach ( $this->getUserBatches() as $userBatch ) {
101 $this->processUsers( $userBatch, $this->getUserDB() );
102 }
103
104 $this->output( "done. (wrote $this->total rows)\n" );
105 }
106
112 protected function getUserDB() {
113 return $this->getDB( DB_PRIMARY );
114 }
115
116 protected function processUsers( array $userBatch, IDatabase $dbw ) {
117 if ( !$userBatch ) {
118 return;
119 }
120 if ( $this->getOption( 'delete' ) ) {
121 $dbw->update( 'user',
122 [ 'user_password' => PasswordFactory::newInvalidPassword()->toString() ],
123 [ 'user_name' => $userBatch ],
124 __METHOD__
125 );
126 } elseif ( $this->getOption( 'prefix' ) ) {
127 $dbw->update( 'user',
128 [ 'user_password = ' . $dbw->buildConcat( [ $dbw->addQuotes( ':null:' ),
129 'user_password' ] ) ],
130 [
131 'NOT (user_password ' . $dbw->buildLike( ':null:', $dbw->anyString() ) . ')',
132 "user_password != " . $dbw->addQuotes( PasswordFactory::newInvalidPassword()->toString() ),
133 'user_password IS NOT NULL',
134 'user_name' => $userBatch,
135 ],
136 __METHOD__
137 );
138 } elseif ( $this->getOption( 'unprefix' ) ) {
139 $dbw->update( 'user',
140 [ 'user_password = ' . $dbw->buildSubString( 'user_password', strlen( ':null:' ) + 1 ) ],
141 [
142 'user_password ' . $dbw->buildLike( ':null:', $dbw->anyString() ),
143 'user_name' => $userBatch,
144 ],
145 __METHOD__
146 );
147 }
148 $this->total += $dbw->affectedRows();
149 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
150 }
151
161 protected function getUserBatches() {
162 if ( $this->user !== null ) {
163 $this->output( "\t ... querying '$this->user'\n" );
164 yield [ [ $this->user ] ];
165 return;
166 }
167
168 $lastUsername = '';
169 $dbw = $this->getDB( DB_PRIMARY );
170 do {
171 $this->output( "\t ... querying from '$lastUsername'\n" );
172 $users = $dbw->selectFieldValues(
173 'user',
174 'user_name',
175 [
176 'user_name > ' . $dbw->addQuotes( $lastUsername ),
177 ],
178 __METHOD__,
179 [
180 'LIMIT' => $this->getBatchSize(),
181 'ORDER BY' => 'user_name ASC',
182 ]
183 );
184 if ( $users ) {
185 yield $users;
186 $lastUsername = end( $users );
187 }
188 } while ( count( $users ) === $this->getBatchSize() );
189 }
190}
getDB()
$wgPasswordConfig
Configuration for built-in password types.
Delete unused local passwords.
execute()
Do the actual work.
__construct()
Default constructor.
string null $user
User to run on, or null for all.
int $total
Number of deleted passwords.
processUsers(array $userBatch, IDatabase $dbw)
getUserBatches()
This method iterates through the requested users and returns their names in batches of self::$mBatchS...
getUserDB()
Get the primary DB handle for the current user batch.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
buildLike( $param,... $params)
LIKE statement wrapper.
buildSubString( $input, $startPosition, $length=null)
Build a SUBSTRING function.
affectedRows()
Get the number of rows affected by the last write query.
buildConcat( $stringList)
Build a concatenation list to feed into a SQL query.
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.
update( $table, $set, $conds, $fname=__METHOD__, $options=[])
Update all rows in a table that match a given condition.
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
Advanced database interface for IDatabase handles that include maintenance methods.
const DB_PRIMARY
Definition defines.php:27