MediaWiki  1.23.6
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 
43  $this->output( "Remove unused accounts\n\n" );
44 
45  # Do an initial scan for inactive accounts and report the result
46  $this->output( "Checking for unused user accounts...\n" );
47  $del = array();
48  $dbr = wfGetDB( DB_SLAVE );
49  $res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_touched' ), '', __METHOD__ );
50  if ( $this->hasOption( 'ignore-groups' ) ) {
51  $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
52  } else {
53  $excludedGroups = array();
54  }
55  $touched = $this->getOption( 'ignore-touched', "1" );
56  if ( !ctype_digit( $touched ) ) {
57  $this->error( "Please put a valid positive integer on the --ignore-touched parameter.", true );
58  }
59  $touchedSeconds = 86400 * $touched;
60  foreach ( $res as $row ) {
61  # Check the account, but ignore it if it's within a $excludedGroups 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 = wfGetDB( DB_MASTER );
79  $dbw->delete( 'user', array( 'user_id' => $del ), __METHOD__ );
80  $dbw->delete( 'user_groups', array( 'ug_user' => $del ), __METHOD__ );
81  $dbw->delete( 'user_former_groups', array( 'ufg_user' => $del ), __METHOD__ );
82  $dbw->delete( 'user_properties', array( 'up_user' => $del ), __METHOD__ );
83  $dbw->delete( 'logging', array( 'log_user' => $del ), __METHOD__ );
84  $dbw->delete( 'recentchanges', array( 'rc_user' => $del ), __METHOD__ );
85  $this->output( "done.\n" );
86  # Update the site_stats.ss_users field
87  $users = $dbw->selectField( 'user', 'COUNT(*)', array(), __METHOD__ );
88  $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), __METHOD__ );
89  } elseif ( $count > 0 ) {
90  $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
91  }
92  $this->output( "\n" );
93  }
94 
103  private function isInactiveAccount( $id, $master = false ) {
104  $dbo = wfGetDB( $master ? DB_MASTER : DB_SLAVE );
105  $checks = array(
106  'revision' => 'rev',
107  'archive' => 'ar',
108  'image' => 'img',
109  'oldimage' => 'oi',
110  'filearchive' => 'fa'
111  );
112  $count = 0;
113 
114  $dbo->begin( __METHOD__ );
115  foreach ( $checks as $table => $fprefix ) {
116  $conds = array( $fprefix . '_user' => $id );
117  $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
118  }
119 
120  $conds = array( 'log_user' => $id, 'log_type != ' . $dbo->addQuotes( 'newusers' ) );
121  $count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds, __METHOD__ );
122 
123  $dbo->commit( __METHOD__ );
124 
125  return $count == 0;
126  }
127 }
128 
129 $maintClass = "RemoveUnusedAccounts";
130 require_once RUN_MAINTENANCE_IF_MAIN;
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:411
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
RemoveUnusedAccounts\__construct
__construct()
Default constructor.
Definition: removeUnusedAccounts.php:34
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3659
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$dbr
$dbr
Definition: testCompression.php:48
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
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:103
RemoveUnusedAccounts
Maintenance script that removes unused user accounts from the database.
Definition: removeUnusedAccounts.php:33
$maintClass
$maintClass
Definition: removeUnusedAccounts.php:129
$count
$count
Definition: UtfNormalTest2.php:96
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: GlobalFunctions.php:2426
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\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
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:181
$res
$res
Definition: database.txt:21