MediaWiki master
removeUnusedAccounts.php
Go to the documentation of this file.
1<?php
29
30require_once __DIR__ . '/Maintenance.php';
31
38 public function __construct() {
39 parent::__construct();
40 $this->addOption( 'delete', 'Actually delete the account' );
41 $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
42 $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
43 }
44
45 public function execute() {
46 $services = $this->getServiceContainer();
47 $userFactory = $services->getUserFactory();
48 $userGroupManager = $services->getUserGroupManager();
49 $this->output( "Remove unused accounts\n\n" );
50
51 # Do an initial scan for inactive accounts and report the result
52 $this->output( "Checking for unused user accounts...\n" );
53 $delUser = [];
54 $delActor = [];
55 $dbr = $this->getReplicaDB();
56 $res = $dbr->newSelectQueryBuilder()
57 ->select( [ 'user_id', 'user_name', 'user_touched', 'actor_id' ] )
58 ->from( 'user' )
59 ->leftJoin( 'actor', null, 'user_id = actor_user' )
60 ->caller( __METHOD__ )->fetchResultSet();
61 if ( $this->hasOption( 'ignore-groups' ) ) {
62 $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
63 } else {
64 $excludedGroups = [];
65 }
66 $touched = $this->getOption( 'ignore-touched', "1" );
67 if ( !ctype_digit( $touched ) ) {
68 $this->fatalError( "Please put a valid positive integer on the --ignore-touched parameter." );
69 }
70 $touchedSeconds = 86400 * $touched;
71 foreach ( $res as $row ) {
72 # Check the account, but ignore it if it's within a $excludedGroups
73 # group or if it's touched within the $touchedSeconds seconds.
74 $instance = $userFactory->newFromId( $row->user_id );
75 if ( count(
76 array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
77 && $this->isInactiveAccount( $instance, $row->actor_id ?? null, true )
78 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds
79 )
80 ) {
81 # Inactive; print out the name and flag it
82 $delUser[] = $row->user_id;
83 if ( isset( $row->actor_id ) && $row->actor_id ) {
84 $delActor[] = $row->actor_id;
85 }
86 $this->output( $row->user_name . "\n" );
87 }
88 }
89 $count = count( $delUser );
90 $this->output( "...found {$count}.\n" );
91
92 # If required, go back and delete each marked account
93 if ( $count > 0 && $this->hasOption( 'delete' ) ) {
94 $this->output( "\nDeleting unused accounts..." );
95 $dbw = $this->getPrimaryDB();
96 $dbw->newDeleteQueryBuilder()
97 ->deleteFrom( 'user' )
98 ->where( [ 'user_id' => $delUser ] )
99 ->caller( __METHOD__ )->execute();
100 # Keep actor rows referenced from ipblocks
101 $stage = $this->getConfig()
102 ->get( MainConfigNames::BlockTargetMigrationStage );
103 if ( $stage & SCHEMA_COMPAT_READ_OLD ) {
104 $keep = $dbw->newSelectQueryBuilder()
105 ->select( 'ipb_by_actor' )
106 ->from( 'ipblocks' )
107 ->where( [ 'ipb_by_actor' => $delActor ] )
108 ->caller( __METHOD__ )->fetchFieldValues();
109 } else {
110 $keep = $dbw->newSelectQueryBuilder()
111 ->select( 'bl_by_actor' )
112 ->from( 'block' )
113 ->where( [ 'bl_by_actor' => $delActor ] )
114 ->caller( __METHOD__ )->fetchFieldValues();
115 }
116 $del = array_diff( $delActor, $keep );
117 if ( $del ) {
118 $dbw->newDeleteQueryBuilder()
119 ->deleteFrom( 'actor' )
120 ->where( [ 'actor_id' => $del ] )
121 ->caller( __METHOD__ )->execute();
122 }
123 if ( $keep ) {
124 $dbw->newUpdateQueryBuilder()
125 ->update( 'actor' )
126 ->set( [ 'actor_user' => null ] )
127 ->where( [ 'actor_id' => $keep ] )
128 ->caller( __METHOD__ )
129 ->execute();
130 }
131 $dbw->newDeleteQueryBuilder()
132 ->deleteFrom( 'user_groups' )
133 ->where( [ 'ug_user' => $delUser ] )
134 ->caller( __METHOD__ )->execute();
135 $dbw->newDeleteQueryBuilder()
136 ->deleteFrom( 'user_former_groups' )
137 ->where( [ 'ufg_user' => $delUser ] )
138 ->caller( __METHOD__ )->execute();
139 $dbw->newDeleteQueryBuilder()
140 ->deleteFrom( 'user_properties' )
141 ->where( [ 'up_user' => $delUser ] )
142 ->caller( __METHOD__ )->execute();
143 $dbw->newDeleteQueryBuilder()
144 ->deleteFrom( 'logging' )
145 ->where( [ 'log_actor' => $delActor ] )
146 ->caller( __METHOD__ )->execute();
147 $dbw->newDeleteQueryBuilder()
148 ->deleteFrom( 'recentchanges' )
149 ->where( [ 'rc_actor' => $delActor ] )
150 ->caller( __METHOD__ )->execute();
151 $this->output( "done.\n" );
152 # Update the site_stats.ss_users field
153 $users = $dbw->newSelectQueryBuilder()
154 ->select( 'COUNT(*)' )
155 ->from( 'user' )
156 ->caller( __METHOD__ )->fetchField();
157 $dbw->newUpdateQueryBuilder()
158 ->update( 'site_stats' )
159 ->set( [ 'ss_users' => $users ] )
160 ->where( [ 'ss_row_id' => 1 ] )
161 ->caller( __METHOD__ )
162 ->execute();
163 } elseif ( $count > 0 ) {
164 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
165 }
166 $this->output( "\n" );
167 }
168
178 private function isInactiveAccount( $user, $actor, $primary = false ) {
179 if ( $actor === null ) {
180 // There's no longer a way for a user to be active in any of
181 // these tables without having an actor ID. The only way to link
182 // to a user row is via an actor row.
183 return true;
184 }
185
186 $dbo = $primary ? $this->getPrimaryDB() : $this->getReplicaDB();
187 $checks = [
188 'archive' => 'ar',
189 'image' => 'img',
190 'oldimage' => 'oi',
191 'filearchive' => 'fa'
192 // re-add when actor migration is complete
193 // 'revision' => 'rev'
194 ];
195 $count = 0;
196
197 $this->beginTransaction( $dbo, __METHOD__ );
198 foreach ( $checks as $table => $prefix ) {
199 $count += (int)$dbo->selectField(
200 $table,
201 'COUNT(*)',
202 [ "{$prefix}_actor" => $actor ],
203 __METHOD__
204 );
205 }
206
207 // Delete this special case when the actor migration is complete
208 $actorQuery = ActorMigration::newMigration()->getWhere( $dbo, 'rev_user', $user );
209 $count += (int)$dbo->selectField(
210 [ 'revision' ] + $actorQuery['tables'],
211 'COUNT(*)',
212 $actorQuery['conds'],
213 __METHOD__,
214 [],
215 $actorQuery['joins']
216 );
217
218 $count += (int)$dbo->newSelectQueryBuilder()
219 ->select( 'COUNT(*)' )
220 ->from( 'logging' )
221 ->where( [ 'log_actor' => $actor, $dbo->expr( 'log_type', '!=', 'newusers' ) ] )
222 ->caller( __METHOD__ )->fetchField();
223
224 $this->commitTransaction( $dbo, __METHOD__ );
225
226 return $count == 0;
227 }
228}
229
230$maintClass = RemoveUnusedAccounts::class;
231require_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.
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
Maintenance script that removes unused user accounts from the database.
execute()
Do the actual work.
__construct()
Default constructor.
Interface for objects representing user identity.