Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / WikiMap.php
blob5d90b127c29d2609a4d761f311baccd9aa6010e8
1 <?php
2 /**
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
20 * @file
23 /**
24 * Helper tools for dealing with other locally-hosted wikis
26 class WikiMap {
28 /**
29 * Get a WikiReference object for $wikiID
31 * @param string $wikiID wiki'd id (generally database name)
32 * @return WikiReference object or null if the wiki was not found
34 public static function getWiki( $wikiID ) {
35 global $wgConf;
37 $wgConf->loadFullData();
39 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
40 if ( $major === null ) {
41 return 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 );
52 /**
53 * Convenience to get the wiki's display name
55 * @todo We can give more info than just the wiki id!
56 * @param string $wikiID 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 );
62 if ( $wiki ) {
63 return $wiki->getDisplayName();
65 return $wikiID;
68 /**
69 * Convenience to get a link to a user page on a foreign wiki
71 * @param string $wikiID wiki'd id (generally database name)
72 * @param string $user user name (must be normalised before calling this function!)
73 * @param string $text 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 );
80 /**
81 * Convenience to get a link to a page on a foreign wiki
83 * @param string $wikiID wiki'd id (generally database name)
84 * @param string $page page name (must be normalised before calling this function!)
85 * @param string $text 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 ) {
89 if ( !$text ) {
90 $text = $page;
93 $url = self::getForeignURL( $wikiID, $page );
94 if ( $url === false ) {
95 return false;
98 return Linker::makeExternalLink( $url, $text );
102 * Convenience to get a url to a page on a foreign wiki
104 * @param string $wikiID wiki'd id (generally database name)
105 * @param string $page 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 );
111 if ( $wiki ) {
112 return $wiki->getFullUrl( $page );
115 return false;
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'
130 * @param $major string
131 * @param $minor string
132 * @param $canonicalServer string
133 * @param $path string
134 * @param $server null|string
136 public function __construct( $major, $minor, $canonicalServer, $path, $server = null ) {
137 $this->mMajor = $major;
138 $this->mMinor = $minor;
139 $this->mCanonicalServer = $canonicalServer;
140 $this->mPath = $path;
141 $this->mServer = $server === null ? $canonicalServer : $server;
145 * @return string
146 * @throws MWException
148 public function getHostname() {
149 $prefixes = array( 'http://', 'https://' );
150 foreach ( $prefixes as $prefix ) {
151 if ( substr( $this->mCanonicalServer, 0, strlen( $prefix ) ) ) {
152 return substr( $this->mCanonicalServer, strlen( $prefix ) );
155 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
159 * Get the the URL in a way to de displayed to the user
160 * More or less Wikimedia specific
162 * @return String
164 public function getDisplayName() {
165 $url = $this->getUrl( '' );
166 $parsed = wfParseUrl( $url );
167 if ( $parsed ) {
168 return $parsed['host'];
169 } else {
170 // Invalid URL. There's no sane thing to do here, so just return it
171 return $url;
176 * Helper function for getUrl()
178 * @todo FIXME: This may be generalized...
179 * @param string $page page name (must be normalised before calling this function!)
180 * @return String: Url fragment
182 private function getLocalUrl( $page ) {
183 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
187 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
189 * @param string $page page name (must be normalised before calling this function!)
190 * @return String: Url
192 public function getCanonicalUrl( $page ) {
193 return $this->mCanonicalServer . $this->getLocalUrl( $page );
197 * Get a canonical server URL
198 * @return string
200 public function getCanonicalServer() {
201 return $this->mCanonicalServer;
205 * Alias for getCanonicalUrl(), for backwards compatibility.
206 * @param $page string
207 * @return String
209 public function getUrl( $page ) {
210 return $this->getCanonicalUrl( $page );
214 * Get a URL based on $wgServer, like Title::getFullURL() would produce
215 * when called locally on the wiki.
217 * @param string $page page name (must be normalized before calling this function!)
218 * @return String: URL
220 public function getFullUrl( $page ) {
221 return $this->mServer .
222 $this->getLocalUrl( $page );