46 $services = MediaWikiServices::getInstance();
47 $userFactory = $services->getUserFactory();
48 $userGroupManager = $services->getUserGroupManager();
49 $this->
output(
"Remove unused accounts\n\n" );
51 # Do an initial scan for inactive accounts and report the result
52 $this->
output(
"Checking for unused user accounts...\n" );
58 [
'user_id',
'user_name',
'user_touched',
'actor_id' ],
62 [
'actor' => [
'LEFT JOIN',
'user_id = actor_user' ] ]
64 if ( $this->
hasOption(
'ignore-groups' ) ) {
65 $excludedGroups = explode(
',', $this->
getOption(
'ignore-groups' ) );
69 $touched = $this->
getOption(
'ignore-touched',
"1" );
70 if ( !ctype_digit( $touched ) ) {
71 $this->
fatalError(
"Please put a valid positive integer on the --ignore-touched parameter." );
73 $touchedSeconds = 86400 * $touched;
74 foreach (
$res as $row ) {
75 # Check the account, but ignore it if it's within a $excludedGroups
76 # group or if it's touched within the $touchedSeconds seconds.
77 $instance = $userFactory->newFromId( $row->user_id );
79 array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
80 && $this->isInactiveAccount( $instance, $row->actor_id ??
null,
true )
84 # Inactive; print out the name and flag it
85 $delUser[] = $row->user_id;
86 if ( isset( $row->actor_id ) && $row->actor_id ) {
87 $delActor[] = $row->actor_id;
89 $this->
output( $row->user_name .
"\n" );
92 $count = count( $delUser );
93 $this->
output(
"...found {$count}.\n" );
95 # If required, go back and delete each marked account
96 if ( $count > 0 && $this->
hasOption(
'delete' ) ) {
97 $this->
output(
"\nDeleting unused accounts..." );
99 $dbw->delete(
'user', [
'user_id' => $delUser ], __METHOD__ );
100 # Keep actor rows referenced from ipblocks
101 $keep = $dbw->selectFieldValues(
102 'ipblocks',
'ipb_by_actor', [
'ipb_by_actor' => $delActor ], __METHOD__
104 $del = array_diff( $delActor, $keep );
106 $dbw->delete(
'actor', [
'actor_id' => $del ], __METHOD__ );
109 $dbw->update(
'actor', [
'actor_user' =>
null ], [
'actor_id' => $keep ], __METHOD__ );
111 $dbw->delete(
'user_groups', [
'ug_user' => $delUser ], __METHOD__ );
112 $dbw->delete(
'user_former_groups', [
'ufg_user' => $delUser ], __METHOD__ );
113 $dbw->delete(
'user_properties', [
'up_user' => $delUser ], __METHOD__ );
114 $dbw->delete(
'logging', [
'log_actor' => $delActor ], __METHOD__ );
115 $dbw->delete(
'recentchanges', [
'rc_actor' => $delActor ], __METHOD__ );
116 $this->
output(
"done.\n" );
117 # Update the site_stats.ss_users field
118 $users = $dbw->selectField(
'user',
'COUNT(*)', [], __METHOD__ );
121 [
'ss_users' => $users ],
122 [
'ss_row_id' => 1 ],
125 } elseif ( $count > 0 ) {
126 $this->
output(
"\nRun the script again with --delete to remove them from the database.\n" );