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
|
|
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-08-09 16:06:18 +00:00
|
|
|
* @file
|
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
|
|
|
* @ingroup Maintenance
|
2006-01-04 12:33:45 +00:00
|
|
|
* @author Rob Church <robchur@gmail.com>
|
|
|
|
|
*/
|
2006-01-05 23:16:11 +00:00
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2006-01-04 13:02:04 +00:00
|
|
|
|
2012-08-09 16:06:18 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that removes unused user accounts from the database.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class RemoveUnusedAccounts extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addOption( 'delete', 'Actually delete the account' );
|
|
|
|
|
$this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
|
|
|
|
|
$this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$this->output( "Remove unused accounts\n\n" );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
# Do an initial scan for inactive accounts and report the result
|
|
|
|
|
$this->output( "Checking for unused user accounts...\n" );
|
2016-02-17 09:09:32 +00:00
|
|
|
$del = [];
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = $this->getDB( DB_REPLICA );
|
2016-02-17 09:09:32 +00:00
|
|
|
$res = $dbr->select( 'user', [ 'user_id', 'user_name', 'user_touched' ], '', __METHOD__ );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->hasOption( 'ignore-groups' ) ) {
|
|
|
|
|
$excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$excludedGroups = [];
|
2009-06-24 02:02:37 +00:00
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
$touched = $this->getOption( 'ignore-touched', "1" );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !ctype_digit( $touched ) ) {
|
2009-08-02 21:55:10 +00:00
|
|
|
$this->error( "Please put a valid positive integer on the --ignore-touched parameter.", true );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
$touchedSeconds = 86400 * $touched;
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $res as $row ) {
|
2014-04-22 20:55:50 +00:00
|
|
|
# Check the account, but ignore it if it's within a $excludedGroups
|
|
|
|
|
# group or if it's touched within the $touchedSeconds seconds.
|
2009-08-02 19:35:17 +00:00
|
|
|
$instance = User::newFromId( $row->user_id );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
|
2009-08-02 19:35:17 +00:00
|
|
|
&& $this->isInactiveAccount( $row->user_id, true )
|
|
|
|
|
&& wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
|
2014-04-23 18:09:26 +00:00
|
|
|
) {
|
2009-08-02 19:35:17 +00:00
|
|
|
# Inactive; print out the name and flag it
|
|
|
|
|
$del[] = $row->user_id;
|
|
|
|
|
$this->output( $row->user_name . "\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$count = count( $del );
|
|
|
|
|
$this->output( "...found {$count}.\n" );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
# If required, go back and delete each marked account
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $count > 0 && $this->hasOption( 'delete' ) ) {
|
2013-07-18 06:31:59 +00:00
|
|
|
$this->output( "\nDeleting unused accounts..." );
|
2015-12-31 00:07:37 +00:00
|
|
|
$dbw = $this->getDB( DB_MASTER );
|
2016-02-17 09:09:32 +00:00
|
|
|
$dbw->delete( 'user', [ 'user_id' => $del ], __METHOD__ );
|
|
|
|
|
$dbw->delete( 'user_groups', [ 'ug_user' => $del ], __METHOD__ );
|
|
|
|
|
$dbw->delete( 'user_former_groups', [ 'ufg_user' => $del ], __METHOD__ );
|
|
|
|
|
$dbw->delete( 'user_properties', [ 'up_user' => $del ], __METHOD__ );
|
|
|
|
|
$dbw->delete( 'logging', [ 'log_user' => $del ], __METHOD__ );
|
|
|
|
|
$dbw->delete( 'recentchanges', [ 'rc_user' => $del ], __METHOD__ );
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "done.\n" );
|
|
|
|
|
# Update the site_stats.ss_users field
|
2016-02-17 09:09:32 +00:00
|
|
|
$users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ );
|
2014-04-22 20:55:50 +00:00
|
|
|
$dbw->update(
|
|
|
|
|
'site_stats',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'ss_users' => $users ],
|
|
|
|
|
[ 'ss_row_id' => 1 ],
|
2014-04-22 20:55:50 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2010-05-22 16:50:39 +00:00
|
|
|
} elseif ( $count > 0 ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "\nRun the script again with --delete to remove them from the database.\n" );
|
|
|
|
|
}
|
|
|
|
|
$this->output( "\n" );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
/**
|
|
|
|
|
* Could the specified user account be deemed inactive?
|
|
|
|
|
* (No edits, no deleted edits, no log entries, no current/old uploads)
|
|
|
|
|
*
|
2014-04-17 20:48:32 +00:00
|
|
|
* @param int $id User's ID
|
|
|
|
|
* @param bool $master Perform checking on the master
|
2009-08-02 19:35:17 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function isInactiveAccount( $id, $master = false ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbo = $this->getDB( $master ? DB_MASTER : DB_REPLICA );
|
2016-02-17 09:09:32 +00:00
|
|
|
$checks = [
|
2012-12-21 21:58:00 +00:00
|
|
|
'revision' => 'rev',
|
|
|
|
|
'archive' => 'ar',
|
|
|
|
|
'image' => 'img',
|
|
|
|
|
'oldimage' => 'oi',
|
|
|
|
|
'filearchive' => 'fa'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2009-08-02 19:35:17 +00:00
|
|
|
$count = 0;
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2015-12-31 00:10:31 +00:00
|
|
|
$this->beginTransaction( $dbo, __METHOD__ );
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $checks as $table => $fprefix ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [ $fprefix . '_user' => $id ];
|
2009-08-02 19:35:17 +00:00
|
|
|
$count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
|
|
|
|
|
}
|
2012-12-21 21:58:00 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$conds = [ 'log_user' => $id, 'log_type != ' . $dbo->addQuotes( 'newusers' ) ];
|
2012-12-21 21:58:00 +00:00
|
|
|
$count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds, __METHOD__ );
|
|
|
|
|
|
2015-12-31 00:10:31 +00:00
|
|
|
$this->commitTransaction( $dbo, __METHOD__ );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
return $count == 0;
|
2006-01-04 12:33:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-05-05 01:38:22 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$maintClass = "RemoveUnusedAccounts";
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|