MediaWiki  REL1_22
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 unused accounts..." );
00078             $dbw = wfGetDB( DB_MASTER );
00079             $dbw->delete( 'user', array( 'user_id' => $del ), __METHOD__ );
00080             $dbw->delete( 'user_groups', array( 'ug_user' => $del ), __METHOD__ );
00081             $dbw->delete( 'user_former_groups', array( 'ufg_user' => $del ), __METHOD__ );
00082             $dbw->delete( 'user_properties', array( 'up_user' => $del ), __METHOD__ );
00083             $dbw->delete( 'logging', array( 'log_user' => $del ), __METHOD__ );
00084             $dbw->delete( 'recentchanges', array( 'rc_user' => $del ), __METHOD__ );
00085             $this->output( "done.\n" );
00086             # Update the site_stats.ss_users field
00087             $users = $dbw->selectField( 'user', 'COUNT(*)', array(), __METHOD__ );
00088             $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), __METHOD__ );
00089         } elseif ( $count > 0 ) {
00090             $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
00091         }
00092         $this->output( "\n" );
00093     }
00094 
00103     private function isInactiveAccount( $id, $master = false ) {
00104         $dbo = wfGetDB( $master ? DB_MASTER : DB_SLAVE );
00105         $checks = array(
00106             'revision' => 'rev',
00107             'archive' => 'ar',
00108             'image' => 'img',
00109             'oldimage' => 'oi',
00110             'filearchive' => 'fa'
00111         );
00112         $count = 0;
00113 
00114         $dbo->begin( __METHOD__ );
00115         foreach ( $checks as $table => $fprefix ) {
00116             $conds = array( $fprefix . '_user' => $id );
00117             $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
00118         }
00119 
00120         $conds = array( 'log_user' => $id, 'log_type != ' . $dbo->addQuotes( 'newusers' ) );
00121         $count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds, __METHOD__ );
00122 
00123         $dbo->commit( __METHOD__ );
00124 
00125         return $count == 0;
00126     }
00127 }
00128 
00129 $maintClass = "RemoveUnusedAccounts";
00130 require_once RUN_MAINTENANCE_IF_MAIN;