Make user functions more generalised so other maintenance scripts can use them
[mediawiki.git] / maintenance / userFunctions.inc
blob8e43a6d2038c4eb8393f5a4caf9d6b52c4781ffa
1 <?php
3 /**
4  * Support functions for dealing with user accounts at a database level
5  *
6  * @package MediaWiki
7  * @subpackage Maintenance
8  * @author Rob Church <robchur@gmail.com>
9  */
11 # Count the number of edits the specified user has made
12 function CountEdits( $user_id ) {
13         # We've *got* to pull this stuff off the master. If the user *has* made an edit, but it hasn't
14         # been replicated to the slaves yet, we'll end up falsely marking them as inactive. This could
15         # (and usually would) lead to their deletion.
16         $dbw =& wfGetDB( DB_MASTER );
17         $sql = 'SELECT COUNT(rev_id) AS count FROM ' . $dbw->tableName( 'revision' ) . ' WHERE rev_user = ' . $user_id;
18         $res = $dbw->query( $sql );
19         $row = $dbw->fetchObject( $res );
20         return( $row->count );
23 # Return an array containing all valid user IDs
24 function GetUsers() {
25         # We're safe enough pulling this off a slave
26         $dbr =& wfGetDB( DB_SLAVE );
27         $sql = 'SELECT user_id FROM ' . $dbr->tableName( 'user' );
28         $res = $dbr->query( $sql );
29         $users = array();
30         while( $row = $dbr->fetchObject( $res ) ) {
31                 $users[] = $row->user_id;
32         }
33         return( $users );
36 # Delete one or more users
37 function DeleteUsers( $users ) {
38         # Need a master, obviously
39         $dbw =& wfGetDB( DB_MASTER );
40         # We'll do it all in one go, for speed
41         $dbw->begin();
42         $table = $dbw->tableName( 'user' );
43         foreach( $users as $user ) {
44                 $dbw->query( 'DELETE FROM ' . $table . ' WHERE user_id = ' . $user . ' LIMIT 1' );
45         }
46         $dbw->commit();