MediaWiki  master
removeUnusedAccounts.php
Go to the documentation of this file.
1 <?php
29 
30 require_once __DIR__ . '/Maintenance.php';
31 
38  public function __construct() {
39  parent::__construct();
40  $this->addOption( 'delete', 'Actually delete the account' );
41  $this->addOption( 'ignore-groups', 'List of comma-separated groups to exclude', false, true );
42  $this->addOption( 'ignore-touched', 'Skip accounts touched in last N days', false, true );
43  }
44 
45  public function execute() {
46  $services = MediaWikiServices::getInstance();
47  $userFactory = $services->getUserFactory();
48  $userGroupManager = $services->getUserGroupManager();
49  $this->output( "Remove unused accounts\n\n" );
50 
51  # Do an initial scan for inactive accounts and report the result
52  $this->output( "Checking for unused user accounts...\n" );
53  $delUser = [];
54  $delActor = [];
55  $dbr = $this->getDB( DB_REPLICA );
56  $res = $dbr->select(
57  [ 'user', 'actor' ],
58  [ 'user_id', 'user_name', 'user_touched', 'actor_id' ],
59  '',
60  __METHOD__,
61  [],
62  [ 'actor' => [ 'LEFT JOIN', 'user_id = actor_user' ] ]
63  );
64  if ( $this->hasOption( 'ignore-groups' ) ) {
65  $excludedGroups = explode( ',', $this->getOption( 'ignore-groups' ) );
66  } else {
67  $excludedGroups = [];
68  }
69  $touched = $this->getOption( 'ignore-touched', "1" );
70  if ( !ctype_digit( $touched ) ) {
71  $this->fatalError( "Please put a valid positive integer on the --ignore-touched parameter." );
72  }
73  $touchedSeconds = 86400 * $touched;
74  foreach ( $res as $row ) {
75  # Check the account, but ignore it if it's within a $excludedGroups
76  # group or if it's touched within the $touchedSeconds seconds.
77  $instance = $userFactory->newFromId( $row->user_id );
78  if ( count(
79  array_intersect( $userGroupManager->getUserEffectiveGroups( $instance ), $excludedGroups ) ) == 0
80  && $this->isInactiveAccount( $instance, $row->actor_id ?? null, true )
81  && wfTimestamp( TS_UNIX, $row->user_touched ) < wfTimestamp( TS_UNIX, time() - $touchedSeconds
82  )
83  ) {
84  # Inactive; print out the name and flag it
85  $delUser[] = $row->user_id;
86  if ( isset( $row->actor_id ) && $row->actor_id ) {
87  $delActor[] = $row->actor_id;
88  }
89  $this->output( $row->user_name . "\n" );
90  }
91  }
92  $count = count( $delUser );
93  $this->output( "...found {$count}.\n" );
94 
95  # If required, go back and delete each marked account
96  if ( $count > 0 && $this->hasOption( 'delete' ) ) {
97  $this->output( "\nDeleting unused accounts..." );
98  $dbw = $this->getDB( DB_PRIMARY );
99  $dbw->delete( 'user', [ 'user_id' => $delUser ], __METHOD__ );
100  # Keep actor rows referenced from ipblocks
101  $keep = $dbw->selectFieldValues(
102  'ipblocks', 'ipb_by_actor', [ 'ipb_by_actor' => $delActor ], __METHOD__
103  );
104  $del = array_diff( $delActor, $keep );
105  if ( $del ) {
106  $dbw->delete( 'actor', [ 'actor_id' => $del ], __METHOD__ );
107  }
108  if ( $keep ) {
109  $dbw->update( 'actor', [ 'actor_user' => null ], [ 'actor_id' => $keep ], __METHOD__ );
110  }
111  $dbw->delete( 'user_groups', [ 'ug_user' => $delUser ], __METHOD__ );
112  $dbw->delete( 'user_former_groups', [ 'ufg_user' => $delUser ], __METHOD__ );
113  $dbw->delete( 'user_properties', [ 'up_user' => $delUser ], __METHOD__ );
114  $dbw->delete( 'logging', [ 'log_actor' => $delActor ], __METHOD__ );
115  $dbw->delete( 'recentchanges', [ 'rc_actor' => $delActor ], __METHOD__ );
116  $this->output( "done.\n" );
117  # Update the site_stats.ss_users field
118  $users = $dbw->selectField( 'user', 'COUNT(*)', [], __METHOD__ );
119  $dbw->update(
120  'site_stats',
121  [ 'ss_users' => $users ],
122  [ 'ss_row_id' => 1 ],
123  __METHOD__
124  );
125  } elseif ( $count > 0 ) {
126  $this->output( "\nRun the script again with --delete to remove them from the database.\n" );
127  }
128  $this->output( "\n" );
129  }
130 
140  private function isInactiveAccount( $user, $actor, $primary = false ) {
141  if ( $actor === null ) {
142  // There's no longer a way for a user to be active in any of
143  // these tables without having an actor ID. The only way to link
144  // to a user row is via an actor row.
145  return true;
146  }
147 
148  $dbo = $this->getDB( $primary ? DB_PRIMARY : DB_REPLICA );
149  $checks = [
150  'archive' => 'ar',
151  'image' => 'img',
152  'oldimage' => 'oi',
153  'filearchive' => 'fa'
154  // re-add when actor migration is complete
155  // 'revision' => 'rev'
156  ];
157  $count = 0;
158 
159  $this->beginTransaction( $dbo, __METHOD__ );
160  foreach ( $checks as $table => $prefix ) {
161  $count += (int)$dbo->selectField(
162  $table,
163  'COUNT(*)',
164  [ "{$prefix}_actor" => $actor ],
165  __METHOD__
166  );
167  }
168 
169  // Delete this special case when the actor migration is complete
170  $actorQuery = ActorMigration::newMigration()->getWhere( $dbo, 'rev_user', $user );
171  $count += (int)$dbo->selectField(
172  [ 'revision' ] + $actorQuery['tables'],
173  'COUNT(*)',
174  $actorQuery['conds'],
175  __METHOD__,
176  [],
177  $actorQuery['joins']
178  );
179 
180  $count += (int)$dbo->selectField(
181  [ 'logging' ],
182  'COUNT(*)',
183  [
184  'log_actor' => $actor,
185  'log_type != ' . $dbo->addQuotes( 'newusers' )
186  ],
187  __METHOD__
188  );
189 
190  $this->commitTransaction( $dbo, __METHOD__ );
191 
192  return $count == 0;
193  }
194 }
195 
196 $maintClass = RemoveUnusedAccounts::class;
197 require_once RUN_MAINTENANCE_IF_MAIN;
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
Maintenance script that removes unused user accounts from the database.
execute()
Do the actual work.
__construct()
Default constructor.
Interface for objects representing user identity.
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28