Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / maintenance / wrapOldPasswords.php
blob85fa7805e6c934be08318bbce3281118142083a4
1 <?php
3 use MediaWiki\MediaWikiServices;
5 /**
6 * Maintenance script to wrap all old-style passwords in a layered type
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
24 * @ingroup Maintenance
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script to wrap all passwords of a certain type in a specified layered
30 * type that wraps around the old type.
32 * @since 1.24
33 * @ingroup Maintenance
35 class WrapOldPasswords extends Maintenance {
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 );
45 public function execute() {
46 global $wgAuth;
48 if ( !$wgAuth->allowSetLocalPassword() ) {
49 $this->error( '$wgAuth does not allow local passwords. Aborting.', true );
52 $passwordFactory = new PasswordFactory();
53 $passwordFactory->init( RequestContext::getMain()->getConfig() );
55 $typeInfo = $passwordFactory->getTypes();
56 $layeredType = $this->getOption( 'type' );
58 // Check that type exists and is a layered type
59 if ( !isset( $typeInfo[$layeredType] ) ) {
60 $this->error( 'Undefined password type', true );
63 $passObj = $passwordFactory->newFromType( $layeredType );
64 if ( !$passObj instanceof LayeredParameterizedPassword ) {
65 $this->error( 'Layered parameterized password type must be used.', true );
68 // Extract the first layer type
69 $typeConfig = $typeInfo[$layeredType];
70 $firstType = $typeConfig['types'][0];
72 // Get a list of password types that are applicable
73 $dbw = $this->getDB( DB_MASTER );
74 $typeCond = 'user_password' . $dbw->buildLike( ":$firstType:", $dbw->anyString() );
76 $minUserId = 0;
77 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
78 do {
79 $this->beginTransaction( $dbw, __METHOD__ );
81 $res = $dbw->select( 'user',
82 [ 'user_id', 'user_name', 'user_password' ],
84 'user_id > ' . $dbw->addQuotes( $minUserId ),
85 $typeCond
87 __METHOD__,
89 'ORDER BY' => 'user_id',
90 'LIMIT' => $this->mBatchSize,
91 'LOCK IN SHARE MODE',
95 /** @var User[] $updateUsers */
96 $updateUsers = [];
97 foreach ( $res as $row ) {
98 if ( $this->hasOption( 'verbose' ) ) {
99 $this->output( "Updating password for user {$row->user_name} ({$row->user_id}).\n" );
102 $user = User::newFromId( $row->user_id );
103 /** @var ParameterizedPassword $password */
104 $password = $passwordFactory->newFromCiphertext( $row->user_password );
105 /** @var LayeredParameterizedPassword $layeredPassword */
106 $layeredPassword = $passwordFactory->newFromType( $layeredType );
107 $layeredPassword->partialCrypt( $password );
109 $updateUsers[] = $user;
110 $dbw->update( 'user',
111 [ 'user_password' => $layeredPassword->toString() ],
112 [ 'user_id' => $row->user_id ],
113 __METHOD__
116 $minUserId = $row->user_id;
119 $this->commitTransaction( $dbw, __METHOD__ );
120 $lbFactory->waitForReplication();
122 // Clear memcached so old passwords are wiped out
123 foreach ( $updateUsers as $user ) {
124 $user->clearSharedCache();
126 } while ( $res->numRows() );
130 $maintClass = "WrapOldPasswords";
131 require_once RUN_MAINTENANCE_IF_MAIN;