MediaWiki  1.34.0
wrapOldPasswords.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
27 
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription( 'Wrap all passwords of a certain type in a new layered type' );
39  $this->addOption( 'type',
40  'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
41  $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
42  $this->setBatchSize( 100 );
43  }
44 
45  public function execute() {
46  $passwordFactory = MediaWikiServices::getInstance()->getPasswordFactory();
47 
48  $typeInfo = $passwordFactory->getTypes();
49  $layeredType = $this->getOption( 'type' );
50 
51  // Check that type exists and is a layered type
52  if ( !isset( $typeInfo[$layeredType] ) ) {
53  $this->fatalError( 'Undefined password type' );
54  }
55 
56  $passObj = $passwordFactory->newFromType( $layeredType );
57  if ( !$passObj instanceof LayeredParameterizedPassword ) {
58  $this->fatalError( 'Layered parameterized password type must be used.' );
59  }
60 
61  // Extract the first layer type
62  $typeConfig = $typeInfo[$layeredType];
63  $firstType = $typeConfig['types'][0];
64 
65  // Get a list of password types that are applicable
66  $dbw = $this->getDB( DB_MASTER );
67  $typeCond = 'user_password' . $dbw->buildLike( ":$firstType:", $dbw->anyString() );
68 
69  $minUserId = 0;
70  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
71  do {
72  $this->beginTransaction( $dbw, __METHOD__ );
73 
74  $res = $dbw->select( 'user',
75  [ 'user_id', 'user_name', 'user_password' ],
76  [
77  'user_id > ' . $dbw->addQuotes( $minUserId ),
78  $typeCond
79  ],
80  __METHOD__,
81  [
82  'ORDER BY' => 'user_id',
83  'LIMIT' => $this->getBatchSize(),
84  'LOCK IN SHARE MODE',
85  ]
86  );
87 
89  $updateUsers = [];
90  foreach ( $res as $row ) {
91  if ( $this->hasOption( 'verbose' ) ) {
92  $this->output( "Updating password for user {$row->user_name} ({$row->user_id}).\n" );
93  }
94 
95  $user = User::newFromId( $row->user_id );
97  $password = $passwordFactory->newFromCiphertext( $row->user_password );
98  '@phan-var ParameterizedPassword $password';
100  $layeredPassword = $passwordFactory->newFromType( $layeredType );
101  '@phan-var LayeredParameterizedPassword $layeredPassword';
102  $layeredPassword->partialCrypt( $password );
103 
104  $updateUsers[] = $user;
105  $dbw->update( 'user',
106  [ 'user_password' => $layeredPassword->toString() ],
107  [ 'user_id' => $row->user_id ],
108  __METHOD__
109  );
110 
111  $minUserId = $row->user_id;
112  }
113 
114  $this->commitTransaction( $dbw, __METHOD__ );
115  $lbFactory->waitForReplication();
116 
117  // Clear memcached so old passwords are wiped out
118  foreach ( $updateUsers as $user ) {
119  $user->clearSharedCache( 'refresh' );
120  }
121  } while ( $res->numRows() );
122  }
123 }
124 
125 $maintClass = WrapOldPasswords::class;
126 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
WrapOldPasswords
Maintenance script to wrap all passwords of a certain type in a specified layered type that wraps aro...
Definition: wrapOldPasswords.php:35
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:539
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
$res
$res
Definition: testCompression.php:52
WrapOldPasswords\__construct
__construct()
Default constructor.
Definition: wrapOldPasswords.php:36
WrapOldPasswords\execute
execute()
Do the actual work.
Definition: wrapOldPasswords.php:45
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1426
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: wrapOldPasswords.php:125
LayeredParameterizedPassword
This password hash type layers one or more parameterized password types on top of each other.
Definition: LayeredParameterizedPassword.php:33
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1441
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394