MediaWiki master
removeUnusedAccounts.php
Go to the documentation of this file.
1<?php
13use Wikimedia\Timestamp\TimestampFormat as TS;
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/Maintenance.php';
17// @codeCoverageIgnoreEnd
18
25 public function __construct() {
26 parent::__construct();
27 $this->addOption( 'delete', 'Actually delete the account' );
28 $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
29 $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
30 }
31
32 public function execute() {
33 $services = $this->getServiceContainer();
34 $userFactory = $services->getUserFactory();
35 $userGroupManager = $services->getUserGroupManager();
36 $this->output( "Remove unused accounts\n\n" );
37
38 # Do an initial scan for inactive accounts and report the result
39 $this->output( "Checking for unused user accounts...\n" );
40 $delUser = [];
41 $delActor = [];
42 $dbr = $this->getReplicaDB();
43 $res = $dbr->newSelectQueryBuilder()
44 ->select( [ 'user_id', 'user_name', 'user_touched', 'actor_id' ] )
45 ->from( 'user' )
46 ->leftJoin( 'actor', null, 'user_id = actor_user' )
47 ->caller( __METHOD__ )->fetchResultSet();
48 if ( $this->hasOption( 'ignore-groups' ) ) {
49 $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
50 } else {
51 $excludedGroups = [];
52 }
53 $touched = $this->getOption( 'ignore-touched', "1" );
54 if ( !ctype_digit( $touched ) ) {
55 $this->fatalError( "Please put a valid positive integer on the --ignore-touched parameter." );
56 }
57 $touchedSeconds = 86400 * $touched;
58 foreach ( $res as $row ) {
59 # Check the account, but ignore it if it's within a $excludedGroups
60 # group or if it's touched within the $touchedSeconds seconds.
61 $instance = $userFactory->newFromId( $row->user_id );
62 if ( count(
63 array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
64 && $this->isInactiveAccount( $row->actor_id ?? null, true )
65 && wfTimestamp( TS::UNIX, $row->user_touched ) < wfTimestamp( TS::UNIX, time() - $touchedSeconds
66 )
67 ) {
68 # Inactive; print out the name and flag it
69 $delUser[] = $row->user_id;
70 if ( isset( $row->actor_id ) && $row->actor_id ) {
71 $delActor[] = $row->actor_id;
72 }
73 $this->output( $row->user_name . "\n" );
74 }
75 }
76 $count = count( $delUser );
77 $this->output( "...found {$count}.\n" );
78
79 # If required, go back and delete each marked account
80 if ( $count > 0 && $this->hasOption( 'delete' ) ) {
81 $this->output( "\nDeleting unused accounts..." );
82 $dbw = $this->getPrimaryDB();
83 $dbw->newDeleteQueryBuilder()
84 ->deleteFrom( 'user' )
85 ->where( [ 'user_id' => $delUser ] )
86 ->caller( __METHOD__ )->execute();
87 # Keep actor rows referenced from block
88 $keep = $dbw->newSelectQueryBuilder()
89 ->select( 'bl_by_actor' )
90 ->from( 'block' )
91 ->where( [ 'bl_by_actor' => $delActor ] )
92 ->caller( __METHOD__ )->fetchFieldValues();
93 $del = array_diff( $delActor, $keep );
94 if ( $del ) {
95 $dbw->newDeleteQueryBuilder()
96 ->deleteFrom( 'actor' )
97 ->where( [ 'actor_id' => $del ] )
98 ->caller( __METHOD__ )->execute();
99 }
100 $dbw->newDeleteQueryBuilder()
101 ->deleteFrom( 'user_groups' )
102 ->where( [ 'ug_user' => $delUser ] )
103 ->caller( __METHOD__ )->execute();
104 $dbw->newDeleteQueryBuilder()
105 ->deleteFrom( 'user_former_groups' )
106 ->where( [ 'ufg_user' => $delUser ] )
107 ->caller( __METHOD__ )->execute();
108 $dbw->newDeleteQueryBuilder()
109 ->deleteFrom( 'user_properties' )
110 ->where( [ 'up_user' => $delUser ] )
111 ->caller( __METHOD__ )->execute();
112 $dbw->newDeleteQueryBuilder()
113 ->deleteFrom( 'logging' )
114 ->where( [ 'log_actor' => $delActor ] )
115 ->caller( __METHOD__ )->execute();
116 $dbw->newDeleteQueryBuilder()
117 ->deleteFrom( 'recentchanges' )
118 ->where( [ 'rc_actor' => $delActor ] )
119 ->caller( __METHOD__ )->execute();
120 $this->output( "done.\n" );
121 # Update the site_stats.ss_users field
122 $users = $dbw->newSelectQueryBuilder()
123 ->select( 'COUNT(*)' )
124 ->from( 'user' )
125 ->caller( __METHOD__ )->fetchField();
126 $dbw->newUpdateQueryBuilder()
127 ->update( 'site_stats' )
128 ->set( [ 'ss_users' => $users ] )
129 ->where( [ 'ss_row_id' => 1 ] )
130 ->caller( __METHOD__ )
131 ->execute();
132 } elseif ( $count > 0 ) {
133 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
134 }
135 $this->output( "\n" );
136 }
137
146 private function isInactiveAccount( $actor, $primary = false ) {
147 if ( $actor === null ) {
148 // There's no longer a way for a user to be active in any of
149 // these tables without having an actor ID. The only way to link
150 // to a user row is via an actor row.
151 return true;
152 }
153
154 $dbo = $primary ? $this->getPrimaryDB() : $this->getReplicaDB();
155 $checks = [
156 'archive' => 'ar',
157 'image' => 'img',
158 'oldimage' => 'oi',
159 'filearchive' => 'fa',
160 'revision' => 'rev',
161 ];
162 $count = 0;
163
164 $this->beginTransactionRound( __METHOD__ );
165 foreach ( $checks as $table => $prefix ) {
166 $count += (int)$dbo->newSelectQueryBuilder()
167 ->select( 'COUNT(*)' )
168 ->from( $table )
169 ->where( [ "{$prefix}_actor" => $actor ] )
170 ->caller( __METHOD__ )
171 ->fetchField();
172 }
173
174 $count += (int)$dbo->newSelectQueryBuilder()
175 ->select( 'COUNT(*)' )
176 ->from( 'logging' )
177 ->where( [ 'log_actor' => $actor, $dbo->expr( 'log_type', '!=', 'newusers' ) ] )
178 ->caller( __METHOD__ )->fetchField();
179
180 $this->commitTransactionRound( __METHOD__ );
181
182 return $count == 0;
183 }
184}
185
186// @codeCoverageIgnoreStart
187$maintClass = RemoveUnusedAccounts::class;
188require_once RUN_MAINTENANCE_IF_MAIN;
189// @codeCoverageIgnoreEnd
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...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
getReplicaDB(string|false $virtualDomain=false)
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getServiceContainer()
Returns the main service container.
getPrimaryDB(string|false $virtualDomain=false)
Maintenance script that removes unused user accounts from the database.
execute()
Do the actual work.
__construct()
Default constructor.