Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 138 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
RemoveUnusedAccounts | |
0.00% |
0 / 135 |
|
0.00% |
0 / 3 |
420 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 95 |
|
0.00% |
0 / 1 |
240 | |||
isInactiveAccount | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | /** |
3 | * Remove unused user accounts from the database |
4 | * An unused account is one which has made no edits |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; if not, write to the Free Software Foundation, Inc., |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | * http://www.gnu.org/copyleft/gpl.html |
20 | * |
21 | * @file |
22 | * @ingroup Maintenance |
23 | * @author Rob Church <robchur@gmail.com> |
24 | */ |
25 | |
26 | use MediaWiki\MainConfigNames; |
27 | use MediaWiki\User\ActorMigration; |
28 | use MediaWiki\User\UserIdentity; |
29 | |
30 | require_once __DIR__ . '/Maintenance.php'; |
31 | |
32 | /** |
33 | * Maintenance script that removes unused user accounts from the database. |
34 | * |
35 | * @ingroup Maintenance |
36 | */ |
37 | class RemoveUnusedAccounts extends Maintenance { |
38 | public function __construct() { |
39 | parent::__construct(); |
40 | $this->addOption( 'delete', 'Actually delete the account' ); |
41 | $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true ); |
42 | $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true ); |
43 | } |
44 | |
45 | public function execute() { |
46 | $services = $this->getServiceContainer(); |
47 | $userFactory = $services->getUserFactory(); |
48 | $userGroupManager = $services->getUserGroupManager(); |
49 | $this->output( "Remove unused accounts\n\n" ); |
50 | |
51 | # Do an initial scan for inactive accounts and report the result |
52 | $this->output( "Checking for unused user accounts...\n" ); |
53 | $delUser = []; |
54 | $delActor = []; |
55 | $dbr = $this->getReplicaDB(); |
56 | $res = $dbr->newSelectQueryBuilder() |
57 | ->select( [ 'user_id', 'user_name', 'user_touched', 'actor_id' ] ) |
58 | ->from( 'user' ) |
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' ) ); |
63 | } else { |
64 | $excludedGroups = []; |
65 | } |
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." ); |
69 | } |
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 ); |
75 | if ( count( |
76 | array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0 |
77 | && $this->isInactiveAccount( $instance, $row->actor_id ?? null, true ) |
78 | && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds |
79 | ) |
80 | ) { |
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; |
85 | } |
86 | $this->output( $row->user_name . "\n" ); |
87 | } |
88 | } |
89 | $count = count( $delUser ); |
90 | $this->output( "...found {$count}.\n" ); |
91 | |
92 | # If required, go back and delete each marked account |
93 | if ( $count > 0 && $this->hasOption( 'delete' ) ) { |
94 | $this->output( "\nDeleting unused accounts..." ); |
95 | $dbw = $this->getPrimaryDB(); |
96 | $dbw->newDeleteQueryBuilder() |
97 | ->deleteFrom( 'user' ) |
98 | ->where( [ 'user_id' => $delUser ] ) |
99 | ->caller( __METHOD__ )->execute(); |
100 | # Keep actor rows referenced from ipblocks |
101 | $stage = $this->getConfig() |
102 | ->get( MainConfigNames::BlockTargetMigrationStage ); |
103 | if ( $stage & SCHEMA_COMPAT_READ_OLD ) { |
104 | $keep = $dbw->newSelectQueryBuilder() |
105 | ->select( 'ipb_by_actor' ) |
106 | ->from( 'ipblocks' ) |
107 | ->where( [ 'ipb_by_actor' => $delActor ] ) |
108 | ->caller( __METHOD__ )->fetchFieldValues(); |
109 | } else { |
110 | $keep = $dbw->newSelectQueryBuilder() |
111 | ->select( 'bl_by_actor' ) |
112 | ->from( 'block' ) |
113 | ->where( [ 'bl_by_actor' => $delActor ] ) |
114 | ->caller( __METHOD__ )->fetchFieldValues(); |
115 | } |
116 | $del = array_diff( $delActor, $keep ); |
117 | if ( $del ) { |
118 | $dbw->newDeleteQueryBuilder() |
119 | ->deleteFrom( 'actor' ) |
120 | ->where( [ 'actor_id' => $del ] ) |
121 | ->caller( __METHOD__ )->execute(); |
122 | } |
123 | if ( $keep ) { |
124 | $dbw->update( 'actor', [ 'actor_user' => null ], [ 'actor_id' => $keep ], __METHOD__ ); |
125 | } |
126 | $dbw->newDeleteQueryBuilder() |
127 | ->deleteFrom( 'user_groups' ) |
128 | ->where( [ 'ug_user' => $delUser ] ) |
129 | ->caller( __METHOD__ )->execute(); |
130 | $dbw->newDeleteQueryBuilder() |
131 | ->deleteFrom( 'user_former_groups' ) |
132 | ->where( [ 'ufg_user' => $delUser ] ) |
133 | ->caller( __METHOD__ )->execute(); |
134 | $dbw->newDeleteQueryBuilder() |
135 | ->deleteFrom( 'user_properties' ) |
136 | ->where( [ 'up_user' => $delUser ] ) |
137 | ->caller( __METHOD__ )->execute(); |
138 | $dbw->newDeleteQueryBuilder() |
139 | ->deleteFrom( 'logging' ) |
140 | ->where( [ 'log_actor' => $delActor ] ) |
141 | ->caller( __METHOD__ )->execute(); |
142 | $dbw->newDeleteQueryBuilder() |
143 | ->deleteFrom( 'recentchanges' ) |
144 | ->where( [ 'rc_actor' => $delActor ] ) |
145 | ->caller( __METHOD__ )->execute(); |
146 | $this->output( "done.\n" ); |
147 | # Update the site_stats.ss_users field |
148 | $users = $dbw->newSelectQueryBuilder() |
149 | ->select( 'COUNT(*)' ) |
150 | ->from( 'user' ) |
151 | ->caller( __METHOD__ )->fetchField(); |
152 | $dbw->update( |
153 | 'site_stats', |
154 | [ 'ss_users' => $users ], |
155 | [ 'ss_row_id' => 1 ], |
156 | __METHOD__ |
157 | ); |
158 | } elseif ( $count > 0 ) { |
159 | $this->output( "\nRun the script again with --delete to remove them from the database.\n" ); |
160 | } |
161 | $this->output( "\n" ); |
162 | } |
163 | |
164 | /** |
165 | * Could the specified user account be deemed inactive? |
166 | * (No edits, no deleted edits, no log entries, no current/old uploads) |
167 | * |
168 | * @param UserIdentity $user |
169 | * @param int|null $actor User's actor ID |
170 | * @param bool $primary Perform checking on the primary DB |
171 | * @return bool |
172 | */ |
173 | private function isInactiveAccount( $user, $actor, $primary = false ) { |
174 | if ( $actor === null ) { |
175 | // There's no longer a way for a user to be active in any of |
176 | // these tables without having an actor ID. The only way to link |
177 | // to a user row is via an actor row. |
178 | return true; |
179 | } |
180 | |
181 | $dbo = $primary ? $this->getPrimaryDB() : $this->getReplicaDB(); |
182 | $checks = [ |
183 | 'archive' => 'ar', |
184 | 'image' => 'img', |
185 | 'oldimage' => 'oi', |
186 | 'filearchive' => 'fa' |
187 | // re-add when actor migration is complete |
188 | // 'revision' => 'rev' |
189 | ]; |
190 | $count = 0; |
191 | |
192 | $this->beginTransaction( $dbo, __METHOD__ ); |
193 | foreach ( $checks as $table => $prefix ) { |
194 | $count += (int)$dbo->selectField( |
195 | $table, |
196 | 'COUNT(*)', |
197 | [ "{$prefix}_actor" => $actor ], |
198 | __METHOD__ |
199 | ); |
200 | } |
201 | |
202 | // Delete this special case when the actor migration is complete |
203 | $actorQuery = ActorMigration::newMigration()->getWhere( $dbo, 'rev_user', $user ); |
204 | $count += (int)$dbo->selectField( |
205 | [ 'revision' ] + $actorQuery['tables'], |
206 | 'COUNT(*)', |
207 | $actorQuery['conds'], |
208 | __METHOD__, |
209 | [], |
210 | $actorQuery['joins'] |
211 | ); |
212 | |
213 | $count += (int)$dbo->newSelectQueryBuilder() |
214 | ->select( 'COUNT(*)' ) |
215 | ->from( 'logging' ) |
216 | ->where( [ 'log_actor' => $actor, $dbo->expr( 'log_type', '!=', 'newusers' ) ] ) |
217 | ->caller( __METHOD__ )->fetchField(); |
218 | |
219 | $this->commitTransaction( $dbo, __METHOD__ ); |
220 | |
221 | return $count == 0; |
222 | } |
223 | } |
224 | |
225 | $maintClass = RemoveUnusedAccounts::class; |
226 | require_once RUN_MAINTENANCE_IF_MAIN; |