Rewritten removeUnusedAccounts to be more efficient, print names of inactive accounts
authorRob Church <robchurch@users.mediawiki.org>
Fri, 5 May 2006 01:38:22 +0000 (01:38 +0000)
committerRob Church <robchurch@users.mediawiki.org>
Fri, 5 May 2006 01:38:22 +0000 (01:38 +0000)
RELEASE-NOTES
maintenance/removeUnusedAccounts.inc [new file with mode: 0644]
maintenance/removeUnusedAccounts.php
maintenance/userFunctions.inc [deleted file]

index dc9dec6..0fcac9b 100644 (file)
@@ -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 (file)
index 0000000..ac15ebe
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+/**
+ * Support functions for the removeUnusedAccounts maintenance script
+ *
+ *
+ * @package MediaWiki
+ * @subpackage Maintenance
+ * @author Rob Church <robchur@gmail.com>
+ */
+
+/**
+ * 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
index 9166a40..17ea09f 100644 (file)
@@ -9,52 +9,48 @@
  * @author Rob Church <robchur@gmail.com>
  */
 
-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 (file)
index a1b8c76..0000000
+++ /dev/null
@@ -1,136 +0,0 @@
-<?php
-
-/**
- * Support functions for dealing with user accounts at a database level
- *
- * @package MediaWiki
- * @subpackage Maintenance
- * @author Rob Church <robchur@gmail.com>
- */
-
-/**
- * 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