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