Moved the bulk of dbsource() to Database.php. Added support for updating wikis with...
[lhc/web/wiklou.git] / maintenance / userFunctions.inc
1 <?php
2
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 */
10
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 );
21 }
22
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 );
34 }
35
36 # Resolve a username to a user ID
37 function GetUserID( $username ) {
38 $dbr =& wfGetDB( DB_SLAVE );
39 $sql = 'SELECT user_id FROM ' . $dbr->tableName( 'user' ) . ' WHERE user_name = "' . $username . '"';
40 $res = $dbr->query( $sql );
41 if( $res !== false ) {
42 while( $row = $dbr->fetchObject( $res ) ) {
43 $ret = $row->user_id;
44 }
45 } else {
46 $ret = false;
47 }
48 return( $ret );
49 }
50
51 # Delete one or more users
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 /**
65 * Add a user to the named group(s)
66 *
67 * @param integer $user User ID
68 * @param mixed $groups Single string or array of strings corresponding to group names
69 * @return bool
70 */
71 function SetUserGroups( $user, $groups ) {
72 $dbw =& wfGetDB( DB_MASTER );
73 foreach( $groups as $group ) {
74 $row = array( 'ug_user' => $user, 'ug_group' => $group );
75 if( !$dbw->insert( 'user_groups', $row, 'SetUserGroups' ) ) {
76 return( false );
77 }
78 }
79 return( true );
80 }
81
82 ?>