Update the Chinese conversion tables
[lhc/web/wiklou.git] / maintenance / removeUnusedAccounts.php
1 <?php
2 /**
3 * Remove unused user accounts from the database
4 * An unused account is one which has made no edits
5 *
6 * @file
7 * @ingroup Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 */
10
11
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';
17
18 if( isset( $options['help'] ) ) {
19 showHelp();
20 exit();
21 }
22
23 # Do an initial scan for inactive accounts and report the result
24 echo( "Checking for unused user accounts...\n" );
25 $del = array();
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 )
35 ) {
36 # Inactive; print out the name and flag it
37 $del[] = $row->user_id;
38 echo( $row->user_name . "\n" );
39 }
40 }
41 $count = count( $del );
42 echo( "...found {$count}.\n" );
43
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 );
49 echo( "done.\n" );
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 );
53 } else {
54 if( $count > 0 )
55 echo( "\nRun the script again with --delete to remove them from the database.\n" );
56 }
57 echo( "\n" );