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->update( 'actor', [ 'actor_user' => null ], [ 'actor_id' => $keep ], __METHOD__ );
125 }
126 $dbw->newDeleteQueryBuilder()
127 ->deleteFrom( 'user_groups' )
128 ->where( [ 'ug_user' => $delUser ] )
129 ->caller( __METHOD__ )->execute();
130 $dbw->newDeleteQueryBuilder()
131 ->deleteFrom( 'user_former_groups' )
132 ->where( [ 'ufg_user' => $delUser ] )
133 ->caller( __METHOD__ )->execute();
134 $dbw->newDeleteQueryBuilder()
135 ->deleteFrom( 'user_properties' )
136 ->where( [ 'up_user' => $delUser ] )
137 ->caller( __METHOD__ )->execute();
138 $dbw->newDeleteQueryBuilder()
139 ->deleteFrom( 'logging' )
140 ->where( [ 'log_actor' => $delActor ] )
141 ->caller( __METHOD__ )->execute();
142 $dbw->newDeleteQueryBuilder()
143 ->deleteFrom( 'recentchanges' )
144 ->where( [ 'rc_actor' => $delActor ] )
145 ->caller( __METHOD__ )->execute();
146 $this->output( "done.\n" );
147 # Update the site_stats.ss_users field
148 $users = $dbw->newSelectQueryBuilder()
149 ->select( 'COUNT(*)' )
150 ->from( 'user' )
151 ->caller( __METHOD__ )->fetchField();
152 $dbw->update(
153 'site_stats',
154 [ 'ss_users' => $users ],
155 [ 'ss_row_id' => 1 ],
156 __METHOD__
157 );
158 } elseif ( $count > 0 ) {
159 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
160 }
161 $this->output( "\n" );
162 }
163
173 private function isInactiveAccount( $user, $actor, $primary = false ) {
174 if ( $actor === null ) {
175 // There's no longer a way for a user to be active in any of
176 // these tables without having an actor ID. The only way to link
177 // to a user row is via an actor row.
178 return true;
179 }
180
181 $dbo = $primary ? $this->getPrimaryDB() : $this->getReplicaDB();
182 $checks = [
183 'archive' => 'ar',
184 'image' => 'img',
185 'oldimage' => 'oi',
186 'filearchive' => 'fa'
187 // re-add when actor migration is complete
188 // 'revision' => 'rev'
189 ];
190 $count = 0;
191
192 $this->beginTransaction( $dbo, __METHOD__ );
193 foreach ( $checks as $table => $prefix ) {
194 $count += (int)$dbo->selectField(
195 $table,
196 'COUNT(*)',
197 [ "{$prefix}_actor" => $actor ],
198 __METHOD__
199 );
200 }
201
202 // Delete this special case when the actor migration is complete
203 $actorQuery = ActorMigration::newMigration()->getWhere( $dbo, 'rev_user', $user );
204 $count += (int)$dbo->selectField(
205 [ 'revision' ] + $actorQuery['tables'],
206 'COUNT(*)',
207 $actorQuery['conds'],
208 __METHOD__,
209 [],
210 $actorQuery['joins']
211 );
212
213 $count += (int)$dbo->newSelectQueryBuilder()
214 ->select( 'COUNT(*)' )
215 ->from( 'logging' )
216 ->where( [ 'log_actor' => $actor, $dbo->expr( 'log_type', '!=', 'newusers' ) ] )
217 ->caller( __METHOD__ )->fetchField();
218
219 $this->commitTransaction( $dbo, __METHOD__ );
220
221 return $count == 0;
222 }
223}
224
225$maintClass = RemoveUnusedAccounts::class;
226require_once RUN_MAINTENANCE_IF_MAIN;
const SCHEMA_COMPAT_READ_OLD
Definition Defines.php:273
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.