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>
12 $options = array( 'help', 'delete' );
13 require_once( 'commandLine.inc' );
14 require_once( 'removeUnusedAccounts.inc' );
15 echo( "Remove Unused Accounts\n\n" );
16 $fname = 'removeUnusedAccounts';
18 if( isset( $options['help'] ) ) {
23 # Do an initial scan for inactive accounts and report the result
24 echo( "Checking for unused user accounts...\n" );
26 $dbr = wfGetDB( DB_SLAVE
);
27 $res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_touched' ), '', $fname );
28 $excludedGroups = array( 'sysop', 'bureaucrat' );
29 while( $row = $dbr->fetchObject( $res ) ) {
30 # Check the account, but ignore it if it's within the "sysop" or "bureaucrat" group.
31 $instance = User
::newFromId( $row->user_id
);
32 if( count( array_intersect( $instance->getGroups(), $excludedGroups ) ) == 0
33 && isInactiveAccount( $row->user_id
, true )
34 && wfTimestamp( TS_UNIX
, $row->user_touched
) < wfTimestamp( TS_UNIX
, time() - 604800 )
36 # Inactive; print out the name and flag it
37 $del[] = $row->user_id
;
38 echo( $row->user_name
. "\n" );
41 $count = count( $del );
42 echo( "...found {$count}.\n" );
44 # If required, go back and delete each marked account
45 if( $count > 0 && isset( $options['delete'] ) ) {
46 echo( "\nDeleting inactive accounts..." );
47 $dbw = wfGetDB( DB_MASTER
);
48 $dbw->delete( 'user', array( 'user_id' => $del ), $fname );
50 # Update the site_stats.ss_users field
51 $users = $dbw->selectField( 'user', 'COUNT(*)', array(), $fname );
52 $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), $fname );
55 echo( "\nRun the script again with --delete to remove them from the database.\n" );