MediaWiki  1.28.0
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 
69  // If we don't have a canonical server or a path containing $1, the
70  // WikiReference isn't going to function properly. Just return null in
71  // that case.
72  if ( !is_string( $canonicalServer ) || !is_string( $path ) || strpos( $path, '$1' ) === false ) {
73  return null;
74  }
75 
76  return new WikiReference( $canonicalServer, $path, $server );
77  }
78 
83  private static function getWikiWikiReferenceFromSites( $wikiID ) {
84  $siteLookup = \MediaWiki\MediaWikiServices::getInstance()->getSiteLookup();
85  $site = $siteLookup->getSite( $wikiID );
86 
87  if ( !$site instanceof MediaWikiSite ) {
88  // Abort if not a MediaWikiSite, as this is about Wikis
89  return null;
90  }
91 
92  $urlParts = wfParseUrl( $site->getPageUrl() );
93  if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
94  // We can't create a meaningful WikiReference without URLs
95  return null;
96  }
97 
98  // XXX: Check whether path contains a $1?
99  $path = $urlParts['path'];
100  if ( isset( $urlParts['query'] ) ) {
101  $path .= '?' . $urlParts['query'];
102  }
103 
104  $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
105  $canonicalServer .= '://' . $urlParts['host'];
106 
107  return new WikiReference( $canonicalServer, $path );
108  }
109 
117  public static function getWikiName( $wikiID ) {
118  $wiki = WikiMap::getWiki( $wikiID );
119 
120  if ( $wiki ) {
121  return $wiki->getDisplayName();
122  }
123  return $wikiID;
124  }
125 
134  public static function foreignUserLink( $wikiID, $user, $text = null ) {
135  return self::makeForeignLink( $wikiID, "User:$user", $text );
136  }
137 
146  public static function makeForeignLink( $wikiID, $page, $text = null ) {
147  if ( !$text ) {
148  $text = $page;
149  }
150 
151  $url = self::getForeignURL( $wikiID, $page );
152  if ( $url === false ) {
153  return false;
154  }
155 
156  return Linker::makeExternalLink( $url, $text );
157  }
158 
168  public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
169  $wiki = WikiMap::getWiki( $wikiID );
170 
171  if ( $wiki ) {
172  return $wiki->getFullUrl( $page, $fragmentId );
173  }
174 
175  return false;
176  }
177 }
178 
184  private $mServer;
185  private $mPath;
186 
192  public function __construct( $canonicalServer, $path, $server = null ) {
193  $this->mCanonicalServer = $canonicalServer;
194  $this->mPath = $path;
195  $this->mServer = $server === null ? $canonicalServer : $server;
196  }
197 
204  public function getDisplayName() {
205  $parsed = wfParseUrl( $this->mCanonicalServer );
206  if ( $parsed ) {
207  return $parsed['host'];
208  } else {
209  // Invalid server spec.
210  // There's no sane thing to do here, so just return the canonical server name in full.
212  }
213  }
214 
226  private function getLocalUrl( $page, $fragmentId = null ) {
227  $page = wfUrlencode( str_replace( ' ', '_', $page ) );
228 
229  if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
230  $page .= '#' . wfUrlencode( $fragmentId );
231  }
232 
233  return str_replace( '$1', $page, $this->mPath );
234  }
235 
244  public function getCanonicalUrl( $page, $fragmentId = null ) {
245  return $this->mCanonicalServer . $this->getLocalUrl( $page, $fragmentId );
246  }
247 
252  public function getCanonicalServer() {
254  }
255 
263  public function getUrl( $page, $fragmentId = null ) {
264  return $this->getCanonicalUrl( $page, $fragmentId );
265  }
266 
276  public function getFullUrl( $page, $fragmentId = null ) {
277  return $this->mServer .
278  $this->getLocalUrl( $page, $fragmentId );
279  }
280 }
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:192
static getWikiWikiReferenceFromSites($wikiID)
Definition: WikiMap.php:83
$mServer
server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
Definition: WikiMap.php:184
static foreignUserLink($wikiID, $user, $text=null)
Convenience to get a link to a user page on a foreign wiki.
Definition: WikiMap.php:134
static getWikiName($wikiID)
Convenience to get the wiki's display name.
Definition: WikiMap.php:117
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:183
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:168
getFullUrl($page, $fragmentId=null)
Get a URL based on $wgServer, like Title::getFullURL() would produce when called locally on the wiki...
Definition: WikiMap.php:276
Reference to a locally-hosted wiki.
Definition: WikiMap.php:182
getCanonicalUrl($page, $fragmentId=null)
Get a canonical (i.e.
Definition: WikiMap.php:244
getCanonicalServer()
Get a canonical server URL.
Definition: WikiMap.php:252
static makeForeignLink($wikiID, $page, $text=null)
Convenience to get a link to a page on a foreign wiki.
Definition: WikiMap.php:146
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:934
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:263
$mPath
path, '/wiki/$1'
Definition: WikiMap.php:185
getDisplayName()
Get the URL in a way to be displayed to the user More or less Wikimedia specific. ...
Definition: WikiMap.php:204
static getWiki($wikiID)
Get a WikiReference object for $wikiID.
Definition: WikiMap.php:34
getLocalUrl($page, $fragmentId=null)
Helper function for getUrl()
Definition: WikiMap.php:226
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:2491