Merge "Drop index oi_name_archive_name on table oldimage"
[lhc/web/wiklou.git] / maintenance / wrapOldPasswords.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * Maintenance script to wrap all old-style passwords in a layered type
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @ingroup Maintenance
25 */
26 require_once __DIR__ . '/Maintenance.php';
27
28 /**
29 * Maintenance script to wrap all passwords of a certain type in a specified layered
30 * type that wraps around the old type.
31 *
32 * @since 1.24
33 * @ingroup Maintenance
34 */
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 );
43 }
44
45 public function execute() {
46 global $wgAuth;
47
48 if ( !$wgAuth->allowSetLocalPassword() ) {
49 $this->error( '$wgAuth does not allow local passwords. Aborting.', true );
50 }
51
52 $passwordFactory = new PasswordFactory();
53 $passwordFactory->init( RequestContext::getMain()->getConfig() );
54
55 $typeInfo = $passwordFactory->getTypes();
56 $layeredType = $this->getOption( 'type' );
57
58 // Check that type exists and is a layered type
59 if ( !isset( $typeInfo[$layeredType] ) ) {
60 $this->error( 'Undefined password type', true );
61 }
62
63 $passObj = $passwordFactory->newFromType( $layeredType );
64 if ( !$passObj instanceof LayeredParameterizedPassword ) {
65 $this->error( 'Layered parameterized password type must be used.', true );
66 }
67
68 // Extract the first layer type
69 $typeConfig = $typeInfo[$layeredType];
70 $firstType = $typeConfig['types'][0];
71
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() );
75
76 $minUserId = 0;
77 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
78 do {
79 $this->beginTransaction( $dbw, __METHOD__ );
80
81 $res = $dbw->select( 'user',
82 [ 'user_id', 'user_name', 'user_password' ],
83 [
84 'user_id > ' . $dbw->addQuotes( $minUserId ),
85 $typeCond
86 ],
87 __METHOD__,
88 [
89 'ORDER BY' => 'user_id',
90 'LIMIT' => $this->mBatchSize,
91 'LOCK IN SHARE MODE',
92 ]
93 );
94
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" );
100 }
101
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 );
108
109 $updateUsers[] = $user;
110 $dbw->update( 'user',
111 [ 'user_password' => $layeredPassword->toString() ],
112 [ 'user_id' => $row->user_id ],
113 __METHOD__
114 );
115
116 $minUserId = $row->user_id;
117 }
118
119 $this->commitTransaction( $dbw, __METHOD__ );
120 $lbFactory->waitForReplication();
121
122 // Clear memcached so old passwords are wiped out
123 foreach ( $updateUsers as $user ) {
124 $user->clearSharedCache();
125 }
126 } while ( $res->numRows() );
127 }
128 }
129
130 $maintClass = "WrapOldPasswords";
131 require_once RUN_MAINTENANCE_IF_MAIN;