MediaWiki REL1_33
removeUnusedAccounts.php
Go to the documentation of this file.
1<?php
26require_once __DIR__ . '/Maintenance.php';
27
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() {
43
44 $this->output( "Remove unused accounts\n\n" );
45
46 # Do an initial scan for inactive accounts and report the result
47 $this->output( "Checking for unused user accounts...\n" );
48 $delUser = [];
49 $delActor = [];
50 $dbr = $this->getDB( DB_REPLICA );
51 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
52 $res = $dbr->select(
53 [ 'user', 'actor' ],
54 [ 'user_id', 'user_name', 'user_touched', 'actor_id' ],
55 '',
56 __METHOD__,
57 [],
58 [ 'actor' => [ 'LEFT JOIN', 'user_id = actor_user' ] ]
59 );
60 } else {
61 $res = $dbr->select( 'user', [ 'user_id', 'user_name', 'user_touched' ], '', __METHOD__ );
62 }
63 if ( $this->hasOption( 'ignore-groups' ) ) {
64 $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
65 } else {
66 $excludedGroups = [];
67 }
68 $touched = $this->getOption( 'ignore-touched', "1" );
69 if ( !ctype_digit( $touched ) ) {
70 $this->fatalError( "Please put a valid positive integer on the --ignore-touched parameter." );
71 }
72 $touchedSeconds = 86400 * $touched;
73 foreach ( $res as $row ) {
74 # Check the account, but ignore it if it's within a $excludedGroups
75 # group or if it's touched within the $touchedSeconds seconds.
76 $instance = User::newFromId( $row->user_id );
77 if ( count( array_intersect( $instance->getEffectiveGroups(), $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 # 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->getDB( DB_MASTER );
96 $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' => 0 ], [ 'actor_id' => $keep ], __METHOD__ );
108 }
109 }
110 $dbw->delete( 'user_groups', [ 'ug_user' => $delUser ], __METHOD__ );
111 $dbw->delete( 'user_former_groups', [ 'ufg_user' => $delUser ], __METHOD__ );
112 $dbw->delete( 'user_properties', [ 'up_user' => $delUser ], __METHOD__ );
114 $dbw->delete( 'logging', [ 'log_actor' => $delActor ], __METHOD__ );
115 $dbw->delete( 'recentchanges', [ 'rc_actor' => $delActor ], __METHOD__ );
116 }
118 $dbw->delete( 'logging', [ 'log_user' => $delUser ], __METHOD__ );
119 $dbw->delete( 'recentchanges', [ 'rc_user' => $delUser ], __METHOD__ );
120 }
121 $this->output( "done.\n" );
122 # Update the site_stats.ss_users field
123 $users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ );
124 $dbw->update(
125 'site_stats',
126 [ 'ss_users' => $users ],
127 [ 'ss_row_id' => 1 ],
128 __METHOD__
129 );
130 } elseif ( $count > 0 ) {
131 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
132 }
133 $this->output( "\n" );
134 }
135
145 private function isInactiveAccount( $id, $actor, $master = false ) {
146 $dbo = $this->getDB( $master ? DB_MASTER : DB_REPLICA );
147 $checks = [
148 'revision' => 'rev',
149 'archive' => 'ar',
150 'image' => 'img',
151 'oldimage' => 'oi',
152 'filearchive' => 'fa'
153 ];
154 $count = 0;
155
156 $migration = ActorMigration::newMigration();
157
158 $user = User::newFromAnyId( $id, null, $actor );
159
160 $this->beginTransaction( $dbo, __METHOD__ );
161 foreach ( $checks as $table => $prefix ) {
162 $actorQuery = $migration->getWhere(
163 $dbo, $prefix . '_user', $user, $prefix !== 'oi' && $prefix !== 'fa'
164 );
165 $count += (int)$dbo->selectField(
166 [ $table ] + $actorQuery['tables'],
167 'COUNT(*)',
168 $actorQuery['conds'],
169 __METHOD__,
170 [],
171 $actorQuery['joins']
172 );
173 }
174
175 $actorQuery = $migration->getWhere( $dbo, 'log_user', $user, false );
176 $count += (int)$dbo->selectField(
177 [ 'logging' ] + $actorQuery['tables'],
178 'COUNT(*)',
179 [
180 $actorQuery['conds'],
181 'log_type != ' . $dbo->addQuotes( 'newusers' )
182 ],
184 [],
185 $actorQuery['joins']
186 );
187
188 $this->commitTransaction( $dbo, __METHOD__ );
189
190 return $count == 0;
191 }
192}
193
194$maintClass = RemoveUnusedAccounts::class;
195require_once RUN_MAINTENANCE_IF_MAIN;
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage.
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 transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
hasOption( $name)
Checks to see if a particular option exists.
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.
isInactiveAccount( $id, $actor, $master=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)
Static factory method for creation from an ID, name, and/or actor ID.
Definition User.php:676
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:609
$res
Definition database.txt:21
const SCHEMA_COMPAT_WRITE_OLD
Definition Defines.php:293
const SCHEMA_COMPAT_WRITE_NEW
Definition Defines.php:295
require_once RUN_MAINTENANCE_IF_MAIN
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26