From 84e93509fc7ef5a3359e4d72b479c0f935e9dbe0 Mon Sep 17 00:00:00 2001 From: Rob Church Date: Fri, 5 May 2006 01:38:22 +0000 Subject: [PATCH] Rewritten removeUnusedAccounts to be more efficient, print names of inactive accounts --- RELEASE-NOTES | 2 + maintenance/removeUnusedAccounts.inc | 47 +++++++++ maintenance/removeUnusedAccounts.php | 72 +++++++------- maintenance/userFunctions.inc | 136 --------------------------- 4 files changed, 83 insertions(+), 174 deletions(-) create mode 100644 maintenance/removeUnusedAccounts.inc delete mode 100644 maintenance/userFunctions.inc diff --git a/RELEASE-NOTES b/RELEASE-NOTES index dc9dec69af..0fcac9bcf3 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -201,6 +201,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * Disallow substituting Special pages when included into a page * (bug 5587) Clean up the languages from references to the Groups special page * Added new group-X and group-X-member messages +* Rewritten removeUnusedAccounts to be more efficient, print names of inactive + accounts == Compatibility == diff --git a/maintenance/removeUnusedAccounts.inc b/maintenance/removeUnusedAccounts.inc new file mode 100644 index 0000000000..ac15ebef82 --- /dev/null +++ b/maintenance/removeUnusedAccounts.inc @@ -0,0 +1,47 @@ + + */ + +/** + * Could the specified user account be deemed inactive? + * (No edits, no deleted edits, no log entries, no current/old uploads) + * + * @param $id User's ID + * @param $master Perform checking on the master + * @return bool + */ +function isInactiveAccount( $id, $master = false ) { + $dbo =& wfGetDB( $master ? DB_MASTER : DB_SLAVE ); + $fname = 'isInactiveAccount'; + $checks = array( 'revision' => 'rev', 'archive' => 'ar', 'logging' => 'log', + 'image' => 'img', 'oldimage' => 'oi' ); + $count = 0; + + $dbo->immediateBegin(); + foreach( $checks as $table => $fprefix ) { + $conds = array( $fprefix . '_user' => $id ); + $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, $fname ); + } + $dbo->immediateCommit(); + + return $count == 0; +} + +/** + * Show help for the maintenance script + */ +function showHelp() { + echo( "Delete unused user accounts from the database.\n\n" ); + echo( "USAGE: php removeUnusedAccounts.php [--delete]\n\n" ); + echo( " --delete : Delete accounts which are discovered to be inactive\n" ); + echo( "\n" ); +} + +?> \ No newline at end of file diff --git a/maintenance/removeUnusedAccounts.php b/maintenance/removeUnusedAccounts.php index 9166a40441..17ea09fc30 100644 --- a/maintenance/removeUnusedAccounts.php +++ b/maintenance/removeUnusedAccounts.php @@ -9,52 +9,48 @@ * @author Rob Church */ -define( 'ACTION_REPORT', 0 ); -define( 'ACTION_DELETE', 1 ); -$options = array( 'delete','help' ); -require_once( 'commandLine.inc' ); -require_once( 'userFunctions.inc' ); +/** + * @todo Don't delete sysops or bureaucrats + */ -echo( "Remove Unused Accounts\nThis script will delete all users who have made no edits and uploaded no files.\n\n" ); +$options = array( 'help', 'delete' ); +require_once( 'commandLine.inc' ); +require_once( 'removeUnusedAccounts.inc' ); +echo( "Remove Unused Accounts\n\n" ); +$fname = 'removeUnusedAccounts'; -# Check parameters -if( @$options['help'] ) { - echo( "USAGE: removeUnusedAccounts.php [--help|--delete]\n\nThe first (default) account is ignored.\n\n" ); - wfDie(); -} else { - $delete = @$options['delete'] ? true : false ; +if( isset( $options['help'] ) ) { + showHelp(); + exit(); } -$count = 0; +# Do an initial scan for inactive accounts and report the result +echo( "Checking for unused user accounts...\n" ); $del = array(); - -# Right, who needs deleting? -$users = GetUsers(); -echo( "Found " . count( $users ) . " accounts.\n\n" ); -echo( "Locating inactive users..." ); -foreach( $users as $user ) { - if( $user != 1 ) { # Don't *touch* the first user account, ever - if( CountEdits( $user, false ) == 0 && CountImages( $user, false ) == 0 && CountLogs( $user, false ) == 0 ) { - # User has no edits or images, mark them for deletion - $del[] = $user; - $count++; - } +$dbr =& wfGetDB( DB_SLAVE ); +$res = $dbr->select( 'user', array( 'user_id', 'user_name' ), '', $fname ); +while( $row = $dbr->fetchObject( $res ) ) { + # Check the account, but ignore it if it's the primary administrator + if( $row->user_id > 1 && isInactiveAccount( $row->user_id, true ) ) { + # Inactive; print out the name and flag it + $del[] = $row->user_id; + echo( $row->user_name . "\n" ); } } -echo( "done.\n" ); - -# Purge the inactive accounts we found -echo( $count . " inactive accounts found.\n" ); -if( $count > 0 ) { - if( $delete ) { - echo( "Deleting..." ); - DeleteUsers( $del ); - echo( "done.\n" ); - } else { - echo "Run the script with the --delete option to remove them from the database.\n"; - } +$count = count( $del ); +echo( "...found {$count}.\n" ); + +# If required, go back and delete each marked account +if( $count > 0 && isset( $options['delete'] ) ) { + echo( "\nDeleting inactive accounts..." ); + $dbw =& wfGetDB( DB_MASTER ); + #$set = implode( ',', $del ); + $dbw->delete( 'user', array( 'user_id' => $del ), $fname ); + echo( "done.\n" ); +} else { + if( $count > 0 ) + echo( "\nRun the script again with --delete to remove them from the database.\n" ); } - echo( "\n" ); ?> diff --git a/maintenance/userFunctions.inc b/maintenance/userFunctions.inc deleted file mode 100644 index a1b8c76068..0000000000 --- a/maintenance/userFunctions.inc +++ /dev/null @@ -1,136 +0,0 @@ - - */ - -/** - * Count the number of edits the specified user has made - * - * @param integer $user User ID - * @param bool $slave Whether or not a slave can be used - * @return integer - */ -function CountEdits( $user, $slave = true ) { - $dbw =& wfGetDB( $slave ? DB_SLAVE: DB_MASTER ); - # Count current edits - $res = $dbw->select( 'revision', 'COUNT(*) AS count', array( 'rev_user' => $user ) ); - $row = $dbw->fetchObject( $res ); - $count = $row->count; - # Count deleted edits - $res = $dbw->select( 'archive', 'COUNT(*) AS count', array( 'ar_user' => $user ) ); - $row = $dbw->fetchObject( $res ); - $count += $row->count; - # Done - return( $count ); -} - -/** - * Count the number of images the specified user has uploaded - * - * @param integer $user User ID - * @param bool $slave Whether or not a slave can be used - * @return integer - */ -function CountImages( $user, $slave = true ) { - $dbw =& wfGetDB( $slave ? DB_SLAVE: DB_MASTER ); - # Count current images - $res = $dbw->select( 'image', 'COUNT(*) AS count', array( 'img_user' => $user ) ); - $row = $dbw->fetchObject( $res ); - $count = $row->count; - # Count deleted edits - $res = $dbw->select( 'oldimage', 'COUNT(*) AS count', array( 'oi_user' => $user ) ); - $row = $dbw->fetchObject( $res ); - $count += $row->count; - # Done - return( $count ); -} - -/** - * Count the number of log entries associated with the specified user - * - * @param integer $user User ID - * @param bool $slave Whether or not a slave can be used - * @return integer - */ -function CountLogs( $user, $slave = true ) { - $dbw =& wfGetDB( $slave ? DB_SLAVE: DB_MASTER ); - # Count log entries - $res = $dbw->select( 'logging', 'COUNT(*) AS count', array( 'log_user' => $user ) ); - $row = $dbw->fetchObject( $res ); - $count = $row->count; - # Done - return( $count ); -} - -/** - * Retrieve all valid user IDs - * - * @return array - */ -function GetUsers() { - $dbr =& wfGetDB( DB_SLAVE ); - $res = $dbr->select( 'user', 'user_id' ); - $users = array(); - while( $row = $dbr->fetchObject( $res ) ) { - $users[] = $row->user_id; - } - return( $users ); -} - -/** - * Resolve a username to a user ID - * - * @param string $username Username - * @return mixed - */ -function GetUserID( $username ) { - $dbr =& wfGetDB( DB_SLAVE ); - $res = $dbr->select( 'user', 'user_id', array( 'user_name' => '"' . $username . '"' ) ); - if( $res !== false ) { - $row = $dbr->fetchObject( $res ); - return( $row->user_id ); - } else { - return( false ); - } -} - -/** - * Delete one or more users - * - * @param mixed $users Single integer or array of integers corresponding to user IDs - * @return bool - */ -function DeleteUsers( $users ) { - $dbw =& wfGetDB( DB_MASTER ); - $dbw->begin(); - foreach( $users as $user ) { - $dbw->delete( 'user', array( 'user_id' => $user ) ); - } - $dbw->commit(); - return( true ); -} - -/** - * Add a user to the named group(s) - * - * @param integer $user User ID - * @param mixed $groups Single string or array of strings corresponding to group names - * @return bool - */ -function SetUserGroups( $user, $groups ) { - $dbw =& wfGetDB( DB_MASTER ); - foreach( $groups as $group ) { - $row = array( 'ug_user' => $user, 'ug_group' => $group ); - if( !$dbw->insert( 'user_groups', $row, 'SetUserGroups' ) ) { - return( false ); - } - } - return( true ); -} - -?> \ No newline at end of file -- 2.20.1