MediaWiki master
populateUserIsTemp.php
Go to the documentation of this file.
1<?php
28
29// @codeCoverageIgnoreStart
30require_once __DIR__ . '/Maintenance.php';
31// @codeCoverageIgnoreEnd
32
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
52 protected function getUpdateKey() {
53 return __CLASS__;
54 }
55
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
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;
111require_once RUN_MAINTENANCE_IF_MAIN;
112// @codeCoverageIgnoreEnd
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
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:48
A database connection without write operations.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28