MediaWiki  REL1_21
removeUnusedAccounts.php
Go to the documentation of this file.
00001 <?php
00026 require_once( __DIR__ . '/Maintenance.php' );
00027 
00033 class RemoveUnusedAccounts extends Maintenance {
00034         public function __construct() {
00035                 parent::__construct();
00036                 $this->addOption( 'delete', 'Actually delete the account' );
00037                 $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
00038                 $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
00039         }
00040 
00041         public function execute() {
00042 
00043                 $this->output( "Remove unused accounts\n\n" );
00044 
00045                 # Do an initial scan for inactive accounts and report the result
00046                 $this->output( "Checking for unused user accounts...\n" );
00047                 $del = array();
00048                 $dbr = wfGetDB( DB_SLAVE );
00049                 $res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_touched' ), '', __METHOD__ );
00050                 if ( $this->hasOption( 'ignore-groups' ) ) {
00051                         $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
00052                 } else {
00053                         $excludedGroups = array();
00054                 }
00055                 $touched = $this->getOption( 'ignore-touched', "1" );
00056                 if ( !ctype_digit( $touched ) ) {
00057                         $this->error( "Please put a valid positive integer on the --ignore-touched parameter.", true );
00058                 }
00059                 $touchedSeconds = 86400 * $touched;
00060                 foreach ( $res as $row ) {
00061                         # Check the account, but ignore it if it's within a $excludedGroups group or if it's touched within the $touchedSeconds seconds.
00062                         $instance = User::newFromId( $row->user_id );
00063                         if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
00064                                 && $this->isInactiveAccount( $row->user_id, true )
00065                                 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
00066                                 ) {
00067                                 # Inactive; print out the name and flag it
00068                                 $del[] = $row->user_id;
00069                                 $this->output( $row->user_name . "\n" );
00070                         }
00071                 }
00072                 $count = count( $del );
00073                 $this->output( "...found {$count}.\n" );
00074 
00075                 # If required, go back and delete each marked account
00076                 if ( $count > 0 && $this->hasOption( 'delete' ) ) {
00077                         $this->output( "\nDeleting inactive accounts..." );
00078                         $dbw = wfGetDB( DB_MASTER );
00079                         $dbw->delete( 'user', array( 'user_id' => $del ), __METHOD__ );
00080                         $dbw->delete( 'logging', array( 'log_user' => $del ), __METHOD__ );
00081                         $dbw->delete( 'recentchanges', array( 'rc_user' => $del ), __METHOD__ );
00082                         $this->output( "done.\n" );
00083                         # Update the site_stats.ss_users field
00084                         $users = $dbw->selectField( 'user', 'COUNT(*)', array(), __METHOD__ );
00085                         $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), __METHOD__ );
00086                 } elseif ( $count > 0 ) {
00087                         $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
00088                 }
00089                 $this->output( "\n" );
00090         }
00091 
00100         private function isInactiveAccount( $id, $master = false ) {
00101                 $dbo = wfGetDB( $master ? DB_MASTER : DB_SLAVE );
00102                 $checks = array(
00103                         'revision' => 'rev',
00104                         'archive' => 'ar',
00105                         'image' => 'img',
00106                         'oldimage' => 'oi',
00107                         'filearchive' => 'fa'
00108                 );
00109                 $count = 0;
00110 
00111                 $dbo->begin( __METHOD__ );
00112                 foreach ( $checks as $table => $fprefix ) {
00113                         $conds = array( $fprefix . '_user' => $id );
00114                         $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
00115                 }
00116 
00117                 $conds = array( 'log_user' => $id, 'log_type != ' . $dbo->addQuotes( 'newusers' ) );
00118                 $count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds, __METHOD__ );
00119 
00120                 $dbo->commit( __METHOD__ );
00121 
00122                 return $count == 0;
00123         }
00124 }
00125 
00126 $maintClass = "RemoveUnusedAccounts";
00127 require_once( RUN_MAINTENANCE_IF_MAIN );