MediaWiki master
removeUnusedAccounts.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
37 public function __construct() {
38 parent::__construct();
39 $this->addOption( 'delete', 'Actually delete the account' );
40 $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
41 $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
42 }
43
44 public function execute() {
45 $services = $this->getServiceContainer();
46 $userFactory = $services->getUserFactory();
47 $userGroupManager = $services->getUserGroupManager();
48 $this->output( "Remove unused accounts\n\n" );
49
50 # Do an initial scan for inactive accounts and report the result
51 $this->output( "Checking for unused user accounts...\n" );
52 $delUser = [];
53 $delActor = [];
54 $dbr = $this->getReplicaDB();
55 $res = $dbr->newSelectQueryBuilder()
56 ->select( [ 'user_id', 'user_name', 'user_touched', 'actor_id' ] )
57 ->from( 'user' )
58 ->leftJoin( 'actor', null, 'user_id = actor_user' )
59 ->caller( __METHOD__ )->fetchResultSet();
60 if ( $this->hasOption( 'ignore-groups' ) ) {
61 $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
62 } else {
63 $excludedGroups = [];
64 }
65 $touched = $this->getOption( 'ignore-touched', "1" );
66 if ( !ctype_digit( $touched ) ) {
67 $this->fatalError( "Please put a valid positive integer on the --ignore-touched parameter." );
68 }
69 $touchedSeconds = 86400 * $touched;
70 foreach ( $res as $row ) {
71 # Check the account, but ignore it if it's within a $excludedGroups
72 # group or if it's touched within the $touchedSeconds seconds.
73 $instance = $userFactory->newFromId( $row->user_id );
74 if ( count(
75 array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
76 && $this->isInactiveAccount( $instance, $row->actor_id ?? null, true )
77 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds
78 )
79 ) {
80 # Inactive; print out the name and flag it
81 $delUser[] = $row->user_id;
82 if ( isset( $row->actor_id ) && $row->actor_id ) {
83 $delActor[] = $row->actor_id;
84 }
85 $this->output( $row->user_name . "\n" );
86 }
87 }
88 $count = count( $delUser );
89 $this->output( "...found {$count}.\n" );
90
91 # If required, go back and delete each marked account
92 if ( $count > 0 && $this->hasOption( 'delete' ) ) {
93 $this->output( "\nDeleting unused accounts..." );
94 $dbw = $this->getPrimaryDB();
95 $dbw->newDeleteQueryBuilder()
96 ->deleteFrom( 'user' )
97 ->where( [ 'user_id' => $delUser ] )
98 ->caller( __METHOD__ )->execute();
99 # Keep actor rows referenced from ipblocks
100 $stage = $this->getConfig()
101 ->get( MainConfigNames::BlockTargetMigrationStage );
102 if ( $stage & SCHEMA_COMPAT_READ_OLD ) {
103 $keep = $dbw->newSelectQueryBuilder()
104 ->select( 'ipb_by_actor' )
105 ->from( 'ipblocks' )
106 ->where( [ 'ipb_by_actor' => $delActor ] )
107 ->caller( __METHOD__ )->fetchFieldValues();
108 } else {
109 $keep = $dbw->newSelectQueryBuilder()
110 ->select( 'bl_by_actor' )
111 ->from( 'block' )
112 ->where( [ 'bl_by_actor' => $delActor ] )
113 ->caller( __METHOD__ )->fetchFieldValues();
114 }
115 $del = array_diff( $delActor, $keep );
116 if ( $del ) {
117 $dbw->newDeleteQueryBuilder()
118 ->deleteFrom( 'actor' )
119 ->where( [ 'actor_id' => $del ] )
120 ->caller( __METHOD__ )->execute();
121 }
122 if ( $keep ) {
123 $dbw->newUpdateQueryBuilder()
124 ->update( 'actor' )
125 ->set( [ 'actor_user' => null ] )
126 ->where( [ 'actor_id' => $keep ] )
127 ->caller( __METHOD__ )
128 ->execute();
129 }
130 $dbw->newDeleteQueryBuilder()
131 ->deleteFrom( 'user_groups' )
132 ->where( [ 'ug_user' => $delUser ] )
133 ->caller( __METHOD__ )->execute();
134 $dbw->newDeleteQueryBuilder()
135 ->deleteFrom( 'user_former_groups' )
136 ->where( [ 'ufg_user' => $delUser ] )
137 ->caller( __METHOD__ )->execute();
138 $dbw->newDeleteQueryBuilder()
139 ->deleteFrom( 'user_properties' )
140 ->where( [ 'up_user' => $delUser ] )
141 ->caller( __METHOD__ )->execute();
142 $dbw->newDeleteQueryBuilder()
143 ->deleteFrom( 'logging' )
144 ->where( [ 'log_actor' => $delActor ] )
145 ->caller( __METHOD__ )->execute();
146 $dbw->newDeleteQueryBuilder()
147 ->deleteFrom( 'recentchanges' )
148 ->where( [ 'rc_actor' => $delActor ] )
149 ->caller( __METHOD__ )->execute();
150 $this->output( "done.\n" );
151 # Update the site_stats.ss_users field
152 $users = $dbw->newSelectQueryBuilder()
153 ->select( 'COUNT(*)' )
154 ->from( 'user' )
155 ->caller( __METHOD__ )->fetchField();
156 $dbw->newUpdateQueryBuilder()
157 ->update( 'site_stats' )
158 ->set( [ 'ss_users' => $users ] )
159 ->where( [ 'ss_row_id' => 1 ] )
160 ->caller( __METHOD__ )
161 ->execute();
162 } elseif ( $count > 0 ) {
163 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
164 }
165 $this->output( "\n" );
166 }
167
177 private function isInactiveAccount( $user, $actor, $primary = false ) {
178 if ( $actor === null ) {
179 // There's no longer a way for a user to be active in any of
180 // these tables without having an actor ID. The only way to link
181 // to a user row is via an actor row.
182 return true;
183 }
184
185 $dbo = $primary ? $this->getPrimaryDB() : $this->getReplicaDB();
186 $checks = [
187 'archive' => 'ar',
188 'image' => 'img',
189 'oldimage' => 'oi',
190 'filearchive' => 'fa',
191 'revision' => 'rev',
192 ];
193 $count = 0;
194
195 $this->beginTransaction( $dbo, __METHOD__ );
196 foreach ( $checks as $table => $prefix ) {
197 $count += (int)$dbo->newSelectQueryBuilder()
198 ->select( 'COUNT(*)' )
199 ->from( $table )
200 ->where( [ "{$prefix}_actor" => $actor ] )
201 ->caller( __METHOD__ )
202 ->fetchField();
203 }
204
205 $count += (int)$dbo->newSelectQueryBuilder()
206 ->select( 'COUNT(*)' )
207 ->from( 'logging' )
208 ->where( [ 'log_actor' => $actor, $dbo->expr( 'log_type', '!=', 'newusers' ) ] )
209 ->caller( __METHOD__ )->fetchField();
210
211 $this->commitTransaction( $dbo, __METHOD__ );
212
213 return $count == 0;
214 }
215}
216
217$maintClass = RemoveUnusedAccounts::class;
218require_once RUN_MAINTENANCE_IF_MAIN;
const SCHEMA_COMPAT_READ_OLD
Definition Defines.php:275
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.
Maintenance script that removes unused user accounts from the database.
execute()
Do the actual work.
__construct()
Default constructor.
Interface for objects representing user identity.