Index: trunk/phase3/includes/WikiMap.php |
— | — | @@ -3,11 +3,17 @@ |
4 | 4 | /** |
5 | 5 | * Helper tools for dealing with other locally-hosted wikis |
6 | 6 | */ |
| 7 | +class WikiMap { |
7 | 8 | |
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 ) { |
10 | 16 | global $wgConf, $IP; |
11 | | - |
| 17 | + |
12 | 18 | $wgConf->loadFullData(); |
13 | 19 | |
14 | 20 | list( $major, $minor ) = $wgConf->siteFromDB( $wikiID ); |
— | — | @@ -22,59 +28,90 @@ |
23 | 29 | } |
24 | 30 | } |
25 | 31 | |
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 ) { |
32 | 43 | return $wiki->getDisplayName(); |
33 | 44 | } |
34 | 45 | return $wiki_id; |
35 | 46 | } |
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 ); |
39 | 58 | } |
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 ) { |
42 | 69 | global $wgUser; |
43 | 70 | $sk = $wgUser->getSkin(); |
44 | 71 | |
45 | 72 | if ( !$text ) |
46 | | - $text=$page; |
| 73 | + $text = $page; |
47 | 74 | |
48 | | - $url = self::getForeignURL( $wiki_id, $page ); |
| 75 | + $url = self::getForeignURL( $wikiID, $page ); |
49 | 76 | if ( $url === false ) |
50 | 77 | return false; |
51 | 78 | |
52 | 79 | return $sk->makeExternalLink( $url, $text ); |
53 | 80 | } |
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 ); |
57 | 91 | |
58 | | - if ($wiki) |
| 92 | + if ( $wiki ) |
59 | 93 | return $wiki->getUrl( $page ); |
60 | 94 | |
61 | 95 | return false; |
62 | 96 | } |
63 | 97 | } |
64 | 98 | |
| 99 | +/** |
| 100 | + * Reference to a locally-hosted wiki |
| 101 | + */ |
65 | 102 | class WikiReference { |
66 | 103 | private $mMinor; ///< 'en', 'meta', 'mediawiki', etc |
67 | 104 | private $mMajor; ///< 'wiki', 'wiktionary', etc |
68 | 105 | private $mServer; ///< server override, 'www.mediawiki.org' |
69 | 106 | private $mPath; ///< path override, '/wiki/$1' |
70 | 107 | |
71 | | - function __construct( $major, $minor, $server, $path ) { |
| 108 | + public function __construct( $major, $minor, $server, $path ) { |
72 | 109 | $this->mMajor = $major; |
73 | 110 | $this->mMinor = $minor; |
74 | 111 | $this->mServer = $server; |
75 | 112 | $this->mPath = $path; |
76 | 113 | } |
77 | 114 | |
78 | | - function getHostname() { |
| 115 | + public function getHostname() { |
79 | 116 | $prefixes = array( 'http://', 'https://' ); |
80 | 117 | foreach ( $prefixes as $prefix ) { |
81 | 118 | if ( substr( $this->mServer, 0, strlen( $prefix ) ) ) { |
— | — | @@ -85,9 +122,12 @@ |
86 | 123 | } |
87 | 124 | |
88 | 125 | /** |
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 |
90 | 130 | */ |
91 | | - function getDisplayName() { |
| 131 | + public function getDisplayName() { |
92 | 132 | $url = $this->getUrl( '' ); |
93 | 133 | $url = preg_replace( '!^https?://!', '', $url ); |
94 | 134 | $url = preg_replace( '!/index\.php(\?title=|/)$!', '/', $url ); |
— | — | @@ -96,14 +136,26 @@ |
97 | 137 | return $url; |
98 | 138 | } |
99 | 139 | |
| 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 | + */ |
100 | 147 | private function getLocalUrl( $page ) { |
101 | | - // FIXME: this may be generalized... |
102 | 148 | return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath ); |
103 | 149 | } |
104 | 150 | |
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 ) { |
106 | 158 | return |
107 | | - $this->mServer . |
| 159 | + $this->mServer . |
108 | 160 | $this->getLocalUrl( $page ); |
109 | 161 | } |
110 | 162 | } |
Index: trunk/phase3/includes/UserRightsProxy.php |
— | — | @@ -1,31 +1,55 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | - |
5 | 4 | /** |
6 | 5 | * Cut-down copy of User interface for local-interwiki-database |
7 | 6 | * user rights manipulation. |
8 | 7 | */ |
9 | 8 | 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 | + */ |
10 | 20 | private function __construct( $db, $database, $name, $id ) { |
11 | 21 | $this->db = $db; |
12 | 22 | $this->database = $database; |
13 | 23 | $this->name = $name; |
14 | 24 | $this->id = intval( $id ); |
15 | 25 | } |
16 | | - |
| 26 | + |
| 27 | + /** |
| 28 | + * Accessor for $this->database |
| 29 | + * |
| 30 | + * @return String: database name |
| 31 | + */ |
17 | 32 | public function getDBName() { |
18 | 33 | return $this->database; |
19 | 34 | } |
20 | 35 | |
21 | 36 | /** |
22 | 37 | * 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 |
24 | 41 | */ |
25 | 42 | public static function validDatabase( $database ) { |
26 | 43 | global $wgLocalDatabases; |
27 | 44 | return in_array( $database, $wgLocalDatabases ); |
28 | 45 | } |
29 | 46 | |
| 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 | + */ |
30 | 54 | public static function whoIs( $database, $id ) { |
31 | 55 | $user = self::newFromId( $database, $id ); |
32 | 56 | if( $user ) { |
— | — | @@ -37,12 +61,22 @@ |
38 | 62 | |
39 | 63 | /** |
40 | 64 | * Factory function; get a remote user entry by ID number. |
| 65 | + * |
| 66 | + * @param $database String: database name |
| 67 | + * @param $id Integer: user ID |
41 | 68 | * @return UserRightsProxy or null if doesn't exist |
42 | 69 | */ |
43 | 70 | public static function newFromId( $database, $id ) { |
44 | 71 | return self::newFromLookup( $database, 'user_id', intval( $id ) ); |
45 | 72 | } |
46 | 73 | |
| 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 | + */ |
47 | 81 | public static function newFromName( $database, $name ) { |
48 | 82 | return self::newFromLookup( $database, 'user_name', $name ); |
49 | 83 | } |
— | — | @@ -66,8 +100,9 @@ |
67 | 101 | /** |
68 | 102 | * Open a database connection to work on for the requested user. |
69 | 103 | * 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 |
72 | 107 | */ |
73 | 108 | public static function getDB( $database ) { |
74 | 109 | global $wgLocalDatabases, $wgDBname; |
— | — | @@ -90,15 +125,27 @@ |
91 | 126 | return $this->getId() == 0; |
92 | 127 | } |
93 | 128 | |
| 129 | + /** |
| 130 | + * Same as User::getName() |
| 131 | + * |
| 132 | + * @return String |
| 133 | + */ |
94 | 134 | public function getName() { |
95 | 135 | return $this->name . '@' . $this->database; |
96 | 136 | } |
97 | 137 | |
| 138 | + /** |
| 139 | + * Same as User::getUserPage() |
| 140 | + * |
| 141 | + * @return Title object |
| 142 | + */ |
98 | 143 | public function getUserPage() { |
99 | 144 | return Title::makeTitle( NS_USER, $this->getName() ); |
100 | 145 | } |
101 | 146 | |
102 | | - // Replaces getUserGroups() |
| 147 | + /** |
| 148 | + * Replaces User::getUserGroups() |
| 149 | + */ |
103 | 150 | function getGroups() { |
104 | 151 | $res = $this->db->select( 'user_groups', |
105 | 152 | array( 'ug_group' ), |
— | — | @@ -111,7 +158,9 @@ |
112 | 159 | return $groups; |
113 | 160 | } |
114 | 161 | |
115 | | - // replaces addUserGroup |
| 162 | + /** |
| 163 | + * Replaces User::addUserGroup() |
| 164 | + */ |
116 | 165 | function addGroup( $group ) { |
117 | 166 | $this->db->insert( 'user_groups', |
118 | 167 | array( |
— | — | @@ -122,7 +171,9 @@ |
123 | 172 | array( 'IGNORE' ) ); |
124 | 173 | } |
125 | 174 | |
126 | | - // replaces removeUserGroup |
| 175 | + /** |
| 176 | + * Replaces User::removeUserGroup() |
| 177 | + */ |
127 | 178 | function removeGroup( $group ) { |
128 | 179 | $this->db->delete( 'user_groups', |
129 | 180 | array( |
— | — | @@ -132,7 +183,9 @@ |
133 | 184 | __METHOD__ ); |
134 | 185 | } |
135 | 186 | |
136 | | - // replaces touchUser |
| 187 | + /** |
| 188 | + * Replaces User::touchUser() |
| 189 | + */ |
137 | 190 | function invalidateCache() { |
138 | 191 | $this->db->update( 'user', |
139 | 192 | array( 'user_touched' => $this->db->timestamp() ), |