Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / maintenance / removeUnusedAccounts.php
blob2d75722858bf6d44960b0023649bb63c9af4f54e
1 <?php
2 /**
3 * Remove unused user accounts from the database
4 * An unused account is one which has made no edits
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
22 * @ingroup Maintenance
23 * @author Rob Church <robchur@gmail.com>
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script that removes unused user accounts from the database.
31 * @ingroup Maintenance
33 class RemoveUnusedAccounts extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addOption( 'delete', 'Actually delete the account' );
37 $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
38 $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
41 public function execute() {
43 $this->output( "Remove unused accounts\n\n" );
45 # Do an initial scan for inactive accounts and report the result
46 $this->output( "Checking for unused user accounts...\n" );
47 $del = array();
48 $dbr = wfGetDB( DB_SLAVE );
49 $res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_touched' ), '', __METHOD__ );
50 if ( $this->hasOption( 'ignore-groups' ) ) {
51 $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
52 } else {
53 $excludedGroups = array();
55 $touched = $this->getOption( 'ignore-touched', "1" );
56 if ( !ctype_digit( $touched ) ) {
57 $this->error( "Please put a valid positive integer on the --ignore-touched parameter.", true );
59 $touchedSeconds = 86400 * $touched;
60 foreach ( $res as $row ) {
61 # Check the account, but ignore it if it's within a $excludedGroups group or if it's touched within the $touchedSeconds seconds.
62 $instance = User::newFromId( $row->user_id );
63 if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
64 && $this->isInactiveAccount( $row->user_id, true )
65 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
66 ) {
67 # Inactive; print out the name and flag it
68 $del[] = $row->user_id;
69 $this->output( $row->user_name . "\n" );
72 $count = count( $del );
73 $this->output( "...found {$count}.\n" );
75 # If required, go back and delete each marked account
76 if ( $count > 0 && $this->hasOption( 'delete' ) ) {
77 $this->output( "\nDeleting inactive accounts..." );
78 $dbw = wfGetDB( DB_MASTER );
79 $dbw->delete( 'user', array( 'user_id' => $del ), __METHOD__ );
80 $dbw->delete( 'logging', array( 'log_user' => $del ), __METHOD__ );
81 $dbw->delete( 'recentchanges', array( 'rc_user' => $del ), __METHOD__ );
82 $this->output( "done.\n" );
83 # Update the site_stats.ss_users field
84 $users = $dbw->selectField( 'user', 'COUNT(*)', array(), __METHOD__ );
85 $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), __METHOD__ );
86 } elseif ( $count > 0 ) {
87 $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
89 $this->output( "\n" );
92 /**
93 * Could the specified user account be deemed inactive?
94 * (No edits, no deleted edits, no log entries, no current/old uploads)
96 * @param $id User's ID
97 * @param $master bool Perform checking on the master
98 * @return bool
100 private function isInactiveAccount( $id, $master = false ) {
101 $dbo = wfGetDB( $master ? DB_MASTER : DB_SLAVE );
102 $checks = array(
103 'revision' => 'rev',
104 'archive' => 'ar',
105 'image' => 'img',
106 'oldimage' => 'oi',
107 'filearchive' => 'fa'
109 $count = 0;
111 $dbo->begin( __METHOD__ );
112 foreach ( $checks as $table => $fprefix ) {
113 $conds = array( $fprefix . '_user' => $id );
114 $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
117 $conds = array( 'log_user' => $id, 'log_type != ' . $dbo->addQuotes( 'newusers' ) );
118 $count += (int)$dbo->selectField( 'logging', 'COUNT(*)', $conds, __METHOD__ );
120 $dbo->commit( __METHOD__ );
122 return $count == 0;
126 $maintClass = "RemoveUnusedAccounts";
127 require_once RUN_MAINTENANCE_IF_MAIN;