MediaWiki 1.41.2
removeUnusedAccounts.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
37 public function __construct() {
38 parent::__construct();
39 $this->addOption( 'delete', 'Actually delete the account' );
40 $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
41 $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
42 }
43
44 public function execute() {
45 $services = $this->getServiceContainer();
46 $userFactory = $services->getUserFactory();
47 $userGroupManager = $services->getUserGroupManager();
48 $this->output( "Remove unused accounts\n\n" );
49
50 # Do an initial scan for inactive accounts and report the result
51 $this->output( "Checking for unused user accounts...\n" );
52 $delUser = [];
53 $delActor = [];
54 $dbr = $this->getDB( DB_REPLICA );
55 $res = $dbr->newSelectQueryBuilder()
56 ->select( [ 'user_id', 'user_name', 'user_touched', 'actor_id' ] )
57 ->from( 'user' )
58 ->leftJoin( 'actor', null, 'user_id = actor_user' )
59 ->caller( __METHOD__ )->fetchResultSet();
60 if ( $this->hasOption( 'ignore-groups' ) ) {
61 $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
62 } else {
63 $excludedGroups = [];
64 }
65 $touched = $this->getOption( 'ignore-touched', "1" );
66 if ( !ctype_digit( $touched ) ) {
67 $this->fatalError( "Please put a valid positive integer on the --ignore-touched parameter." );
68 }
69 $touchedSeconds = 86400 * $touched;
70 foreach ( $res as $row ) {
71 # Check the account, but ignore it if it's within a $excludedGroups
72 # group or if it's touched within the $touchedSeconds seconds.
73 $instance = $userFactory->newFromId( $row->user_id );
74 if ( count(
75 array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
76 && $this->isInactiveAccount( $instance, $row->actor_id ?? null, true )
77 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds
78 )
79 ) {
80 # Inactive; print out the name and flag it
81 $delUser[] = $row->user_id;
82 if ( isset( $row->actor_id ) && $row->actor_id ) {
83 $delActor[] = $row->actor_id;
84 }
85 $this->output( $row->user_name . "\n" );
86 }
87 }
88 $count = count( $delUser );
89 $this->output( "...found {$count}.\n" );
90
91 # If required, go back and delete each marked account
92 if ( $count > 0 && $this->hasOption( 'delete' ) ) {
93 $this->output( "\nDeleting unused accounts..." );
94 $dbw = $this->getDB( DB_PRIMARY );
95 $dbw->delete( 'user', [ 'user_id' => $delUser ], __METHOD__ );
96 # Keep actor rows referenced from ipblocks
97 $keep = $dbw->newSelectQueryBuilder()
98 ->select( 'ipb_by_actor' )
99 ->from( 'ipblocks' )
100 ->where( [ 'ipb_by_actor' => $delActor ] )
101 ->caller( __METHOD__ )->fetchFieldValues();
102 $del = array_diff( $delActor, $keep );
103 if ( $del ) {
104 $dbw->delete( 'actor', [ 'actor_id' => $del ], __METHOD__ );
105 }
106 if ( $keep ) {
107 $dbw->update( 'actor', [ 'actor_user' => null ], [ 'actor_id' => $keep ], __METHOD__ );
108 }
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->newSelectQueryBuilder()
117 ->select( 'COUNT(*)' )
118 ->from( 'user' )
119 ->caller( __METHOD__ )->fetchField();
120 $dbw->update(
121 'site_stats',
122 [ 'ss_users' => $users ],
123 [ 'ss_row_id' => 1 ],
124 __METHOD__
125 );
126 } elseif ( $count > 0 ) {
127 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
128 }
129 $this->output( "\n" );
130 }
131
141 private function isInactiveAccount( $user, $actor, $primary = false ) {
142 if ( $actor === null ) {
143 // There's no longer a way for a user to be active in any of
144 // these tables without having an actor ID. The only way to link
145 // to a user row is via an actor row.
146 return true;
147 }
148
149 $dbo = $this->getDB( $primary ? DB_PRIMARY : DB_REPLICA );
150 $checks = [
151 'archive' => 'ar',
152 'image' => 'img',
153 'oldimage' => 'oi',
154 'filearchive' => 'fa'
155 // re-add when actor migration is complete
156 // 'revision' => 'rev'
157 ];
158 $count = 0;
159
160 $this->beginTransaction( $dbo, __METHOD__ );
161 foreach ( $checks as $table => $prefix ) {
162 $count += (int)$dbo->selectField(
163 $table,
164 'COUNT(*)',
165 [ "{$prefix}_actor" => $actor ],
166 __METHOD__
167 );
168 }
169
170 // Delete this special case when the actor migration is complete
171 $actorQuery = ActorMigration::newMigration()->getWhere( $dbo, 'rev_user', $user );
172 $count += (int)$dbo->selectField(
173 [ 'revision' ] + $actorQuery['tables'],
174 'COUNT(*)',
175 $actorQuery['conds'],
176 __METHOD__,
177 [],
178 $actorQuery['joins']
179 );
180
181 $count += (int)$dbo->newSelectQueryBuilder()
182 ->select( 'COUNT(*)' )
183 ->from( 'logging' )
184 ->where( [ 'log_actor' => $actor, 'log_type != ' . $dbo->addQuotes( 'newusers' ) ] )
185 ->caller( __METHOD__ )->fetchField();
186
187 $this->commitTransaction( $dbo, __METHOD__ );
188
189 return $count == 0;
190 }
191}
192
193$maintClass = RemoveUnusedAccounts::class;
194require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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.
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.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28