2006-01-04 12:33:45 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Remove unused user accounts from the database
|
|
|
|
|
* An unused account is one which has made no edits
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
2006-01-04 12:33:45 +00:00
|
|
|
* @author Rob Church <robchur@gmail.com>
|
|
|
|
|
*/
|
2006-01-05 23:16:11 +00:00
|
|
|
|
2006-05-05 01:38:22 +00:00
|
|
|
$options = array( 'help', 'delete' );
|
|
|
|
|
require_once( 'commandLine.inc' );
|
|
|
|
|
require_once( 'removeUnusedAccounts.inc' );
|
|
|
|
|
echo( "Remove Unused Accounts\n\n" );
|
|
|
|
|
$fname = 'removeUnusedAccounts';
|
2006-01-04 13:02:04 +00:00
|
|
|
|
2006-05-05 01:38:22 +00:00
|
|
|
if( isset( $options['help'] ) ) {
|
|
|
|
|
showHelp();
|
2009-04-06 14:41:33 +00:00
|
|
|
exit(1);
|
2006-01-04 13:02:04 +00:00
|
|
|
}
|
2006-01-04 12:33:45 +00:00
|
|
|
|
2006-05-05 01:38:22 +00:00
|
|
|
# Do an initial scan for inactive accounts and report the result
|
|
|
|
|
echo( "Checking for unused user accounts...\n" );
|
2006-01-04 12:33:45 +00:00
|
|
|
$del = array();
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2008-07-26 23:42:29 +00:00
|
|
|
$res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_touched' ), '', $fname );
|
2008-08-11 10:06:54 +00:00
|
|
|
if( isset( $options['ignore-groups'] ) ) {
|
|
|
|
|
$excludedGroups = explode( ',', $options['ignore-groups'] );
|
|
|
|
|
} else { $excludedGroups = array(); }
|
|
|
|
|
$touchedSeconds = 0;
|
|
|
|
|
if( isset( $options['ignore-touched'] ) ) {
|
|
|
|
|
$touchedParamError = 0;
|
|
|
|
|
if( ctype_digit( $options['ignore-touched'] ) ) {
|
|
|
|
|
if( $options['ignore-touched'] <= 0 ) {
|
|
|
|
|
$touchedParamError = 1;
|
|
|
|
|
}
|
|
|
|
|
} else { $touchedParamError = 1; }
|
|
|
|
|
if( $touchedParamError == 1 ) {
|
|
|
|
|
die( "Please put a valid positive integer on the --ignore-touched parameter.\n" );
|
|
|
|
|
} else { $touchedSeconds = 86400 * $options['ignore-touched']; }
|
|
|
|
|
}
|
2006-05-05 01:38:22 +00:00
|
|
|
while( $row = $dbr->fetchObject( $res ) ) {
|
2008-08-11 10:06:54 +00:00
|
|
|
# Check the account, but ignore it if it's within a $excludedGroups group or if it's touched within the $touchedSeconds seconds.
|
2008-07-26 23:42:29 +00:00
|
|
|
$instance = User::newFromId( $row->user_id );
|
2008-08-11 10:06:54 +00:00
|
|
|
if( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
|
|
|
|
|
&& isInactiveAccount( $row->user_id, true )
|
|
|
|
|
&& wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
|
2008-07-26 23:42:29 +00:00
|
|
|
) {
|
2006-05-05 01:38:22 +00:00
|
|
|
# Inactive; print out the name and flag it
|
|
|
|
|
$del[] = $row->user_id;
|
|
|
|
|
echo( $row->user_name . "\n" );
|
2006-01-04 12:33:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-05-05 01:38:22 +00:00
|
|
|
$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..." );
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2006-05-05 01:38:22 +00:00
|
|
|
$dbw->delete( 'user', array( 'user_id' => $del ), $fname );
|
|
|
|
|
echo( "done.\n" );
|
2006-06-20 14:03:58 +00:00
|
|
|
# Update the site_stats.ss_users field
|
|
|
|
|
$users = $dbw->selectField( 'user', 'COUNT(*)', array(), $fname );
|
|
|
|
|
$dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), $fname );
|
2006-05-05 01:38:22 +00:00
|
|
|
} else {
|
|
|
|
|
if( $count > 0 )
|
|
|
|
|
echo( "\nRun the script again with --delete to remove them from the database.\n" );
|
2006-01-04 13:02:04 +00:00
|
|
|
}
|
2006-01-05 23:52:18 +00:00
|
|
|
echo( "\n" );
|