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 $options = array( 'help', 'delete' );
12 require_once( 'commandLine.inc' );
13 require_once( 'removeUnusedAccounts.inc' );
14 echo( "Remove Unused Accounts\n\n" );
15 $fname = 'removeUnusedAccounts';
17 if( isset( $options['help'] ) ) {
22 # Do an initial scan for inactive accounts and report the result
23 echo( "Checking for unused user accounts...\n" );
25 $dbr = wfGetDB( DB_SLAVE
);
26 $res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_touched' ), '', $fname );
27 if( isset( $options['ignore-groups'] ) ) {
28 $excludedGroups = explode( ',', $options['ignore-groups'] );
29 } else { $excludedGroups = array(); }
31 if( isset( $options['ignore-touched'] ) ) {
32 $touchedParamError = 0;
33 if( ctype_digit( $options['ignore-touched'] ) ) {
34 if( $options['ignore-touched'] <= 0 ) {
35 $touchedParamError = 1;
37 } else { $touchedParamError = 1; }
38 if( $touchedParamError == 1 ) {
39 die( "Please put a valid positive integer on the --ignore-touched parameter.\n" );
40 } else { $touchedSeconds = 86400 * $options['ignore-touched']; }
42 while( $row = $dbr->fetchObject( $res ) ) {
43 # Check the account, but ignore it if it's within a $excludedGroups group or if it's touched within the $touchedSeconds seconds.
44 $instance = User
::newFromId( $row->user_id
);
45 if( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
46 && isInactiveAccount( $row->user_id
, true )
47 && wfTimestamp( TS_UNIX
, $row->user_touched
) < wfTimestamp( TS_UNIX
, time() - $touchedSeconds )
49 # Inactive; print out the name and flag it
50 $del[] = $row->user_id
;
51 echo( $row->user_name
. "\n" );
54 $count = count( $del );
55 echo( "...found {$count}.\n" );
57 # If required, go back and delete each marked account
58 if( $count > 0 && isset( $options['delete'] ) ) {
59 echo( "\nDeleting inactive accounts..." );
60 $dbw = wfGetDB( DB_MASTER
);
61 $dbw->delete( 'user', array( 'user_id' => $del ), $fname );
63 # Update the site_stats.ss_users field
64 $users = $dbw->selectField( 'user', 'COUNT(*)', array(), $fname );
65 $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), $fname );
68 echo( "\nRun the script again with --delete to remove them from the database.\n" );