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