33b9a0c1baffb123960c8d8779a3eb428d11d71e
[lhc/web/wiklou.git] / maintenance / removeUnusedAccounts.php
1 <?php
2
3 /**
4 * Remove unused user accounts from the database
5 * An unused account is one which has made no edits
6 *
7 * @package MediaWiki
8 * @subpackage Maintenance
9 * @author Rob Church <robchur@gmail.com>
10 */
11
12 /**
13 * @todo Don't delete sysops or bureaucrats
14 */
15
16 $options = array( 'help', 'delete' );
17 require_once( 'commandLine.inc' );
18 require_once( 'removeUnusedAccounts.inc' );
19 echo( "Remove Unused Accounts\n\n" );
20 $fname = 'removeUnusedAccounts';
21
22 if( isset( $options['help'] ) ) {
23 showHelp();
24 exit();
25 }
26
27 # Do an initial scan for inactive accounts and report the result
28 echo( "Checking for unused user accounts...\n" );
29 $del = array();
30 $dbr =& wfGetDB( DB_SLAVE );
31 $res = $dbr->select( 'user', array( 'user_id', 'user_name' ), '', $fname );
32 while( $row = $dbr->fetchObject( $res ) ) {
33 # Check the account, but ignore it if it's the primary administrator
34 if( $row->user_id > 1 && isInactiveAccount( $row->user_id, true ) ) {
35 # Inactive; print out the name and flag it
36 $del[] = $row->user_id;
37 echo( $row->user_name . "\n" );
38 }
39 }
40 $count = count( $del );
41 echo( "...found {$count}.\n" );
42
43 # If required, go back and delete each marked account
44 if( $count > 0 && isset( $options['delete'] ) ) {
45 echo( "\nDeleting inactive accounts..." );
46 $dbw =& wfGetDB( DB_MASTER );
47 $dbw->delete( 'user', array( 'user_id' => $del ), $fname );
48 echo( "done.\n" );
49 # Update the site_stats.ss_users field
50 $users = $dbw->selectField( 'user', 'COUNT(*)', array(), $fname );
51 $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), $fname );
52 } else {
53 if( $count > 0 )
54 echo( "\nRun the script again with --delete to remove them from the database.\n" );
55 }
56 echo( "\n" );
57
58 ?>