Remove Profiler specific code from fileOpPerfTest
[mediawiki.git] / includes / WikiMap.php
blobcf97984ce17b25ebd5be6cf65860510fc22c89c8
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 ] );
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 $siteLookup = \MediaWiki\MediaWikiServices::getInstance()->getSiteLookup();
77 $site = $siteLookup->getSite( $wikiID );
79 if ( !$site instanceof MediaWikiSite ) {
80 // Abort if not a MediaWikiSite, as this is about Wikis
81 return null;
84 $urlParts = wfParseUrl( $site->getPageUrl() );
85 if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
86 // We can't create a meaningful WikiReference without URLs
87 return null;
90 // XXX: Check whether path contains a $1?
91 $path = $urlParts['path'];
92 if ( isset( $urlParts['query'] ) ) {
93 $path .= '?' . $urlParts['query'];
96 $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
97 $canonicalServer .= '://' . $urlParts['host'];
99 return new WikiReference( $canonicalServer, $path );
103 * Convenience to get the wiki's display name
105 * @todo We can give more info than just the wiki id!
106 * @param string $wikiID Wiki'd id (generally database name)
107 * @return string|int Wiki's name or $wiki_id if the wiki was not found
109 public static function getWikiName( $wikiID ) {
110 $wiki = WikiMap::getWiki( $wikiID );
112 if ( $wiki ) {
113 return $wiki->getDisplayName();
115 return $wikiID;
119 * Convenience to get a link to a user page on a foreign wiki
121 * @param string $wikiID Wiki'd id (generally database name)
122 * @param string $user User name (must be normalised before calling this function!)
123 * @param string $text Link's text; optional, default to "User:$user"
124 * @return string HTML link or false if the wiki was not found
126 public static function foreignUserLink( $wikiID, $user, $text = null ) {
127 return self::makeForeignLink( $wikiID, "User:$user", $text );
131 * Convenience to get a link to a page on a foreign wiki
133 * @param string $wikiID Wiki'd id (generally database name)
134 * @param string $page Page name (must be normalised before calling this function!)
135 * @param string $text Link's text; optional, default to $page
136 * @return string HTML link or false if the wiki was not found
138 public static function makeForeignLink( $wikiID, $page, $text = null ) {
139 if ( !$text ) {
140 $text = $page;
143 $url = self::getForeignURL( $wikiID, $page );
144 if ( $url === false ) {
145 return false;
148 return Linker::makeExternalLink( $url, $text );
152 * Convenience to get a url to a page on a foreign wiki
154 * @param string $wikiID Wiki'd id (generally database name)
155 * @param string $page Page name (must be normalised before calling this function!)
156 * @param string|null $fragmentId
158 * @return string|bool URL or false if the wiki was not found
160 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
161 $wiki = WikiMap::getWiki( $wikiID );
163 if ( $wiki ) {
164 return $wiki->getFullUrl( $page, $fragmentId );
167 return false;
172 * Reference to a locally-hosted wiki
174 class WikiReference {
175 private $mCanonicalServer; ///< canonical server URL, e.g. 'https://www.mediawiki.org'
176 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
177 private $mPath; ///< path, '/wiki/$1'
180 * @param string $canonicalServer
181 * @param string $path
182 * @param null|string $server
184 public function __construct( $canonicalServer, $path, $server = null ) {
185 $this->mCanonicalServer = $canonicalServer;
186 $this->mPath = $path;
187 $this->mServer = $server === null ? $canonicalServer : $server;
191 * Get the URL in a way to be displayed to the user
192 * More or less Wikimedia specific
194 * @return string
196 public function getDisplayName() {
197 $parsed = wfParseUrl( $this->mCanonicalServer );
198 if ( $parsed ) {
199 return $parsed['host'];
200 } else {
201 // Invalid server spec.
202 // There's no sane thing to do here, so just return the canonical server name in full.
203 return $this->mCanonicalServer;
208 * Helper function for getUrl()
210 * @todo FIXME: This may be generalized...
212 * @param string $page Page name (must be normalised before calling this function!
213 * May contain a section part.)
214 * @param string|null $fragmentId
216 * @return string relative URL, without the server part.
218 private function getLocalUrl( $page, $fragmentId = null ) {
219 $page = wfUrlencode( str_replace( ' ', '_', $page ) );
221 if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
222 $page .= '#' . wfUrlencode( $fragmentId );
225 return str_replace( '$1', $page, $this->mPath );
229 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
231 * @param string $page Page name (must be normalised before calling this function!)
232 * @param string|null $fragmentId
234 * @return string Url
236 public function getCanonicalUrl( $page, $fragmentId = null ) {
237 return $this->mCanonicalServer . $this->getLocalUrl( $page, $fragmentId );
241 * Get a canonical server URL
242 * @return string
244 public function getCanonicalServer() {
245 return $this->mCanonicalServer;
249 * Alias for getCanonicalUrl(), for backwards compatibility.
250 * @param string $page
251 * @param string|null $fragmentId
253 * @return string
255 public function getUrl( $page, $fragmentId = null ) {
256 return $this->getCanonicalUrl( $page, $fragmentId );
260 * Get a URL based on $wgServer, like Title::getFullURL() would produce
261 * when called locally on the wiki.
263 * @param string $page Page name (must be normalized before calling this function!)
264 * @param string|null $fragmentId
266 * @return string URL
268 public function getFullUrl( $page, $fragmentId = null ) {
269 return $this->mServer .
270 $this->getLocalUrl( $page, $fragmentId );