MediaWiki  1.33.0
Site.php
Go to the documentation of this file.
1 <?php
2 
29 class Site implements Serializable {
30  const TYPE_UNKNOWN = 'unknown';
31  const TYPE_MEDIAWIKI = 'mediawiki';
32 
33  const GROUP_NONE = 'none';
34 
35  const ID_INTERWIKI = 'interwiki';
36  const ID_EQUIVALENT = 'equivalent';
37 
38  const SOURCE_LOCAL = 'local';
39 
40  const PATH_LINK = 'link';
41 
49  const SERIAL_VERSION_ID = '2013-01-23';
50 
56  protected $globalId = null;
57 
64 
70  protected $group = self::GROUP_NONE;
71 
78 
84  protected $languageCode = null;
85 
94  protected $localIds = [];
95 
101  protected $extraData = [];
102 
108  protected $extraConfig = [];
109 
115  protected $forward = false;
116 
122  protected $internalId = null;
123 
129  public function __construct( $type = self::TYPE_UNKNOWN ) {
130  $this->type = $type;
131  }
132 
140  public function getGlobalId() {
141  return $this->globalId;
142  }
143 
153  public function setGlobalId( $globalId ) {
154  if ( $globalId !== null && !is_string( $globalId ) ) {
155  throw new MWException( '$globalId needs to be string or null' );
156  }
157 
158  $this->globalId = $globalId;
159  }
160 
168  public function getType() {
169  return $this->type;
170  }
171 
179  public function getGroup() {
180  return $this->group;
181  }
182 
192  public function setGroup( $group ) {
193  if ( !is_string( $group ) ) {
194  throw new MWException( '$group needs to be a string' );
195  }
196 
197  $this->group = $group;
198  }
199 
207  public function getSource() {
208  return $this->source;
209  }
210 
220  public function setSource( $source ) {
221  if ( !is_string( $source ) ) {
222  throw new MWException( '$source needs to be a string' );
223  }
224 
225  $this->source = $source;
226  }
227 
236  public function shouldForward() {
237  return $this->forward;
238  }
239 
250  public function setForward( $shouldForward ) {
251  if ( !is_bool( $shouldForward ) ) {
252  throw new MWException( '$shouldForward needs to be a boolean' );
253  }
254 
255  $this->forward = $shouldForward;
256  }
257 
266  public function getDomain() {
267  $path = $this->getLinkPath();
268 
269  if ( $path === null ) {
270  return null;
271  }
272 
273  return parse_url( $path, PHP_URL_HOST );
274  }
275 
284  public function getProtocol() {
285  $path = $this->getLinkPath();
286 
287  if ( $path === null ) {
288  return '';
289  }
290 
291  $protocol = parse_url( $path, PHP_URL_SCHEME );
292 
293  // Malformed URL
294  if ( $protocol === false ) {
295  throw new MWException( "failed to parse URL '$path'" );
296  }
297 
298  // No schema
299  if ( $protocol === null ) {
300  // Used for protocol relative URLs
301  $protocol = '';
302  }
303 
304  return $protocol;
305  }
306 
317  public function setLinkPath( $fullUrl ) {
318  $type = $this->getLinkPathType();
319 
320  if ( $type === null ) {
321  throw new MWException( "This Site does not support link paths." );
322  }
323 
324  $this->setPath( $type, $fullUrl );
325  }
326 
334  public function getLinkPath() {
335  $type = $this->getLinkPathType();
336  return $type === null ? null : $this->getPath( $type );
337  }
338 
351  public function getLinkPathType() {
352  return self::PATH_LINK;
353  }
354 
370  public function getPageUrl( $pageName = false ) {
371  $url = $this->getLinkPath();
372 
373  if ( $url === false ) {
374  return false;
375  }
376 
377  if ( $pageName !== false ) {
378  $url = str_replace( '$1', rawurlencode( $pageName ), $url );
379  }
380 
381  return $url;
382  }
383 
398  public function normalizePageName( $pageName ) {
399  return $pageName;
400  }
401 
409  public function getExtraData() {
410  return $this->extraData;
411  }
412 
420  public function setExtraData( array $extraData ) {
421  $this->extraData = $extraData;
422  }
423 
431  public function getExtraConfig() {
432  return $this->extraConfig;
433  }
434 
442  public function setExtraConfig( array $extraConfig ) {
443  $this->extraConfig = $extraConfig;
444  }
445 
454  public function getLanguageCode() {
455  return $this->languageCode;
456  }
457 
465  public function setLanguageCode( $languageCode ) {
466  if ( $languageCode !== null && !Language::isValidCode( $languageCode ) ) {
467  throw new InvalidArgumentException( "$languageCode is not a valid language code." );
468  }
469  $this->languageCode = $languageCode;
470  }
471 
479  public function getInternalId() {
480  return $this->internalId;
481  }
482 
491  public function setInternalId( $internalId = null ) {
492  $this->internalId = $internalId;
493  }
494 
503  public function addLocalId( $type, $identifier ) {
504  if ( $this->localIds === false ) {
505  $this->localIds = [];
506  }
507 
508  if ( !array_key_exists( $type, $this->localIds ) ) {
509  $this->localIds[$type] = [];
510  }
511 
512  if ( !in_array( $identifier, $this->localIds[$type] ) ) {
513  $this->localIds[$type][] = $identifier;
514  }
515  }
516 
524  public function addInterwikiId( $identifier ) {
525  $this->addLocalId( self::ID_INTERWIKI, $identifier );
526  }
527 
535  public function addNavigationId( $identifier ) {
536  $this->addLocalId( self::ID_EQUIVALENT, $identifier );
537  }
538 
546  public function getInterwikiIds() {
547  return array_key_exists( self::ID_INTERWIKI, $this->localIds )
548  ? $this->localIds[self::ID_INTERWIKI]
549  : [];
550  }
551 
560  public function getNavigationIds() {
561  return array_key_exists( self::ID_EQUIVALENT, $this->localIds )
562  ? $this->localIds[self::ID_EQUIVALENT] :
563  [];
564  }
565 
573  public function getLocalIds() {
574  return $this->localIds;
575  }
576 
588  public function setPath( $pathType, $fullUrl ) {
589  if ( !is_string( $fullUrl ) ) {
590  throw new MWException( '$fullUrl needs to be a string' );
591  }
592 
593  if ( !array_key_exists( 'paths', $this->extraData ) ) {
594  $this->extraData['paths'] = [];
595  }
596 
597  $this->extraData['paths'][$pathType] = $fullUrl;
598  }
599 
609  public function getPath( $pathType ) {
610  $paths = $this->getAllPaths();
611  return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : null;
612  }
613 
622  public function getAllPaths() {
623  return array_key_exists( 'paths', $this->extraData ) ? $this->extraData['paths'] : [];
624  }
625 
633  public function removePath( $pathType ) {
634  if ( array_key_exists( 'paths', $this->extraData ) ) {
635  unset( $this->extraData['paths'][$pathType] );
636  }
637  }
638 
646  public static function newForType( $siteType ) {
647  global $wgSiteTypes;
648 
649  if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
650  return new $wgSiteTypes[$siteType]();
651  }
652 
653  return new Site();
654  }
655 
663  public function serialize() {
664  $fields = [
665  'globalid' => $this->globalId,
666  'type' => $this->type,
667  'group' => $this->group,
668  'source' => $this->source,
669  'language' => $this->languageCode,
670  'localids' => $this->localIds,
671  'config' => $this->extraConfig,
672  'data' => $this->extraData,
673  'forward' => $this->forward,
674  'internalid' => $this->internalId,
675 
676  ];
677 
678  return serialize( $fields );
679  }
680 
688  public function unserialize( $serialized ) {
689  $fields = unserialize( $serialized );
690 
691  $this->__construct( $fields['type'] );
692 
693  $this->setGlobalId( $fields['globalid'] );
694  $this->setGroup( $fields['group'] );
695  $this->setSource( $fields['source'] );
696  $this->setLanguageCode( $fields['language'] );
697  $this->localIds = $fields['localids'];
698  $this->setExtraConfig( $fields['config'] );
699  $this->setExtraData( $fields['data'] );
700  $this->setForward( $fields['forward'] );
701  $this->setInternalId( $fields['internalid'] );
702  }
703 }
Site\ID_EQUIVALENT
const ID_EQUIVALENT
Definition: Site.php:36
Site\getInterwikiIds
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:546
Site\$extraConfig
array $extraConfig
Definition: Site.php:108
source
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify as strings Extensions should add to this list prev or next refreshes the diff cache allow viewing deleted revs source
Definition: hooks.txt:1640
Site\getType
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:168
Site\getDomain
getDomain()
Returns the domain of the site, ie en.wikipedia.org Or false if it's not known.
Definition: Site.php:266
Site\$languageCode
string null $languageCode
Definition: Site.php:84
Site\setSource
setSource( $source)
Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:220
Site\getPageUrl
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition: Site.php:370
Site\getInternalId
getInternalId()
Returns the set internal identifier for the site.
Definition: Site.php:479
Site\setLinkPath
setLinkPath( $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:317
$serialized
foreach( $res as $row) $serialized
Definition: testCompression.php:81
Site\setGroup
setGroup( $group)
Sets the group of the site (ie wikipedia).
Definition: Site.php:192
Site\setInternalId
setInternalId( $internalId=null)
Sets the internal identifier for the site.
Definition: Site.php:491
Site\getPath
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:609
Site\getLanguageCode
getLanguageCode()
Returns language code of the sites primary language.
Definition: Site.php:454
Site\ID_INTERWIKI
const ID_INTERWIKI
Definition: Site.php:35
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:35
Site\newForType
static newForType( $siteType)
Definition: Site.php:646
Site\$type
string $type
Definition: Site.php:63
Site\$internalId
int null $internalId
Definition: Site.php:122
Site\__construct
__construct( $type=self::TYPE_UNKNOWN)
Definition: Site.php:129
Site\serialize
serialize()
Definition: Site.php:663
MWException
MediaWiki exception.
Definition: MWException.php:26
Site\$globalId
string null $globalId
Definition: Site.php:56
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:30
Site\getGlobalId
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:140
Site\getSource
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:207
Site\setForward
setForward( $shouldForward)
Sets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:250
Site\setLanguageCode
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition: Site.php:465
Site\getGroup
getGroup()
Gets the group of the site (ie wikipedia).
Definition: Site.php:179
Site\addInterwikiId
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition: Site.php:524
Site\addLocalId
addLocalId( $type, $identifier)
Adds a local identifier.
Definition: Site.php:503
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
Site\unserialize
unserialize( $serialized)
Definition: Site.php:688
Site
Definition: Site.php:29
Site\getLinkPathType
getLinkPathType()
Returns the main path type, that is the type of the path that should generally be used to construct l...
Definition: Site.php:351
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
Site\$localIds
array[] $localIds
Holds the local ids for this site.
Definition: Site.php:94
Site\setExtraData
setExtraData(array $extraData)
Sets the type specific fields.
Definition: Site.php:420
Site\addNavigationId
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition: Site.php:535
Site\$group
string $group
Definition: Site.php:70
Site\setPath
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:588
Site\$extraData
array $extraData
Definition: Site.php:101
Site\getNavigationIds
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition: Site.php:560
Site\getExtraConfig
getExtraConfig()
Returns the type specific config.
Definition: Site.php:431
Site\getAllPaths
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:622
Site\removePath
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition: Site.php:633
Site\$forward
bool $forward
Definition: Site.php:115
Language\isValidCode
static isValidCode( $code)
Returns true if a language code string is of a valid form, whether or not it exists.
Definition: Language.php:387
group
invalid e g too many</span ></p > ! end ! test with< references/> in group ! wikitext Wikipedia rocks< ref > Proceeds of vol XXI</ref > Wikipedia rocks< ref group="note"> Proceeds of vol XXI</ref >< references/>< references group="note"/> ! html< p > Wikipedia rocks< sup id="cite_ref-1" class="reference">< a href="#cite_note-1"> &Wikipedia rocks< sup id="cite_ref-2" class="reference">< a href="#cite_note-2"> &</p >< div class="mw-references-wrap">< ol class="references">< li id="cite_note-1">< span class="mw-cite-backlink">< a href="#cite_ref-1"> ↑</a ></span >< span class="reference-text"> Proceeds of vol XXI</span ></li ></ol ></div >< div class="mw-references-wrap">< ol class="references">< li id="cite_note-2">< span class="mw-cite-backlink">< a href="#cite_ref-2"> ↑</a ></span >< span class="reference-text"> Proceeds of vol XXI</span ></li ></ol ></div > ! end ! test with< references/> in group
Definition: citeParserTests.txt:349
$wgSiteTypes
$wgSiteTypes
Register handlers for specific types of sites.
Definition: DefaultSettings.php:8617
Site\setExtraConfig
setExtraConfig(array $extraConfig)
Sets the type specific config.
Definition: Site.php:442
Site\setGlobalId
setGlobalId( $globalId)
Sets the global site identifier (ie enwiktionary).
Definition: Site.php:153
$path
$path
Definition: NoLocalSettings.php:25
Site\normalizePageName
normalizePageName( $pageName)
Attempt to normalize the page name in some fashion.
Definition: Site.php:398
Site\shouldForward
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:236
Site\GROUP_NONE
const GROUP_NONE
Definition: Site.php:33
Site\SOURCE_LOCAL
const SOURCE_LOCAL
Definition: Site.php:38
Site\PATH_LINK
const PATH_LINK
Definition: Site.php:40
Site\getProtocol
getProtocol()
Returns the protocol of the site.
Definition: Site.php:284
Site\TYPE_MEDIAWIKI
const TYPE_MEDIAWIKI
Definition: Site.php:31
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:22
Site\$source
string $source
Definition: Site.php:77
Site\getExtraData
getExtraData()
Returns the type specific fields.
Definition: Site.php:409
Site\getLinkPath
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition: Site.php:334
Site\getLocalIds
getLocalIds()
Returns all local ids.
Definition: Site.php:573