00001 <?php 00011 $options = array( 'help', 'delete' ); 00012 require_once( 'commandLine.inc' ); 00013 require_once( 'removeUnusedAccounts.inc' ); 00014 echo( "Remove Unused Accounts\n\n" ); 00015 $fname = 'removeUnusedAccounts'; 00016 00017 if( isset( $options['help'] ) ) { 00018 showHelp(); 00019 exit(); 00020 } 00021 00022 # Do an initial scan for inactive accounts and report the result 00023 echo( "Checking for unused user accounts...\n" ); 00024 $del = array(); 00025 $dbr = wfGetDB( DB_SLAVE ); 00026 $res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_touched' ), '', $fname ); 00027 if( isset( $options['ignore-groups'] ) ) { 00028 $excludedGroups = explode( ',', $options['ignore-groups'] ); 00029 } else { $excludedGroups = array(); } 00030 $touchedSeconds = 0; 00031 if( isset( $options['ignore-touched'] ) ) { 00032 $touchedParamError = 0; 00033 if( ctype_digit( $options['ignore-touched'] ) ) { 00034 if( $options['ignore-touched'] <= 0 ) { 00035 $touchedParamError = 1; 00036 } 00037 } else { $touchedParamError = 1; } 00038 if( $touchedParamError == 1 ) { 00039 die( "Please put a valid positive integer on the --ignore-touched parameter.\n" ); 00040 } else { $touchedSeconds = 86400 * $options['ignore-touched']; } 00041 } 00042 while( $row = $dbr->fetchObject( $res ) ) { 00043 # Check the account, but ignore it if it's within a $excludedGroups group or if it's touched within the $touchedSeconds seconds. 00044 $instance = User::newFromId( $row->user_id ); 00045 if( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0 00046 && isInactiveAccount( $row->user_id, true ) 00047 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds ) 00048 ) { 00049 # Inactive; print out the name and flag it 00050 $del[] = $row->user_id; 00051 echo( $row->user_name . "\n" ); 00052 } 00053 } 00054 $count = count( $del ); 00055 echo( "...found {$count}.\n" ); 00056 00057 # If required, go back and delete each marked account 00058 if( $count > 0 && isset( $options['delete'] ) ) { 00059 echo( "\nDeleting inactive accounts..." ); 00060 $dbw = wfGetDB( DB_MASTER ); 00061 $dbw->delete( 'user', array( 'user_id' => $del ), $fname ); 00062 echo( "done.\n" ); 00063 # Update the site_stats.ss_users field 00064 $users = $dbw->selectField( 'user', 'COUNT(*)', array(), $fname ); 00065 $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), $fname ); 00066 } else { 00067 if( $count > 0 ) 00068 echo( "\nRun the script again with --delete to remove them from the database.\n" ); 00069 } 00070 echo( "\n" );