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 $site = $siteLookup->getSite( $wikiID );
80
81 if ( !$site instanceof MediaWikiSite ) {
82 // Abort if not a MediaWikiSite, as this is about Wikis
83 return null;
84 }
85
86 $urlParts = wfGetUrlUtils()->parse( $site->getPageUrl() );
87 if ( $urlParts === null || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
88 // We can't create a meaningful WikiReference without URLs
89 return null;
90 }
91
92 // XXX: Check whether path contains a $1?
93 $path = $urlParts['path'];
94 if ( isset( $urlParts['query'] ) ) {
95 $path .= '?' . $urlParts['query'];
96 }
97
98 $canonicalServer = $urlParts['scheme'] ?? 'http';
99 $canonicalServer .= '://' . $urlParts['host'];
100 if ( isset( $urlParts['port'] ) ) {
101 $canonicalServer .= ':' . $urlParts['port'];
102 }
103
104 return new WikiReference( $canonicalServer, $path );
105 }
106
114 public static function getWikiName( $wikiID ) {
115 $wiki = self::getWiki( $wikiID );
116 return $wiki ? $wiki->getDisplayName() : $wikiID;
117 }
118
127 public static function foreignUserLink( $wikiID, $user, $text = null ) {
128 return self::makeForeignLink( $wikiID, "User:$user", $text );
129 }
130
139 public static function makeForeignLink( $wikiID, $page, $text = null ) {
140 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle
141 global $wgTitle;
142 if ( !$text ) {
143 $text = $page;
144 }
145
146 $url = self::getForeignURL( $wikiID, $page );
147 if ( $url === false ) {
148 return false;
149 }
150
151 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
152 return $linkRenderer->makeExternalLink(
153 $url,
154 $text,
155 $wgTitle ?? SpecialPage::getTitleFor( 'Badtitle' )
156 );
157 }
158
168 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
169 $wiki = self::getWiki( $wikiID );
170
171 if ( $wiki ) {
172 return $wiki->getFullUrl( $page, $fragmentId );
173 }
174
175 return false;
176 }
177
185 public static function getCanonicalServerInfoForAllWikis() {
186 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
187
188 return $cache->getWithSetCallback(
189 $cache->makeGlobalKey( 'wikimap-canonical-urls' ),
190 $cache::TTL_DAY,
191 static function () {
193
194 $infoMap = [];
195 // Make sure at least the current wiki is set, for simple configurations.
196 // This also makes it the first in the map, which is useful for common cases.
197 $wikiId = self::getCurrentWikiId();
198 $infoMap[$wikiId] = [
199 'url' => $wgCanonicalServer,
200 'parts' => wfGetUrlUtils()->parse( $wgCanonicalServer )
201 ];
202
203 foreach ( $wgLocalDatabases as $wikiId ) {
204 $wikiReference = self::getWiki( $wikiId );
205 if ( $wikiReference ) {
206 $url = $wikiReference->getCanonicalServer();
207 $infoMap[$wikiId] = [ 'url' => $url, 'parts' => wfGetUrlUtils()->parse( $url ) ];
208 }
209 }
210
211 return $infoMap;
212 }
213 );
214 }
215
221 public static function getWikiFromUrl( $url ) {
222 global $wgCanonicalServer;
223
224 if ( str_starts_with( $url, "$wgCanonicalServer/" ) ) {
225 // Optimisation: Handle the common case.
226 // (Duplicates self::getCanonicalServerInfoForAllWikis)
227 return self::getCurrentWikiId();
228 }
229
230 $urlPartsCheck = wfGetUrlUtils()->parse( $url );
231 if ( $urlPartsCheck === null
232 || !in_array( $urlPartsCheck['scheme'], [ '', 'http', 'https' ], true )
233 ) {
234 return false;
235 }
236
237 static $relevantKeys = [ 'host' => 1, 'port' => 1 ];
238 $urlPartsCheck = array_intersect_key( $urlPartsCheck, $relevantKeys );
239
240 foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) {
241 $urlParts = $info['parts'];
242 if ( $urlParts === false ) {
243 continue;
244 }
245
246 $urlParts = array_intersect_key( $urlParts, $relevantKeys );
247 if ( $urlParts == $urlPartsCheck ) {
248 return $wikiId;
249 }
250 }
251
252 return false;
253 }
254
268 public static function getWikiIdFromDbDomain( $domain ) {
269 $domain = DatabaseDomain::newFromId( $domain );
270 // Since the schema was not always part of the wiki ID, try to maintain backwards
271 // compatibility with some common cases. Assume that if the DB domain schema is just
272 // the installer default then it is probably the case that the schema is the same for
273 // all wikis in the farm. Historically, any wiki farm had to make the database/prefix
274 // combination unique per wiki. Omit the schema if it does not seem wiki specific.
275 if ( !in_array( $domain->getSchema(), [ null, 'mediawiki' ], true ) ) {
276 // This means a site admin may have specifically tailored the schemas.
277 // Domain IDs might use the form <DB>-<project>- or <DB>-<project>-<language>_,
278 // meaning that the schema portion must be accounted for to disambiguate wikis.
279 return "{$domain->getDatabase()}-{$domain->getSchema()}-{$domain->getTablePrefix()}";
280 }
281 // Note that if this wiki ID is passed as a domain ID to LoadBalancer, then it can
282 // handle the schema by assuming the generic "mediawiki" schema if needed.
283 return strlen( $domain->getTablePrefix() )
284 ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
285 : (string)$domain->getDatabase();
286 }
287
292 public static function getCurrentWikiDbDomain() {
294 // Avoid invoking LBFactory to avoid any chance of recursion
295 return new DatabaseDomain( $wgDBname, $wgDBmwschema, (string)$wgDBprefix );
296 }
297
302 public static function getCurrentWikiId() {
303 return self::getWikiIdFromDbDomain( self::getCurrentWikiDbDomain() );
304 }
305
311 public static function isCurrentWikiDbDomain( $domain ) {
312 return self::getCurrentWikiDbDomain()->equals( $domain );
313 }
314
320 public static function isCurrentWikiId( $wikiId ) {
321 return ( self::getCurrentWikiId() === $wikiId );
322 }
323}
wfGetUrlUtils()
$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:221
static getCanonicalServerInfoForAllWikis()
Get canonical server info for all local wikis in the map that have one.
Definition WikiMap.php:185
static isCurrentWikiDbDomain( $domain)
Definition WikiMap.php:311
static isCurrentWikiId( $wikiId)
Definition WikiMap.php:320
static foreignUserLink( $wikiID, $user, $text=null)
Convenience method to get a link to a user page on a foreign wiki.
Definition WikiMap.php:127
static getCurrentWikiDbDomain()
Definition WikiMap.php:292
static getForeignURL( $wikiID, $page, $fragmentId=null)
Convenience method to get a url to a page on a foreign wiki.
Definition WikiMap.php:168
static makeForeignLink( $wikiID, $page, $text=null)
Convenience method to get a link to a page on a foreign wiki.
Definition WikiMap.php:139
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:268
static getWikiName( $wikiID)
Convenience to get the wiki's display name.
Definition WikiMap.php:114
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.