00001 <?php
00002
00007 class UserRightsProxy {
00008
00019 private function __construct( $db, $database, $name, $id ) {
00020 $this->db = $db;
00021 $this->database = $database;
00022 $this->name = $name;
00023 $this->id = intval( $id );
00024 }
00025
00031 public function getDBName() {
00032 return $this->database;
00033 }
00034
00041 public static function validDatabase( $database ) {
00042 global $wgLocalDatabases;
00043 return in_array( $database, $wgLocalDatabases );
00044 }
00045
00053 public static function whoIs( $database, $id ) {
00054 $user = self::newFromId( $database, $id );
00055 if( $user ) {
00056 return $user->name;
00057 } else {
00058 return false;
00059 }
00060 }
00061
00069 public static function newFromId( $database, $id ) {
00070 return self::newFromLookup( $database, 'user_id', intval( $id ) );
00071 }
00072
00080 public static function newFromName( $database, $name ) {
00081 return self::newFromLookup( $database, 'user_name', $name );
00082 }
00083
00084 private static function newFromLookup( $database, $field, $value ) {
00085 $db = self::getDB( $database );
00086 if( $db ) {
00087 $row = $db->selectRow( 'user',
00088 array( 'user_id', 'user_name' ),
00089 array( $field => $value ),
00090 __METHOD__ );
00091 if( $row !== false ) {
00092 return new UserRightsProxy( $db, $database,
00093 $row->user_name,
00094 intval( $row->user_id ) );
00095 }
00096 }
00097 return null;
00098 }
00099
00107 public static function getDB( $database ) {
00108 global $wgLocalDatabases, $wgDBname;
00109 if( self::validDatabase( $database ) ) {
00110 if( $database == $wgDBname ) {
00111
00112 return wfGetDB( DB_MASTER );
00113 } else {
00114 return wfGetDB( DB_MASTER, array(), $database );
00115 }
00116 }
00117 return null;
00118 }
00119
00120 public function getId() {
00121 return $this->id;
00122 }
00123
00124 public function isAnon() {
00125 return $this->getId() == 0;
00126 }
00127
00133 public function getName() {
00134 return $this->name . '@' . $this->database;
00135 }
00136
00142 public function getUserPage() {
00143 return Title::makeTitle( NS_USER, $this->getName() );
00144 }
00145
00149 function getGroups() {
00150 $res = $this->db->select( 'user_groups',
00151 array( 'ug_group' ),
00152 array( 'ug_user' => $this->id ),
00153 __METHOD__ );
00154 $groups = array();
00155 while( $row = $this->db->fetchObject( $res ) ) {
00156 $groups[] = $row->ug_group;
00157 }
00158 return $groups;
00159 }
00160
00164 function addGroup( $group ) {
00165 $this->db->insert( 'user_groups',
00166 array(
00167 'ug_user' => $this->id,
00168 'ug_group' => $group,
00169 ),
00170 __METHOD__,
00171 array( 'IGNORE' ) );
00172 }
00173
00177 function removeGroup( $group ) {
00178 $this->db->delete( 'user_groups',
00179 array(
00180 'ug_user' => $this->id,
00181 'ug_group' => $group,
00182 ),
00183 __METHOD__ );
00184 }
00185
00189 function invalidateCache() {
00190 $this->db->update( 'user',
00191 array( 'user_touched' => $this->db->timestamp() ),
00192 array( 'user_id' => $this->id ),
00193 __METHOD__ );
00194
00195 global $wgMemc;
00196 if ( function_exists( 'wfForeignMemcKey' ) ) {
00197 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
00198 } else {
00199 $key = "$this->database:user:id:" . $this->id;
00200 }
00201 $wgMemc->delete( $key );
00202 }
00203 }