* 80 chars width
[lhc/web/wiklou.git] / maintenance / removeUnusedAccounts.inc
1 <?php
2
3 /**
4 * Support functions for the removeUnusedAccounts script
5 *
6 * @package MediaWiki
7 * @subpackage Maintenance
8 * @author Rob Church <robchur@gmail.com>
9 */
10
11 define( 'ACTION_REPORT', 0 );
12 define( 'ACTION_DELETE', 1 );
13
14 /**
15 * Count the number of edits the specified user has made
16 * @param $user_id A database user id.
17 * @return integer Number of edits made by the given user.
18 */
19 function CountEdits( $user_id ) {
20 # We've *got* to pull this stuff off the master. If the user *has* made
21 # an edit, but it hasn't been replicated to the slaves yet, we'll end up
22 # falsely marking them as inactive. This could (and usually would) lead
23 # to their deletion.
24 $dbw =& wfGetDB( DB_MASTER );
25 $sql = 'SELECT COUNT(rev_id) AS count FROM ' . $dbw->tableName( 'revision' ) . ' WHERE rev_user = ' . $user_id;
26 $res = $dbw->query( $sql );
27 $row = $dbw->fetchObject( $res );
28 return( $row->count );
29 }
30
31 /**
32 * Return an array containing all valid user IDs
33 * @return array Array of User:: object(s).
34 */
35 function GetUsers() {
36 # We're safe enough pulling this off a slave
37 $dbr =& wfGetDB( DB_SLAVE );
38 $sql = 'SELECT user_id FROM ' . $dbr->tableName( 'user' );
39 $res = $dbr->query( $sql );
40 $users = array();
41 while( $row = $dbr->fetchObject( $res ) ) {
42 $users[] = $row->user_id;
43 }
44 return( $users );
45 }
46
47 /**
48 * Delete one or more users.
49 * You will probably use GetUsers() first to get a list of users :o)
50 * @param array An array of User:: object(s)
51 */
52 function DeleteUsers( $users ) {
53 # Need a master, obviously
54 $dbw =& wfGetDB( DB_MASTER );
55 # We'll do it all in one go, for speed
56 $dbw->begin();
57 $table = $dbw->tableName( 'user' );
58 foreach( $users as $user ) {
59 $dbw->query( 'DELETE FROM ' . $table . ' WHERE user_id = ' . $user . ' LIMIT 1' );
60 }
61 $dbw->commit();
62 }
63
64 ?>