Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / WikiMap.php
blob325831eef0ab35fc3f037a59c412d077452c8096
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 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|null WikiReference object or null if the wiki was not found
34 public static function getWiki( $wikiID ) {
35 $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
36 if ( $wikiReference ) {
37 return $wikiReference;
40 // Try sites, if $wgConf failed
41 return self::getWikiWikiReferenceFromSites( $wikiID );
44 /**
45 * @param string $wikiID
46 * @return WikiReference|null WikiReference object or null if the wiki was not found
48 private static function getWikiReferenceFromWgConf( $wikiID ) {
49 global $wgConf;
51 $wgConf->loadFullData();
53 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
54 if ( $major === null ) {
55 return null;
57 $server = $wgConf->get( 'wgServer', $wikiID, $major,
58 array( 'lang' => $minor, 'site' => $major ) );
60 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
61 array( 'lang' => $minor, 'site' => $major ) );
62 if ( $canonicalServer === false || $canonicalServer === null ) {
63 $canonicalServer = $server;
66 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
67 array( 'lang' => $minor, 'site' => $major ) );
68 return new WikiReference( $canonicalServer, $path, $server );
71 /**
72 * @param string $wikiID
73 * @return WikiReference|null WikiReference object or null if the wiki was not found
75 private static function getWikiWikiReferenceFromSites( $wikiID ) {
76 static $siteStore = null;
77 if ( !$siteStore ) {
78 // Replace once T114471 got fixed and don't do the caching here.
79 $siteStore = SiteSQLStore::newInstance();
82 $site = $siteStore->getSite( $wikiID );
84 if ( !$site instanceof MediaWikiSite ) {
85 // Abort if not a MediaWikiSite, as this is about Wikis
86 return null;
89 $urlParts = wfParseUrl( $site->getPageUrl() );
90 if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
91 // We can't create a meaningful WikiReference without URLs
92 return null;
95 // XXX: Check whether path contains a $1?
96 $path = $urlParts['path'];
97 if ( isset( $urlParts['query'] ) ) {
98 $path .= '?' . $urlParts['query'];
101 $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
102 $canonicalServer .= '://' . $urlParts['host'];
104 return new WikiReference( $canonicalServer, $path );
108 * Convenience to get the wiki's display name
110 * @todo We can give more info than just the wiki id!
111 * @param string $wikiID Wiki'd id (generally database name)
112 * @return string|int Wiki's name or $wiki_id if the wiki was not found
114 public static function getWikiName( $wikiID ) {
115 $wiki = WikiMap::getWiki( $wikiID );
117 if ( $wiki ) {
118 return $wiki->getDisplayName();
120 return $wikiID;
124 * Convenience to get a link to a user page on a foreign wiki
126 * @param string $wikiID Wiki'd id (generally database name)
127 * @param string $user User name (must be normalised before calling this function!)
128 * @param string $text Link's text; optional, default to "User:$user"
129 * @return string HTML link or false if the wiki was not found
131 public static function foreignUserLink( $wikiID, $user, $text = null ) {
132 return self::makeForeignLink( $wikiID, "User:$user", $text );
136 * Convenience to get a link to a page on a foreign wiki
138 * @param string $wikiID Wiki'd id (generally database name)
139 * @param string $page Page name (must be normalised before calling this function!)
140 * @param string $text Link's text; optional, default to $page
141 * @return string HTML link or false if the wiki was not found
143 public static function makeForeignLink( $wikiID, $page, $text = null ) {
144 if ( !$text ) {
145 $text = $page;
148 $url = self::getForeignURL( $wikiID, $page );
149 if ( $url === false ) {
150 return false;
153 return Linker::makeExternalLink( $url, $text );
157 * Convenience to get a url to a page on a foreign wiki
159 * @param string $wikiID Wiki'd id (generally database name)
160 * @param string $page Page name (must be normalised before calling this function!)
161 * @param string|null $fragmentId
163 * @return string|bool URL or false if the wiki was not found
165 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
166 $wiki = WikiMap::getWiki( $wikiID );
168 if ( $wiki ) {
169 return $wiki->getFullUrl( $page, $fragmentId );
172 return false;
177 * Reference to a locally-hosted wiki
179 class WikiReference {
180 private $mCanonicalServer; ///< canonical server URL, e.g. 'https://www.mediawiki.org'
181 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
182 private $mPath; ///< path, '/wiki/$1'
185 * @param string $canonicalServer
186 * @param string $path
187 * @param null|string $server
189 public function __construct( $canonicalServer, $path, $server = null ) {
190 $this->mCanonicalServer = $canonicalServer;
191 $this->mPath = $path;
192 $this->mServer = $server === null ? $canonicalServer : $server;
196 * Get the URL in a way to be displayed to the user
197 * More or less Wikimedia specific
199 * @return string
201 public function getDisplayName() {
202 $parsed = wfParseUrl( $this->mCanonicalServer );
203 if ( $parsed ) {
204 return $parsed['host'];
205 } else {
206 // Invalid server spec.
207 // There's no sane thing to do here, so just return the canonical server name in full.
208 return $this->mCanonicalServer;
213 * Helper function for getUrl()
215 * @todo FIXME: This may be generalized...
217 * @param string $page Page name (must be normalised before calling this function!
218 * May contain a section part.)
219 * @param string|null $fragmentId
221 * @return string relative URL, without the server part.
223 private function getLocalUrl( $page, $fragmentId = null ) {
224 $page = wfUrlEncode( str_replace( ' ', '_', $page ) );
226 if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
227 $page .= '#' . wfUrlEncode( $fragmentId );
230 return str_replace( '$1', $page, $this->mPath );
234 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
236 * @param string $page Page name (must be normalised before calling this function!)
237 * @param string|null $fragmentId
239 * @return string Url
241 public function getCanonicalUrl( $page, $fragmentId = null ) {
242 return $this->mCanonicalServer . $this->getLocalUrl( $page, $fragmentId );
246 * Get a canonical server URL
247 * @return string
249 public function getCanonicalServer() {
250 return $this->mCanonicalServer;
254 * Alias for getCanonicalUrl(), for backwards compatibility.
255 * @param string $page
256 * @param string|null $fragmentId
258 * @return string
260 public function getUrl( $page, $fragmentId = null ) {
261 return $this->getCanonicalUrl( $page, $fragmentId );
265 * Get a URL based on $wgServer, like Title::getFullURL() would produce
266 * when called locally on the wiki.
268 * @param string $page Page name (must be normalized before calling this function!)
269 * @param string|null $fragmentId
271 * @return string URL
273 public function getFullUrl( $page, $fragmentId = null ) {
274 return $this->mServer .
275 $this->getLocalUrl( $page, $fragmentId );