Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / maintenance / removeUnusedAccounts.php
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 require_once __DIR__ . '/Maintenance.php';
27
28 /**
29 * Maintenance script that removes unused user accounts from the database.
30 *
31 * @ingroup Maintenance
32 */
33 class RemoveUnusedAccounts extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addOption( 'delete', 'Actually delete the account' );
37 $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
38 $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
39 }
40
41 public function execute() {
42 $this->output( "Remove unused accounts\n\n" );
43
44 # Do an initial scan for inactive accounts and report the result
45 $this->output( "Checking for unused user accounts...\n" );
46 $delUser = [];
47 $delActor = [];
48 $dbr = $this->getDB( DB_REPLICA );
49 $res = $dbr->select(
50 [ 'user', 'actor' ],
51 [ 'user_id', 'user_name', 'user_touched', 'actor_id' ],
52 '',
53 __METHOD__,
54 [],
55 [ 'actor' => [ 'LEFT JOIN', 'user_id = actor_user' ] ]
56 );
57 if ( $this->hasOption( 'ignore-groups' ) ) {
58 $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
59 } else {
60 $excludedGroups = [];
61 }
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." );
65 }
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 )
73 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
74 ) {
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;
79 }
80 $this->output( $row->user_name . "\n" );
81 }
82 }
83 $count = count( $delUser );
84 $this->output( "...found {$count}.\n" );
85
86 # If required, go back and delete each marked account
87 if ( $count > 0 && $this->hasOption( 'delete' ) ) {
88 $this->output( "\nDeleting unused accounts..." );
89 $dbw = $this->getDB( DB_MASTER );
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__
94 );
95 $del = array_diff( $delActor, $keep );
96 if ( $del ) {
97 $dbw->delete( 'actor', [ 'actor_id' => $del ], __METHOD__ );
98 }
99 if ( $keep ) {
100 $dbw->update( 'actor', [ 'actor_user' => 0 ], [ 'actor_id' => $keep ], __METHOD__ );
101 }
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__ );
110 $dbw->update(
111 'site_stats',
112 [ 'ss_users' => $users ],
113 [ 'ss_row_id' => 1 ],
114 __METHOD__
115 );
116 } elseif ( $count > 0 ) {
117 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
118 }
119 $this->output( "\n" );
120 }
121
122 /**
123 * Could the specified user account be deemed inactive?
124 * (No edits, no deleted edits, no log entries, no current/old uploads)
125 *
126 * @param int $id User's ID
127 * @param int|null $actor User's actor ID
128 * @param bool $master Perform checking on the master
129 * @return bool
130 */
131 private function isInactiveAccount( $id, $actor, $master = false ) {
132 $dbo = $this->getDB( $master ? DB_MASTER : DB_REPLICA );
133 $checks = [
134 'revision' => 'rev',
135 'archive' => 'ar',
136 'image' => 'img',
137 'oldimage' => 'oi',
138 'filearchive' => 'fa'
139 ];
140 $count = 0;
141
142 $migration = ActorMigration::newMigration();
143
144 $user = User::newFromAnyId( $id, null, $actor );
145
146 $this->beginTransaction( $dbo, __METHOD__ );
147 foreach ( $checks as $table => $prefix ) {
148 $actorQuery = $migration->getWhere(
149 $dbo, $prefix . '_user', $user, $prefix !== 'oi' && $prefix !== 'fa'
150 );
151 $count += (int)$dbo->selectField(
152 [ $table ] + $actorQuery['tables'],
153 'COUNT(*)',
154 $actorQuery['conds'],
155 __METHOD__,
156 [],
157 $actorQuery['joins']
158 );
159 }
160
161 $actorQuery = $migration->getWhere( $dbo, 'log_user', $user, false );
162 $count += (int)$dbo->selectField(
163 [ 'logging' ] + $actorQuery['tables'],
164 'COUNT(*)',
165 [
166 $actorQuery['conds'],
167 'log_type != ' . $dbo->addQuotes( 'newusers' )
168 ],
169 __METHOD__,
170 [],
171 $actorQuery['joins']
172 );
173
174 $this->commitTransaction( $dbo, __METHOD__ );
175
176 return $count == 0;
177 }
178 }
179
180 $maintClass = RemoveUnusedAccounts::class;
181 require_once RUN_MAINTENANCE_IF_MAIN;