MediaWiki  REL1_22
UserRightsProxy.php
Go to the documentation of this file.
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         $db = self::getDB( $database, $ignoreInvalidDB );
00117         if ( $db ) {
00118             $row = $db->selectRow( 'user',
00119                 array( 'user_id', 'user_name' ),
00120                 array( $field => $value ),
00121                 __METHOD__ );
00122             if ( $row !== false ) {
00123                 return new UserRightsProxy( $db, $database,
00124                     $row->user_name,
00125                     intval( $row->user_id ) );
00126             }
00127         }
00128         return null;
00129     }
00130 
00139     public static function getDB( $database, $ignoreInvalidDB = false ) {
00140         global $wgDBname;
00141         if ( $ignoreInvalidDB || self::validDatabase( $database ) ) {
00142             if ( $database == $wgDBname ) {
00143                 // Hmm... this shouldn't happen though. :)
00144                 return wfGetDB( DB_MASTER );
00145             } else {
00146                 return wfGetDB( DB_MASTER, array(), $database );
00147             }
00148         }
00149         return null;
00150     }
00151 
00155     public function getId() {
00156         return $this->id;
00157     }
00158 
00162     public function isAnon() {
00163         return $this->getId() == 0;
00164     }
00165 
00171     public function getName() {
00172         return $this->name . '@' . $this->database;
00173     }
00174 
00180     public function getUserPage() {
00181         return Title::makeTitle( NS_USER, $this->getName() );
00182     }
00183 
00188     function getGroups() {
00189         $res = $this->db->select( 'user_groups',
00190             array( 'ug_group' ),
00191             array( 'ug_user' => $this->id ),
00192             __METHOD__ );
00193         $groups = array();
00194         foreach ( $res as $row ) {
00195             $groups[] = $row->ug_group;
00196         }
00197         return $groups;
00198     }
00199 
00203     function addGroup( $group ) {
00204         $this->db->insert( 'user_groups',
00205             array(
00206                 'ug_user' => $this->id,
00207                 'ug_group' => $group,
00208             ),
00209             __METHOD__,
00210             array( 'IGNORE' ) );
00211     }
00212 
00216     function removeGroup( $group ) {
00217         $this->db->delete( 'user_groups',
00218             array(
00219                 'ug_user' => $this->id,
00220                 'ug_group' => $group,
00221             ),
00222             __METHOD__ );
00223     }
00224 
00228     public function setOption( $option, $value ) {
00229         $this->newOptions[$option] = $value;
00230     }
00231 
00232     public function saveSettings() {
00233         $rows = array();
00234         foreach ( $this->newOptions as $option => $value ) {
00235             $rows[] = array(
00236                 'up_user' => $this->id,
00237                 'up_property' => $option,
00238                 'up_value' => $value,
00239             );
00240         }
00241         $this->db->replace( 'user_properties',
00242             array( array( 'up_user', 'up_property' ) ),
00243             $rows, __METHOD__
00244         );
00245         $this->invalidateCache();
00246     }
00247 
00251     function invalidateCache() {
00252         $this->db->update( 'user',
00253             array( 'user_touched' => $this->db->timestamp() ),
00254             array( 'user_id' => $this->id ),
00255             __METHOD__ );
00256 
00257         global $wgMemc;
00258         $key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
00259         $wgMemc->delete( $key );
00260     }
00261 }