MediaWiki master
populateUserIsTemp.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
38
39 private TempUserConfig $tempUserConfig;
40 private IDatabase $dbw;
41 private IReadableDatabase $dbr;
42
43 public function __construct() {
44 parent::__construct();
45 $this->addDescription( 'Populates the user_is_temp field of the user table.' );
46 $this->setBatchSize( 200 );
47 }
48
50 protected function getUpdateKey() {
51 return __CLASS__;
52 }
53
55 protected function doDBUpdates() {
56 $this->initServices();
57
58 if ( !$this->tempUserConfig->isEnabled() ) {
59 // If temporary user auto-creation is disabled, then just return early as there will be no rows to update.
60 return true;
61 }
62
63 // Generate a SelectQueryBuilder that selects all temporary users (based on the configured match patterns)
64 // which do have user_is_temp set to 0 (the default) in the the user table.
65 $queryBuilder = $this->dbr->newSelectQueryBuilder()
66 ->select( 'user_id' )
67 ->from( 'user' )
68 ->where( [
69 'user_is_temp' => 0,
70 $this->tempUserConfig->getMatchCondition( $this->dbr, 'user_name', IExpression::LIKE ),
71 ] )
72 ->limit( $this->getBatchSize() ?? 200 )
73 ->caller( __METHOD__ );
74
75 do {
76 // Get a batch of user IDs for temporary accounts that do not have user_is_temp set to 1.
77 $batch = $queryBuilder->fetchFieldValues();
78 if ( count( $batch ) ) {
79 // If there are user IDs in the batch, then update the user_is_temp column to '1' for these rows.
80 $this->dbw->newUpdateQueryBuilder()
81 ->update( 'user' )
82 ->set( [ 'user_is_temp' => 1 ] )
83 ->where( [ 'user_id' => $batch ] )
84 ->caller( __METHOD__ )
85 ->execute();
86 }
87 } while ( count( $batch ) >= ( $this->getBatchSize() ?? 200 ) );
88
89 return true;
90 }
91
100 protected function initServices(): void {
101 $this->tempUserConfig = $this->getServiceContainer()->getTempUserConfig();
102 $this->dbw = $this->getDB( DB_PRIMARY );
103 $this->dbr = $this->getDB( DB_REPLICA );
104 }
105}
106
107$maintClass = PopulateUserIsTemp::class;
108require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
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.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
A database connection without write operations.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28