Go to the documentation of this file.
26 require_once __DIR__ .
'/Maintenance.php';
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 );
44 $this->
output(
"Remove unused accounts\n\n" );
46 # Do an initial scan for inactive accounts and report the result
47 $this->
output(
"Checking for unused user accounts...\n" );
54 [
'user_id',
'user_name',
'user_touched',
'actor_id' ],
58 [
'actor' => [
'LEFT JOIN',
'user_id = actor_user' ] ]
61 $res =
$dbr->select(
'user', [
'user_id',
'user_name',
'user_touched' ],
'', __METHOD__ );
63 if ( $this->
hasOption(
'ignore-groups' ) ) {
64 $excludedGroups = explode(
',', $this->
getOption(
'ignore-groups' ) );
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." );
72 $touchedSeconds = 86400 * $touched;
74 # Check the account, but ignore it if it's within a $excludedGroups
75 # group or if it's touched within the $touchedSeconds seconds.
77 if (
count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
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;
86 $this->
output( $row->user_name .
"\n" );
89 $count =
count( $delUser );
90 $this->
output(
"...found {$count}.\n" );
92 # If required, go back and delete each marked account
93 if ( $count > 0 && $this->
hasOption(
'delete' ) ) {
94 $this->
output(
"\nDeleting unused accounts..." );
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__
102 $del = array_diff( $delActor, $keep );
104 $dbw->delete(
'actor', [
'actor_id' => $del ], __METHOD__ );
107 $dbw->update(
'actor', [
'actor_user' => 0 ], [
'actor_id' => $keep ], __METHOD__ );
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__ );
118 $dbw->delete(
'logging', [
'log_user' => $delUser ], __METHOD__ );
119 $dbw->delete(
'recentchanges', [
'rc_user' => $delUser ], __METHOD__ );
121 $this->
output(
"done.\n" );
122 # Update the site_stats.ss_users field
123 $users = $dbw->selectField(
'user',
'COUNT(*)', [], __METHOD__ );
126 [
'ss_users' => $users ],
127 [
'ss_row_id' => 1 ],
130 } elseif ( $count > 0 ) {
131 $this->
output(
"\nRun the script again with --delete to remove them from the database.\n" );
152 'filearchive' =>
'fa'
161 foreach ( $checks
as $table => $prefix ) {
162 $actorQuery = $migration->getWhere(
163 $dbo, $prefix .
'_user',
$user, $prefix !==
'oi' && $prefix !==
'fa'
165 $count += (int)$dbo->selectField(
166 [ $table ] + $actorQuery[
'tables'],
168 $actorQuery[
'conds'],
175 $actorQuery = $migration->getWhere( $dbo,
'log_user',
$user,
false );
176 $count += (int)$dbo->selectField(
177 [
'logging' ] + $actorQuery[
'tables'],
180 $actorQuery[
'conds'],
181 'log_type != ' . $dbo->addQuotes(
'newusers' )
isInactiveAccount( $id, $actor, $master=false)
Could the specified user account be deemed inactive? (No edits, no deleted edits, no log entries,...
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
static newFromId( $id)
Static factory method for creation from a given user ID.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
__construct()
Default constructor.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
require_once RUN_MAINTENANCE_IF_MAIN
static newFromAnyId( $userId, $userName, $actorId)
Static factory method for creation from an ID, name, and/or actor ID.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
static newMigration()
Static constructor.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
const SCHEMA_COMPAT_WRITE_OLD
Maintenance script that removes unused user accounts from the database.
const SCHEMA_COMPAT_WRITE_NEW
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
getOption( $name, $default=null)
Get an option, or return the default.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
execute()
Do the actual work.
hasOption( $name)
Checks to see if a particular option exists.
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage.