MediaWiki master
wrapOldPasswords.php
Go to the documentation of this file.
1<?php
16
17// @codeCoverageIgnoreStart
18require_once __DIR__ . '/Maintenance.php';
19// @codeCoverageIgnoreEnd
20
29 public function __construct() {
30 parent::__construct();
31 $this->addDescription( 'Wrap all passwords of a certain type in a new layered type. '
32 . 'The script runs in dry-run mode by default (use --update to update rows)' );
33 $this->addOption( 'type',
34 'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
35 $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
36 $this->addOption( 'update', 'Actually wrap passwords', false, false, 'u' );
37 $this->setBatchSize( 3 );
38 }
39
40 public function execute() {
41 $passwordFactory = $this->getServiceContainer()->getPasswordFactory();
42
43 $typeInfo = $passwordFactory->getTypes();
44 $layeredType = $this->getOption( 'type' );
45
46 // Check that type exists and is a layered type
47 if ( !isset( $typeInfo[$layeredType] ) ) {
48 $this->fatalError( 'Undefined password type' );
49 }
50
51 $passObj = $passwordFactory->newFromType( $layeredType );
52 if ( !$passObj instanceof LayeredParameterizedPassword ) {
53 $this->fatalError( 'Layered parameterized password type must be used.' );
54 }
55
56 // Extract the first layer type
57 $typeConfig = $typeInfo[$layeredType];
58 $firstType = $typeConfig['types'][0];
59
60 $update = $this->hasOption( 'update' );
61
62 // Get a list of password types that are applicable
63 $dbw = $this->getPrimaryDB();
64
65 $count = 0;
66 $minUserId = 0;
67 while ( true ) {
68 if ( $update ) {
69 $this->beginTransactionRound( __METHOD__ );
70 }
71
72 $start = microtime( true );
73 $res = $dbw->newSelectQueryBuilder()
74 ->select( [ 'user_id', 'user_name', 'user_password' ] )
75 ->lockInShareMode()
76 ->from( 'user' )
77 ->where( [
78 $dbw->expr( 'user_id', '>', $minUserId ),
79 $dbw->expr(
80 'user_password',
81 IExpression::LIKE,
82 new LikeValue( ":$firstType:", $dbw->anyString() )
83 ),
84 ] )
85 ->orderBy( 'user_id' )
86 ->limit( $this->getBatchSize() )
87 ->caller( __METHOD__ )->fetchResultSet();
88
89 if ( $res->numRows() === 0 ) {
90 if ( $update ) {
91 $this->commitTransactionRound( __METHOD__ );
92 }
93 break;
94 }
95
97 $updateUsers = [];
98 foreach ( $res as $row ) {
99 $user = User::newFromId( $row->user_id );
101 $password = $passwordFactory->newFromCiphertext( $row->user_password );
102 '@phan-var ParameterizedPassword $password';
104 $layeredPassword = $passwordFactory->newFromType( $layeredType );
105 '@phan-var LayeredParameterizedPassword $layeredPassword';
106 $layeredPassword->partialCrypt( $password );
107
108 if ( $this->hasOption( 'verbose' ) ) {
109 $this->output(
110 "Updating password for user {$row->user_name} ({$row->user_id}) from " .
111 "type {$password->getType()} to {$layeredPassword->getType()}.\n"
112 );
113 }
114
115 $count++;
116 if ( $update ) {
117 $updateUsers[] = $user;
118 $dbw->newUpdateQueryBuilder()
119 ->update( 'user' )
120 ->set( [ 'user_password' => $layeredPassword->toString() ] )
121 ->where( [ 'user_id' => $row->user_id ] )
122 ->caller( __METHOD__ )
123 ->execute();
124 }
125
126 $minUserId = $row->user_id;
127 }
128
129 if ( $update ) {
130 $this->commitTransactionRound( __METHOD__ );
131
132 // Clear memcached so old passwords are wiped out
133 foreach ( $updateUsers as $user ) {
134 $user->clearSharedCache( 'refresh' );
135 }
136 }
137
138 $this->output( "Last id processed: $minUserId; Actually updated: $count...\n" );
139 $delta = microtime( true ) - $start;
140 $this->output( sprintf(
141 "%4d passwords wrapped in %6.2fms (%6.2fms each)\n",
142 $res->numRows(),
143 $delta * 1000.0,
144 ( $delta / $res->numRows() ) * 1000.0
145 ) );
146 }
147
148 if ( $update ) {
149 $this->output( "$count users rows updated.\n" );
150 } else {
151 $this->output( "$count user rows found using old password formats. "
152 . "Run script again with --update to update these rows.\n" );
153 }
154 }
155}
156
157// @codeCoverageIgnoreStart
158$maintClass = WrapOldPasswords::class;
159require_once RUN_MAINTENANCE_IF_MAIN;
160// @codeCoverageIgnoreEnd
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.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getServiceContainer()
Returns the main service container.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
This password hash type layers one or more parameterized password types on top of each other.
Helper class for password hash types that have a delimited set of parameters inside the hash.
User class for the MediaWiki software.
Definition User.php:130
Content of like value.
Definition LikeValue.php:14
Maintenance script to wrap all passwords of a certain type in a specified layered type that wraps aro...
execute()
Do the actual work.
__construct()
Default constructor.