MediaWiki  1.27.1
WikiMap.php
Go to the documentation of this file.
1 <?php
26 class WikiMap {
27 
34  public static function getWiki( $wikiID ) {
35  $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
36  if ( $wikiReference ) {
37  return $wikiReference;
38  }
39 
40  // Try sites, if $wgConf failed
41  return self::getWikiWikiReferenceFromSites( $wikiID );
42  }
43 
48  private static function getWikiReferenceFromWgConf( $wikiID ) {
50 
51  $wgConf->loadFullData();
52 
53  list( $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  return new WikiReference( $canonicalServer, $path, $server );
69  }
70 
75  private static function getWikiWikiReferenceFromSites( $wikiID ) {
76  $siteLookup = \MediaWiki\MediaWikiServices::getInstance()->getSiteLookup();
77  $site = $siteLookup->getSite( $wikiID );
78 
79  if ( !$site instanceof MediaWikiSite ) {
80  // Abort if not a MediaWikiSite, as this is about Wikis
81  return null;
82  }
83 
84  $urlParts = wfParseUrl( $site->getPageUrl() );
85  if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
86  // We can't create a meaningful WikiReference without URLs
87  return null;
88  }
89 
90  // XXX: Check whether path contains a $1?
91  $path = $urlParts['path'];
92  if ( isset( $urlParts['query'] ) ) {
93  $path .= '?' . $urlParts['query'];
94  }
95 
96  $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
97  $canonicalServer .= '://' . $urlParts['host'];
98 
99  return new WikiReference( $canonicalServer, $path );
100  }
101 
109  public static function getWikiName( $wikiID ) {
110  $wiki = WikiMap::getWiki( $wikiID );
111 
112  if ( $wiki ) {
113  return $wiki->getDisplayName();
114  }
115  return $wikiID;
116  }
117 
126  public static function foreignUserLink( $wikiID, $user, $text = null ) {
127  return self::makeForeignLink( $wikiID, "User:$user", $text );
128  }
129 
138  public static function makeForeignLink( $wikiID, $page, $text = null ) {
139  if ( !$text ) {
140  $text = $page;
141  }
142 
143  $url = self::getForeignURL( $wikiID, $page );
144  if ( $url === false ) {
145  return false;
146  }
147 
148  return Linker::makeExternalLink( $url, $text );
149  }
150 
160  public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
161  $wiki = WikiMap::getWiki( $wikiID );
162 
163  if ( $wiki ) {
164  return $wiki->getFullUrl( $page, $fragmentId );
165  }
166 
167  return false;
168  }
169 }
170 
176  private $mServer;
177  private $mPath;
178 
184  public function __construct( $canonicalServer, $path, $server = null ) {
185  $this->mCanonicalServer = $canonicalServer;
186  $this->mPath = $path;
187  $this->mServer = $server === null ? $canonicalServer : $server;
188  }
189 
196  public function getDisplayName() {
197  $parsed = wfParseUrl( $this->mCanonicalServer );
198  if ( $parsed ) {
199  return $parsed['host'];
200  } else {
201  // Invalid server spec.
202  // There's no sane thing to do here, so just return the canonical server name in full.
204  }
205  }
206 
218  private function getLocalUrl( $page, $fragmentId = null ) {
219  $page = wfUrlencode( str_replace( ' ', '_', $page ) );
220 
221  if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
222  $page .= '#' . wfUrlencode( $fragmentId );
223  }
224 
225  return str_replace( '$1', $page, $this->mPath );
226  }
227 
236  public function getCanonicalUrl( $page, $fragmentId = null ) {
237  return $this->mCanonicalServer . $this->getLocalUrl( $page, $fragmentId );
238  }
239 
244  public function getCanonicalServer() {
246  }
247 
255  public function getUrl( $page, $fragmentId = null ) {
256  return $this->getCanonicalUrl( $page, $fragmentId );
257  }
258 
268  public function getFullUrl( $page, $fragmentId = null ) {
269  return $this->mServer .
270  $this->getLocalUrl( $page, $fragmentId );
271  }
272 }
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
__construct($canonicalServer, $path, $server=null)
Definition: WikiMap.php:184
static getWikiWikiReferenceFromSites($wikiID)
Definition: WikiMap.php:75
$mServer
server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
Definition: WikiMap.php:176
static foreignUserLink($wikiID, $user, $text=null)
Convenience to get a link to a user page on a foreign wiki.
Definition: WikiMap.php:126
static getWikiName($wikiID)
Convenience to get the wiki's display name.
Definition: WikiMap.php:109
wfUrlencode($s)
We want some things to be included as literal characters in our title URLs for prettiness, which urlencode encodes by default.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
$mCanonicalServer
canonical server URL, e.g. 'https://www.mediawiki.org'
Definition: WikiMap.php:175
Class representing a MediaWiki site.
static getForeignURL($wikiID, $page, $fragmentId=null)
Convenience to get a url to a page on a foreign wiki.
Definition: WikiMap.php:160
getFullUrl($page, $fragmentId=null)
Get a URL based on $wgServer, like Title::getFullURL() would produce when called locally on the wiki...
Definition: WikiMap.php:268
Reference to a locally-hosted wiki.
Definition: WikiMap.php:174
getCanonicalUrl($page, $fragmentId=null)
Get a canonical (i.e.
Definition: WikiMap.php:236
getCanonicalServer()
Get a canonical server URL.
Definition: WikiMap.php:244
static makeForeignLink($wikiID, $page, $text=null)
Convenience to get a link to a page on a foreign wiki.
Definition: WikiMap.php:138
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 local account $user
Definition: hooks.txt:242
static getWikiReferenceFromWgConf($wikiID)
Definition: WikiMap.php:48
$wgConf
wgConf hold the site configuration.
static makeExternalLink($url, $text, $escape=true, $linktype= '', $attribs=[], $title=null)
Make an external link.
Definition: Linker.php:1052
Helper tools for dealing with other wikis.
Definition: WikiMap.php:26
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:35
getUrl($page, $fragmentId=null)
Alias for getCanonicalUrl(), for backwards compatibility.
Definition: WikiMap.php:255
$mPath
path, '/wiki/$1'
Definition: WikiMap.php:177
getDisplayName()
Get the URL in a way to be displayed to the user More or less Wikimedia specific. ...
Definition: WikiMap.php:196
static getWiki($wikiID)
Get a WikiReference object for $wikiID.
Definition: WikiMap.php:34
getLocalUrl($page, $fragmentId=null)
Helper function for getUrl()
Definition: WikiMap.php:218
wfParseUrl($url)
parse_url() work-alike, but non-broken.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2338