Added experimental support for restoration
[mediawiki.git] / maintenance / userFunctions.inc
bloba1b8c76068444fd773fe9dc8c6fe023f5d21b42f
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 /**
12  * Count the number of edits the specified user has made
13  *
14  * @param integer $user User ID
15  * @param bool $slave Whether or not a slave can be used
16  * @return integer
17  */
18 function CountEdits( $user, $slave = true ) {
19         $dbw =& wfGetDB( $slave ? DB_SLAVE: DB_MASTER );
20         # Count current edits
21         $res = $dbw->select( 'revision', 'COUNT(*) AS count', array( 'rev_user' => $user ) );
22         $row = $dbw->fetchObject( $res );
23         $count = $row->count;
24         # Count deleted edits
25         $res = $dbw->select( 'archive', 'COUNT(*) AS count', array( 'ar_user' => $user ) );
26         $row = $dbw->fetchObject( $res );
27         $count += $row->count;
28         # Done
29         return( $count );
32 /**
33  * Count the number of images the specified user has uploaded
34  *
35  * @param integer $user User ID
36  * @param bool $slave Whether or not a slave can be used
37  * @return integer
38  */
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 );
44         $count = $row->count;
45         # Count deleted edits
46         $res = $dbw->select( 'oldimage', 'COUNT(*) AS count', array( 'oi_user' => $user ) );
47         $row = $dbw->fetchObject( $res );
48         $count += $row->count;
49         # Done
50         return( $count );
53 /**
54  * Count the number of log entries associated with the specified user
55  *
56  * @param integer $user User ID
57  * @param bool $slave Whether or not a slave can be used
58  * @return integer
59  */
60 function CountLogs( $user, $slave = true ) {
61         $dbw =& wfGetDB( $slave ? DB_SLAVE: DB_MASTER );
62         # Count log entries
63         $res = $dbw->select( 'logging', 'COUNT(*) AS count', array( 'log_user' => $user ) );
64         $row = $dbw->fetchObject( $res );
65         $count = $row->count;
66         # Done
67         return( $count );
70 /**
71  * Retrieve all valid user IDs
72  *
73  * @return array
74  */
75 function GetUsers() {
76         $dbr =& wfGetDB( DB_SLAVE );
77         $res = $dbr->select( 'user', 'user_id' );
78         $users = array();
79         while( $row = $dbr->fetchObject( $res ) ) {
80                 $users[] = $row->user_id;
81         }
82         return( $users );
85 /**
86  * Resolve a username to a user ID
87  *
88  * @param string $username Username
89  * @return mixed
90  */
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 );
97         } else {
98                 return( false );
99         }
103  * Delete one or more users
105  * @param mixed $users Single integer or array of integers corresponding to user IDs
106  * @return bool
107  */
108 function DeleteUsers( $users ) {
109         $dbw =& wfGetDB( DB_MASTER );
110         $dbw->begin();
111         foreach( $users as $user ) {
112                 $dbw->delete( 'user', array( 'user_id' => $user ) );
113         }
114         $dbw->commit();
115         return( true );
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
123  * @return bool
124  */
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' ) ) {
130                         return( false );
131                 }
132         }
133         return( true );