MediaWiki  master
removeUnusedAccounts.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
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 );
39  }
40 
41  public function execute() {
42  $this->output( "Remove unused accounts\n\n" );
43 
44  # Do an initial scan for inactive accounts and report the result
45  $this->output( "Checking for unused user accounts...\n" );
46  $delUser = [];
47  $delActor = [];
48  $dbr = $this->getDB( DB_REPLICA );
49  $res = $dbr->select(
50  [ 'user', 'actor' ],
51  [ 'user_id', 'user_name', 'user_touched', 'actor_id' ],
52  '',
53  __METHOD__,
54  [],
55  [ 'actor' => [ 'LEFT JOIN', 'user_id = actor_user' ] ]
56  );
57  if ( $this->hasOption( 'ignore-groups' ) ) {
58  $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
59  } else {
60  $excludedGroups = [];
61  }
62  $touched = $this->getOption( 'ignore-touched', "1" );
63  if ( !ctype_digit( $touched ) ) {
64  $this->fatalError( "Please put a valid positive integer on the --ignore-touched parameter." );
65  }
66  $touchedSeconds = 86400 * $touched;
67  foreach ( $res as $row ) {
68  # Check the account, but ignore it if it's within a $excludedGroups
69  # group or if it's touched within the $touchedSeconds seconds.
70  $instance = User::newFromId( $row->user_id );
71  if ( count( array_intersect( $instance->getEffectiveGroups(), $excludedGroups ) ) == 0
72  && $this->isInactiveAccount( $row->user_id, $row->actor_id ?? null, true )
73  && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds )
74  ) {
75  # Inactive; print out the name and flag it
76  $delUser[] = $row->user_id;
77  if ( isset( $row->actor_id ) && $row->actor_id ) {
78  $delActor[] = $row->actor_id;
79  }
80  $this->output( $row->user_name . "\n" );
81  }
82  }
83  $count = count( $delUser );
84  $this->output( "...found {$count}.\n" );
85 
86  # If required, go back and delete each marked account
87  if ( $count > 0 && $this->hasOption( 'delete' ) ) {
88  $this->output( "\nDeleting unused accounts..." );
89  $dbw = $this->getDB( DB_MASTER );
90  $dbw->delete( 'user', [ 'user_id' => $delUser ], __METHOD__ );
91  # Keep actor rows referenced from ipblocks
92  $keep = $dbw->selectFieldValues(
93  'ipblocks', 'ipb_by_actor', [ 'ipb_by_actor' => $delActor ], __METHOD__
94  );
95  $del = array_diff( $delActor, $keep );
96  if ( $del ) {
97  $dbw->delete( 'actor', [ 'actor_id' => $del ], __METHOD__ );
98  }
99  if ( $keep ) {
100  $dbw->update( 'actor', [ 'actor_user' => 0 ], [ 'actor_id' => $keep ], __METHOD__ );
101  }
102  $dbw->delete( 'user_groups', [ 'ug_user' => $delUser ], __METHOD__ );
103  $dbw->delete( 'user_former_groups', [ 'ufg_user' => $delUser ], __METHOD__ );
104  $dbw->delete( 'user_properties', [ 'up_user' => $delUser ], __METHOD__ );
105  $dbw->delete( 'logging', [ 'log_actor' => $delActor ], __METHOD__ );
106  $dbw->delete( 'recentchanges', [ 'rc_actor' => $delActor ], __METHOD__ );
107  $this->output( "done.\n" );
108  # Update the site_stats.ss_users field
109  $users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ );
110  $dbw->update(
111  'site_stats',
112  [ 'ss_users' => $users ],
113  [ 'ss_row_id' => 1 ],
114  __METHOD__
115  );
116  } elseif ( $count > 0 ) {
117  $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
118  }
119  $this->output( "\n" );
120  }
121 
131  private function isInactiveAccount( $id, $actor, $master = false ) {
132  $dbo = $this->getDB( $master ? DB_MASTER : DB_REPLICA );
133  $checks = [
134  'revision' => 'rev',
135  'archive' => 'ar',
136  'image' => 'img',
137  'oldimage' => 'oi',
138  'filearchive' => 'fa'
139  ];
140  $count = 0;
141 
142  $migration = ActorMigration::newMigration();
143 
144  $user = User::newFromAnyId( $id, null, $actor );
145 
146  $this->beginTransaction( $dbo, __METHOD__ );
147  foreach ( $checks as $table => $prefix ) {
148  $actorQuery = $migration->getWhere(
149  $dbo, $prefix . '_user', $user, $prefix !== 'oi' && $prefix !== 'fa'
150  );
151  $count += (int)$dbo->selectField(
152  [ $table ] + $actorQuery['tables'],
153  'COUNT(*)',
154  $actorQuery['conds'],
155  __METHOD__,
156  [],
157  $actorQuery['joins']
158  );
159  }
160 
161  $actorQuery = $migration->getWhere( $dbo, 'log_user', $user, false );
162  $count += (int)$dbo->selectField(
163  [ 'logging' ] + $actorQuery['tables'],
164  'COUNT(*)',
165  [
166  $actorQuery['conds'],
167  'log_type != ' . $dbo->addQuotes( 'newusers' )
168  ],
169  __METHOD__,
170  [],
171  $actorQuery['joins']
172  );
173 
174  $this->commitTransaction( $dbo, __METHOD__ );
175 
176  return $count == 0;
177  }
178 }
179 
180 $maintClass = RemoveUnusedAccounts::class;
181 require_once RUN_MAINTENANCE_IF_MAIN;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
static newFromAnyId( $userId, $userName, $actorId, $dbDomain=false)
Static factory method for creation from an ID, name, and/or actor ID.
Definition: User.php:617
Maintenance script that removes unused user accounts from the database.
hasOption( $name)
Checks to see if a particular option exists.
const DB_MASTER
Definition: defines.php:26
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static newMigration()
Static constructor.
output( $out, $channel=null)
Throw some output to the user.
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:560
isInactiveAccount( $id, $actor, $master=false)
Could the specified user account be deemed inactive? (No edits, no deleted edits, no log entries...
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
const DB_REPLICA
Definition: defines.php:25
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.