MediaWiki
REL1_24
|
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 00062 # group or if it's touched within the $touchedSeconds seconds. 00063 $instance = User::newFromId( $row->user_id ); 00064 if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0 00065 && $this->isInactiveAccount( $row->user_id, true ) 00066 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds ) 00067 ) { 00068 # Inactive; print out the name and flag it 00069 $del[] = $row->user_id; 00070 $this->output( $row->user_name . "\n" ); 00071 } 00072 } 00073 $count = count( $del ); 00074 $this->output( "...found {$count}.\n" ); 00075 00076 # If required, go back and delete each marked account 00077 if ( $count > 0 && $this->hasOption( 'delete' ) ) { 00078 $this->output( "\nDeleting unused accounts..." ); 00079 $dbw = wfGetDB( DB_MASTER ); 00080 $dbw->delete( 'user', array( 'user_id' => $del ), __METHOD__ ); 00081 $dbw->delete( 'user_groups', array( 'ug_user' => $del ), __METHOD__ ); 00082 $dbw->delete( 'user_former_groups', array( 'ufg_user' => $del ), __METHOD__ ); 00083 $dbw->delete( 'user_properties', array( 'up_user' => $del ), __METHOD__ ); 00084 $dbw->delete( 'logging', array( 'log_user' => $del ), __METHOD__ ); 00085 $dbw->delete( 'recentchanges', array( 'rc_user' => $del ), __METHOD__ ); 00086 $this->output( "done.\n" ); 00087 # Update the site_stats.ss_users field 00088 $users = $dbw->selectField( 'user', 'COUNT(*)', array(), __METHOD__ ); 00089 $dbw->update( 00090 'site_stats', 00091 array( 'ss_users' => $users ), 00092 array( 'ss_row_id' => 1 ), 00093 __METHOD__ 00094 ); 00095 } elseif ( $count > 0 ) { 00096 $this->output( "\nRun the script again with --delete to remove them from the database.\n" ); 00097 } 00098 $this->output( "\n" ); 00099 } 00100 00109 private function isInactiveAccount( $id, $master = false ) { 00110 $dbo = wfGetDB( $master ? DB_MASTER : DB_SLAVE ); 00111 $checks = array( 00112 'revision' => 'rev', 00113 'archive' => 'ar', 00114 'image' => 'img', 00115 'oldimage' => 'oi', 00116 'filearchive' => 'fa' 00117 ); 00118 $count = 0; 00119 00120 $dbo->begin( __METHOD__ ); 00121 foreach ( $checks as $table => $fprefix ) { 00122 $conds = array( $fprefix . '_user' => $id ); 00123 $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ ); 00124 } 00125 00126 $conds = array( 'log_user' => $id, 'log_type != ' . $dbo->addQuotes( 'newusers' ) ); 00127 $count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds, __METHOD__ ); 00128 00129 $dbo->commit( __METHOD__ ); 00130 00131 return $count == 0; 00132 } 00133 } 00134 00135 $maintClass = "RemoveUnusedAccounts"; 00136 require_once RUN_MAINTENANCE_IF_MAIN;