MediaWiki master
DeleteLocalPasswords.php
Go to the documentation of this file.
1<?php
30
31require_once __DIR__ . '/../Maintenance.php';
32
49 protected $user;
50
52 protected $total;
53
54 public function __construct() {
55 parent::__construct();
56 $this->addDescription( "Deletes local password for users." );
57 $this->setBatchSize( 1000 );
58
59 $this->addOption( 'user', 'If specified, only checks the given user', false, true );
60 $this->addOption( 'delete', 'Really delete. To prevent accidents, you must provide this flag.' );
61 $this->addOption( 'prefix', "Instead of deleting, make passwords invalid by prefixing with "
62 . "':null:'. Make sure PasswordConfig has a 'null' entry. This is meant for testing before "
63 . "hard delete." );
64 $this->addOption( 'unprefix', 'Instead of deleting, undo the effect of --prefix.' );
65 }
66
67 protected function initialize() {
68 if (
69 (int)$this->hasOption( 'delete' ) + (int)$this->hasOption( 'prefix' )
70 + (int)$this->hasOption( 'unprefix' ) !== 1
71 ) {
72 $this->fatalError( "Exactly one of the 'delete', 'prefix', 'unprefix' options must be used\n" );
73 }
74 if ( $this->hasOption( 'prefix' ) || $this->hasOption( 'unprefix' ) ) {
75 $passwordHashTypes = $this->getServiceContainer()->getPasswordFactory()->getTypes();
76 if (
77 !isset( $passwordHashTypes['null'] )
78 || $passwordHashTypes['null']['class'] !== InvalidPassword::class
79 ) {
80 $this->fatalError(
81<<<'ERROR'
82'null' password entry missing. To use password prefixing, add
83 $wgPasswordConfig['null'] = [ 'class' => InvalidPassword::class ];
84to your configuration (and remove once the passwords were deleted).
85ERROR
86 );
87 }
88 }
89
90 $user = $this->getOption( 'user', false );
91 if ( $user !== false ) {
92 $userNameUtils = $this->getServiceContainer()->getUserNameUtils();
93 $this->user = $userNameUtils->getCanonical( $user );
94 if ( $this->user === false ) {
95 $this->fatalError( "Invalid user name\n" );
96 }
97 }
98 }
99
100 public function execute() {
101 $this->initialize();
102
103 foreach ( $this->getUserBatches() as $userBatch ) {
104 $this->processUsers( $userBatch, $this->getUserDB() );
105 }
106
107 $this->output( "done. (wrote $this->total rows)\n" );
108 }
109
115 protected function getUserDB() {
116 return $this->getPrimaryDB();
117 }
118
119 protected function processUsers( array $userBatch, IDatabase $dbw ) {
120 if ( !$userBatch ) {
121 return;
122 }
123 if ( $this->getOption( 'delete' ) ) {
125 ->update( 'user' )
126 ->set( [ 'user_password' => PasswordFactory::newInvalidPassword()->toString() ] )
127 ->where( [ 'user_name' => $userBatch ] )
128 ->caller( __METHOD__ )->execute();
129 } elseif ( $this->getOption( 'prefix' ) ) {
131 ->update( 'user' )
132 ->set( [
133 'user_password' => new RawSQLValue(
134 $dbw->buildConcat( [ $dbw->addQuotes( ':null:' ), 'user_password' ] )
135 )
136 ] )
137 ->where( [
138 $dbw->expr( 'user_password', IExpression::NOT_LIKE, new LikeValue( ':null:', $dbw->anyString() ) ),
139 $dbw->expr( 'user_password', '!=', PasswordFactory::newInvalidPassword()->toString() ),
140 $dbw->expr( 'user_password', '!=', null ),
141 'user_name' => $userBatch,
142 ] )
143 ->caller( __METHOD__ )->execute();
144 } elseif ( $this->getOption( 'unprefix' ) ) {
146 ->update( 'user' )
147 ->set( [
148 'user_password' => new RawSQLValue(
149 $dbw->buildSubString( 'user_password', strlen( ':null:' ) + 1 )
150 )
151 ] )
152 ->where( [
153 $dbw->expr( 'user_password', IExpression::LIKE, new LikeValue( ':null:', $dbw->anyString() ) ),
154 'user_name' => $userBatch,
155 ] )
156 ->caller( __METHOD__ )->execute();
157 }
158 $this->total += $dbw->affectedRows();
159 $this->waitForReplication();
160 }
161
171 protected function getUserBatches() {
172 if ( $this->user !== null ) {
173 $this->output( "\t ... querying '$this->user'\n" );
174 yield [ [ $this->user ] ];
175 return;
176 }
177
178 $lastUsername = '';
179 $dbw = $this->getPrimaryDB();
180 do {
181 $this->output( "\t ... querying from '$lastUsername'\n" );
182 $users = $dbw->newSelectQueryBuilder()
183 ->select( 'user_name' )
184 ->from( 'user' )
185 ->where( $dbw->expr( 'user_name', '>', $lastUsername ) )
186 ->orderBy( 'user_name ASC' )
187 ->limit( $this->getBatchSize() )
188 ->caller( __METHOD__ )->fetchFieldValues();
189 if ( $users ) {
190 yield $users;
191 $lastUsername = end( $users );
192 }
193 } while ( count( $users ) === $this->getBatchSize() );
194 }
195}
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.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
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.
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.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
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.