Merge "Make line breaks in <blockquote> behave like <div> (bug 6200)."
[mediawiki.git] / includes / WikiMap.php
blobda4416de31d73e757da357b7c3135c3e1a215ac4
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 $server = $wgConf->get( 'wgServer', $wikiID, $major,
44 array( 'lang' => $minor, 'site' => $major ) );
46 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
47 array( 'lang' => $minor, 'site' => $major ) );
48 if ( $canonicalServer === false || $canonicalServer === null ) {
49 $canonicalServer = $server;
52 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
53 array( 'lang' => $minor, 'site' => $major ) );
54 return new WikiReference( $major, $minor, $canonicalServer, $path, $server );
57 /**
58 * Convenience to get the wiki's display name
60 * @todo We can give more info than just the wiki id!
61 * @param string $wikiID wiki'd id (generally database name)
62 * @return string|int Wiki's name or $wiki_id if the wiki was not found
64 public static function getWikiName( $wikiID ) {
65 $wiki = WikiMap::getWiki( $wikiID );
67 if ( $wiki ) {
68 return $wiki->getDisplayName();
70 return $wikiID;
73 /**
74 * Convenience to get a link to a user page on a foreign wiki
76 * @param string $wikiID wiki'd id (generally database name)
77 * @param string $user user name (must be normalised before calling this function!)
78 * @param string $text link's text; optional, default to "User:$user"
79 * @return String: HTML link or false if the wiki was not found
81 public static function foreignUserLink( $wikiID, $user, $text = null ) {
82 return self::makeForeignLink( $wikiID, "User:$user", $text );
85 /**
86 * Convenience to get a link to a page on a foreign wiki
88 * @param string $wikiID wiki'd id (generally database name)
89 * @param string $page page name (must be normalised before calling this function!)
90 * @param string $text link's text; optional, default to $page
91 * @return String: HTML link or false if the wiki was not found
93 public static function makeForeignLink( $wikiID, $page, $text = null ) {
94 if ( !$text ) {
95 $text = $page;
98 $url = self::getForeignURL( $wikiID, $page );
99 if ( $url === false ) {
100 return false;
103 return Linker::makeExternalLink( $url, $text );
107 * Convenience to get a url to a page on a foreign wiki
109 * @param string $wikiID wiki'd id (generally database name)
110 * @param string $page page name (must be normalised before calling this function!)
111 * @return String: URL or false if the wiki was not found
113 public static function getForeignURL( $wikiID, $page ) {
114 $wiki = WikiMap::getWiki( $wikiID );
116 if ( $wiki ) {
117 return $wiki->getFullUrl( $page );
120 return false;
125 * Reference to a locally-hosted wiki
127 class WikiReference {
128 private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
129 private $mMajor; ///< 'wiki', 'wiktionary', etc
130 private $mCanonicalServer; ///< canonical server URL, e.g. 'http://www.mediawiki.org'
131 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
132 private $mPath; ///< path, '/wiki/$1'
135 * @param $major string
136 * @param $minor string
137 * @param $canonicalServer string
138 * @param $path string
139 * @param $server null|string
141 public function __construct( $major, $minor, $canonicalServer, $path, $server = null ) {
142 $this->mMajor = $major;
143 $this->mMinor = $minor;
144 $this->mCanonicalServer = $canonicalServer;
145 $this->mPath = $path;
146 $this->mServer = $server === null ? $canonicalServer : $server;
150 * @return string
151 * @throws MWException
153 public function getHostname() {
154 $prefixes = array( 'http://', 'https://' );
155 foreach ( $prefixes as $prefix ) {
156 if ( substr( $this->mCanonicalServer, 0, strlen( $prefix ) ) ) {
157 return substr( $this->mCanonicalServer, strlen( $prefix ) );
160 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
164 * Get the the URL in a way to de displayed to the user
165 * More or less Wikimedia specific
167 * @return String
169 public function getDisplayName() {
170 $url = $this->getUrl( '' );
171 $parsed = wfParseUrl( $url );
172 if ( $parsed ) {
173 return $parsed['host'];
174 } else {
175 // Invalid URL. There's no sane thing to do here, so just return it
176 return $url;
181 * Helper function for getUrl()
183 * @todo FIXME: This may be generalized...
184 * @param string $page page name (must be normalised before calling this function!)
185 * @return String: Url fragment
187 private function getLocalUrl( $page ) {
188 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
192 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
194 * @param string $page page name (must be normalised before calling this function!)
195 * @return String: Url
197 public function getCanonicalUrl( $page ) {
198 return $this->mCanonicalServer . $this->getLocalUrl( $page );
202 * Get a canonical server URL
203 * @return string
205 public function getCanonicalServer() {
206 return $this->mCanonicalServer;
210 * Alias for getCanonicalUrl(), for backwards compatibility.
211 * @param $page string
212 * @return String
214 public function getUrl( $page ) {
215 return $this->getCanonicalUrl( $page );
219 * Get a URL based on $wgServer, like Title::getFullURL() would produce
220 * when called locally on the wiki.
222 * @param string $page page name (must be normalized before calling this function!)
223 * @return String: URL
225 public function getFullUrl( $page ) {
226 return $this->mServer .
227 $this->getLocalUrl( $page );