3 * Tools for dealing with other locally-hosted wikis.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Helper tools for dealing with other locally-hosted wikis
29 * Get a WikiReference object for $wikiID
31 * @param $wikiID String: wiki'd id (generally database name)
32 * @return WikiReference object or null if the wiki was not found
34 public static function getWiki( $wikiID ) {
37 $wgConf->loadFullData();
39 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
40 if( $major === null ) {
43 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
44 array( 'lang' => $minor, 'site' => $major ) );
45 $server = $wgConf->get( 'wgServer', $wikiID, $major,
46 array( 'lang' => $minor, 'site' => $major ) );
47 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
48 array( 'lang' => $minor, 'site' => $major ) );
49 return new WikiReference( $major, $minor, $canonicalServer, $path, $server );
53 * Convenience to get the wiki's display name
55 * @todo We can give more info than just the wiki id!
56 * @param $wikiID String: wiki'd id (generally database name)
57 * @return string|int Wiki's name or $wiki_id if the wiki was not found
59 public static function getWikiName( $wikiID ) {
60 $wiki = WikiMap
::getWiki( $wikiID );
63 return $wiki->getDisplayName();
69 * Convenience to get a link to a user page on a foreign wiki
71 * @param $wikiID String: wiki'd id (generally database name)
72 * @param $user String: user name (must be normalised before calling this function!)
73 * @param $text String: link's text; optional, default to "User:$user"
74 * @return String: HTML link or false if the wiki was not found
76 public static function foreignUserLink( $wikiID, $user, $text=null ) {
77 return self
::makeForeignLink( $wikiID, "User:$user", $text );
81 * Convenience to get a link to a page on a foreign wiki
83 * @param $wikiID String: wiki'd id (generally database name)
84 * @param $page String: page name (must be normalised before calling this function!)
85 * @param $text String: link's text; optional, default to $page
86 * @return String: HTML link or false if the wiki was not found
88 public static function makeForeignLink( $wikiID, $page, $text=null ) {
93 $url = self
::getForeignURL( $wikiID, $page );
94 if ( $url === false ) {
98 return Linker
::makeExternalLink( $url, $text );
102 * Convenience to get a url to a page on a foreign wiki
104 * @param $wikiID String: wiki'd id (generally database name)
105 * @param $page String: page name (must be normalised before calling this function!)
106 * @return String: URL or false if the wiki was not found
108 public static function getForeignURL( $wikiID, $page ) {
109 $wiki = WikiMap
::getWiki( $wikiID );
112 return $wiki->getUrl( $page );
120 * Reference to a locally-hosted wiki
122 class WikiReference
{
123 private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
124 private $mMajor; ///< 'wiki', 'wiktionary', etc
125 private $mCanonicalServer; ///< canonical server URL, e.g. 'http://www.mediawiki.org'
126 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
127 private $mPath; ///< path, '/wiki/$1'
129 public function __construct( $major, $minor, $canonicalServer, $path, $server = null ) {
130 $this->mMajor
= $major;
131 $this->mMinor
= $minor;
132 $this->mCanonicalServer
= $canonicalServer;
133 $this->mPath
= $path;
134 $this->mServer
= $server === null ?
$canonicalServer : $server;
139 * @throws MWException
141 public function getHostname() {
142 $prefixes = array( 'http://', 'https://' );
143 foreach ( $prefixes as $prefix ) {
144 if ( substr( $this->mCanonicalServer
, 0, strlen( $prefix ) ) ) {
145 return substr( $this->mCanonicalServer
, strlen( $prefix ) );
148 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
152 * Get the the URL in a way to de displayed to the user
153 * More or less Wikimedia specific
157 public function getDisplayName() {
158 $url = $this->getUrl( '' );
159 $parsed = wfParseUrl( $url );
161 return $parsed['host'];
163 // Invalid URL. There's no sane thing to do here, so just return it
169 * Helper function for getUrl()
171 * @todo FIXME: This may be generalized...
172 * @param $page String: page name (must be normalised before calling this function!)
173 * @return String: Url fragment
175 private function getLocalUrl( $page ) {
176 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath
);
180 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
182 * @param $page String: page name (must be normalised before calling this function!)
183 * @return String: Url
185 public function getCanonicalUrl( $page ) {
186 return $this->mCanonicalServer
. $this->getLocalUrl( $page );
190 * Alias for getCanonicalUrl(), for backwards compatibility.
193 public function getUrl( $page ) {
194 return $this->getCanonicalUrl( $page );
198 * Get a URL based on $wgServer, like Title::getFullUrl() would produce
199 * when called locally on the wiki.
201 * @param $page String: page name (must be normalized before calling this function!)
202 * @return String: URL
204 public function getFullUrl( $page ) {
207 $this->getLocalUrl( $page );