Merge "Print chained exceptions when maintenance script fails."
[lhc/web/wiklou.git] / maintenance / wrapOldPasswords.php
1 <?php
2 /**
3 * Maintenance script to wrap all old-style passwords in a layered type
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 use MediaWiki\MediaWikiServices;
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 $passwordFactory = MediaWikiServices::getInstance()->getPasswordFactory();
47
48 $typeInfo = $passwordFactory->getTypes();
49 $layeredType = $this->getOption( 'type' );
50
51 // Check that type exists and is a layered type
52 if ( !isset( $typeInfo[$layeredType] ) ) {
53 $this->fatalError( 'Undefined password type' );
54 }
55
56 $passObj = $passwordFactory->newFromType( $layeredType );
57 if ( !$passObj instanceof LayeredParameterizedPassword ) {
58 $this->fatalError( 'Layered parameterized password type must be used.' );
59 }
60
61 // Extract the first layer type
62 $typeConfig = $typeInfo[$layeredType];
63 $firstType = $typeConfig['types'][0];
64
65 // Get a list of password types that are applicable
66 $dbw = $this->getDB( DB_MASTER );
67 $typeCond = 'user_password' . $dbw->buildLike( ":$firstType:", $dbw->anyString() );
68
69 $minUserId = 0;
70 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
71 do {
72 $this->beginTransaction( $dbw, __METHOD__ );
73
74 $res = $dbw->select( 'user',
75 [ 'user_id', 'user_name', 'user_password' ],
76 [
77 'user_id > ' . $dbw->addQuotes( $minUserId ),
78 $typeCond
79 ],
80 __METHOD__,
81 [
82 'ORDER BY' => 'user_id',
83 'LIMIT' => $this->getBatchSize(),
84 'LOCK IN SHARE MODE',
85 ]
86 );
87
88 /** @var User[] $updateUsers */
89 $updateUsers = [];
90 foreach ( $res as $row ) {
91 if ( $this->hasOption( 'verbose' ) ) {
92 $this->output( "Updating password for user {$row->user_name} ({$row->user_id}).\n" );
93 }
94
95 $user = User::newFromId( $row->user_id );
96 /** @var ParameterizedPassword $password */
97 $password = $passwordFactory->newFromCiphertext( $row->user_password );
98 /** @var LayeredParameterizedPassword $layeredPassword */
99 $layeredPassword = $passwordFactory->newFromType( $layeredType );
100 $layeredPassword->partialCrypt( $password );
101
102 $updateUsers[] = $user;
103 $dbw->update( 'user',
104 [ 'user_password' => $layeredPassword->toString() ],
105 [ 'user_id' => $row->user_id ],
106 __METHOD__
107 );
108
109 $minUserId = $row->user_id;
110 }
111
112 $this->commitTransaction( $dbw, __METHOD__ );
113 $lbFactory->waitForReplication();
114
115 // Clear memcached so old passwords are wiped out
116 foreach ( $updateUsers as $user ) {
117 $user->clearSharedCache( 'refresh' );
118 }
119 } while ( $res->numRows() );
120 }
121 }
122
123 $maintClass = WrapOldPasswords::class;
124 require_once RUN_MAINTENANCE_IF_MAIN;