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" );
56 $res = $dbr->newSelectQueryBuilder()
57 ->select( [
'user_id',
'user_name',
'user_touched',
'actor_id' ] )
59 ->leftJoin(
'actor',
null,
'user_id = actor_user' )
60 ->caller( __METHOD__ )->fetchResultSet();
61 if ( $this->
hasOption(
'ignore-groups' ) ) {
62 $excludedGroups = explode(
',', $this->
getOption(
'ignore-groups' ) );
66 $touched = $this->
getOption(
'ignore-touched',
"1" );
67 if ( !ctype_digit( $touched ) ) {
68 $this->
fatalError(
"Please put a valid positive integer on the --ignore-touched parameter." );
70 $touchedSeconds = 86400 * $touched;
71 foreach ( $res as $row ) {
72 # Check the account, but ignore it if it's within a $excludedGroups
73 # group or if it's touched within the $touchedSeconds seconds.
74 $instance = $userFactory->newFromId( $row->user_id );
76 array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
77 && $this->isInactiveAccount( $instance, $row->actor_id ??
null,
true )
81 # Inactive; print out the name and flag it
82 $delUser[] = $row->user_id;
83 if ( isset( $row->actor_id ) && $row->actor_id ) {
84 $delActor[] = $row->actor_id;
86 $this->
output( $row->user_name .
"\n" );
89 $count = count( $delUser );
90 $this->
output(
"...found {$count}.\n" );
92 # If required, go back and delete each marked account
93 if ( $count > 0 && $this->
hasOption(
'delete' ) ) {
94 $this->
output(
"\nDeleting unused accounts..." );
96 $dbw->newDeleteQueryBuilder()
97 ->deleteFrom(
'user' )
98 ->where( [
'user_id' => $delUser ] )
99 ->caller( __METHOD__ )->execute();
100 # Keep actor rows referenced from block
101 $keep = $dbw->newSelectQueryBuilder()
102 ->select(
'bl_by_actor' )
104 ->where( [
'bl_by_actor' => $delActor ] )
105 ->caller( __METHOD__ )->fetchFieldValues();
106 $del = array_diff( $delActor, $keep );
108 $dbw->newDeleteQueryBuilder()
109 ->deleteFrom(
'actor' )
110 ->where( [
'actor_id' => $del ] )
111 ->caller( __METHOD__ )->execute();
114 $dbw->newUpdateQueryBuilder()
116 ->set( [
'actor_user' =>
null ] )
117 ->where( [
'actor_id' => $keep ] )
118 ->caller( __METHOD__ )
121 $dbw->newDeleteQueryBuilder()
122 ->deleteFrom(
'user_groups' )
123 ->where( [
'ug_user' => $delUser ] )
124 ->caller( __METHOD__ )->execute();
125 $dbw->newDeleteQueryBuilder()
126 ->deleteFrom(
'user_former_groups' )
127 ->where( [
'ufg_user' => $delUser ] )
128 ->caller( __METHOD__ )->execute();
129 $dbw->newDeleteQueryBuilder()
130 ->deleteFrom(
'user_properties' )
131 ->where( [
'up_user' => $delUser ] )
132 ->caller( __METHOD__ )->execute();
133 $dbw->newDeleteQueryBuilder()
134 ->deleteFrom(
'logging' )
135 ->where( [
'log_actor' => $delActor ] )
136 ->caller( __METHOD__ )->execute();
137 $dbw->newDeleteQueryBuilder()
138 ->deleteFrom(
'recentchanges' )
139 ->where( [
'rc_actor' => $delActor ] )
140 ->caller( __METHOD__ )->execute();
141 $this->
output(
"done.\n" );
142 # Update the site_stats.ss_users field
143 $users = $dbw->newSelectQueryBuilder()
144 ->select(
'COUNT(*)' )
146 ->caller( __METHOD__ )->fetchField();
147 $dbw->newUpdateQueryBuilder()
148 ->update(
'site_stats' )
149 ->set( [
'ss_users' => $users ] )
150 ->where( [
'ss_row_id' => 1 ] )
151 ->caller( __METHOD__ )
153 } elseif ( $count > 0 ) {
154 $this->
output(
"\nRun the script again with --delete to remove them from the database.\n" );