MediaWiki  1.30.0
removeUnusedAccounts.php
Go to the documentation of this file.
1 <?php
26 require_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() {
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  $del = [];
47  $dbr = $this->getDB( DB_REPLICA );
48  $res = $dbr->select( 'user', [ 'user_id', 'user_name', 'user_touched' ], '', __METHOD__ );
49  if ( $this->hasOption( 'ignore-groups' ) ) {
50  $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
51  } else {
52  $excludedGroups = [];
53  }
54  $touched = $this->getOption( 'ignore-touched', "1" );
55  if ( !ctype_digit( $touched ) ) {
56  $this->error( "Please put a valid positive integer on the --ignore-touched parameter.", true );
57  }
58  $touchedSeconds = 86400 * $touched;
59  foreach ( $res as $row ) {
60  # Check the account, but ignore it if it's within a $excludedGroups
61  # group or if it's touched within the $touchedSeconds seconds.
62  $instance = User::newFromId( $row->user_id );
63  if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
64  && $this->isInactiveAccount( $row->user_id, true )
65  && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
66  ) {
67  # Inactive; print out the name and flag it
68  $del[] = $row->user_id;
69  $this->output( $row->user_name . "\n" );
70  }
71  }
72  $count = count( $del );
73  $this->output( "...found {$count}.\n" );
74 
75  # If required, go back and delete each marked account
76  if ( $count > 0 && $this->hasOption( 'delete' ) ) {
77  $this->output( "\nDeleting unused accounts..." );
78  $dbw = $this->getDB( DB_MASTER );
79  $dbw->delete( 'user', [ 'user_id' => $del ], __METHOD__ );
80  $dbw->delete( 'user_groups', [ 'ug_user' => $del ], __METHOD__ );
81  $dbw->delete( 'user_former_groups', [ 'ufg_user' => $del ], __METHOD__ );
82  $dbw->delete( 'user_properties', [ 'up_user' => $del ], __METHOD__ );
83  $dbw->delete( 'logging', [ 'log_user' => $del ], __METHOD__ );
84  $dbw->delete( 'recentchanges', [ 'rc_user' => $del ], __METHOD__ );
85  $this->output( "done.\n" );
86  # Update the site_stats.ss_users field
87  $users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ );
88  $dbw->update(
89  'site_stats',
90  [ 'ss_users' => $users ],
91  [ 'ss_row_id' => 1 ],
92  __METHOD__
93  );
94  } elseif ( $count > 0 ) {
95  $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
96  }
97  $this->output( "\n" );
98  }
99 
108  private function isInactiveAccount( $id, $master = false ) {
109  $dbo = $this->getDB( $master ? DB_MASTER : DB_REPLICA );
110  $checks = [
111  'revision' => 'rev',
112  'archive' => 'ar',
113  'image' => 'img',
114  'oldimage' => 'oi',
115  'filearchive' => 'fa'
116  ];
117  $count = 0;
118 
119  $this->beginTransaction( $dbo, __METHOD__ );
120  foreach ( $checks as $table => $fprefix ) {
121  $conds = [ $fprefix . '_user' => $id ];
122  $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
123  }
124 
125  $conds = [ 'log_user' => $id, 'log_type != ' . $dbo->addQuotes( 'newusers' ) ];
126  $count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds, __METHOD__ );
127 
128  $this->commitTransaction( $dbo, __METHOD__ );
129 
130  return $count == 0;
131  }
132 }
133 
134 $maintClass = "RemoveUnusedAccounts";
135 require_once RUN_MAINTENANCE_IF_MAIN;
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:573
captcha-old.count
count
Definition: captcha-old.py:249
RemoveUnusedAccounts\__construct
__construct()
Default constructor.
Definition: removeUnusedAccounts.php:34
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2040
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
php
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
Definition: injection.txt:35
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1278
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
RemoveUnusedAccounts\isInactiveAccount
isInactiveAccount( $id, $master=false)
Could the specified user account be deemed inactive? (No edits, no deleted edits, no log entries,...
Definition: removeUnusedAccounts.php:108
RemoveUnusedAccounts
Maintenance script that removes unused user accounts from the database.
Definition: removeUnusedAccounts.php:33
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1293
$maintClass
$maintClass
Definition: removeUnusedAccounts.php:134
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
as
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
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
RemoveUnusedAccounts\execute
execute()
Do the actual work.
Definition: removeUnusedAccounts.php:41
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:236