MediaWiki master
WikiMap.php
Go to the documentation of this file.
1<?php
8
13
19class WikiMap {
20
27 public static function getWiki( $wikiID ) {
28 if ( $wikiID === self::getCurrentWikiId() ) {
31 }
32
33 $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
34 if ( $wikiReference ) {
35 return $wikiReference;
36 }
37
38 // Try sites, if $wgConf failed
39 return self::getWikiWikiReferenceFromSites( $wikiID );
40 }
41
46 private static function getWikiReferenceFromWgConf( $wikiID ) {
47 global $wgConf;
48 '@phan-var \MediaWiki\Config\SiteConfiguration $wgConf';
49
50 $wgConf->loadFullData();
51
52 // FIXME: T417511
53 [ $major, $minor ] = $wgConf->siteFromDB( $wikiID );
54 if ( $major === null ) {
55 return null;
56 }
57 $server = $wgConf->get( 'wgServer', $wikiID, $major,
58 [ 'lang' => $minor, 'site' => $major ] );
59
60 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
61 [ 'lang' => $minor, 'site' => $major ] );
62 if ( $canonicalServer === false || $canonicalServer === null ) {
63 $canonicalServer = $server;
64 }
65
66 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
67 [ 'lang' => $minor, 'site' => $major ] );
68
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 ) || !str_contains( $path, '$1' ) ) {
73 return null;
74 }
75
76 return new WikiReference( $canonicalServer, $path, $server );
77 }
78
83 private static function getWikiWikiReferenceFromSites( $wikiID ) {
84 $siteLookup = MediaWikiServices::getInstance()->getSiteLookup();
85 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
86 $site = $siteLookup->getSite( $wikiID );
87
88 if ( !$site instanceof MediaWikiSite ) {
89 // Abort if not a MediaWikiSite, as this is about Wikis
90 return null;
91 }
92
93 $urlParts = $urlUtils->parse( $site->getPageUrl() );
94 if ( $urlParts === null || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
95 // We can't create a meaningful WikiReference without URLs
96 return null;
97 }
98
99 // XXX: Check whether path contains a $1?
100 $path = $urlParts['path'];
101 if ( isset( $urlParts['query'] ) ) {
102 $path .= '?' . $urlParts['query'];
103 }
104
105 $canonicalServer = $urlParts['scheme'] ?? 'http';
106 $canonicalServer .= '://' . $urlParts['host'];
107 if ( isset( $urlParts['port'] ) ) {
108 $canonicalServer .= ':' . $urlParts['port'];
109 }
110
111 return new WikiReference( $canonicalServer, $path );
112 }
113
121 public static function getWikiName( $wikiID ) {
122 $wiki = self::getWiki( $wikiID );
123 return $wiki ? $wiki->getDisplayName() : $wikiID;
124 }
125
134 public static function foreignUserLink( $wikiID, $user, $text = null ) {
135 return self::makeForeignLink( $wikiID, "User:$user", $text );
136 }
137
146 public static function makeForeignLink( $wikiID, $page, $text = null ) {
147 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle
148 global $wgTitle;
149 if ( !$text ) {
150 $text = $page;
151 }
152
153 $url = self::getForeignURL( $wikiID, $page );
154 if ( $url === false ) {
155 return false;
156 }
157
158 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
159 return $linkRenderer->makeExternalLink(
160 $url,
161 $text,
162 $wgTitle ?? SpecialPage::getTitleFor( 'Badtitle' )
163 );
164 }
165
175 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
176 $wiki = self::getWiki( $wikiID );
177
178 if ( $wiki ) {
179 return $wiki->getFullUrl( $page, $fragmentId );
180 }
181
182 return false;
183 }
184
192 public static function getCanonicalServerInfoForAllWikis() {
193 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
194
195 return $cache->getWithSetCallback(
196 $cache->makeGlobalKey( 'wikimap-canonical-urls' ),
197 $cache::TTL_DAY,
198 static function () {
200
201 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
202
203 $infoMap = [];
204 // Make sure at least the current wiki is set, for simple configurations.
205 // This also makes it the first in the map, which is useful for common cases.
206 $wikiId = self::getCurrentWikiId();
207 $infoMap[$wikiId] = [
208 'url' => $wgCanonicalServer,
209 'parts' => $urlUtils->parse( $wgCanonicalServer )
210 ];
211
212 foreach ( $wgLocalDatabases as $wikiId ) {
213 $wikiReference = self::getWiki( $wikiId );
214 if ( $wikiReference ) {
215 $url = $wikiReference->getCanonicalServer();
216 $infoMap[$wikiId] = [ 'url' => $url, 'parts' => $urlUtils->parse( $url ) ];
217 }
218 }
219
220 return $infoMap;
221 }
222 );
223 }
224
230 public static function getWikiFromUrl( $url ) {
231 global $wgCanonicalServer;
232
233 if ( str_starts_with( $url, "$wgCanonicalServer/" ) ) {
234 // Optimisation: Handle the common case.
235 // (Duplicates self::getCanonicalServerInfoForAllWikis)
236 return self::getCurrentWikiId();
237 }
238
239 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
240 $urlPartsCheck = $urlUtils->parse( $url );
241 if ( $urlPartsCheck === null
242 || !in_array( $urlPartsCheck['scheme'], [ '', 'http', 'https' ], true )
243 ) {
244 return false;
245 }
246
247 static $relevantKeys = [ 'host' => 1, 'port' => 1 ];
248 $urlPartsCheck = array_intersect_key( $urlPartsCheck, $relevantKeys );
249
250 foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) {
251 $urlParts = $info['parts'];
252 if ( $urlParts === false ) {
253 continue;
254 }
255
256 $urlParts = array_intersect_key( $urlParts, $relevantKeys );
257 if ( $urlParts == $urlPartsCheck ) {
258 return $wikiId;
259 }
260 }
261
262 return false;
263 }
264
278 public static function getWikiIdFromDbDomain( $domain ) {
279 $domain = DatabaseDomain::newFromId( $domain );
280 // Since the schema was not always part of the wiki ID, try to maintain backwards
281 // compatibility with some common cases. Assume that if the DB domain schema is just
282 // the installer default then it is probably the case that the schema is the same for
283 // all wikis in the farm. Historically, any wiki farm had to make the database/prefix
284 // combination unique per wiki. Omit the schema if it does not seem wiki specific.
285 if ( !in_array( $domain->getSchema(), [ null, 'mediawiki' ], true ) ) {
286 // This means a site admin may have specifically tailored the schemas.
287 // Domain IDs might use the form <DB>-<project>- or <DB>-<project>-<language>_,
288 // meaning that the schema portion must be accounted for to disambiguate wikis.
289 return "{$domain->getDatabase()}-{$domain->getSchema()}-{$domain->getTablePrefix()}";
290 }
291 // Note that if this wiki ID is passed as a domain ID to LoadBalancer, then it can
292 // handle the schema by assuming the generic "mediawiki" schema if needed.
293 return strlen( $domain->getTablePrefix() )
294 ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
295 : (string)$domain->getDatabase();
296 }
297
302 public static function getCurrentWikiDbDomain() {
304 // Avoid invoking LBFactory to avoid any chance of recursion
305 return new DatabaseDomain( $wgDBname, $wgDBmwschema, (string)$wgDBprefix );
306 }
307
312 public static function getCurrentWikiId() {
313 return self::getWikiIdFromDbDomain( self::getCurrentWikiDbDomain() );
314 }
315
321 public static function isCurrentWikiDbDomain( $domain ) {
322 return self::getCurrentWikiDbDomain()->equals( $domain );
323 }
324
330 public static function isCurrentWikiId( $wikiId ) {
331 return ( self::getCurrentWikiId() === $wikiId );
332 }
333}
$wgConf
$wgConf hold the site configuration.
Definition Setup.php:137
if(MW_ENTRY_POINT==='index') if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli') global $wgTitle
Definition Setup.php:529
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Class representing a MediaWiki site.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:19
static getWikiFromUrl( $url)
Definition WikiMap.php:230
static getCanonicalServerInfoForAllWikis()
Get canonical server info for all local wikis in the map that have one.
Definition WikiMap.php:192
static isCurrentWikiDbDomain( $domain)
Definition WikiMap.php:321
static isCurrentWikiId( $wikiId)
Definition WikiMap.php:330
static foreignUserLink( $wikiID, $user, $text=null)
Convenience method to get a link to a user page on a foreign wiki.
Definition WikiMap.php:134
static getCurrentWikiDbDomain()
Definition WikiMap.php:302
static getForeignURL( $wikiID, $page, $fragmentId=null)
Convenience method to get a url to a page on a foreign wiki.
Definition WikiMap.php:175
static makeForeignLink( $wikiID, $page, $text=null)
Convenience method to get a link to a page on a foreign wiki.
Definition WikiMap.php:146
static getWiki( $wikiID)
Get a WikiReference object for $wikiID.
Definition WikiMap.php:27
static getWikiIdFromDbDomain( $domain)
Get the wiki ID of a database domain.
Definition WikiMap.php:278
static getWikiName( $wikiID)
Convenience to get the wiki's display name.
Definition WikiMap.php:121
Reference to a locally-hosted wiki.
Class to handle database/schema/prefix specifications for IDatabase.
$wgDBprefix
Config variable stub for the DBprefix setting, for use by phpdoc and IDEs.
$wgLocalDatabases
Config variable stub for the LocalDatabases setting, for use by phpdoc and IDEs.
$wgDBmwschema
Config variable stub for the DBmwschema setting, for use by phpdoc and IDEs.
$wgDBname
Config variable stub for the DBname setting, for use by phpdoc and IDEs.
$wgArticlePath
Config variable stub for the ArticlePath setting, for use by phpdoc and IDEs.
$wgCanonicalServer
Config variable stub for the CanonicalServer setting, for use by phpdoc and IDEs.
$wgServer
Config variable stub for the Server setting, for use by phpdoc and IDEs.