ApiParse: don't reparse language link titles
[mediawiki.git] / maintenance / wrapOldPasswords.php
blob3ee3fb3559f46c47e8d7350b0b191643e031bda7
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\Password\LayeredParameterizedPassword;
25 use MediaWiki\Password\ParameterizedPassword;
26 use MediaWiki\User\User;
27 use Wikimedia\Rdbms\IExpression;
28 use Wikimedia\Rdbms\LikeValue;
30 // @codeCoverageIgnoreStart
31 require_once __DIR__ . '/Maintenance.php';
32 // @codeCoverageIgnoreEnd
34 /**
35 * Maintenance script to wrap all passwords of a certain type in a specified layered
36 * type that wraps around the old type.
38 * @since 1.24
39 * @ingroup Maintenance
41 class WrapOldPasswords extends Maintenance {
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( 'Wrap all passwords of a certain type in a new layered type. '
45 . 'The script runs in dry-run mode by default (use --update to update rows)' );
46 $this->addOption( 'type',
47 'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
48 $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
49 $this->addOption( 'update', 'Actually wrap passwords', false, false, 'u' );
50 $this->setBatchSize( 3 );
53 public function execute() {
54 $passwordFactory = $this->getServiceContainer()->getPasswordFactory();
56 $typeInfo = $passwordFactory->getTypes();
57 $layeredType = $this->getOption( 'type' );
59 // Check that type exists and is a layered type
60 if ( !isset( $typeInfo[$layeredType] ) ) {
61 $this->fatalError( 'Undefined password type' );
64 $passObj = $passwordFactory->newFromType( $layeredType );
65 if ( !$passObj instanceof LayeredParameterizedPassword ) {
66 $this->fatalError( 'Layered parameterized password type must be used.' );
69 // Extract the first layer type
70 $typeConfig = $typeInfo[$layeredType];
71 $firstType = $typeConfig['types'][0];
73 $update = $this->hasOption( 'update' );
75 // Get a list of password types that are applicable
76 $dbw = $this->getPrimaryDB();
78 $count = 0;
79 $minUserId = 0;
80 while ( true ) {
81 if ( $update ) {
82 $this->beginTransaction( $dbw, __METHOD__ );
85 $start = microtime( true );
86 $res = $dbw->newSelectQueryBuilder()
87 ->select( [ 'user_id', 'user_name', 'user_password' ] )
88 ->lockInShareMode()
89 ->from( 'user' )
90 ->where( [
91 $dbw->expr( 'user_id', '>', $minUserId ),
92 $dbw->expr(
93 'user_password',
94 IExpression::LIKE,
95 new LikeValue( ":$firstType:", $dbw->anyString() )
97 ] )
98 ->orderBy( 'user_id' )
99 ->limit( $this->getBatchSize() )
100 ->caller( __METHOD__ )->fetchResultSet();
102 if ( $res->numRows() === 0 ) {
103 if ( $update ) {
104 $this->commitTransaction( $dbw, __METHOD__ );
106 break;
109 /** @var User[] $updateUsers */
110 $updateUsers = [];
111 foreach ( $res as $row ) {
112 $user = User::newFromId( $row->user_id );
113 /** @var ParameterizedPassword $password */
114 $password = $passwordFactory->newFromCiphertext( $row->user_password );
115 '@phan-var ParameterizedPassword $password';
116 /** @var LayeredParameterizedPassword $layeredPassword */
117 $layeredPassword = $passwordFactory->newFromType( $layeredType );
118 '@phan-var LayeredParameterizedPassword $layeredPassword';
119 $layeredPassword->partialCrypt( $password );
121 if ( $this->hasOption( 'verbose' ) ) {
122 $this->output(
123 "Updating password for user {$row->user_name} ({$row->user_id}) from " .
124 "type {$password->getType()} to {$layeredPassword->getType()}.\n"
128 $count++;
129 if ( $update ) {
130 $updateUsers[] = $user;
131 $dbw->newUpdateQueryBuilder()
132 ->update( 'user' )
133 ->set( [ 'user_password' => $layeredPassword->toString() ] )
134 ->where( [ 'user_id' => $row->user_id ] )
135 ->caller( __METHOD__ )
136 ->execute();
139 $minUserId = $row->user_id;
142 if ( $update ) {
143 $this->commitTransaction( $dbw, __METHOD__ );
145 // Clear memcached so old passwords are wiped out
146 foreach ( $updateUsers as $user ) {
147 $user->clearSharedCache( 'refresh' );
151 $this->output( "Last id processed: $minUserId; Actually updated: $count...\n" );
152 $delta = microtime( true ) - $start;
153 $this->output( sprintf(
154 "%4d passwords wrapped in %6.2fms (%6.2fms each)\n",
155 $res->numRows(),
156 $delta * 1000.0,
157 ( $delta / $res->numRows() ) * 1000.0
158 ) );
161 if ( $update ) {
162 $this->output( "$count users rows updated.\n" );
163 } else {
164 $this->output( "$count user rows found using old password formats. "
165 . "Run script again with --update to update these rows.\n" );
170 // @codeCoverageIgnoreStart
171 $maintClass = WrapOldPasswords::class;
172 require_once RUN_MAINTENANCE_IF_MAIN;
173 // @codeCoverageIgnoreEnd