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