From: Rob Church Date: Wed, 4 Jan 2006 12:33:45 +0000 (+0000) Subject: Maintenance script to delete unused accounts X-Git-Tag: 1.6.0~775 X-Git-Url: http://git.cyclocoop.org/%27.parametre_url%28%20%20%20generer_action_auteur%28%27charger_plugin%27%2C%20%27update_flux%27%29%2C%27update_flux%27%2C%20%27oui%27%29.%27?a=commitdiff_plain;h=799244ec2ff2b61c3bef01a489b533b761322bf8;p=lhc%2Fweb%2Fwiklou.git Maintenance script to delete unused accounts --- diff --git a/maintenance/removeUnusedAccounts.inc b/maintenance/removeUnusedAccounts.inc new file mode 100644 index 0000000000..ac3b5c4ebc --- /dev/null +++ b/maintenance/removeUnusedAccounts.inc @@ -0,0 +1,49 @@ + + */ + +# Count the number of edits the specified user has made +function CountEdits( $user_id ) { + # We've *got* to pull this stuff off the master. If the user *has* made an edit, but it hasn't + # been replicated to the slaves yet, we'll end up falsely marking them as inactive. This could + # (and usually would) lead to their deletion. + $dbw =& wfGetDB( DB_MASTER ); + $sql = 'SELECT COUNT(rev_id) AS count FROM ' . $dbw->tableName( 'revision' ) . ' WHERE rev_user = ' . $user_id; + $res = $dbw->query( $sql ); + $row = $dbw->fetchObject( $res ); + return( $row->count ); +} + +# Return an array containing all valid user IDs +function GetUsers() { + # We're safe enough pulling this off a slave + $dbr =& wfGetDB( DB_SLAVE ); + $sql = 'SELECT user_id FROM ' . $dbr->tableName( 'user' ); + $res = $dbr->query( $sql ); + $users = array(); + while( $row = $dbr->fetchObject( $res ) ) { + $users[] = $row->user_id; + } + return( $users ); +} + +# Delete one or more users +function DeleteUsers( $users ) { + # Need a master, obviously + $dbw =& wfGetDB( DB_MASTER ); + # We'll do it all in one go, for speed + $dbw->begin(); + $table = $dbw->tableName( 'user' ); + foreach( $users as $user ) { + $dbw->query( 'DELETE FROM ' . $table . ' WHERE user_id = ' . $user . ' LIMIT 1' ); + } + $dbw->commit(); +} + +?> \ No newline at end of file diff --git a/maintenance/removeUnusedAccounts.php b/maintenance/removeUnusedAccounts.php new file mode 100644 index 0000000000..dd9214f0d4 --- /dev/null +++ b/maintenance/removeUnusedAccounts.php @@ -0,0 +1,42 @@ + + */ + +require_once( 'commandLine.inc' ); +require_once( 'removeUnusedAccounts.inc' ); +echo( "REMOVE UNUSED ACCOUNTS\nThis script will delete all users who have made no edits.\n\n" ); + +$count = 0; +$del = array(); + +# Right, who needs deleting? +$users = GetUsers(); +echo( "Found " . count( $users ) . " accounts.\n" ); +echo( "Locating inactive users..." ); +foreach( $users as $user ) { + if( $user != 1 ) { # Don't *touch* the first user account, ever + if( CountEdits( $user ) == 0 ) { + # User has no edits, mark them for deletion + $del[] = $user; + $count++; + } + } +} +echo( "done.\n" ); + +# Purge the inactive accounts we found +echo( $count . " inactive accounts found. Deleting..." ); +DeleteUsers( $del ); +echo( "done.\n" ); + +# We're done +echo( "Complete.\n" ); + +?> \ No newline at end of file