4 * Support functions for dealing with user accounts at a database level
7 * @subpackage Maintenance
8 * @author Rob Church <robchur@gmail.com>
12 * Count the number of edits the specified user has made
14 * @param integer $user User ID
15 * @param bool $slave Whether or not a slave can be used
18 function CountEdits( $user, $slave = true ) {
19 $dbw =& wfGetDB( $slave ? DB_SLAVE: DB_MASTER );
21 $res = $dbw->select( 'revision', 'COUNT(*) AS count', array( 'rev_user' => $user ) );
22 $row = $dbw->fetchObject( $res );
25 $res = $dbw->select( 'archive', 'COUNT(*) AS count', array( 'ar_user' => $user ) );
26 $row = $dbw->fetchObject( $res );
27 $count += $row->count;
33 * Count the number of images the specified user has uploaded
35 * @param integer $user User ID
36 * @param bool $slave Whether or not a slave can be used
39 function CountImages( $user, $slave = true ) {
40 $dbw =& wfGetDB( $slave ? DB_SLAVE: DB_MASTER );
41 # Count current images
42 $res = $dbw->select( 'image', 'COUNT(*) AS count', array( 'img_user' => $user ) );
43 $row = $dbw->fetchObject( $res );
46 $res = $dbw->select( 'oldimage', 'COUNT(*) AS count', array( 'oi_user' => $user ) );
47 $row = $dbw->fetchObject( $res );
48 $count += $row->count;
54 * Count the number of log entries associated with the specified user
56 * @param integer $user User ID
57 * @param bool $slave Whether or not a slave can be used
60 function CountLogs( $user, $slave = true ) {
61 $dbw =& wfGetDB( $slave ? DB_SLAVE: DB_MASTER );
63 $res = $dbw->select( 'logging', 'COUNT(*) AS count', array( 'log_user' => $user ) );
64 $row = $dbw->fetchObject( $res );
71 * Retrieve all valid user IDs
76 $dbr =& wfGetDB( DB_SLAVE );
77 $res = $dbr->select( 'user', 'user_id' );
79 while( $row = $dbr->fetchObject( $res ) ) {
80 $users[] = $row->user_id;
86 * Resolve a username to a user ID
88 * @param string $username Username
91 function GetUserID( $username ) {
92 $dbr =& wfGetDB( DB_SLAVE );
93 $res = $dbr->select( 'user', 'user_id', array( 'user_name' => '"' . $username . '"' ) );
94 if( $res !== false ) {
95 $row = $dbr->fetchObject( $res );
96 return( $row->user_id );
103 * Delete one or more users
105 * @param mixed $users Single integer or array of integers corresponding to user IDs
108 function DeleteUsers( $users ) {
109 $dbw =& wfGetDB( DB_MASTER );
111 foreach( $users as $user ) {
112 $dbw->delete( 'user', array( 'user_id' => $user ) );
119 * Add a user to the named group(s)
121 * @param integer $user User ID
122 * @param mixed $groups Single string or array of strings corresponding to group names
125 function SetUserGroups( $user, $groups ) {
126 $dbw =& wfGetDB( DB_MASTER );
127 foreach( $groups as $group ) {
128 $row = array( 'ug_user' => $user, 'ug_group' => $group );
129 if( !$dbw->insert( 'user_groups', $row, 'SetUserGroups' ) ) {