Debian Codegraph @

maintenance/populateUserIsTemp.php

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