MediaWiki master
DeleteLocalPasswords.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Maintenance;
11
18
19// @codeCoverageIgnoreStart
20require_once __DIR__ . '/../Maintenance.php';
21// @codeCoverageIgnoreEnd
22
39 protected $user;
40
42 protected $total;
43
44 public function __construct() {
45 parent::__construct();
46 $this->addDescription( "Deletes local password for users." );
47 $this->setBatchSize( 1000 );
48
49 $this->addOption( 'user', 'If specified, only checks the given user', false, true );
50 $this->addOption( 'delete', 'Really delete. To prevent accidents, you must provide this flag.' );
51 $this->addOption( 'prefix', "Instead of deleting, make passwords invalid by prefixing with "
52 . "':null:'. Make sure PasswordConfig has a 'null' entry. This is meant for testing before "
53 . "hard delete." );
54 $this->addOption( 'unprefix', 'Instead of deleting, undo the effect of --prefix.' );
55 }
56
57 protected function initialize() {
58 if (
59 (int)$this->hasOption( 'delete' ) + (int)$this->hasOption( 'prefix' )
60 + (int)$this->hasOption( 'unprefix' ) !== 1
61 ) {
62 $this->fatalError( "Exactly one of the 'delete', 'prefix', 'unprefix' options must be used\n" );
63 }
64 if ( $this->hasOption( 'prefix' ) || $this->hasOption( 'unprefix' ) ) {
65 $passwordHashTypes = $this->getServiceContainer()->getPasswordFactory()->getTypes();
66 if (
67 !isset( $passwordHashTypes['null'] )
68 || $passwordHashTypes['null']['class'] !== InvalidPassword::class
69 ) {
70 $this->fatalError(
71<<<'ERROR'
72'null' password entry missing. To use password prefixing, add
73 $wgPasswordConfig['null'] = [ 'class' => InvalidPassword::class ];
74to your configuration (and remove once the passwords were deleted).
75ERROR
76 );
77 }
78 }
79
80 $user = $this->getOption( 'user', false );
81 if ( $user !== false ) {
82 $userNameUtils = $this->getServiceContainer()->getUserNameUtils();
83 $this->user = $userNameUtils->getCanonical( $user );
84 if ( $this->user === false ) {
85 $this->fatalError( "Invalid user name\n" );
86 }
87 }
88 }
89
90 public function execute() {
91 $this->initialize();
92
93 foreach ( $this->getUserBatches() as $userBatch ) {
94 $this->processUsers( $userBatch, $this->getUserDB() );
95 }
96
97 $this->output( "done. (wrote $this->total rows)\n" );
98 }
99
105 protected function getUserDB() {
106 return $this->getPrimaryDB();
107 }
108
109 protected function processUsers( array $userBatch, IDatabase $dbw ) {
110 if ( !$userBatch ) {
111 return;
112 }
113 if ( $this->getOption( 'delete' ) ) {
115 ->update( 'user' )
116 ->set( [ 'user_password' => PasswordFactory::newInvalidPassword()->toString() ] )
117 ->where( [ 'user_name' => $userBatch ] )
118 ->caller( __METHOD__ )->execute();
119 } elseif ( $this->getOption( 'prefix' ) ) {
121 ->update( 'user' )
122 ->set( [
123 'user_password' => new RawSQLValue(
124 $dbw->buildConcat( [ $dbw->addQuotes( ':null:' ), 'user_password' ] )
125 )
126 ] )
127 ->where( [
128 $dbw->expr( 'user_password', IExpression::NOT_LIKE, new LikeValue( ':null:', $dbw->anyString() ) ),
129 $dbw->expr( 'user_password', '!=', PasswordFactory::newInvalidPassword()->toString() ),
130 $dbw->expr( 'user_password', '!=', null ),
131 'user_name' => $userBatch,
132 ] )
133 ->caller( __METHOD__ )->execute();
134 } elseif ( $this->getOption( 'unprefix' ) ) {
136 ->update( 'user' )
137 ->set( [
138 'user_password' => new RawSQLValue(
139 $dbw->buildSubString( 'user_password', strlen( ':null:' ) + 1 )
140 )
141 ] )
142 ->where( [
143 $dbw->expr( 'user_password', IExpression::LIKE, new LikeValue( ':null:', $dbw->anyString() ) ),
144 'user_name' => $userBatch,
145 ] )
146 ->caller( __METHOD__ )->execute();
147 }
148 $this->total += $dbw->affectedRows();
149 $this->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->getPrimaryDB();
170 do {
171 $this->output( "\t ... querying from '$lastUsername'\n" );
172 $users = $dbw->newSelectQueryBuilder()
173 ->select( 'user_name' )
174 ->from( 'user' )
175 ->where( $dbw->expr( 'user_name', '>', $lastUsername ) )
176 ->orderBy( 'user_name ASC' )
177 ->limit( $this->getBatchSize() )
178 ->caller( __METHOD__ )->fetchFieldValues();
179 if ( $users ) {
180 yield $users;
181 $lastUsername = end( $users );
182 }
183 } while ( count( $users ) === $this->getBatchSize() );
184 }
185}
186
188class_alias( DeleteLocalPasswords::class, 'DeleteLocalPasswords' );
processUsers(array $userBatch, IDatabase $dbw)
getUserDB()
Get the primary DB handle for the current user batch.
string null $user
User to run on, or null for all.
getUserBatches()
This method iterates through the requested users and returns their names in batches of self::$mBatchS...
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
Represents an invalid password hash.
Factory class for creating and checking Password objects.
Content of like value.
Definition LikeValue.php:14
Raw SQL value to be used in query builders.
$wgPasswordConfig
Config variable stub for the PasswordConfig setting, for use by phpdoc and IDEs.
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
Interface to a relational database.
Definition IDatabase.php:31
newUpdateQueryBuilder()
Get an UpdateQueryBuilder bound to this connection.
affectedRows()
Get the number of rows affected by the last query method call.
expr(string $field, string $op, $value)
See Expression::__construct()
buildSubString( $input, $startPosition, $length=null)
Build a SUBSTRING function.
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.
buildConcat( $stringList)
Build a concatenation list to feed into a SQL query.
Update the CREDITS list by merging in the list of git commit authors.