]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/removeUnusedAccounts.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.git] / maintenance / removeUnusedAccounts.php
1 <?php
2 /**
3  * Remove unused user accounts from the database
4  * An unused account is one which has made no edits
5  *
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.
10  *
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.
15  *
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
20  *
21  * @ingroup Maintenance
22  * @author Rob Church <robchur@gmail.com>
23  */
24
25 require_once( dirname(__FILE__) . '/Maintenance.php' );
26
27 class RemoveUnusedAccounts extends Maintenance {
28         public function __construct() {
29                 parent::__construct();
30                 $this->addOption( 'delete', 'Actually delete the account' );
31                 $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
32                 $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
33         }
34
35         public function execute() {
36
37                 $this->output( "Remove unused accounts\n\n" );
38                 
39                 # Do an initial scan for inactive accounts and report the result
40                 $this->output( "Checking for unused user accounts...\n" );
41                 $del = array();
42                 $dbr = wfGetDB( DB_SLAVE );
43                 $res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_touched' ), '', __METHOD__ );
44                 if( $this->hasOption('ignore-groups') ) {
45                         $excludedGroups = explode( ',', $this->getOption('ignore-groups') );
46                 } else { 
47                         $excludedGroups = array();
48                 }
49                 $touched = $this->getOption( 'ignore-touched', "1" );
50                 if( !ctype_digit( $touched ) ) {
51                         $this->error( "Please put a valid positive integer on the --ignore-touched parameter.", true );
52                 }
53                 $touchedSeconds = 86400 * $touched;
54                 foreach( $res as $row ) {
55                         # Check the account, but ignore it if it's within a $excludedGroups group or if it's touched within the $touchedSeconds seconds.
56                         $instance = User::newFromId( $row->user_id );
57                         if( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
58                                 && $this->isInactiveAccount( $row->user_id, true )
59                                 && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
60                                 ) {
61                                 # Inactive; print out the name and flag it
62                                 $del[] = $row->user_id;
63                                 $this->output( $row->user_name . "\n" );
64                         }
65                 }
66                 $count = count( $del );
67                 $this->output( "...found {$count}.\n" );
68         
69                 # If required, go back and delete each marked account
70                 if( $count > 0 && $this->hasOption('delete') ) {
71                         $this->output( "\nDeleting inactive accounts..." );
72                         $dbw = wfGetDB( DB_MASTER );
73                         $dbw->delete( 'user', array( 'user_id' => $del ), __METHOD__ );
74                         $this->output( "done.\n" );
75                         # Update the site_stats.ss_users field
76                         $users = $dbw->selectField( 'user', 'COUNT(*)', array(), __METHOD__ );
77                         $dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), __METHOD__ );
78                 } elseif( $count > 0 ) {
79                         $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
80                 }
81                 $this->output( "\n" );
82         }
83         
84         /**
85          * Could the specified user account be deemed inactive?
86          * (No edits, no deleted edits, no log entries, no current/old uploads)
87          *
88          * @param $id User's ID
89          * @param $master Perform checking on the master
90          * @return bool
91          */
92         private function isInactiveAccount( $id, $master = false ) {
93                 $dbo = wfGetDB( $master ? DB_MASTER : DB_SLAVE );
94                 $checks = array( 'revision' => 'rev', 'archive' => 'ar', 'logging' => 'log',
95                                                  'image' => 'img', 'oldimage' => 'oi' );
96                 $count = 0;
97         
98                 $dbo->begin();
99                 foreach( $checks as $table => $fprefix ) {
100                         $conds = array( $fprefix . '_user' => $id );
101                         $count += (int)$dbo->selectField( $table, 'COUNT(*)', $conds, __METHOD__ );
102                 }
103                 $dbo->commit();
104         
105                 return $count == 0;
106         }
107 }
108
109 $maintClass = "RemoveUnusedAccounts";
110 require_once( DO_MAINTENANCE );