rdbms: Avoid selectDB() call in LoadMonitor new connections
[mediawiki.git] / includes / WikiMap.php
blob6a532e5da5f3a748a9d32979ff103ddf11bf8cf8
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 [ 'lang' => $minor, 'site' => $major ] );
60 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
61 [ 'lang' => $minor, 'site' => $major ] );
62 if ( $canonicalServer === false || $canonicalServer === null ) {
63 $canonicalServer = $server;
66 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
67 [ 'lang' => $minor, 'site' => $major ] );
69 // If we don't have a canonical server or a path containing $1, the
70 // WikiReference isn't going to function properly. Just return null in
71 // that case.
72 if ( !is_string( $canonicalServer ) || !is_string( $path ) || strpos( $path, '$1' ) === false ) {
73 return null;
76 return new WikiReference( $canonicalServer, $path, $server );
79 /**
80 * @param string $wikiID
81 * @return WikiReference|null WikiReference object or null if the wiki was not found
83 private static function getWikiWikiReferenceFromSites( $wikiID ) {
84 $siteLookup = \MediaWiki\MediaWikiServices::getInstance()->getSiteLookup();
85 $site = $siteLookup->getSite( $wikiID );
87 if ( !$site instanceof MediaWikiSite ) {
88 // Abort if not a MediaWikiSite, as this is about Wikis
89 return null;
92 $urlParts = wfParseUrl( $site->getPageUrl() );
93 if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
94 // We can't create a meaningful WikiReference without URLs
95 return null;
98 // XXX: Check whether path contains a $1?
99 $path = $urlParts['path'];
100 if ( isset( $urlParts['query'] ) ) {
101 $path .= '?' . $urlParts['query'];
104 $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
105 $canonicalServer .= '://' . $urlParts['host'];
107 return new WikiReference( $canonicalServer, $path );
111 * Convenience to get the wiki's display name
113 * @todo We can give more info than just the wiki id!
114 * @param string $wikiID Wiki'd id (generally database name)
115 * @return string|int Wiki's name or $wiki_id if the wiki was not found
117 public static function getWikiName( $wikiID ) {
118 $wiki = self::getWiki( $wikiID );
120 if ( $wiki ) {
121 return $wiki->getDisplayName();
123 return $wikiID;
127 * Convenience to get a link to a user page on a foreign wiki
129 * @param string $wikiID Wiki'd id (generally database name)
130 * @param string $user User name (must be normalised before calling this function!)
131 * @param string $text Link's text; optional, default to "User:$user"
132 * @return string HTML link or false if the wiki was not found
134 public static function foreignUserLink( $wikiID, $user, $text = null ) {
135 return self::makeForeignLink( $wikiID, "User:$user", $text );
139 * Convenience to get a link to a page on a foreign wiki
141 * @param string $wikiID Wiki'd id (generally database name)
142 * @param string $page Page name (must be normalised before calling this function!)
143 * @param string $text Link's text; optional, default to $page
144 * @return string|false HTML link or false if the wiki was not found
146 public static function makeForeignLink( $wikiID, $page, $text = null ) {
147 if ( !$text ) {
148 $text = $page;
151 $url = self::getForeignURL( $wikiID, $page );
152 if ( $url === false ) {
153 return false;
156 return Linker::makeExternalLink( $url, $text );
160 * Convenience to get a url to a page on a foreign wiki
162 * @param string $wikiID Wiki'd id (generally database name)
163 * @param string $page Page name (must be normalised before calling this function!)
164 * @param string|null $fragmentId
166 * @return string|bool URL or false if the wiki was not found
168 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
169 $wiki = self::getWiki( $wikiID );
171 if ( $wiki ) {
172 return $wiki->getFullUrl( $page, $fragmentId );
175 return false;