29 require_once __DIR__ .
'/Maintenance.php';
38 parent::__construct();
39 $this->
addOption(
'delete',
'Actually delete the account' );
40 $this->
addOption(
'ignore-groups',
'List of comma-separated groups to exclude',
false,
true );
41 $this->
addOption(
'ignore-touched',
'Skip accounts touched in last N days',
false,
true );
46 $userFactory = $services->getUserFactory();
47 $userGroupManager = $services->getUserGroupManager();
48 $this->
output(
"Remove unused accounts\n\n" );
50 # Do an initial scan for inactive accounts and report the result
51 $this->
output(
"Checking for unused user accounts...\n" );
55 $res = $dbr->newSelectQueryBuilder()
56 ->select( [
'user_id',
'user_name',
'user_touched',
'actor_id' ] )
58 ->leftJoin(
'actor',
null,
'user_id = actor_user' )
59 ->caller( __METHOD__ )->fetchResultSet();
60 if ( $this->
hasOption(
'ignore-groups' ) ) {
61 $excludedGroups = explode(
',', $this->
getOption(
'ignore-groups' ) );
65 $touched = $this->
getOption(
'ignore-touched',
"1" );
66 if ( !ctype_digit( $touched ) ) {
67 $this->
fatalError(
"Please put a valid positive integer on the --ignore-touched parameter." );
69 $touchedSeconds = 86400 * $touched;
70 foreach ( $res as $row ) {
71 # Check the account, but ignore it if it's within a $excludedGroups
72 # group or if it's touched within the $touchedSeconds seconds.
73 $instance = $userFactory->newFromId( $row->user_id );
75 array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
76 && $this->isInactiveAccount( $instance, $row->actor_id ??
null,
true )
80 # Inactive; print out the name and flag it
81 $delUser[] = $row->user_id;
82 if ( isset( $row->actor_id ) && $row->actor_id ) {
83 $delActor[] = $row->actor_id;
85 $this->
output( $row->user_name .
"\n" );
88 $count = count( $delUser );
89 $this->
output(
"...found {$count}.\n" );
91 # If required, go back and delete each marked account
92 if ( $count > 0 && $this->
hasOption(
'delete' ) ) {
93 $this->
output(
"\nDeleting unused accounts..." );
95 $dbw->delete(
'user', [
'user_id' => $delUser ], __METHOD__ );
96 # Keep actor rows referenced from ipblocks
97 $keep = $dbw->newSelectQueryBuilder()
98 ->select(
'ipb_by_actor' )
100 ->where( [
'ipb_by_actor' => $delActor ] )
101 ->caller( __METHOD__ )->fetchFieldValues();
102 $del = array_diff( $delActor, $keep );
104 $dbw->delete(
'actor', [
'actor_id' => $del ], __METHOD__ );
107 $dbw->update(
'actor', [
'actor_user' =>
null ], [
'actor_id' => $keep ], __METHOD__ );
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->newSelectQueryBuilder()
117 ->select(
'COUNT(*)' )
119 ->caller( __METHOD__ )->fetchField();
122 [
'ss_users' => $users ],
123 [
'ss_row_id' => 1 ],
126 } elseif ( $count > 0 ) {
127 $this->
output(
"\nRun the script again with --delete to remove them from the database.\n" );
141 private function isInactiveAccount( $user, $actor, $primary =
false ) {
142 if ( $actor ===
null ) {
154 'filearchive' =>
'fa'
161 foreach ( $checks as $table => $prefix ) {
162 $count += (int)$dbo->selectField(
165 [
"{$prefix}_actor" => $actor ],
171 $actorQuery = ActorMigration::newMigration()->getWhere( $dbo,
'rev_user', $user );
172 $count += (int)$dbo->selectField(
173 [
'revision' ] + $actorQuery[
'tables'],
175 $actorQuery[
'conds'],
181 $count += (int)$dbo->newSelectQueryBuilder()
182 ->select(
'COUNT(*)' )
184 ->where( [
'log_actor' => $actor,
'log_type != ' . $dbo->addQuotes(
'newusers' ) ] )
185 ->caller( __METHOD__ )->fetchField();
194 require_once RUN_MAINTENANCE_IF_MAIN;
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...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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.
getServiceContainer()
Returns the main service container.
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.
Maintenance script that removes unused user accounts from the database.
execute()
Do the actual work.
__construct()
Default constructor.