<?php
-
/**
* Cut-down copy of User interface for local-interwiki-database
* user rights manipulation.
*/
class UserRightsProxy {
+
+ /**
+ * 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
+ */
private function __construct( $db, $database, $name, $id ) {
$this->db = $db;
$this->database = $database;
$this->name = $name;
$this->id = intval( $id );
}
-
+
+ /**
+ * Accessor for $this->database
+ *
+ * @return String: database name
+ */
public function getDBName() {
return $this->database;
}
/**
* Confirm the selected database name is a valid local interwiki database name.
- * @return bool
+ *
+ * @param $database String: database name
+ * @return Boolean
*/
public static function validDatabase( $database ) {
global $wgLocalDatabases;
return in_array( $database, $wgLocalDatabases );
}
+ /**
+ * Same as User::whoIs()
+ *
+ * @param $database String: database name
+ * @param $id Integer: user ID
+ * @return String: user name or false if the user doesn't exist
+ */
public static function whoIs( $database, $id ) {
$user = self::newFromId( $database, $id );
if( $user ) {
/**
* Factory function; get a remote user entry by ID number.
+ *
+ * @param $database String: database name
+ * @param $id Integer: user ID
* @return UserRightsProxy or null if doesn't exist
*/
public static function newFromId( $database, $id ) {
return self::newFromLookup( $database, 'user_id', intval( $id ) );
}
+ /**
+ * Factory function; get a remote user entry by name.
+ *
+ * @param $database String: database name
+ * @param $name String: user name
+ * @return UserRightsProxy or null if doesn't exist
+ */
public static function newFromName( $database, $name ) {
return self::newFromLookup( $database, 'user_name', $name );
}
/**
* Open a database connection to work on for the requested user.
* This may be a new connection to another database for remote users.
- * @param $database string
- * @return Database or null if invalid selection
+ *
+ * @param $database String
+ * @return DatabaseBase or null if invalid selection
*/
public static function getDB( $database ) {
global $wgLocalDatabases, $wgDBname;
return $this->getId() == 0;
}
+ /**
+ * Same as User::getName()
+ *
+ * @return String
+ */
public function getName() {
return $this->name . '@' . $this->database;
}
+ /**
+ * Same as User::getUserPage()
+ *
+ * @return Title object
+ */
public function getUserPage() {
return Title::makeTitle( NS_USER, $this->getName() );
}
- // Replaces getUserGroups()
+ /**
+ * Replaces User::getUserGroups()
+ */
function getGroups() {
$res = $this->db->select( 'user_groups',
array( 'ug_group' ),
return $groups;
}
- // replaces addUserGroup
+ /**
+ * Replaces User::addUserGroup()
+ */
function addGroup( $group ) {
$this->db->insert( 'user_groups',
array(
array( 'IGNORE' ) );
}
- // replaces removeUserGroup
+ /**
+ * Replaces User::removeUserGroup()
+ */
function removeGroup( $group ) {
$this->db->delete( 'user_groups',
array(
__METHOD__ );
}
- // replaces touchUser
+ /**
+ * Replaces User::touchUser()
+ */
function invalidateCache() {
$this->db->update( 'user',
array( 'user_touched' => $this->db->timestamp() ),
/**
* Helper tools for dealing with other locally-hosted wikis
*/
-
class WikiMap {
- static function getWiki( $wikiID ) {
+
+ /**
+ * Get a WikiReference object for $wikiID
+ *
+ * @param $wikiID String: wiki'd id (generally database name)
+ * @return WikiReference object or null if the wiki was not found
+ */
+ public static function getWiki( $wikiID ) {
global $wgConf, $IP;
-
+
$wgConf->loadFullData();
list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
}
}
- // Convenience functions from GlobalBlocking
- static function getWikiName( $wiki_id ) {
- // We can give more info than just the wiki id!
- $wiki = WikiMap::getWiki( $wiki_id );
-
- if ($wiki) {
+ /**
+ * Convenience to get the wiki's display name
+ *
+ * @todo We can give more info than just the wiki id!
+ * @param $wikiID String: wiki'd id (generally database name)
+ * @return Wiki's name or $wiki_id if the wiki was not found
+ */
+ public static function getWikiName( $wikiID ) {
+ $wiki = WikiMap::getWiki( $wikiID );
+
+ if ( $wiki ) {
return $wiki->getDisplayName();
}
return $wiki_id;
}
-
- static function foreignUserLink( $wiki_id, $user, $text=null ) {
- return self::makeForeignLink( $wiki_id, "User:$user", $text );
+
+ /**
+ * Convenience to get a link to a user page on a foreign wiki
+ *
+ * @param $wikiID String: wiki'd id (generally database name)
+ * @param $user String: user name (must be normalised before calling this function!)
+ * @param $text String: link's text; optional, default to "User:$user"
+ * @return String: HTML link or false if the wiki was not found
+ */
+ public static function foreignUserLink( $wikiID, $user, $text=null ) {
+ return self::makeForeignLink( $wikiID, "User:$user", $text );
}
-
- static function makeForeignLink( $wiki_id, $page, $text=null ) {
+
+ /**
+ * Convenience to get a link to a page on a foreign wiki
+ *
+ * @param $wikiID String: wiki'd id (generally database name)
+ * @param $page String: page name (must be normalised before calling this function!)
+ * @param $text String: link's text; optional, default to $page
+ * @return String: HTML link or false if the wiki was not found
+ */
+ public static function makeForeignLink( $wikiID, $page, $text=null ) {
global $wgUser;
$sk = $wgUser->getSkin();
if ( !$text )
- $text=$page;
+ $text = $page;
- $url = self::getForeignURL( $wiki_id, $page );
+ $url = self::getForeignURL( $wikiID, $page );
if ( $url === false )
return false;
return $sk->makeExternalLink( $url, $text );
}
-
- static function getForeignURL( $wiki_id, $page ) {
- $wiki = WikiMap::getWiki( $wiki_id );
+
+ /**
+ * Convenience to get a url to a page on a foreign wiki
+ *
+ * @param $wikiID String: wiki'd id (generally database name)
+ * @param $page String: page name (must be normalised before calling this function!)
+ * @return String: URL or false if the wiki was not found
+ */
+ public static function getForeignURL( $wikiID, $page ) {
+ $wiki = WikiMap::getWiki( $wikiID );
- if ($wiki)
+ if ( $wiki )
return $wiki->getUrl( $page );
return false;
}
}
+/**
+ * Reference to a locally-hosted wiki
+ */
class WikiReference {
private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
private $mMajor; ///< 'wiki', 'wiktionary', etc
private $mServer; ///< server override, 'www.mediawiki.org'
private $mPath; ///< path override, '/wiki/$1'
- function __construct( $major, $minor, $server, $path ) {
+ public function __construct( $major, $minor, $server, $path ) {
$this->mMajor = $major;
$this->mMinor = $minor;
$this->mServer = $server;
$this->mPath = $path;
}
- function getHostname() {
+ public function getHostname() {
$prefixes = array( 'http://', 'https://' );
foreach ( $prefixes as $prefix ) {
if ( substr( $this->mServer, 0, strlen( $prefix ) ) ) {
}
/**
- * pretty it up
+ * Get the the URL in a way to de displayed to the user
+ * More or less Wikimedia specific
+ *
+ * @return String
*/
- function getDisplayName() {
+ public function getDisplayName() {
$url = $this->getUrl( '' );
$url = preg_replace( '!^https?://!', '', $url );
$url = preg_replace( '!/index\.php(\?title=|/)$!', '/', $url );
return $url;
}
+ /**
+ * Helper function for getUrl()
+ *
+ * @todo FIXME: this may be generalized...
+ * @param $page String: page name (must be normalised before calling this function!)
+ * @return String: Url fragment
+ */
private function getLocalUrl( $page ) {
- // FIXME: this may be generalized...
return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
}
- function getUrl( $page ) {
+ /**
+ * Get a URL to a page on this foreign wiki
+ *
+ * @param $page String: page name (must be normalised before calling this function!)
+ * @return String: Url
+ */
+ public function getUrl( $page ) {
return
- $this->mServer .
+ $this->mServer .
$this->getLocalUrl( $page );
}
}