bf03154572d9a1403ad000f232f10686cb27fd59
3 * Remove unused user accounts from the database
4 * An unused account is one which has made no edits
8 * @author Rob Church <robchur@gmail.com>
11 require_once( "Maintenance.php" );
13 class RemoveUnusedAccounts
extends Maintenance
{
14 public function __construct() {
15 parent
::__construct();
16 $this->addParam( 'delete', 'Actually delete the account' );
17 $this->addParam( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
18 $this->addParam( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
21 public function execute() {
23 $this->output( "Remove unused accounts\n\n" );
25 # Do an initial scan for inactive accounts and report the result
26 $this->output( "Checking for unused user accounts...\n" );
28 $dbr = wfGetDB( DB_SLAVE
);
29 $res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_touched' ), '', __METHOD__
);
30 if( $this->hasOption('ignore-groups') ) {
31 $excludedGroups = explode( ',', $this->getOption('ignore-groups') );
33 $excludedGroups = array();
35 $touched = $this->getOption( 'ignore-touched', "1" );
36 if( !ctype_digit( $touched ) ) {
37 $this->error( "Please put a valid positive integer on the --ignore-touched parameter.\n", true );
39 $touchedSeconds = 86400 * $touched;
40 while( $row = $dbr->fetchObject( $res ) ) {
41 # Check the account, but ignore it if it's within a $excludedGroups group or if it's touched within the $touchedSeconds seconds.
42 $instance = User
::newFromId( $row->user_id
);
43 if( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
44 && $this->isInactiveAccount( $row->user_id
, true )
45 && wfTimestamp( TS_UNIX
, $row->user_touched
) < wfTimestamp( TS_UNIX
, time() - $touchedSeconds )
47 # Inactive; print out the name and flag it
48 $del[] = $row->user_id
;
49 $this->output( $row->user_name
. "\n" );
52 $count = count( $del );
53 $this->output( "...found {$count}.\n" );
55 # If required, go back and delete each marked account
56 if( $count > 0 && $this->hasOption('delete') ) {
57 $this->output( "\nDeleting inactive accounts..." );
58 $dbw = wfGetDB( DB_MASTER
);
59 $dbw->delete( 'user', array( 'user_id' => $del ), __METHOD__
);
60 $this->output( "done.\n" );
61 # Update the site_stats.ss_users field
62 $users = $dbw->selectField( 'user', 'COUNT(*)', array(), __METHOD__
);
63 $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), __METHOD__
);
64 } elseif( $count > 0 ) {
65 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
67 $this->output( "\n" );
71 * Could the specified user account be deemed inactive?
72 * (No edits, no deleted edits, no log entries, no current/old uploads)
74 * @param $id User's ID
75 * @param $master Perform checking on the master
78 private function isInactiveAccount( $id, $master = false ) {
79 $dbo = wfGetDB( $master ? DB_MASTER
: DB_SLAVE
);
80 $checks = array( 'revision' => 'rev', 'archive' => 'ar', 'logging' => 'log',
81 'image' => 'img', 'oldimage' => 'oi' );
84 $dbo->immediateBegin();
85 foreach( $checks as $table => $fprefix ) {
86 $conds = array( $fprefix . '_user' => $id );
87 $count +
= (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__
);
89 $dbo->immediateCommit();
95 $maintClass = "RemoveUnusedAccounts";
96 require_once( DO_MAINTENANCE
);