42 $this->
output(
"Remove unused accounts\n\n" );
44 # Do an initial scan for inactive accounts and report the result
45 $this->
output(
"Checking for unused user accounts...\n" );
51 [
'user_id',
'user_name',
'user_touched',
'actor_id' ],
55 [
'actor' => [
'LEFT JOIN',
'user_id = actor_user' ] ]
57 if ( $this->
hasOption(
'ignore-groups' ) ) {
58 $excludedGroups = explode(
',', $this->
getOption(
'ignore-groups' ) );
62 $touched = $this->
getOption(
'ignore-touched',
"1" );
63 if ( !ctype_digit( $touched ) ) {
64 $this->
fatalError(
"Please put a valid positive integer on the --ignore-touched parameter." );
66 $touchedSeconds = 86400 * $touched;
67 foreach (
$res as $row ) {
68 # Check the account, but ignore it if it's within a $excludedGroups
69 # group or if it's touched within the $touchedSeconds seconds.
70 $instance = User::newFromId( $row->user_id );
71 if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
72 && $this->isInactiveAccount( $row->user_id, $row->actor_id ??
null,
true )
75 # Inactive; print out the name and flag it
76 $delUser[] = $row->user_id;
77 if ( isset( $row->actor_id ) && $row->actor_id ) {
78 $delActor[] = $row->actor_id;
80 $this->
output( $row->user_name .
"\n" );
83 $count = count( $delUser );
84 $this->
output(
"...found {$count}.\n" );
86 # If required, go back and delete each marked account
87 if ( $count > 0 && $this->
hasOption(
'delete' ) ) {
88 $this->
output(
"\nDeleting unused accounts..." );
90 $dbw->delete(
'user', [
'user_id' => $delUser ], __METHOD__ );
91 # Keep actor rows referenced from ipblocks
92 $keep = $dbw->selectFieldValues(
93 'ipblocks',
'ipb_by_actor', [
'ipb_by_actor' => $delActor ], __METHOD__
95 $del = array_diff( $delActor, $keep );
97 $dbw->delete(
'actor', [
'actor_id' => $del ], __METHOD__ );
100 $dbw->update(
'actor', [
'actor_user' => 0 ], [
'actor_id' => $keep ], __METHOD__ );
102 $dbw->delete(
'user_groups', [
'ug_user' => $delUser ], __METHOD__ );
103 $dbw->delete(
'user_former_groups', [
'ufg_user' => $delUser ], __METHOD__ );
104 $dbw->delete(
'user_properties', [
'up_user' => $delUser ], __METHOD__ );
105 $dbw->delete(
'logging', [
'log_actor' => $delActor ], __METHOD__ );
106 $dbw->delete(
'recentchanges', [
'rc_actor' => $delActor ], __METHOD__ );
107 $this->
output(
"done.\n" );
108 # Update the site_stats.ss_users field
109 $users = $dbw->selectField(
'user',
'COUNT(*)', [], __METHOD__ );
112 [
'ss_users' => $users ],
113 [
'ss_row_id' => 1 ],
116 } elseif ( $count > 0 ) {
117 $this->
output(
"\nRun the script again with --delete to remove them from the database.\n" );
138 'filearchive' =>
'fa'
142 $migration = ActorMigration::newMigration();
144 $user = User::newFromAnyId( $id,
null, $actor );
147 foreach ( $checks as $table => $prefix ) {
148 $actorQuery = $migration->getWhere(
149 $dbo, $prefix .
'_user', $user, $prefix !==
'oi' && $prefix !==
'fa'
151 $count += (int)$dbo->selectField(
152 [ $table ] + $actorQuery[
'tables'],
154 $actorQuery[
'conds'],
161 $actorQuery = $migration->getWhere( $dbo,
'log_user', $user,
false );
162 $count += (int)$dbo->selectField(
163 [
'logging' ] + $actorQuery[
'tables'],
166 $actorQuery[
'conds'],
167 'log_type != ' . $dbo->addQuotes(
'newusers' )