MediaWiki  1.29.2
wrapOldPasswords.php
Go to the documentation of this file.
1 <?php
2 
4 
26 require_once __DIR__ . '/Maintenance.php';
27 
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 
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 );
104  $password = $passwordFactory->newFromCiphertext( $row->user_password );
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;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:103
WrapOldPasswords
Maintenance script to wrap all passwords of a certain type in a specified layered type that wraps aro...
Definition: wrapOldPasswords.php:35
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:579
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$lbFactory
$lbFactory
Definition: doMaintenance.php:117
WrapOldPasswords\__construct
__construct()
Default constructor.
Definition: wrapOldPasswords.php:36
WrapOldPasswords\execute
execute()
Do the actual work.
Definition: wrapOldPasswords.php:45
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1278
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:504
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: wrapOldPasswords.php:130
LayeredParameterizedPassword
This password hash type layers one or more parameterized password types on top of each other.
Definition: LayeredParameterizedPassword.php:33
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1293
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:468
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
PasswordFactory
Factory class for creating and checking Password objects.
Definition: PasswordFactory.php:28
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314