MediaWiki REL1_37
removeUnusedAccounts.php
Go to the documentation of this file.
1<?php
27
28require_once __DIR__ . '/Maintenance.php';
29
36 public function __construct() {
37 parent::__construct();
38 $this->addOption( 'delete', 'Actually delete the account' );
39 $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
40 $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
41 }
42
43 public function execute() {
44 $services = MediaWikiServices::getInstance();
45 $userFactory = $services->getUserFactory();
46 $userGroupManager = $services->getUserGroupManager();
47 $this->output( "Remove unused accounts\n\n" );
48
49 # Do an initial scan for inactive accounts and report the result
50 $this->output( "Checking for unused user accounts...\n" );
51 $delUser = [];
52 $delActor = [];
53 $dbr = $this->getDB( DB_REPLICA );
54 $res = $dbr->select(
55 [ 'user', 'actor' ],
56 [ 'user_id', 'user_name', 'user_touched', 'actor_id' ],
57 '',
58 __METHOD__,
59 [],
60 [ 'actor' => [ 'LEFT JOIN', 'user_id = actor_user' ] ]
61 );
62 if ( $this->hasOption( 'ignore-groups' ) ) {
63 $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
64 } else {
65 $excludedGroups = [];
66 }
67 $touched = $this->getOption( 'ignore-touched', "1" );
68 if ( !ctype_digit( $touched ) ) {
69 $this->fatalError( "Please put a valid positive integer on the --ignore-touched parameter." );
70 }
71 $touchedSeconds = 86400 * $touched;
72 foreach ( $res as $row ) {
73 # Check the account, but ignore it if it's within a $excludedGroups
74 # group or if it's touched within the $touchedSeconds seconds.
75 $instance = $userFactory->newFromId( $row->user_id );
76 if ( count(
77 array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
78 && $this->isInactiveAccount( $row->user_id, $row->actor_id ?? null, true )
79 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds
80 )
81 ) {
82 # Inactive; print out the name and flag it
83 $delUser[] = $row->user_id;
84 if ( isset( $row->actor_id ) && $row->actor_id ) {
85 $delActor[] = $row->actor_id;
86 }
87 $this->output( $row->user_name . "\n" );
88 }
89 }
90 $count = count( $delUser );
91 $this->output( "...found {$count}.\n" );
92
93 # If required, go back and delete each marked account
94 if ( $count > 0 && $this->hasOption( 'delete' ) ) {
95 $this->output( "\nDeleting unused accounts..." );
96 $dbw = $this->getDB( DB_PRIMARY );
97 $dbw->delete( 'user', [ 'user_id' => $delUser ], __METHOD__ );
98 # Keep actor rows referenced from ipblocks
99 $keep = $dbw->selectFieldValues(
100 'ipblocks', 'ipb_by_actor', [ 'ipb_by_actor' => $delActor ], __METHOD__
101 );
102 $del = array_diff( $delActor, $keep );
103 if ( $del ) {
104 $dbw->delete( 'actor', [ 'actor_id' => $del ], __METHOD__ );
105 }
106 if ( $keep ) {
107 $dbw->update( 'actor', [ 'actor_user' => null ], [ 'actor_id' => $keep ], __METHOD__ );
108 }
109 $dbw->delete( 'user_groups', [ 'ug_user' => $delUser ], __METHOD__ );
110 $dbw->delete( 'user_former_groups', [ 'ufg_user' => $delUser ], __METHOD__ );
111 $dbw->delete( 'user_properties', [ 'up_user' => $delUser ], __METHOD__ );
112 $dbw->delete( 'logging', [ 'log_actor' => $delActor ], __METHOD__ );
113 $dbw->delete( 'recentchanges', [ 'rc_actor' => $delActor ], __METHOD__ );
114 $this->output( "done.\n" );
115 # Update the site_stats.ss_users field
116 $users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ );
117 $dbw->update(
118 'site_stats',
119 [ 'ss_users' => $users ],
120 [ 'ss_row_id' => 1 ],
121 __METHOD__
122 );
123 } elseif ( $count > 0 ) {
124 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
125 }
126 $this->output( "\n" );
127 }
128
138 private function isInactiveAccount( $id, $actor, $primary = false ) {
139 if ( $actor === null ) {
140 // There's no longer a way for a user to be active in any of
141 // these tables without having an actor ID. The only way to link
142 // to a user row is via an actor row.
143 return true;
144 }
145
146 $dbo = $this->getDB( $primary ? DB_PRIMARY : DB_REPLICA );
147 $checks = [
148 'archive' => 'ar',
149 'image' => 'img',
150 'oldimage' => 'oi',
151 'filearchive' => 'fa'
152 // re-add when actor migration is complete
153 // 'revision' => 'rev'
154 ];
155 $count = 0;
156
157 $this->beginTransaction( $dbo, __METHOD__ );
158 foreach ( $checks as $table => $prefix ) {
159 $count += (int)$dbo->selectField(
160 $table,
161 'COUNT(*)',
162 [ "{$prefix}_actor" => $actor ],
163 __METHOD__
164 );
165 }
166
167 // Delete this special case when the actor migration is complete
168 $user = User::newFromAnyId( $id, null, $actor );
169 $actorQuery = ActorMigration::newMigration()->getWhere( $dbo, 'rev_user', $user );
170 $count += (int)$dbo->selectField(
171 [ 'revision' ] + $actorQuery['tables'],
172 'COUNT(*)',
173 $actorQuery['conds'],
174 __METHOD__,
175 [],
176 $actorQuery['joins']
177 );
178
179 $count += (int)$dbo->selectField(
180 [ 'logging' ],
181 'COUNT(*)',
182 [
183 'log_actor' => $actor,
184 'log_type != ' . $dbo->addQuotes( 'newusers' )
185 ],
186 __METHOD__
187 );
188
189 $this->commitTransaction( $dbo, __METHOD__ );
190
191 return $count == 0;
192 }
193}
194
195$maintClass = RemoveUnusedAccounts::class;
196require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Maintenance script that removes unused user accounts from the database.
execute()
Do the actual work.
isInactiveAccount( $id, $actor, $primary=false)
Could the specified user account be deemed inactive? (No edits, no deleted edits, no log entries,...
__construct()
Default constructor.
static newFromAnyId( $userId, $userName, $actorId, $dbDomain=false)
Static factory method for creation from an ID, name, and/or actor ID.
Definition User.php:713
const DB_REPLICA
Definition defines.php:25
const DB_PRIMARY
Definition defines.php:27