44 $services = MediaWikiServices::getInstance();
45 $userFactory = $services->getUserFactory();
46 $userGroupManager = $services->getUserGroupManager();
47 $this->
output(
"Remove unused accounts\n\n" );
49 # Do an initial scan for inactive accounts and report the result
50 $this->
output(
"Checking for unused user accounts...\n" );
56 [
'user_id',
'user_name',
'user_touched',
'actor_id' ],
60 [
'actor' => [
'LEFT JOIN',
'user_id = actor_user' ] ]
62 if ( $this->
hasOption(
'ignore-groups' ) ) {
63 $excludedGroups = explode(
',', $this->
getOption(
'ignore-groups' ) );
67 $touched = $this->
getOption(
'ignore-touched',
"1" );
68 if ( !ctype_digit( $touched ) ) {
69 $this->
fatalError(
"Please put a valid positive integer on the --ignore-touched parameter." );
71 $touchedSeconds = 86400 * $touched;
72 foreach (
$res as $row ) {
73 # Check the account, but ignore it if it's within a $excludedGroups
74 # group or if it's touched within the $touchedSeconds seconds.
75 $instance = $userFactory->newFromId( $row->user_id );
77 array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
78 && $this->isInactiveAccount( $row->user_id, $row->actor_id ??
null,
true )
82 # Inactive; print out the name and flag it
83 $delUser[] = $row->user_id;
84 if ( isset( $row->actor_id ) && $row->actor_id ) {
85 $delActor[] = $row->actor_id;
87 $this->
output( $row->user_name .
"\n" );
90 $count = count( $delUser );
91 $this->
output(
"...found {$count}.\n" );
93 # If required, go back and delete each marked account
94 if ( $count > 0 && $this->
hasOption(
'delete' ) ) {
95 $this->
output(
"\nDeleting unused accounts..." );
97 $dbw->delete(
'user', [
'user_id' => $delUser ], __METHOD__ );
98 # Keep actor rows referenced from ipblocks
99 $keep = $dbw->selectFieldValues(
100 'ipblocks',
'ipb_by_actor', [
'ipb_by_actor' => $delActor ], __METHOD__
102 $del = array_diff( $delActor, $keep );
104 $dbw->delete(
'actor', [
'actor_id' => $del ], __METHOD__ );
107 $dbw->update(
'actor', [
'actor_user' =>
null ], [
'actor_id' => $keep ], __METHOD__ );
109 $dbw->delete(
'user_groups', [
'ug_user' => $delUser ], __METHOD__ );
110 $dbw->delete(
'user_former_groups', [
'ufg_user' => $delUser ], __METHOD__ );
111 $dbw->delete(
'user_properties', [
'up_user' => $delUser ], __METHOD__ );
112 $dbw->delete(
'logging', [
'log_actor' => $delActor ], __METHOD__ );
113 $dbw->delete(
'recentchanges', [
'rc_actor' => $delActor ], __METHOD__ );
114 $this->
output(
"done.\n" );
115 # Update the site_stats.ss_users field
116 $users = $dbw->selectField(
'user',
'COUNT(*)', [], __METHOD__ );
119 [
'ss_users' => $users ],
120 [
'ss_row_id' => 1 ],
123 } elseif ( $count > 0 ) {
124 $this->
output(
"\nRun the script again with --delete to remove them from the database.\n" );
139 if ( $actor ===
null ) {
151 'filearchive' =>
'fa'
158 foreach ( $checks as $table => $prefix ) {
159 $count += (int)$dbo->selectField(
162 [
"{$prefix}_actor" => $actor ],
169 $actorQuery = ActorMigration::newMigration()->getWhere( $dbo,
'rev_user', $user );
170 $count += (int)$dbo->selectField(
171 [
'revision' ] + $actorQuery[
'tables'],
173 $actorQuery[
'conds'],
179 $count += (int)$dbo->selectField(
183 'log_actor' => $actor,
184 'log_type != ' . $dbo->addQuotes(
'newusers' )