r60988 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60987‎ | r60988 | r60989 >
Date:21:49, 12 January 2010
Author:ialex
Status:ok
Tags:
Comment:
document a bit
Modified paths:
  • /trunk/phase3/includes/UserRightsProxy.php (modified) (history)
  • /trunk/phase3/includes/WikiMap.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/WikiMap.php
@@ -3,11 +3,17 @@
44 /**
55 * Helper tools for dealing with other locally-hosted wikis
66 */
 7+class WikiMap {
78
8 -class WikiMap {
9 - static function getWiki( $wikiID ) {
 9+ /**
 10+ * Get a WikiReference object for $wikiID
 11+ *
 12+ * @param $wikiID String: wiki'd id (generally database name)
 13+ * @return WikiReference object or null if the wiki was not found
 14+ */
 15+ public static function getWiki( $wikiID ) {
1016 global $wgConf, $IP;
11 -
 17+
1218 $wgConf->loadFullData();
1319
1420 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
@@ -22,59 +28,90 @@
2329 }
2430 }
2531
26 - // Convenience functions from GlobalBlocking
27 - static function getWikiName( $wiki_id ) {
28 - // We can give more info than just the wiki id!
29 - $wiki = WikiMap::getWiki( $wiki_id );
30 -
31 - if ($wiki) {
 32+ /**
 33+ * Convenience to get the wiki's display name
 34+ *
 35+ * @todo We can give more info than just the wiki id!
 36+ * @param $wikiID String: wiki'd id (generally database name)
 37+ * @return Wiki's name or $wiki_id if the wiki was not found
 38+ */
 39+ public static function getWikiName( $wikiID ) {
 40+ $wiki = WikiMap::getWiki( $wikiID );
 41+
 42+ if ( $wiki ) {
3243 return $wiki->getDisplayName();
3344 }
3445 return $wiki_id;
3546 }
36 -
37 - static function foreignUserLink( $wiki_id, $user, $text=null ) {
38 - return self::makeForeignLink( $wiki_id, "User:$user", $text );
 47+
 48+ /**
 49+ * Convenience to get a link to a user page on a foreign wiki
 50+ *
 51+ * @param $wikiID String: wiki'd id (generally database name)
 52+ * @param $user String: user name (must be normalised before calling this function!)
 53+ * @param $text String: link's text; optional, default to "User:$user"
 54+ * @return String: HTML link or false if the wiki was not found
 55+ */
 56+ public static function foreignUserLink( $wikiID, $user, $text=null ) {
 57+ return self::makeForeignLink( $wikiID, "User:$user", $text );
3958 }
40 -
41 - static function makeForeignLink( $wiki_id, $page, $text=null ) {
 59+
 60+ /**
 61+ * Convenience to get a link to a page on a foreign wiki
 62+ *
 63+ * @param $wikiID String: wiki'd id (generally database name)
 64+ * @param $page String: page name (must be normalised before calling this function!)
 65+ * @param $text String: link's text; optional, default to $page
 66+ * @return String: HTML link or false if the wiki was not found
 67+ */
 68+ public static function makeForeignLink( $wikiID, $page, $text=null ) {
4269 global $wgUser;
4370 $sk = $wgUser->getSkin();
4471
4572 if ( !$text )
46 - $text=$page;
 73+ $text = $page;
4774
48 - $url = self::getForeignURL( $wiki_id, $page );
 75+ $url = self::getForeignURL( $wikiID, $page );
4976 if ( $url === false )
5077 return false;
5178
5279 return $sk->makeExternalLink( $url, $text );
5380 }
54 -
55 - static function getForeignURL( $wiki_id, $page ) {
56 - $wiki = WikiMap::getWiki( $wiki_id );
 81+
 82+ /**
 83+ * Convenience to get a url to a page on a foreign wiki
 84+ *
 85+ * @param $wikiID String: wiki'd id (generally database name)
 86+ * @param $page String: page name (must be normalised before calling this function!)
 87+ * @return String: URL or false if the wiki was not found
 88+ */
 89+ public static function getForeignURL( $wikiID, $page ) {
 90+ $wiki = WikiMap::getWiki( $wikiID );
5791
58 - if ($wiki)
 92+ if ( $wiki )
5993 return $wiki->getUrl( $page );
6094
6195 return false;
6296 }
6397 }
6498
 99+/**
 100+ * Reference to a locally-hosted wiki
 101+ */
65102 class WikiReference {
66103 private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
67104 private $mMajor; ///< 'wiki', 'wiktionary', etc
68105 private $mServer; ///< server override, 'www.mediawiki.org'
69106 private $mPath; ///< path override, '/wiki/$1'
70107
71 - function __construct( $major, $minor, $server, $path ) {
 108+ public function __construct( $major, $minor, $server, $path ) {
72109 $this->mMajor = $major;
73110 $this->mMinor = $minor;
74111 $this->mServer = $server;
75112 $this->mPath = $path;
76113 }
77114
78 - function getHostname() {
 115+ public function getHostname() {
79116 $prefixes = array( 'http://', 'https://' );
80117 foreach ( $prefixes as $prefix ) {
81118 if ( substr( $this->mServer, 0, strlen( $prefix ) ) ) {
@@ -85,9 +122,12 @@
86123 }
87124
88125 /**
89 - * pretty it up
 126+ * Get the the URL in a way to de displayed to the user
 127+ * More or less Wikimedia specific
 128+ *
 129+ * @return String
90130 */
91 - function getDisplayName() {
 131+ public function getDisplayName() {
92132 $url = $this->getUrl( '' );
93133 $url = preg_replace( '!^https?://!', '', $url );
94134 $url = preg_replace( '!/index\.php(\?title=|/)$!', '/', $url );
@@ -96,14 +136,26 @@
97137 return $url;
98138 }
99139
 140+ /**
 141+ * Helper function for getUrl()
 142+ *
 143+ * @todo FIXME: this may be generalized...
 144+ * @param $page String: page name (must be normalised before calling this function!)
 145+ * @return String: Url fragment
 146+ */
100147 private function getLocalUrl( $page ) {
101 - // FIXME: this may be generalized...
102148 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
103149 }
104150
105 - function getUrl( $page ) {
 151+ /**
 152+ * Get a URL to a page on this foreign wiki
 153+ *
 154+ * @param $page String: page name (must be normalised before calling this function!)
 155+ * @return String: Url
 156+ */
 157+ public function getUrl( $page ) {
106158 return
107 - $this->mServer .
 159+ $this->mServer .
108160 $this->getLocalUrl( $page );
109161 }
110162 }
Index: trunk/phase3/includes/UserRightsProxy.php
@@ -1,31 +1,55 @@
22 <?php
33
4 -
54 /**
65 * Cut-down copy of User interface for local-interwiki-database
76 * user rights manipulation.
87 */
98 class UserRightsProxy {
 9+
 10+ /**
 11+ * Constructor.
 12+ *
 13+ * @see newFromId()
 14+ * @see newFromName()
 15+ * @param $db DatabaseBase: db connection
 16+ * @param $database String: database name
 17+ * @param $name String: user name
 18+ * @param $id Integer: user ID
 19+ */
1020 private function __construct( $db, $database, $name, $id ) {
1121 $this->db = $db;
1222 $this->database = $database;
1323 $this->name = $name;
1424 $this->id = intval( $id );
1525 }
16 -
 26+
 27+ /**
 28+ * Accessor for $this->database
 29+ *
 30+ * @return String: database name
 31+ */
1732 public function getDBName() {
1833 return $this->database;
1934 }
2035
2136 /**
2237 * Confirm the selected database name is a valid local interwiki database name.
23 - * @return bool
 38+ *
 39+ * @param $database String: database name
 40+ * @return Boolean
2441 */
2542 public static function validDatabase( $database ) {
2643 global $wgLocalDatabases;
2744 return in_array( $database, $wgLocalDatabases );
2845 }
2946
 47+ /**
 48+ * Same as User::whoIs()
 49+ *
 50+ * @param $database String: database name
 51+ * @param $id Integer: user ID
 52+ * @return String: user name or false if the user doesn't exist
 53+ */
3054 public static function whoIs( $database, $id ) {
3155 $user = self::newFromId( $database, $id );
3256 if( $user ) {
@@ -37,12 +61,22 @@
3862
3963 /**
4064 * Factory function; get a remote user entry by ID number.
 65+ *
 66+ * @param $database String: database name
 67+ * @param $id Integer: user ID
4168 * @return UserRightsProxy or null if doesn't exist
4269 */
4370 public static function newFromId( $database, $id ) {
4471 return self::newFromLookup( $database, 'user_id', intval( $id ) );
4572 }
4673
 74+ /**
 75+ * Factory function; get a remote user entry by name.
 76+ *
 77+ * @param $database String: database name
 78+ * @param $name String: user name
 79+ * @return UserRightsProxy or null if doesn't exist
 80+ */
4781 public static function newFromName( $database, $name ) {
4882 return self::newFromLookup( $database, 'user_name', $name );
4983 }
@@ -66,8 +100,9 @@
67101 /**
68102 * Open a database connection to work on for the requested user.
69103 * This may be a new connection to another database for remote users.
70 - * @param $database string
71 - * @return Database or null if invalid selection
 104+ *
 105+ * @param $database String
 106+ * @return DatabaseBase or null if invalid selection
72107 */
73108 public static function getDB( $database ) {
74109 global $wgLocalDatabases, $wgDBname;
@@ -90,15 +125,27 @@
91126 return $this->getId() == 0;
92127 }
93128
 129+ /**
 130+ * Same as User::getName()
 131+ *
 132+ * @return String
 133+ */
94134 public function getName() {
95135 return $this->name . '@' . $this->database;
96136 }
97137
 138+ /**
 139+ * Same as User::getUserPage()
 140+ *
 141+ * @return Title object
 142+ */
98143 public function getUserPage() {
99144 return Title::makeTitle( NS_USER, $this->getName() );
100145 }
101146
102 - // Replaces getUserGroups()
 147+ /**
 148+ * Replaces User::getUserGroups()
 149+ */
103150 function getGroups() {
104151 $res = $this->db->select( 'user_groups',
105152 array( 'ug_group' ),
@@ -111,7 +158,9 @@
112159 return $groups;
113160 }
114161
115 - // replaces addUserGroup
 162+ /**
 163+ * Replaces User::addUserGroup()
 164+ */
116165 function addGroup( $group ) {
117166 $this->db->insert( 'user_groups',
118167 array(
@@ -122,7 +171,9 @@
123172 array( 'IGNORE' ) );
124173 }
125174
126 - // replaces removeUserGroup
 175+ /**
 176+ * Replaces User::removeUserGroup()
 177+ */
127178 function removeGroup( $group ) {
128179 $this->db->delete( 'user_groups',
129180 array(
@@ -132,7 +183,9 @@
133184 __METHOD__ );
134185 }
135186
136 - // replaces touchUser
 187+ /**
 188+ * Replaces User::touchUser()
 189+ */
137190 function invalidateCache() {
138191 $this->db->update( 'user',
139192 array( 'user_touched' => $this->db->timestamp() ),

Status & tagging log