MediaWiki master
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 (int)$this->hasOption( 'delete' ) + (int)$this->hasOption( 'prefix' )
67 + (int)$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 = $this->getServiceContainer()->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 = $this->getServiceContainer()->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->getPrimaryDB();
114 }
115
116 protected function processUsers( array $userBatch, IDatabase $dbw ) {
117 if ( !$userBatch ) {
118 return;
119 }
120 if ( $this->getOption( 'delete' ) ) {
122 ->update( 'user' )
123 ->set( [ 'user_password' => PasswordFactory::newInvalidPassword()->toString() ] )
124 ->where( [ 'user_name' => $userBatch ] )
125 ->caller( __METHOD__ )->execute();
126 } elseif ( $this->getOption( 'prefix' ) ) {
128 ->update( 'user' )
129 ->set( [ 'user_password = ' . $dbw->buildConcat( [ $dbw->addQuotes( ':null:' ),
130 'user_password' ] ) ] )
131 ->where( [
132 'NOT (user_password ' . $dbw->buildLike( ':null:', $dbw->anyString() ) . ')',
133 $dbw->expr( 'user_password', '!=', PasswordFactory::newInvalidPassword()->toString() ),
134 'user_password IS NOT NULL',
135 'user_name' => $userBatch,
136 ] )
137 ->caller( __METHOD__ )->execute();
138 } elseif ( $this->getOption( 'unprefix' ) ) {
140 ->update( 'user' )
141 ->set( [ 'user_password = ' . $dbw->buildSubString( 'user_password', strlen( ':null:' ) + 1 ) ] )
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}
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.
Content of like value.
Definition LikeValue.php:14
$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:36
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.
buildLike( $param,... $params)
LIKE statement wrapper.
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.