MediaWiki  REL1_31
WikiMap.php
Go to the documentation of this file.
1 <?php
25 
29 class WikiMap {
30 
37  public static function getWiki( $wikiID ) {
38  $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
39  if ( $wikiReference ) {
40  return $wikiReference;
41  }
42 
43  // Try sites, if $wgConf failed
44  return self::getWikiWikiReferenceFromSites( $wikiID );
45  }
46 
51  private static function getWikiReferenceFromWgConf( $wikiID ) {
53 
54  $wgConf->loadFullData();
55 
56  list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
57  if ( $major === null ) {
58  return null;
59  }
60  $server = $wgConf->get( 'wgServer', $wikiID, $major,
61  [ 'lang' => $minor, 'site' => $major ] );
62 
63  $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
64  [ 'lang' => $minor, 'site' => $major ] );
65  if ( $canonicalServer === false || $canonicalServer === null ) {
66  $canonicalServer = $server;
67  }
68 
69  $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
70  [ 'lang' => $minor, 'site' => $major ] );
71 
72  // If we don't have a canonical server or a path containing $1, the
73  // WikiReference isn't going to function properly. Just return null in
74  // that case.
75  if ( !is_string( $canonicalServer ) || !is_string( $path ) || strpos( $path, '$1' ) === false ) {
76  return null;
77  }
78 
79  return new WikiReference( $canonicalServer, $path, $server );
80  }
81 
86  private static function getWikiWikiReferenceFromSites( $wikiID ) {
87  $siteLookup = MediaWikiServices::getInstance()->getSiteLookup();
88  $site = $siteLookup->getSite( $wikiID );
89 
90  if ( !$site instanceof MediaWikiSite ) {
91  // Abort if not a MediaWikiSite, as this is about Wikis
92  return null;
93  }
94 
95  $urlParts = wfParseUrl( $site->getPageUrl() );
96  if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
97  // We can't create a meaningful WikiReference without URLs
98  return null;
99  }
100 
101  // XXX: Check whether path contains a $1?
102  $path = $urlParts['path'];
103  if ( isset( $urlParts['query'] ) ) {
104  $path .= '?' . $urlParts['query'];
105  }
106 
107  $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
108  $canonicalServer .= '://' . $urlParts['host'];
109 
110  return new WikiReference( $canonicalServer, $path );
111  }
112 
120  public static function getWikiName( $wikiID ) {
121  $wiki = self::getWiki( $wikiID );
122 
123  if ( $wiki ) {
124  return $wiki->getDisplayName();
125  }
126  return $wikiID;
127  }
128 
137  public static function foreignUserLink( $wikiID, $user, $text = null ) {
138  return self::makeForeignLink( $wikiID, "User:$user", $text );
139  }
140 
149  public static function makeForeignLink( $wikiID, $page, $text = null ) {
150  if ( !$text ) {
151  $text = $page;
152  }
153 
154  $url = self::getForeignURL( $wikiID, $page );
155  if ( $url === false ) {
156  return false;
157  }
158 
159  return Linker::makeExternalLink( $url, $text );
160  }
161 
171  public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
172  $wiki = self::getWiki( $wikiID );
173 
174  if ( $wiki ) {
175  return $wiki->getFullUrl( $page, $fragmentId );
176  }
177 
178  return false;
179  }
180 
187  public static function getCanonicalServerInfoForAllWikis() {
188  $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
189 
190  return $cache->getWithSetCallback(
191  $cache->makeGlobalKey( 'wikimap', 'canonical-urls' ),
192  $cache::TTL_DAY,
193  function () {
195 
196  $infoMap = [];
197  // Make sure at least the current wiki is set, for simple configurations.
198  // This also makes it the first in the map, which is useful for common cases.
199  $infoMap[wfWikiID()] = [
200  'url' => $wgCanonicalServer,
201  'parts' => wfParseUrl( $wgCanonicalServer )
202  ];
203 
204  foreach ( $wgLocalDatabases as $wikiId ) {
205  $wikiReference = self::getWiki( $wikiId );
206  if ( $wikiReference ) {
207  $url = $wikiReference->getCanonicalServer();
208  $infoMap[$wikiId] = [ 'url' => $url, 'parts' => wfParseUrl( $url ) ];
209  }
210  }
211 
212  return $infoMap;
213  }
214  );
215  }
216 
222  public static function getWikiFromUrl( $url ) {
223  $urlPartsCheck = wfParseUrl( $url );
224  if ( $urlPartsCheck === false ) {
225  return false;
226  }
227 
228  $urlPartsCheck = array_intersect_key( $urlPartsCheck, [ 'host' => 1, 'port' => 1 ] );
229  foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) {
230  $urlParts = $info['parts'];
231  if ( $urlParts === false ) {
232  continue; // sanity
233  }
234 
235  $urlParts = array_intersect_key( $urlParts, [ 'host' => 1, 'port' => 1 ] );
236  if ( $urlParts == $urlPartsCheck ) {
237  return $wikiId;
238  }
239  }
240 
241  return false;
242  }
243 
252  public static function getWikiIdFromDomain( $domain ) {
253  if ( !( $domain instanceof DatabaseDomain ) ) {
254  $domain = DatabaseDomain::newFromId( $domain );
255  }
256 
257  return strlen( $domain->getTablePrefix() )
258  ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
259  : $domain->getDatabase();
260  }
261 
267  public static function isCurrentWikiDbDomain( $domain ) {
268  $domain = DatabaseDomain::newFromId( $domain );
269  $curDomain = self::getCurrentWikiDbDomain();
270 
271  if ( !in_array( $curDomain->getSchema(), [ null, 'mediawiki' ], true ) ) {
272  // Include the schema if it is set and is not the default placeholder.
273  // This means a site admin may have specifically taylored the schemas.
274  // Domain IDs might use the form <DB>-<project>-<language>, meaning that
275  // the schema portion must be accounted for to disambiguate wikis.
276  return $curDomain->equals( $domain );
277  }
278 
279  return (
280  $curDomain->getDatabase() === $domain->getDatabase() &&
281  $curDomain->getTablePrefix() === $domain->getTablePrefix()
282  );
283  }
284 
289  public static function getCurrentWikiDbDomain() {
291  // Avoid invoking LBFactory to avoid any chance of recursion
292  return new DatabaseDomain( $wgDBname, $wgDBmwschema, (string)$wgDBprefix );
293  }
294 }
$wgConf
$wgConf
wgConf hold the site configuration.
Definition: DefaultSettings.php:58
WikiMap\isCurrentWikiDbDomain
static isCurrentWikiDbDomain( $domain)
Definition: WikiMap.php:267
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:247
WikiMap\foreignUserLink
static foreignUserLink( $wikiID, $user, $text=null)
Convenience to get a link to a user page on a foreign wiki.
Definition: WikiMap.php:137
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:289
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
WikiMap\getWikiFromUrl
static getWikiFromUrl( $url)
Definition: WikiMap.php:222
WikiMap\getForeignURL
static getForeignURL( $wikiID, $page, $fragmentId=null)
Convenience to get a url to a page on a foreign wiki.
Definition: WikiMap.php:171
$wgDBmwschema
$wgDBmwschema
Mediawiki schema.
Definition: DefaultSettings.php:1889
$wgDBprefix
$wgDBprefix
Table name prefix.
Definition: DefaultSettings.php:1871
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:801
$wgDBname
controlled by $wgMainCacheType controlled by $wgParserCacheType controlled by $wgMessageCacheType If you set CACHE_NONE to one of the three control default value for MediaWiki still create a but requests to it are no ops and we always fall through to the database If the cache daemon can t be it should also disable itself fairly smoothly By $wgMemc is used but when it is $parserMemc or $messageMemc this is mentioned $wgDBname
Definition: memcached.txt:138
WikiMap\getWiki
static getWiki( $wikiID)
Get a WikiReference object for $wikiID.
Definition: WikiMap.php:37
function
when a variable name is used in a function
Definition: design.txt:93
Linker\makeExternalLink
static makeExternalLink( $url, $text, $escape=true, $linktype='', $attribs=[], $title=null)
Make an external link.
Definition: Linker.php:843
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$wgCanonicalServer
$wgCanonicalServer
Canonical URL of the server, to use in IRC feeds and notification e-mails.
Definition: DefaultSettings.php:115
WikiMap\getWikiIdFromDomain
static getWikiIdFromDomain( $domain)
Get the wiki ID of a database domain.
Definition: WikiMap.php:252
$wgLocalDatabases
$wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
Definition: DefaultSettings.php:2094
WikiMap\getWikiReferenceFromWgConf
static getWikiReferenceFromWgConf( $wikiID)
Definition: WikiMap.php:51
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:2763
WikiMap\getWikiWikiReferenceFromSites
static getWikiWikiReferenceFromSites( $wikiID)
Definition: WikiMap.php:86
WikiMap\makeForeignLink
static makeForeignLink( $wikiID, $page, $text=null)
Convenience to get a link to a page on a foreign wiki.
Definition: WikiMap.php:149
WikiMap
Helper tools for dealing with other locally-hosted wikis.
Definition: WikiMap.php:29
$cache
$cache
Definition: mcc.php:33
WikiReference
Reference to a locally-hosted wiki.
Definition: WikiReference.php:26
$path
$path
Definition: NoLocalSettings.php:25
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
MediaWikiSite
Class representing a MediaWiki site.
Definition: MediaWikiSite.php:38
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:25
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/prefix specification for IDatabase domains.
Definition: DatabaseDomain.php:28
WikiMap\getWikiName
static getWikiName( $wikiID)
Convenience to get the wiki's display name.
Definition: WikiMap.php:120
WikiMap\getCanonicalServerInfoForAllWikis
static getCanonicalServerInfoForAllWikis()
Get canonical server info for all local wikis in the map that have one.
Definition: WikiMap.php:187