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