]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/removeUnusedAccounts.php
MediaWiki 1.14.0-scripts
[autoinstallsdev/mediawiki.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 $options = array( 'help', 'delete' );
12 require_once( 'commandLine.inc' );
13 require_once( 'removeUnusedAccounts.inc' );
14 echo( "Remove Unused Accounts\n\n" );
15 $fname = 'removeUnusedAccounts';
16
17 if( isset( $options['help'] ) ) {
18         showHelp();
19         exit();
20 }
21
22 # Do an initial scan for inactive accounts and report the result
23 echo( "Checking for unused user accounts...\n" );
24 $del = array();
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(); }
30 $touchedSeconds = 0;
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;
36                 }
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']; }
41 }
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 )
48                 ) {
49                 # Inactive; print out the name and flag it
50                 $del[] = $row->user_id;
51                 echo( $row->user_name . "\n" );
52         }
53 }
54 $count = count( $del );
55 echo( "...found {$count}.\n" );
56
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 );
62         echo( "done.\n" );
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 );
66 } else {
67         if( $count > 0 )
68                 echo( "\nRun the script again with --delete to remove them from the database.\n" );
69 }
70 echo( "\n" );