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