MediaWiki  REL1_19
UserRightsProxy.php
Go to the documentation of this file.
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                 $this->newOptions = array();
00025         }
00026 
00032         public function getDBName() {
00033                 return $this->database;
00034         }
00035 
00042         public static function validDatabase( $database ) {
00043                 global $wgLocalDatabases;
00044                 return in_array( $database, $wgLocalDatabases );
00045         }
00046 
00055         public static function whoIs( $database, $id, $ignoreInvalidDB = false ) {
00056                 $user = self::newFromId( $database, $id, $ignoreInvalidDB );
00057                 if( $user ) {
00058                         return $user->name;
00059                 } else {
00060                         return false;
00061                 }
00062         }
00063 
00072         public static function newFromId( $database, $id, $ignoreInvalidDB = false ) {
00073                 return self::newFromLookup( $database, 'user_id', intval( $id ), $ignoreInvalidDB );
00074         }
00075 
00084         public static function newFromName( $database, $name, $ignoreInvalidDB = false ) {
00085                 return self::newFromLookup( $database, 'user_name', $name, $ignoreInvalidDB );
00086         }
00087 
00095         private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
00096                 $db = self::getDB( $database, $ignoreInvalidDB );
00097                 if( $db ) {
00098                         $row = $db->selectRow( 'user',
00099                                 array( 'user_id', 'user_name' ),
00100                                 array( $field => $value ),
00101                                 __METHOD__ );
00102                         if( $row !== false ) {
00103                                 return new UserRightsProxy( $db, $database,
00104                                         $row->user_name,
00105                                         intval( $row->user_id ) );
00106                         }
00107                 }
00108                 return null;
00109         }
00110 
00119         public static function getDB( $database, $ignoreInvalidDB = false ) {
00120                 global $wgDBname;
00121                 if( self::validDatabase( $database ) ) {
00122                         if( $database == $wgDBname ) {
00123                                 // Hmm... this shouldn't happen though. :)
00124                                 return wfGetDB( DB_MASTER );
00125                         } else {
00126                                 return wfGetDB( DB_MASTER, array(), $database );
00127                         }
00128                 }
00129                 return null;
00130         }
00131 
00135         public function getId() {
00136                 return $this->id;
00137         }
00138 
00142         public function isAnon() {
00143                 return $this->getId() == 0;
00144         }
00145 
00151         public function getName() {
00152                 return $this->name . '@' . $this->database;
00153         }
00154 
00160         public function getUserPage() {
00161                 return Title::makeTitle( NS_USER, $this->getName() );
00162         }
00163 
00167         function getGroups() {
00168                 $res = $this->db->select( 'user_groups',
00169                         array( 'ug_group' ),
00170                         array( 'ug_user' => $this->id ),
00171                         __METHOD__ );
00172                 $groups = array();
00173                 foreach ( $res as $row ) {
00174                         $groups[] = $row->ug_group;
00175                 }
00176                 return $groups;
00177         }
00178 
00182         function addGroup( $group ) {
00183                 $this->db->insert( 'user_groups',
00184                         array(
00185                                 'ug_user' => $this->id,
00186                                 'ug_group' => $group,
00187                         ),
00188                         __METHOD__,
00189                         array( 'IGNORE' ) );
00190         }
00191 
00195         function removeGroup( $group ) {
00196                 $this->db->delete( 'user_groups',
00197                         array(
00198                                 'ug_user' => $this->id,
00199                                 'ug_group' => $group,
00200                         ),
00201                         __METHOD__ );
00202         }
00203 
00207         public function setOption( $option, $value ) {
00208                 $this->newOptions[$option] = $value;
00209         }
00210 
00211         public function saveSettings() {
00212                 $rows = array();
00213                 foreach ( $this->newOptions as $option => $value ) {
00214                         $rows[] = array(
00215                                 'up_user' => $this->id,
00216                                 'up_property' => $option,
00217                                 'up_value' => $value,
00218                         );
00219                 }
00220                 $this->db->replace( 'user_properties',
00221                         array( array( 'up_user', 'up_property' ) ),
00222                         $rows, __METHOD__
00223                 );
00224                 $this->invalidateCache();
00225         }
00226 
00230         function invalidateCache() {
00231                 $this->db->update( 'user',
00232                         array( 'user_touched' => $this->db->timestamp() ),
00233                         array( 'user_id' => $this->id ),
00234                         __METHOD__ );
00235 
00236                 global $wgMemc;
00237                 $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
00238                 $wgMemc->delete( $key );
00239         }
00240 }