Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / maintenance / populateUserIsTemp.php
blob1891706ad585984c3209ab77444cb14b8572ddf3
1 <?php
2 /**
3 * Fills the user_is_temp column of the user table for users created before MW 1.42.
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
24 use MediaWiki\Maintenance\LoggedUpdateMaintenance;
25 use MediaWiki\User\TempUser\TempUserConfig;
26 use Wikimedia\Rdbms\IDatabase;
27 use Wikimedia\Rdbms\IExpression;
28 use Wikimedia\Rdbms\IReadableDatabase;
30 // @codeCoverageIgnoreStart
31 require_once __DIR__ . '/Maintenance.php';
32 // @codeCoverageIgnoreEnd
34 /**
35 * Maintenance script that Fills the user_is_temp column of the user table for users created before MW 1.42.
37 * @since 1.42
38 * @ingroup Maintenance
40 class PopulateUserIsTemp extends LoggedUpdateMaintenance {
42 private TempUserConfig $tempUserConfig;
43 private IDatabase $dbw;
44 private IReadableDatabase $dbr;
46 public function __construct() {
47 parent::__construct();
48 $this->addDescription( 'Populates the user_is_temp field of the user table.' );
49 $this->setBatchSize( 200 );
52 /** @inheritDoc */
53 protected function getUpdateKey() {
54 return __CLASS__;
57 /** @inheritDoc */
58 protected function doDBUpdates() {
59 $this->initServices();
61 if ( !$this->tempUserConfig->isKnown() ) {
62 // If temporary user auto-creation is not known, then just return early as there will be no rows to update.
63 return true;
66 // Generate a SelectQueryBuilder that selects all temporary users (based on the configured match patterns)
67 // which do have user_is_temp set to 0 (the default) in the the user table.
68 $queryBuilder = $this->dbr->newSelectQueryBuilder()
69 ->select( 'user_id' )
70 ->from( 'user' )
71 ->where( [
72 'user_is_temp' => 0,
73 $this->tempUserConfig->getMatchCondition( $this->dbr, 'user_name', IExpression::LIKE ),
74 ] )
75 ->limit( $this->getBatchSize() ?? 200 )
76 ->caller( __METHOD__ );
78 do {
79 // Get a batch of user IDs for temporary accounts that do not have user_is_temp set to 1.
80 $batch = $queryBuilder->fetchFieldValues();
81 if ( count( $batch ) ) {
82 // If there are user IDs in the batch, then update the user_is_temp column to '1' for these rows.
83 $this->dbw->newUpdateQueryBuilder()
84 ->update( 'user' )
85 ->set( [ 'user_is_temp' => 1 ] )
86 ->where( [ 'user_id' => $batch ] )
87 ->caller( __METHOD__ )
88 ->execute();
90 } while ( count( $batch ) >= ( $this->getBatchSize() ?? 200 ) );
92 return true;
95 /**
96 * Initialise the services and database connections used by this script.
98 * This code is not in ::doDBUpdates() so that this can be skipped in unit tests
99 * and the IReadableDatabase object can be strongly typed.
101 * @return void
103 protected function initServices(): void {
104 $this->tempUserConfig = $this->getServiceContainer()->getTempUserConfig();
105 $this->dbw = $this->getDB( DB_PRIMARY );
106 $this->dbr = $this->getDB( DB_REPLICA );
110 // @codeCoverageIgnoreStart
111 $maintClass = PopulateUserIsTemp::class;
112 require_once RUN_MAINTENANCE_IF_MAIN;
113 // @codeCoverageIgnoreEnd