00001 <?php
00002
00003
00008 class UserRightsProxy {
00009 private function __construct( $db, $database, $name, $id ) {
00010 $this->db = $db;
00011 $this->database = $database;
00012 $this->name = $name;
00013 $this->id = intval( $id );
00014 }
00015
00020 public static function validDatabase( $database ) {
00021 global $wgLocalDatabases;
00022 return in_array( $database, $wgLocalDatabases );
00023 }
00024
00025 public static function whoIs( $database, $id ) {
00026 $user = self::newFromId( $database, $id );
00027 if( $user ) {
00028 return $user->name;
00029 } else {
00030 return false;
00031 }
00032 }
00033
00038 public static function newFromId( $database, $id ) {
00039 return self::newFromLookup( $database, 'user_id', intval( $id ) );
00040 }
00041
00042 public static function newFromName( $database, $name ) {
00043 return self::newFromLookup( $database, 'user_name', $name );
00044 }
00045
00046 private static function newFromLookup( $database, $field, $value ) {
00047 $db = self::getDB( $database );
00048 if( $db ) {
00049 $row = $db->selectRow( 'user',
00050 array( 'user_id', 'user_name' ),
00051 array( $field => $value ),
00052 __METHOD__ );
00053 if( $row !== false ) {
00054 return new UserRightsProxy( $db, $database,
00055 $row->user_name,
00056 intval( $row->user_id ) );
00057 }
00058 }
00059 return null;
00060 }
00061
00068 public static function getDB( $database ) {
00069 global $wgLocalDatabases, $wgDBname;
00070 if( self::validDatabase( $database ) ) {
00071 if( $database == $wgDBname ) {
00072
00073 return wfGetDB( DB_MASTER );
00074 } else {
00075 return wfGetDB( DB_MASTER, array(), $database );
00076 }
00077 }
00078 return null;
00079 }
00080
00081 public function getId() {
00082 return $this->id;
00083 }
00084
00085 public function isAnon() {
00086 return $this->getId() == 0;
00087 }
00088
00089 public function getName() {
00090 return $this->name . '@' . $this->database;
00091 }
00092
00093 public function getUserPage() {
00094 return Title::makeTitle( NS_USER, $this->getName() );
00095 }
00096
00097
00098 function getGroups() {
00099 $res = $this->db->select( 'user_groups',
00100 array( 'ug_group' ),
00101 array( 'ug_user' => $this->id ),
00102 __METHOD__ );
00103 $groups = array();
00104 while( $row = $this->db->fetchObject( $res ) ) {
00105 $groups[] = $row->ug_group;
00106 }
00107 return $groups;
00108 }
00109
00110
00111 function addGroup( $group ) {
00112 $this->db->insert( 'user_groups',
00113 array(
00114 'ug_user' => $this->id,
00115 'ug_group' => $group,
00116 ),
00117 __METHOD__,
00118 array( 'IGNORE' ) );
00119 }
00120
00121
00122 function removeGroup( $group ) {
00123 $this->db->delete( 'user_groups',
00124 array(
00125 'ug_user' => $this->id,
00126 'ug_group' => $group,
00127 ),
00128 __METHOD__ );
00129 }
00130
00131
00132 function invalidateCache() {
00133 $this->db->update( 'user',
00134 array( 'user_touched' => $this->db->timestamp() ),
00135 array( 'user_id' => $this->id ),
00136 __METHOD__ );
00137
00138 global $wgMemc;
00139 if ( function_exists( 'wfForeignMemcKey' ) ) {
00140 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
00141 } else {
00142 $key = "$this->database:user:id:" . $this->id;
00143 }
00144 $wgMemc->delete( $key );
00145 }
00146 }