MediaWiki
REL1_24
|
00001 <?php 00027 class UserRightsProxy { 00028 00039 private function __construct( $db, $database, $name, $id ) { 00040 $this->db = $db; 00041 $this->database = $database; 00042 $this->name = $name; 00043 $this->id = intval( $id ); 00044 $this->newOptions = array(); 00045 } 00046 00052 public function getDBName() { 00053 return $this->database; 00054 } 00055 00062 public static function validDatabase( $database ) { 00063 global $wgLocalDatabases; 00064 return in_array( $database, $wgLocalDatabases ); 00065 } 00066 00075 public static function whoIs( $database, $id, $ignoreInvalidDB = false ) { 00076 $user = self::newFromId( $database, $id, $ignoreInvalidDB ); 00077 if ( $user ) { 00078 return $user->name; 00079 } else { 00080 return false; 00081 } 00082 } 00083 00092 public static function newFromId( $database, $id, $ignoreInvalidDB = false ) { 00093 return self::newFromLookup( $database, 'user_id', intval( $id ), $ignoreInvalidDB ); 00094 } 00095 00104 public static function newFromName( $database, $name, $ignoreInvalidDB = false ) { 00105 return self::newFromLookup( $database, 'user_name', $name, $ignoreInvalidDB ); 00106 } 00107 00115 private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) { 00116 global $wgSharedDB, $wgSharedTables; 00117 // If the user table is shared, perform the user query on it, but don't pass it to the UserRightsProxy, 00118 // as user rights are normally not shared. 00119 if ( $wgSharedDB && in_array( 'user', $wgSharedTables ) ) { 00120 $userdb = self::getDB( $wgSharedDB, $ignoreInvalidDB ); 00121 } else { 00122 $userdb = self::getDB( $database, $ignoreInvalidDB ); 00123 } 00124 00125 $db = self::getDB( $database, $ignoreInvalidDB ); 00126 00127 if ( $db && $userdb ) { 00128 $row = $userdb->selectRow( 'user', 00129 array( 'user_id', 'user_name' ), 00130 array( $field => $value ), 00131 __METHOD__ ); 00132 00133 if ( $row !== false ) { 00134 return new UserRightsProxy( $db, $database, 00135 $row->user_name, 00136 intval( $row->user_id ) ); 00137 } 00138 } 00139 return null; 00140 } 00141 00150 public static function getDB( $database, $ignoreInvalidDB = false ) { 00151 global $wgDBname; 00152 if ( $ignoreInvalidDB || self::validDatabase( $database ) ) { 00153 if ( $database == $wgDBname ) { 00154 // Hmm... this shouldn't happen though. :) 00155 return wfGetDB( DB_MASTER ); 00156 } else { 00157 return wfGetDB( DB_MASTER, array(), $database ); 00158 } 00159 } 00160 return null; 00161 } 00162 00166 public function getId() { 00167 return $this->id; 00168 } 00169 00173 public function isAnon() { 00174 return $this->getId() == 0; 00175 } 00176 00182 public function getName() { 00183 return $this->name . '@' . $this->database; 00184 } 00185 00191 public function getUserPage() { 00192 return Title::makeTitle( NS_USER, $this->getName() ); 00193 } 00194 00199 function getGroups() { 00200 $res = $this->db->select( 'user_groups', 00201 array( 'ug_group' ), 00202 array( 'ug_user' => $this->id ), 00203 __METHOD__ ); 00204 $groups = array(); 00205 foreach ( $res as $row ) { 00206 $groups[] = $row->ug_group; 00207 } 00208 return $groups; 00209 } 00210 00215 function addGroup( $group ) { 00216 $this->db->insert( 'user_groups', 00217 array( 00218 'ug_user' => $this->id, 00219 'ug_group' => $group, 00220 ), 00221 __METHOD__, 00222 array( 'IGNORE' ) ); 00223 } 00224 00229 function removeGroup( $group ) { 00230 $this->db->delete( 'user_groups', 00231 array( 00232 'ug_user' => $this->id, 00233 'ug_group' => $group, 00234 ), 00235 __METHOD__ ); 00236 } 00237 00243 public function setOption( $option, $value ) { 00244 $this->newOptions[$option] = $value; 00245 } 00246 00247 public function saveSettings() { 00248 $rows = array(); 00249 foreach ( $this->newOptions as $option => $value ) { 00250 $rows[] = array( 00251 'up_user' => $this->id, 00252 'up_property' => $option, 00253 'up_value' => $value, 00254 ); 00255 } 00256 $this->db->replace( 'user_properties', 00257 array( array( 'up_user', 'up_property' ) ), 00258 $rows, __METHOD__ 00259 ); 00260 $this->invalidateCache(); 00261 } 00262 00266 function invalidateCache() { 00267 $this->db->update( 'user', 00268 array( 'user_touched' => $this->db->timestamp() ), 00269 array( 'user_id' => $this->id ), 00270 __METHOD__ ); 00271 00272 global $wgMemc; 00273 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id ); 00274 $wgMemc->delete( $key ); 00275 } 00276 }