Merge "doc: SpanInterface: more dev-friendly comments"
[mediawiki.git] / maintenance / wrapOldPasswords.php
blob25725e134da53f2d417adf6e49888c6c83b3ad84
1 <?php
2 /**
3 * Maintenance script to wrap all old-style passwords in a layered type
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\Maintenance\Maintenance;
25 use MediaWiki\Password\LayeredParameterizedPassword;
26 use MediaWiki\Password\ParameterizedPassword;
27 use MediaWiki\User\User;
28 use Wikimedia\Rdbms\IExpression;
29 use Wikimedia\Rdbms\LikeValue;
31 // @codeCoverageIgnoreStart
32 require_once __DIR__ . '/Maintenance.php';
33 // @codeCoverageIgnoreEnd
35 /**
36 * Maintenance script to wrap all passwords of a certain type in a specified layered
37 * type that wraps around the old type.
39 * @since 1.24
40 * @ingroup Maintenance
42 class WrapOldPasswords extends Maintenance {
43 public function __construct() {
44 parent::__construct();
45 $this->addDescription( 'Wrap all passwords of a certain type in a new layered type. '
46 . 'The script runs in dry-run mode by default (use --update to update rows)' );
47 $this->addOption( 'type',
48 'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
49 $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
50 $this->addOption( 'update', 'Actually wrap passwords', false, false, 'u' );
51 $this->setBatchSize( 3 );
54 public function execute() {
55 $passwordFactory = $this->getServiceContainer()->getPasswordFactory();
57 $typeInfo = $passwordFactory->getTypes();
58 $layeredType = $this->getOption( 'type' );
60 // Check that type exists and is a layered type
61 if ( !isset( $typeInfo[$layeredType] ) ) {
62 $this->fatalError( 'Undefined password type' );
65 $passObj = $passwordFactory->newFromType( $layeredType );
66 if ( !$passObj instanceof LayeredParameterizedPassword ) {
67 $this->fatalError( 'Layered parameterized password type must be used.' );
70 // Extract the first layer type
71 $typeConfig = $typeInfo[$layeredType];
72 $firstType = $typeConfig['types'][0];
74 $update = $this->hasOption( 'update' );
76 // Get a list of password types that are applicable
77 $dbw = $this->getPrimaryDB();
79 $count = 0;
80 $minUserId = 0;
81 while ( true ) {
82 if ( $update ) {
83 $this->beginTransaction( $dbw, __METHOD__ );
86 $start = microtime( true );
87 $res = $dbw->newSelectQueryBuilder()
88 ->select( [ 'user_id', 'user_name', 'user_password' ] )
89 ->lockInShareMode()
90 ->from( 'user' )
91 ->where( [
92 $dbw->expr( 'user_id', '>', $minUserId ),
93 $dbw->expr(
94 'user_password',
95 IExpression::LIKE,
96 new LikeValue( ":$firstType:", $dbw->anyString() )
98 ] )
99 ->orderBy( 'user_id' )
100 ->limit( $this->getBatchSize() )
101 ->caller( __METHOD__ )->fetchResultSet();
103 if ( $res->numRows() === 0 ) {
104 if ( $update ) {
105 $this->commitTransaction( $dbw, __METHOD__ );
107 break;
110 /** @var User[] $updateUsers */
111 $updateUsers = [];
112 foreach ( $res as $row ) {
113 $user = User::newFromId( $row->user_id );
114 /** @var ParameterizedPassword $password */
115 $password = $passwordFactory->newFromCiphertext( $row->user_password );
116 '@phan-var ParameterizedPassword $password';
117 /** @var LayeredParameterizedPassword $layeredPassword */
118 $layeredPassword = $passwordFactory->newFromType( $layeredType );
119 '@phan-var LayeredParameterizedPassword $layeredPassword';
120 $layeredPassword->partialCrypt( $password );
122 if ( $this->hasOption( 'verbose' ) ) {
123 $this->output(
124 "Updating password for user {$row->user_name} ({$row->user_id}) from " .
125 "type {$password->getType()} to {$layeredPassword->getType()}.\n"
129 $count++;
130 if ( $update ) {
131 $updateUsers[] = $user;
132 $dbw->newUpdateQueryBuilder()
133 ->update( 'user' )
134 ->set( [ 'user_password' => $layeredPassword->toString() ] )
135 ->where( [ 'user_id' => $row->user_id ] )
136 ->caller( __METHOD__ )
137 ->execute();
140 $minUserId = $row->user_id;
143 if ( $update ) {
144 $this->commitTransaction( $dbw, __METHOD__ );
146 // Clear memcached so old passwords are wiped out
147 foreach ( $updateUsers as $user ) {
148 $user->clearSharedCache( 'refresh' );
152 $this->output( "Last id processed: $minUserId; Actually updated: $count...\n" );
153 $delta = microtime( true ) - $start;
154 $this->output( sprintf(
155 "%4d passwords wrapped in %6.2fms (%6.2fms each)\n",
156 $res->numRows(),
157 $delta * 1000.0,
158 ( $delta / $res->numRows() ) * 1000.0
159 ) );
162 if ( $update ) {
163 $this->output( "$count users rows updated.\n" );
164 } else {
165 $this->output( "$count user rows found using old password formats. "
166 . "Run script again with --update to update these rows.\n" );
171 // @codeCoverageIgnoreStart
172 $maintClass = WrapOldPasswords::class;
173 require_once RUN_MAINTENANCE_IF_MAIN;
174 // @codeCoverageIgnoreEnd