MediaWiki  REL1_20
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                         $this->output( "done.\n" );
00081                         # Update the site_stats.ss_users field
00082                         $users = $dbw->selectField( 'user', 'COUNT(*)', array(), __METHOD__ );
00083                         $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), __METHOD__ );
00084                 } elseif ( $count > 0 ) {
00085                         $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
00086                 }
00087                 $this->output( "\n" );
00088         }
00089 
00098         private function isInactiveAccount( $id, $master = false ) {
00099                 $dbo = wfGetDB( $master ? DB_MASTER : DB_SLAVE );
00100                 $checks = array( 'revision' => 'rev', 'archive' => 'ar', 'logging' => 'log',
00101                                                  'image' => 'img', 'oldimage' => 'oi', 'filearchive' => 'fa' );
00102                 $count = 0;
00103 
00104                 $dbo->begin( __METHOD__ );
00105                 foreach ( $checks as $table => $fprefix ) {
00106                         $conds = array( $fprefix . '_user' => $id );
00107                         $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
00108                 }
00109                 $dbo->commit( __METHOD__ );
00110 
00111                 return $count == 0;
00112         }
00113 }
00114 
00115 $maintClass = "RemoveUnusedAccounts";
00116 require_once( RUN_MAINTENANCE_IF_MAIN );