* (bug 12553) Fixed invalid XHTML in edit conflict screen
[lhc/web/wiklou.git] / includes / UserRightsProxy.php
1 <?php
2
3
4 /**
5 * Cut-down copy of User interface for local-interwiki-database
6 * user rights manipulation.
7 */
8 class UserRightsProxy {
9 private function __construct( $db, $database, $name, $id ) {
10 $this->db = $db;
11 $this->database = $database;
12 $this->name = $name;
13 $this->id = intval( $id );
14 }
15
16 /**
17 * Confirm the selected database name is a valid local interwiki database name.
18 * @return bool
19 */
20 public static function validDatabase( $database ) {
21 global $wgLocalDatabases;
22 return in_array( $database, $wgLocalDatabases );
23 }
24
25 public static function whoIs( $database, $id ) {
26 $user = self::newFromId( $database, $id );
27 if( $user ) {
28 return $user->name;
29 } else {
30 return false;
31 }
32 }
33
34 /**
35 * Factory function; get a remote user entry by ID number.
36 * @return UserRightsProxy or null if doesn't exist
37 */
38 public static function newFromId( $database, $id ) {
39 return self::newFromLookup( $database, 'user_id', intval( $id ) );
40 }
41
42 public static function newFromName( $database, $name ) {
43 return self::newFromLookup( $database, 'user_name', $name );
44 }
45
46 private static function newFromLookup( $database, $field, $value ) {
47 $db = self::getDB( $database );
48 if( $db ) {
49 $row = $db->selectRow( 'user',
50 array( 'user_id', 'user_name' ),
51 array( $field => $value ),
52 __METHOD__ );
53 if( $row !== false ) {
54 return new UserRightsProxy( $db, $database,
55 $row->user_name,
56 intval( $row->user_id ) );
57 }
58 }
59 return null;
60 }
61
62 /**
63 * Open a database connection to work on for the requested user.
64 * This may be a new connection to another database for remote users.
65 * @param string $database
66 * @return Database or null if invalid selection
67 */
68 private static function getDB( $database ) {
69 global $wgLocalDatabases, $wgDBname;
70 if( self::validDatabase( $database ) ) {
71 if( $database == $wgDBname ) {
72 // Hmm... this shouldn't happen though. :)
73 return wfGetDB( DB_MASTER );
74 } else {
75 global $wgDBuser, $wgDBpassword;
76 $server = self::getMaster( $database );
77 return new Database( $server, $wgDBuser, $wgDBpassword, $database );
78 }
79 }
80 return null;
81 }
82
83 /**
84 * Return the master server to connect to for the requested database.
85 */
86 private static function getMaster( $database ) {
87 global $wgDBserver, $wgAlternateMaster;
88 if( isset( $wgAlternateMaster[$database] ) ) {
89 return $wgAlternateMaster[$database];
90 }
91 return $wgDBserver;
92 }
93
94 public function getId() {
95 return $this->id;
96 }
97
98 public function isAnon() {
99 return $this->getId() == 0;
100 }
101
102 public function getName() {
103 return $this->name . '@' . $this->database;
104 }
105
106 public function getUserPage() {
107 return Title::makeTitle( NS_USER, $this->getName() );
108 }
109
110 // Replaces getUserGroups()
111 function getGroups() {
112 $res = $this->db->select( 'user_groups',
113 array( 'ug_group' ),
114 array( 'ug_user' => $this->id ),
115 __METHOD__ );
116 $groups = array();
117 while( $row = $this->db->fetchObject( $res ) ) {
118 $groups[] = $row->ug_group;
119 }
120 return $groups;
121 }
122
123 // replaces addUserGroup
124 function addGroup( $group ) {
125 $this->db->insert( 'user_groups',
126 array(
127 'ug_user' => $this->id,
128 'ug_group' => $group,
129 ),
130 __METHOD__,
131 array( 'IGNORE' ) );
132 }
133
134 // replaces removeUserGroup
135 function removeGroup( $group ) {
136 $this->db->delete( 'user_groups',
137 array(
138 'ug_user' => $this->id,
139 'ug_group' => $group,
140 ),
141 __METHOD__ );
142 }
143
144 // replaces touchUser
145 function invalidateCache() {
146 $this->db->update( 'user',
147 array( 'user_touched' => $this->db->timestamp() ),
148 array( 'user_id' => $this->id ),
149 __METHOD__ );
150
151 global $wgMemc;
152 if ( function_exists( 'wfForeignMemcKey' ) ) {
153 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
154 } else {
155 $key = "$this->database:user:id:" . $this->id;
156 }
157 $wgMemc->delete( $key );
158 }
159 }
160
161 ?>