Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / maintenance / wrapOldPasswords.php
blob0a859c0ef4f28e331192bc1d3a6a62745c654433
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
23 require_once __DIR__ . '/Maintenance.php';
25 /**
26 * Maintenance script to wrap all passwords of a certain type in a specified layered
27 * type that wraps around the old type.
29 * @since 1.24
30 * @ingroup Maintenance
32 class WrapOldPasswords extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription( 'Wrap all passwords of a certain type in a new layered type' );
36 $this->addOption( 'type',
37 'Password type to wrap passwords in (must inherit LayeredParameterizedPassword)', true, true );
38 $this->addOption( 'verbose', 'Enables verbose output', false, false, 'v' );
39 $this->setBatchSize( 100 );
42 public function execute() {
43 global $wgAuth;
45 if ( !$wgAuth->allowSetLocalPassword() ) {
46 $this->error( '$wgAuth does not allow local passwords. Aborting.', true );
49 $passwordFactory = new PasswordFactory();
50 $passwordFactory->init( RequestContext::getMain()->getConfig() );
52 $typeInfo = $passwordFactory->getTypes();
53 $layeredType = $this->getOption( 'type' );
55 // Check that type exists and is a layered type
56 if ( !isset( $typeInfo[$layeredType] ) ) {
57 $this->error( 'Undefined password type', true );
60 $passObj = $passwordFactory->newFromType( $layeredType );
61 if ( !$passObj instanceof LayeredParameterizedPassword ) {
62 $this->error( 'Layered parameterized password type must be used.', true );
65 // Extract the first layer type
66 $typeConfig = $typeInfo[$layeredType];
67 $firstType = $typeConfig['types'][0];
69 // Get a list of password types that are applicable
70 $dbw = $this->getDB( DB_MASTER );
71 $typeCond = 'user_password' . $dbw->buildLike( ":$firstType:", $dbw->anyString() );
73 $minUserId = 0;
74 do {
75 $this->beginTransaction( $dbw, __METHOD__ );
77 $res = $dbw->select( 'user',
78 [ 'user_id', 'user_name', 'user_password' ],
80 'user_id > ' . $dbw->addQuotes( $minUserId ),
81 $typeCond
83 __METHOD__,
85 'ORDER BY' => 'user_id',
86 'LIMIT' => $this->mBatchSize,
87 'LOCK IN SHARE MODE',
91 /** @var User[] $updateUsers */
92 $updateUsers = [];
93 foreach ( $res as $row ) {
94 if ( $this->hasOption( 'verbose' ) ) {
95 $this->output( "Updating password for user {$row->user_name} ({$row->user_id}).\n" );
98 $user = User::newFromId( $row->user_id );
99 /** @var ParameterizedPassword $password */
100 $password = $passwordFactory->newFromCiphertext( $row->user_password );
101 /** @var LayeredParameterizedPassword $layeredPassword */
102 $layeredPassword = $passwordFactory->newFromType( $layeredType );
103 $layeredPassword->partialCrypt( $password );
105 $updateUsers[] = $user;
106 $dbw->update( 'user',
107 [ 'user_password' => $layeredPassword->toString() ],
108 [ 'user_id' => $row->user_id ],
109 __METHOD__
112 $minUserId = $row->user_id;
115 $this->commitTransaction( $dbw, __METHOD__ );
116 LBFactory::waitForReplication();
118 // Clear memcached so old passwords are wiped out
119 foreach ( $updateUsers as $user ) {
120 $user->clearSharedCache();
122 } while ( $res->numRows() );
126 $maintClass = "WrapOldPasswords";
127 require_once RUN_MAINTENANCE_IF_MAIN;