MediaWiki master
populateUserIsTemp.php
Go to the documentation of this file.
1<?php
29
30// @codeCoverageIgnoreStart
31require_once __DIR__ . '/Maintenance.php';
32// @codeCoverageIgnoreEnd
33
41
42 private TempUserConfig $tempUserConfig;
43 private IDatabase $dbw;
44 private IReadableDatabase $dbr;
45
46 public function __construct() {
47 parent::__construct();
48 $this->addDescription( 'Populates the user_is_temp field of the user table.' );
49 $this->setBatchSize( 200 );
50 }
51
53 protected function getUpdateKey() {
54 return __CLASS__;
55 }
56
58 protected function doDBUpdates() {
59 $this->initServices();
60
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;
64 }
65
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__ );
77
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();
89 }
90 } while ( count( $batch ) >= ( $this->getBatchSize() ?? 200 ) );
91
92 return true;
93 }
94
101 protected function initServices(): void {
102 $this->tempUserConfig = $this->getServiceContainer()->getTempUserConfig();
103 $this->dbw = $this->getDB( DB_PRIMARY );
104 $this->dbr = $this->getDB( DB_REPLICA );
105 }
106}
107
108// @codeCoverageIgnoreStart
109$maintClass = PopulateUserIsTemp::class;
110require_once RUN_MAINTENANCE_IF_MAIN;
111// @codeCoverageIgnoreEnd
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getBatchSize()
Returns batch size.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Maintenance script that Fills the user_is_temp column of the user table for users created before MW 1...
__construct()
Default constructor.
initServices()
Initialise the services and database connections used by this script.
getUpdateKey()
Get the update key name to go in the update log table.string
doDBUpdates()
Do the actual work.All child classes will need to implement this. Return true to log the update as do...
Interface for temporary user creation config and name matching.
Interface to a relational database.
Definition IDatabase.php:45
A database connection without write operations.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28