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 $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
29 if ( $wikiReference ) {
30 return $wikiReference;
31 }
32
33 // Try sites, if $wgConf failed
34 return self::getWikiWikiReferenceFromSites( $wikiID );
35 }
36
41 private static function getWikiReferenceFromWgConf( $wikiID ) {
42 global $wgConf;
43 '@phan-var \MediaWiki\Config\SiteConfiguration $wgConf';
44
45 $wgConf->loadFullData();
46
47 [ $major, $minor ] = $wgConf->siteFromDB( $wikiID );
48 if ( $major === null ) {
49 return null;
50 }
51 $server = $wgConf->get( 'wgServer', $wikiID, $major,
52 [ 'lang' => $minor, 'site' => $major ] );
53
54 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
55 [ 'lang' => $minor, 'site' => $major ] );
56 if ( $canonicalServer === false || $canonicalServer === null ) {
57 $canonicalServer = $server;
58 }
59
60 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
61 [ 'lang' => $minor, 'site' => $major ] );
62
63 // If we don't have a canonical server or a path containing $1, the
64 // WikiReference isn't going to function properly. Just return null in
65 // that case.
66 if ( !is_string( $canonicalServer ) || !is_string( $path ) || !str_contains( $path, '$1' ) ) {
67 return null;
68 }
69
70 return new WikiReference( $canonicalServer, $path, $server );
71 }
72
77 private static function getWikiWikiReferenceFromSites( $wikiID ) {
78 $siteLookup = MediaWikiServices::getInstance()->getSiteLookup();
79 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
80 $site = $siteLookup->getSite( $wikiID );
81
82 if ( !$site instanceof MediaWikiSite ) {
83 // Abort if not a MediaWikiSite, as this is about Wikis
84 return null;
85 }
86
87 $urlParts = $urlUtils->parse( $site->getPageUrl() );
88 if ( $urlParts === null || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
89 // We can't create a meaningful WikiReference without URLs
90 return null;
91 }
92
93 // XXX: Check whether path contains a $1?
94 $path = $urlParts['path'];
95 if ( isset( $urlParts['query'] ) ) {
96 $path .= '?' . $urlParts['query'];
97 }
98
99 $canonicalServer = $urlParts['scheme'] ?? 'http';
100 $canonicalServer .= '://' . $urlParts['host'];
101 if ( isset( $urlParts['port'] ) ) {
102 $canonicalServer .= ':' . $urlParts['port'];
103 }
104
105 return new WikiReference( $canonicalServer, $path );
106 }
107
115 public static function getWikiName( $wikiID ) {
116 $wiki = self::getWiki( $wikiID );
117 return $wiki ? $wiki->getDisplayName() : $wikiID;
118 }
119
128 public static function foreignUserLink( $wikiID, $user, $text = null ) {
129 return self::makeForeignLink( $wikiID, "User:$user", $text );
130 }
131
140 public static function makeForeignLink( $wikiID, $page, $text = null ) {
141 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle
142 global $wgTitle;
143 if ( !$text ) {
144 $text = $page;
145 }
146
147 $url = self::getForeignURL( $wikiID, $page );
148 if ( $url === false ) {
149 return false;
150 }
151
152 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
153 return $linkRenderer->makeExternalLink(
154 $url,
155 $text,
156 $wgTitle ?? SpecialPage::getTitleFor( 'Badtitle' )
157 );
158 }
159
169 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
170 $wiki = self::getWiki( $wikiID );
171
172 if ( $wiki ) {
173 return $wiki->getFullUrl( $page, $fragmentId );
174 }
175
176 return false;
177 }
178
186 public static function getCanonicalServerInfoForAllWikis() {
187 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
188
189 return $cache->getWithSetCallback(
190 $cache->makeGlobalKey( 'wikimap-canonical-urls' ),
191 $cache::TTL_DAY,
192 static function () {
194
195 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
196
197 $infoMap = [];
198 // Make sure at least the current wiki is set, for simple configurations.
199 // This also makes it the first in the map, which is useful for common cases.
200 $wikiId = self::getCurrentWikiId();
201 $infoMap[$wikiId] = [
202 'url' => $wgCanonicalServer,
203 'parts' => $urlUtils->parse( $wgCanonicalServer )
204 ];
205
206 foreach ( $wgLocalDatabases as $wikiId ) {
207 $wikiReference = self::getWiki( $wikiId );
208 if ( $wikiReference ) {
209 $url = $wikiReference->getCanonicalServer();
210 $infoMap[$wikiId] = [ 'url' => $url, 'parts' => $urlUtils->parse( $url ) ];
211 }
212 }
213
214 return $infoMap;
215 }
216 );
217 }
218
224 public static function getWikiFromUrl( $url ) {
225 global $wgCanonicalServer;
226
227 if ( str_starts_with( $url, "$wgCanonicalServer/" ) ) {
228 // Optimisation: Handle the common case.
229 // (Duplicates self::getCanonicalServerInfoForAllWikis)
230 return self::getCurrentWikiId();
231 }
232
233 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
234 $urlPartsCheck = $urlUtils->parse( $url );
235 if ( $urlPartsCheck === null
236 || !in_array( $urlPartsCheck['scheme'], [ '', 'http', 'https' ], true )
237 ) {
238 return false;
239 }
240
241 static $relevantKeys = [ 'host' => 1, 'port' => 1 ];
242 $urlPartsCheck = array_intersect_key( $urlPartsCheck, $relevantKeys );
243
244 foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) {
245 $urlParts = $info['parts'];
246 if ( $urlParts === false ) {
247 continue;
248 }
249
250 $urlParts = array_intersect_key( $urlParts, $relevantKeys );
251 if ( $urlParts == $urlPartsCheck ) {
252 return $wikiId;
253 }
254 }
255
256 return false;
257 }
258
272 public static function getWikiIdFromDbDomain( $domain ) {
273 $domain = DatabaseDomain::newFromId( $domain );
274 // Since the schema was not always part of the wiki ID, try to maintain backwards
275 // compatibility with some common cases. Assume that if the DB domain schema is just
276 // the installer default then it is probably the case that the schema is the same for
277 // all wikis in the farm. Historically, any wiki farm had to make the database/prefix
278 // combination unique per wiki. Omit the schema if it does not seem wiki specific.
279 if ( !in_array( $domain->getSchema(), [ null, 'mediawiki' ], true ) ) {
280 // This means a site admin may have specifically tailored the schemas.
281 // Domain IDs might use the form <DB>-<project>- or <DB>-<project>-<language>_,
282 // meaning that the schema portion must be accounted for to disambiguate wikis.
283 return "{$domain->getDatabase()}-{$domain->getSchema()}-{$domain->getTablePrefix()}";
284 }
285 // Note that if this wiki ID is passed as a domain ID to LoadBalancer, then it can
286 // handle the schema by assuming the generic "mediawiki" schema if needed.
287 return strlen( $domain->getTablePrefix() )
288 ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
289 : (string)$domain->getDatabase();
290 }
291
296 public static function getCurrentWikiDbDomain() {
298 // Avoid invoking LBFactory to avoid any chance of recursion
299 return new DatabaseDomain( $wgDBname, $wgDBmwschema, (string)$wgDBprefix );
300 }
301
306 public static function getCurrentWikiId() {
307 return self::getWikiIdFromDbDomain( self::getCurrentWikiDbDomain() );
308 }
309
315 public static function isCurrentWikiDbDomain( $domain ) {
316 return self::getCurrentWikiDbDomain()->equals( $domain );
317 }
318
324 public static function isCurrentWikiId( $wikiId ) {
325 return ( self::getCurrentWikiId() === $wikiId );
326 }
327}
$wgConf
$wgConf hold the site configuration.
Definition Setup.php:139
if(MW_ENTRY_POINT==='index') if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgTitle
Definition Setup.php:551
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:224
static getCanonicalServerInfoForAllWikis()
Get canonical server info for all local wikis in the map that have one.
Definition WikiMap.php:186
static isCurrentWikiDbDomain( $domain)
Definition WikiMap.php:315
static isCurrentWikiId( $wikiId)
Definition WikiMap.php:324
static foreignUserLink( $wikiID, $user, $text=null)
Convenience method to get a link to a user page on a foreign wiki.
Definition WikiMap.php:128
static getCurrentWikiDbDomain()
Definition WikiMap.php:296
static getForeignURL( $wikiID, $page, $fragmentId=null)
Convenience method to get a url to a page on a foreign wiki.
Definition WikiMap.php:169
static makeForeignLink( $wikiID, $page, $text=null)
Convenience method to get a link to a page on a foreign wiki.
Definition WikiMap.php:140
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:272
static getWikiName( $wikiID)
Convenience to get the wiki's display name.
Definition WikiMap.php:115
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.