MediaWiki master
WikiMap.php
Go to the documentation of this file.
1<?php
22
27
31class WikiMap {
32
39 public static function getWiki( $wikiID ) {
40 $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
41 if ( $wikiReference ) {
42 return $wikiReference;
43 }
44
45 // Try sites, if $wgConf failed
46 return self::getWikiWikiReferenceFromSites( $wikiID );
47 }
48
53 private static function getWikiReferenceFromWgConf( $wikiID ) {
54 global $wgConf;
55 '@phan-var \MediaWiki\Config\SiteConfiguration $wgConf';
56
57 $wgConf->loadFullData();
58
59 [ $major, $minor ] = $wgConf->siteFromDB( $wikiID );
60 if ( $major === null ) {
61 return null;
62 }
63 $server = $wgConf->get( 'wgServer', $wikiID, $major,
64 [ 'lang' => $minor, 'site' => $major ] );
65
66 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
67 [ 'lang' => $minor, 'site' => $major ] );
68 if ( $canonicalServer === false || $canonicalServer === null ) {
69 $canonicalServer = $server;
70 }
71
72 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
73 [ 'lang' => $minor, 'site' => $major ] );
74
75 // If we don't have a canonical server or a path containing $1, the
76 // WikiReference isn't going to function properly. Just return null in
77 // that case.
78 if ( !is_string( $canonicalServer ) || !is_string( $path ) || strpos( $path, '$1' ) === false ) {
79 return null;
80 }
81
82 return new WikiReference( $canonicalServer, $path, $server );
83 }
84
89 private static function getWikiWikiReferenceFromSites( $wikiID ) {
90 $siteLookup = MediaWikiServices::getInstance()->getSiteLookup();
91 $site = $siteLookup->getSite( $wikiID );
92
93 if ( !$site instanceof MediaWikiSite ) {
94 // Abort if not a MediaWikiSite, as this is about Wikis
95 return null;
96 }
97
98 $urlParts = wfParseUrl( $site->getPageUrl() );
99 if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
100 // We can't create a meaningful WikiReference without URLs
101 return null;
102 }
103
104 // XXX: Check whether path contains a $1?
105 $path = $urlParts['path'];
106 if ( isset( $urlParts['query'] ) ) {
107 $path .= '?' . $urlParts['query'];
108 }
109
110 $canonicalServer = $urlParts['scheme'] ?? 'http';
111 $canonicalServer .= '://' . $urlParts['host'];
112 if ( isset( $urlParts['port'] ) ) {
113 $canonicalServer .= ':' . $urlParts['port'];
114 }
115
116 return new WikiReference( $canonicalServer, $path );
117 }
118
126 public static function getWikiName( $wikiID ) {
127 $wiki = self::getWiki( $wikiID );
128 return $wiki ? $wiki->getDisplayName() : $wikiID;
129 }
130
139 public static function foreignUserLink( $wikiID, $user, $text = null ) {
140 return self::makeForeignLink( $wikiID, "User:$user", $text );
141 }
142
151 public static function makeForeignLink( $wikiID, $page, $text = null ) {
152 global $wgTitle;
153 if ( !$text ) {
154 $text = $page;
155 }
156
157 $url = self::getForeignURL( $wikiID, $page );
158 if ( $url === false ) {
159 return false;
160 }
161
162 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
163 return $linkRenderer->makeExternalLink(
164 $url,
165 $text,
166 $wgTitle ?? SpecialPage::getTitleFor( 'Badtitle' )
167 );
168 }
169
179 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
180 $wiki = self::getWiki( $wikiID );
181
182 if ( $wiki ) {
183 return $wiki->getFullUrl( $page, $fragmentId );
184 }
185
186 return false;
187 }
188
196 public static function getCanonicalServerInfoForAllWikis() {
197 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
198
199 return $cache->getWithSetCallback(
200 $cache->makeGlobalKey( 'wikimap-canonical-urls' ),
201 $cache::TTL_DAY,
202 static function () {
204
205 $infoMap = [];
206 // Make sure at least the current wiki is set, for simple configurations.
207 // This also makes it the first in the map, which is useful for common cases.
208 $wikiId = self::getCurrentWikiId();
209 $infoMap[$wikiId] = [
210 'url' => $wgCanonicalServer,
211 'parts' => wfParseUrl( $wgCanonicalServer )
212 ];
213
214 foreach ( $wgLocalDatabases as $wikiId ) {
215 $wikiReference = self::getWiki( $wikiId );
216 if ( $wikiReference ) {
217 $url = $wikiReference->getCanonicalServer();
218 $infoMap[$wikiId] = [ 'url' => $url, 'parts' => wfParseUrl( $url ) ];
219 }
220 }
221
222 return $infoMap;
223 }
224 );
225 }
226
232 public static function getWikiFromUrl( $url ) {
233 global $wgCanonicalServer;
234
235 if ( strpos( $url, "$wgCanonicalServer/" ) === 0 ) {
236 // Optimisation: Handle the common case.
237 // (Duplicates self::getCanonicalServerInfoForAllWikis)
238 return self::getCurrentWikiId();
239 }
240
241 $urlPartsCheck = wfParseUrl( $url );
242 if ( $urlPartsCheck === false ) {
243 return false;
244 }
245
246 static $relevantKeys = [ 'host' => 1, 'port' => 1 ];
247 $urlPartsCheck = array_intersect_key( $urlPartsCheck, $relevantKeys );
248
249 foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) {
250 $urlParts = $info['parts'];
251 if ( $urlParts === false ) {
252 continue;
253 }
254
255 $urlParts = array_intersect_key( $urlParts, $relevantKeys );
256 if ( $urlParts == $urlPartsCheck ) {
257 return $wikiId;
258 }
259 }
260
261 return false;
262 }
263
277 public static function getWikiIdFromDbDomain( $domain ) {
278 $domain = DatabaseDomain::newFromId( $domain );
279 // Since the schema was not always part of the wiki ID, try to maintain backwards
280 // compatibility with some common cases. Assume that if the DB domain schema is just
281 // the installer default then it is probably the case that the schema is the same for
282 // all wikis in the farm. Historically, any wiki farm had to make the database/prefix
283 // combination unique per wiki. Omit the schema if it does not seem wiki specific.
284 if ( !in_array( $domain->getSchema(), [ null, 'mediawiki' ], true ) ) {
285 // This means a site admin may have specifically tailored the schemas.
286 // Domain IDs might use the form <DB>-<project>- or <DB>-<project>-<language>_,
287 // meaning that the schema portion must be accounted for to disambiguate wikis.
288 return "{$domain->getDatabase()}-{$domain->getSchema()}-{$domain->getTablePrefix()}";
289 }
290 // Note that if this wiki ID is passed as a domain ID to LoadBalancer, then it can
291 // handle the schema by assuming the generic "mediawiki" schema if needed.
292 return strlen( $domain->getTablePrefix() )
293 ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
294 : (string)$domain->getDatabase();
295 }
296
301 public static function getCurrentWikiDbDomain() {
303 // Avoid invoking LBFactory to avoid any chance of recursion
304 return new DatabaseDomain( $wgDBname, $wgDBmwschema, (string)$wgDBprefix );
305 }
306
311 public static function getCurrentWikiId() {
312 return self::getWikiIdFromDbDomain( self::getCurrentWikiDbDomain() );
313 }
314
320 public static function isCurrentWikiDbDomain( $domain ) {
321 return self::getCurrentWikiDbDomain()->equals( $domain );
322 }
323
329 public static function isCurrentWikiId( $wikiId ) {
330 return ( self::getCurrentWikiId() === $wikiId );
331 }
332}
333
335class_alias( WikiMap::class, 'WikiMap' );
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
$wgConf
$wgConf hold the site configuration.
Definition Setup.php:149
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgTitle
Definition Setup.php:538
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:31
static getWikiFromUrl( $url)
Definition WikiMap.php:232
static getCanonicalServerInfoForAllWikis()
Get canonical server info for all local wikis in the map that have one.
Definition WikiMap.php:196
static isCurrentWikiDbDomain( $domain)
Definition WikiMap.php:320
static isCurrentWikiId( $wikiId)
Definition WikiMap.php:329
static foreignUserLink( $wikiID, $user, $text=null)
Convenience to get a link to a user page on a foreign wiki.
Definition WikiMap.php:139
static getCurrentWikiDbDomain()
Definition WikiMap.php:301
static getForeignURL( $wikiID, $page, $fragmentId=null)
Convenience to get a url to a page on a foreign wiki.
Definition WikiMap.php:179
static makeForeignLink( $wikiID, $page, $text=null)
Convenience to get a link to a page on a foreign wiki.
Definition WikiMap.php:151
static getWiki( $wikiID)
Get a WikiReference object for $wikiID.
Definition WikiMap.php:39
static getWikiIdFromDbDomain( $domain)
Get the wiki ID of a database domain.
Definition WikiMap.php:277
static getWikiName( $wikiID)
Convenience to get the wiki's display name.
Definition WikiMap.php:126
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.
$wgCanonicalServer
Config variable stub for the CanonicalServer setting, for use by phpdoc and IDEs.