Change layout of the mustBePosted format to standardise it
[lhc/web/wiklou.git] / includes / UserRightsProxy.php
1 <?php
2
3 /**
4 * Cut-down copy of User interface for local-interwiki-database
5 * user rights manipulation.
6 */
7 class UserRightsProxy {
8
9 /**
10 * Constructor.
11 *
12 * @see newFromId()
13 * @see newFromName()
14 * @param $db DatabaseBase: db connection
15 * @param $database String: database name
16 * @param $name String: user name
17 * @param $id Integer: user ID
18 */
19 private function __construct( $db, $database, $name, $id ) {
20 $this->db = $db;
21 $this->database = $database;
22 $this->name = $name;
23 $this->id = intval( $id );
24 }
25
26 /**
27 * Accessor for $this->database
28 *
29 * @return String: database name
30 */
31 public function getDBName() {
32 return $this->database;
33 }
34
35 /**
36 * Confirm the selected database name is a valid local interwiki database name.
37 *
38 * @param $database String: database name
39 * @return Boolean
40 */
41 public static function validDatabase( $database ) {
42 global $wgLocalDatabases;
43 return in_array( $database, $wgLocalDatabases );
44 }
45
46 /**
47 * Same as User::whoIs()
48 *
49 * @param $database String: database name
50 * @param $id Integer: user ID
51 * @return String: user name or false if the user doesn't exist
52 */
53 public static function whoIs( $database, $id ) {
54 $user = self::newFromId( $database, $id );
55 if( $user ) {
56 return $user->name;
57 } else {
58 return false;
59 }
60 }
61
62 /**
63 * Factory function; get a remote user entry by ID number.
64 *
65 * @param $database String: database name
66 * @param $id Integer: user ID
67 * @return UserRightsProxy or null if doesn't exist
68 */
69 public static function newFromId( $database, $id ) {
70 return self::newFromLookup( $database, 'user_id', intval( $id ) );
71 }
72
73 /**
74 * Factory function; get a remote user entry by name.
75 *
76 * @param $database String: database name
77 * @param $name String: user name
78 * @return UserRightsProxy or null if doesn't exist
79 */
80 public static function newFromName( $database, $name ) {
81 return self::newFromLookup( $database, 'user_name', $name );
82 }
83
84 private static function newFromLookup( $database, $field, $value ) {
85 $db = self::getDB( $database );
86 if( $db ) {
87 $row = $db->selectRow( 'user',
88 array( 'user_id', 'user_name' ),
89 array( $field => $value ),
90 __METHOD__ );
91 if( $row !== false ) {
92 return new UserRightsProxy( $db, $database,
93 $row->user_name,
94 intval( $row->user_id ) );
95 }
96 }
97 return null;
98 }
99
100 /**
101 * Open a database connection to work on for the requested user.
102 * This may be a new connection to another database for remote users.
103 *
104 * @param $database String
105 * @return DatabaseBase or null if invalid selection
106 */
107 public static function getDB( $database ) {
108 global $wgLocalDatabases, $wgDBname;
109 if( self::validDatabase( $database ) ) {
110 if( $database == $wgDBname ) {
111 // Hmm... this shouldn't happen though. :)
112 return wfGetDB( DB_MASTER );
113 } else {
114 return wfGetDB( DB_MASTER, array(), $database );
115 }
116 }
117 return null;
118 }
119
120 public function getId() {
121 return $this->id;
122 }
123
124 public function isAnon() {
125 return $this->getId() == 0;
126 }
127
128 /**
129 * Same as User::getName()
130 *
131 * @return String
132 */
133 public function getName() {
134 return $this->name . '@' . $this->database;
135 }
136
137 /**
138 * Same as User::getUserPage()
139 *
140 * @return Title object
141 */
142 public function getUserPage() {
143 return Title::makeTitle( NS_USER, $this->getName() );
144 }
145
146 /**
147 * Replaces User::getUserGroups()
148 */
149 function getGroups() {
150 $res = $this->db->select( 'user_groups',
151 array( 'ug_group' ),
152 array( 'ug_user' => $this->id ),
153 __METHOD__ );
154 $groups = array();
155 while( $row = $this->db->fetchObject( $res ) ) {
156 $groups[] = $row->ug_group;
157 }
158 return $groups;
159 }
160
161 /**
162 * Replaces User::addUserGroup()
163 */
164 function addGroup( $group ) {
165 $this->db->insert( 'user_groups',
166 array(
167 'ug_user' => $this->id,
168 'ug_group' => $group,
169 ),
170 __METHOD__,
171 array( 'IGNORE' ) );
172 }
173
174 /**
175 * Replaces User::removeUserGroup()
176 */
177 function removeGroup( $group ) {
178 $this->db->delete( 'user_groups',
179 array(
180 'ug_user' => $this->id,
181 'ug_group' => $group,
182 ),
183 __METHOD__ );
184 }
185
186 /**
187 * Replaces User::touchUser()
188 */
189 function invalidateCache() {
190 $this->db->update( 'user',
191 array( 'user_touched' => $this->db->timestamp() ),
192 array( 'user_id' => $this->id ),
193 __METHOD__ );
194
195 global $wgMemc;
196 if ( function_exists( 'wfForeignMemcKey' ) ) {
197 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
198 } else {
199 $key = "$this->database:user:id:" . $this->id;
200 }
201 $wgMemc->delete( $key );
202 }
203 }