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