2007-12-27 02:31:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cut-down copy of User interface for local-interwiki-database
|
|
|
|
|
* user rights manipulation.
|
|
|
|
|
*/
|
|
|
|
|
class UserRightsProxy {
|
2010-01-12 21:49:47 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
|
|
|
|
* @see newFromId()
|
|
|
|
|
* @see newFromName()
|
|
|
|
|
* @param $db DatabaseBase: db connection
|
|
|
|
|
* @param $database String: database name
|
|
|
|
|
* @param $name String: user name
|
|
|
|
|
* @param $id Integer: user ID
|
|
|
|
|
*/
|
2007-12-27 02:31:58 +00:00
|
|
|
private function __construct( $db, $database, $name, $id ) {
|
|
|
|
|
$this->db = $db;
|
|
|
|
|
$this->database = $database;
|
|
|
|
|
$this->name = $name;
|
|
|
|
|
$this->id = intval( $id );
|
2010-08-07 16:08:22 +00:00
|
|
|
$this->newOptions = array();
|
2007-12-27 02:31:58 +00:00
|
|
|
}
|
2010-01-12 21:49:47 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Accessor for $this->database
|
|
|
|
|
*
|
|
|
|
|
* @return String: database name
|
|
|
|
|
*/
|
2009-09-23 03:42:18 +00:00
|
|
|
public function getDBName() {
|
|
|
|
|
return $this->database;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-12-27 02:31:58 +00:00
|
|
|
/**
|
|
|
|
|
* Confirm the selected database name is a valid local interwiki database name.
|
2010-01-12 21:49:47 +00:00
|
|
|
*
|
|
|
|
|
* @param $database String: database name
|
|
|
|
|
* @return Boolean
|
2007-12-27 02:31:58 +00:00
|
|
|
*/
|
|
|
|
|
public static function validDatabase( $database ) {
|
|
|
|
|
global $wgLocalDatabases;
|
|
|
|
|
return in_array( $database, $wgLocalDatabases );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-12 21:49:47 +00:00
|
|
|
/**
|
|
|
|
|
* Same as User::whoIs()
|
|
|
|
|
*
|
|
|
|
|
* @param $database String: database name
|
|
|
|
|
* @param $id Integer: user ID
|
2010-08-07 16:08:22 +00:00
|
|
|
* @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
|
2010-01-12 21:49:47 +00:00
|
|
|
* @return String: user name or false if the user doesn't exist
|
|
|
|
|
*/
|
2010-08-07 16:08:22 +00:00
|
|
|
public static function whoIs( $database, $id, $ignoreInvalidDB = false ) {
|
|
|
|
|
$user = self::newFromId( $database, $id, $ignoreInvalidDB );
|
2007-12-27 02:31:58 +00:00
|
|
|
if( $user ) {
|
|
|
|
|
return $user->name;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-12-27 02:31:58 +00:00
|
|
|
/**
|
|
|
|
|
* Factory function; get a remote user entry by ID number.
|
2010-01-12 21:49:47 +00:00
|
|
|
*
|
|
|
|
|
* @param $database String: database name
|
|
|
|
|
* @param $id Integer: user ID
|
2010-08-07 16:08:22 +00:00
|
|
|
* @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
|
2007-12-27 02:31:58 +00:00
|
|
|
* @return UserRightsProxy or null if doesn't exist
|
|
|
|
|
*/
|
2010-08-07 16:08:22 +00:00
|
|
|
public static function newFromId( $database, $id, $ignoreInvalidDB = false ) {
|
|
|
|
|
return self::newFromLookup( $database, 'user_id', intval( $id ), $ignoreInvalidDB );
|
2007-12-27 02:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-12 21:49:47 +00:00
|
|
|
/**
|
|
|
|
|
* Factory function; get a remote user entry by name.
|
|
|
|
|
*
|
|
|
|
|
* @param $database String: database name
|
|
|
|
|
* @param $name String: user name
|
2010-08-07 16:08:22 +00:00
|
|
|
* @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
|
2010-01-12 21:49:47 +00:00
|
|
|
* @return UserRightsProxy or null if doesn't exist
|
|
|
|
|
*/
|
2010-08-07 16:08:22 +00:00
|
|
|
public static function newFromName( $database, $name, $ignoreInvalidDB = false ) {
|
|
|
|
|
return self::newFromLookup( $database, 'user_name', $name, $ignoreInvalidDB );
|
2007-12-27 02:31:58 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-08-07 16:08:22 +00:00
|
|
|
private static function newFromLookup( $database, $field, $value, $ignoreInvalidDB = false ) {
|
|
|
|
|
$db = self::getDB( $database, $ignoreInvalidDB );
|
2007-12-27 02:31:58 +00:00
|
|
|
if( $db ) {
|
|
|
|
|
$row = $db->selectRow( 'user',
|
|
|
|
|
array( 'user_id', 'user_name' ),
|
|
|
|
|
array( $field => $value ),
|
|
|
|
|
__METHOD__ );
|
|
|
|
|
if( $row !== false ) {
|
|
|
|
|
return new UserRightsProxy( $db, $database,
|
|
|
|
|
$row->user_name,
|
|
|
|
|
intval( $row->user_id ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Open a database connection to work on for the requested user.
|
|
|
|
|
* This may be a new connection to another database for remote users.
|
2010-01-12 21:49:47 +00:00
|
|
|
*
|
|
|
|
|
* @param $database String
|
2010-08-07 16:08:22 +00:00
|
|
|
* @param $ignoreInvalidDB Boolean: if true, don't check if $database is in $wgLocalDatabases
|
2010-01-12 21:49:47 +00:00
|
|
|
* @return DatabaseBase or null if invalid selection
|
2007-12-27 02:31:58 +00:00
|
|
|
*/
|
2010-08-07 16:08:22 +00:00
|
|
|
public static function getDB( $database, $ignoreInvalidDB = false ) {
|
2010-07-24 19:11:52 +00:00
|
|
|
global $wgDBname;
|
2007-12-27 02:31:58 +00:00
|
|
|
if( self::validDatabase( $database ) ) {
|
|
|
|
|
if( $database == $wgDBname ) {
|
|
|
|
|
// Hmm... this shouldn't happen though. :)
|
|
|
|
|
return wfGetDB( DB_MASTER );
|
|
|
|
|
} else {
|
2008-03-30 09:48:15 +00:00
|
|
|
return wfGetDB( DB_MASTER, array(), $database );
|
2007-12-27 02:31:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-12-27 02:31:58 +00:00
|
|
|
public function getId() {
|
|
|
|
|
return $this->id;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-12-27 02:31:58 +00:00
|
|
|
public function isAnon() {
|
|
|
|
|
return $this->getId() == 0;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-12 21:49:47 +00:00
|
|
|
/**
|
|
|
|
|
* Same as User::getName()
|
|
|
|
|
*
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
2007-12-27 02:31:58 +00:00
|
|
|
public function getName() {
|
|
|
|
|
return $this->name . '@' . $this->database;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-12 21:49:47 +00:00
|
|
|
/**
|
|
|
|
|
* Same as User::getUserPage()
|
|
|
|
|
*
|
|
|
|
|
* @return Title object
|
|
|
|
|
*/
|
2007-12-27 02:31:58 +00:00
|
|
|
public function getUserPage() {
|
|
|
|
|
return Title::makeTitle( NS_USER, $this->getName() );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-12 21:49:47 +00:00
|
|
|
/**
|
|
|
|
|
* Replaces User::getUserGroups()
|
|
|
|
|
*/
|
2007-12-27 02:31:58 +00:00
|
|
|
function getGroups() {
|
|
|
|
|
$res = $this->db->select( 'user_groups',
|
|
|
|
|
array( 'ug_group' ),
|
|
|
|
|
array( 'ug_user' => $this->id ),
|
|
|
|
|
__METHOD__ );
|
|
|
|
|
$groups = array();
|
|
|
|
|
while( $row = $this->db->fetchObject( $res ) ) {
|
|
|
|
|
$groups[] = $row->ug_group;
|
|
|
|
|
}
|
|
|
|
|
return $groups;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-12 21:49:47 +00:00
|
|
|
/**
|
|
|
|
|
* Replaces User::addUserGroup()
|
|
|
|
|
*/
|
2007-12-27 02:31:58 +00:00
|
|
|
function addGroup( $group ) {
|
|
|
|
|
$this->db->insert( 'user_groups',
|
|
|
|
|
array(
|
|
|
|
|
'ug_user' => $this->id,
|
|
|
|
|
'ug_group' => $group,
|
|
|
|
|
),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
array( 'IGNORE' ) );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-12 21:49:47 +00:00
|
|
|
/**
|
|
|
|
|
* Replaces User::removeUserGroup()
|
|
|
|
|
*/
|
2007-12-27 02:31:58 +00:00
|
|
|
function removeGroup( $group ) {
|
|
|
|
|
$this->db->delete( 'user_groups',
|
|
|
|
|
array(
|
|
|
|
|
'ug_user' => $this->id,
|
|
|
|
|
'ug_group' => $group,
|
|
|
|
|
),
|
|
|
|
|
__METHOD__ );
|
|
|
|
|
}
|
2010-08-07 16:08:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replaces User::setOption()
|
|
|
|
|
*/
|
|
|
|
|
public function setOption( $option, $value ) {
|
|
|
|
|
$this->newOptions[$option] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function saveSettings() {
|
|
|
|
|
$rows = array();
|
|
|
|
|
foreach ( $this->newOptions as $option => $value ) {
|
|
|
|
|
$rows[] = array(
|
|
|
|
|
'up_user' => $this->id,
|
|
|
|
|
'up_property' => $option,
|
|
|
|
|
'up_value' => $value,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$this->db->replace( 'user_properties',
|
|
|
|
|
array( array( 'up_user', 'up_property' ) ),
|
|
|
|
|
$rows, __METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->invalidateCache();
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-12 21:49:47 +00:00
|
|
|
/**
|
|
|
|
|
* Replaces User::touchUser()
|
|
|
|
|
*/
|
2007-12-27 02:31:58 +00:00
|
|
|
function invalidateCache() {
|
|
|
|
|
$this->db->update( 'user',
|
|
|
|
|
array( 'user_touched' => $this->db->timestamp() ),
|
|
|
|
|
array( 'user_id' => $this->id ),
|
|
|
|
|
__METHOD__ );
|
|
|
|
|
|
|
|
|
|
global $wgMemc;
|
2010-08-06 16:02:42 +00:00
|
|
|
$key = wfForeignMemcKey( $this->database, false, 'user', 'id', $this->id );
|
2007-12-27 02:31:58 +00:00
|
|
|
$wgMemc->delete( $key );
|
|
|
|
|
}
|
|
|
|
|
}
|