123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- <?php
- /**
- * Represents a single site.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @since 1.21
- *
- * @file
- * @ingroup Site
- *
- * @license GPL-2.0-or-later
- * @author Jeroen De Dauw < jeroendedauw@gmail.com >
- */
- class Site implements Serializable {
- const TYPE_UNKNOWN = 'unknown';
- const TYPE_MEDIAWIKI = 'mediawiki';
- const GROUP_NONE = 'none';
- const ID_INTERWIKI = 'interwiki';
- const ID_EQUIVALENT = 'equivalent';
- const SOURCE_LOCAL = 'local';
- const PATH_LINK = 'link';
- /**
- * A version ID that identifies the serialization structure used by getSerializationData()
- * and unserialize(). This is useful for constructing cache keys in cases where the cache relies
- * on serialization for storing the SiteList.
- *
- * @var string A string uniquely identifying the version of the serialization structure.
- */
- const SERIAL_VERSION_ID = '2013-01-23';
- /**
- * @since 1.21
- *
- * @var string|null
- */
- protected $globalId = null;
- /**
- * @since 1.21
- *
- * @var string
- */
- protected $type = self::TYPE_UNKNOWN;
- /**
- * @since 1.21
- *
- * @var string
- */
- protected $group = self::GROUP_NONE;
- /**
- * @since 1.21
- *
- * @var string
- */
- protected $source = self::SOURCE_LOCAL;
- /**
- * @since 1.21
- *
- * @var string|null
- */
- protected $languageCode = null;
- /**
- * Holds the local ids for this site.
- * local id type => [ ids for this type (strings) ]
- *
- * @since 1.21
- *
- * @var array[]|false
- */
- protected $localIds = [];
- /**
- * @since 1.21
- *
- * @var array
- */
- protected $extraData = [];
- /**
- * @since 1.21
- *
- * @var array
- */
- protected $extraConfig = [];
- /**
- * @since 1.21
- *
- * @var bool
- */
- protected $forward = false;
- /**
- * @since 1.21
- *
- * @var int|null
- */
- protected $internalId = null;
- /**
- * @since 1.21
- *
- * @param string $type
- */
- public function __construct( $type = self::TYPE_UNKNOWN ) {
- $this->type = $type;
- }
- /**
- * Returns the global site identifier (ie enwiktionary).
- *
- * @since 1.21
- *
- * @return string|null
- */
- public function getGlobalId() {
- return $this->globalId;
- }
- /**
- * Sets the global site identifier (ie enwiktionary).
- *
- * @since 1.21
- *
- * @param string|null $globalId
- *
- * @throws MWException
- */
- public function setGlobalId( $globalId ) {
- if ( $globalId !== null && !is_string( $globalId ) ) {
- throw new MWException( '$globalId needs to be string or null' );
- }
- $this->globalId = $globalId;
- }
- /**
- * Returns the type of the site (ie mediawiki).
- *
- * @since 1.21
- *
- * @return string
- */
- public function getType() {
- return $this->type;
- }
- /**
- * Gets the group of the site (ie wikipedia).
- *
- * @since 1.21
- *
- * @return string
- */
- public function getGroup() {
- return $this->group;
- }
- /**
- * Sets the group of the site (ie wikipedia).
- *
- * @since 1.21
- *
- * @param string $group
- *
- * @throws MWException
- */
- public function setGroup( $group ) {
- if ( !is_string( $group ) ) {
- throw new MWException( '$group needs to be a string' );
- }
- $this->group = $group;
- }
- /**
- * Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
- *
- * @since 1.21
- *
- * @return string
- */
- public function getSource() {
- return $this->source;
- }
- /**
- * Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
- *
- * @since 1.21
- *
- * @param string $source
- *
- * @throws MWException
- */
- public function setSource( $source ) {
- if ( !is_string( $source ) ) {
- throw new MWException( '$source needs to be a string' );
- }
- $this->source = $source;
- }
- /**
- * Gets if site.tld/path/key:pageTitle should forward users to the page on
- * the actual site, where "key" is the local identifier.
- *
- * @since 1.21
- *
- * @return bool
- */
- public function shouldForward() {
- return $this->forward;
- }
- /**
- * Sets if site.tld/path/key:pageTitle should forward users to the page on
- * the actual site, where "key" is the local identifier.
- *
- * @since 1.21
- *
- * @param bool $shouldForward
- *
- * @throws MWException
- */
- public function setForward( $shouldForward ) {
- if ( !is_bool( $shouldForward ) ) {
- throw new MWException( '$shouldForward needs to be a boolean' );
- }
- $this->forward = $shouldForward;
- }
- /**
- * Returns the domain of the site, ie en.wikipedia.org
- * Or false if it's not known.
- *
- * @since 1.21
- *
- * @return string|null
- */
- public function getDomain() {
- $path = $this->getLinkPath();
- if ( $path === null ) {
- return null;
- }
- return parse_url( $path, PHP_URL_HOST );
- }
- /**
- * Returns the protocol of the site.
- *
- * @since 1.21
- *
- * @throws MWException
- * @return string
- */
- public function getProtocol() {
- $path = $this->getLinkPath();
- if ( $path === null ) {
- return '';
- }
- $protocol = parse_url( $path, PHP_URL_SCHEME );
- // Malformed URL
- if ( $protocol === false ) {
- throw new MWException( "failed to parse URL '$path'" );
- }
- // No schema
- if ( $protocol === null ) {
- // Used for protocol relative URLs
- $protocol = '';
- }
- return $protocol;
- }
- /**
- * Sets the path used to construct links with.
- * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
- *
- * @param string $fullUrl
- *
- * @since 1.21
- *
- * @throws MWException
- */
- public function setLinkPath( $fullUrl ) {
- $type = $this->getLinkPathType();
- if ( $type === null ) {
- throw new MWException( "This Site does not support link paths." );
- }
- $this->setPath( $type, $fullUrl );
- }
- /**
- * Returns the path used to construct links with or false if there is no such path.
- *
- * Shall be equivalent to getPath( getLinkPathType() ).
- *
- * @return string|null
- */
- public function getLinkPath() {
- $type = $this->getLinkPathType();
- return $type === null ? null : $this->getPath( $type );
- }
- /**
- * Returns the main path type, that is the type of the path that should
- * generally be used to construct links to the target site.
- *
- * This default implementation returns Site::PATH_LINK as the default path
- * type. Subclasses can override this to define a different default path
- * type, or return false to disable site links.
- *
- * @since 1.21
- *
- * @return string|null
- */
- public function getLinkPathType() {
- return self::PATH_LINK;
- }
- /**
- * Returns the full URL for the given page on the site.
- * Or null if the needed information is not known.
- *
- * This generated URL is usually based upon the path returned by getLinkPath(),
- * but this is not a requirement.
- *
- * This implementation returns a URL constructed using the path returned by getLinkPath().
- *
- * @since 1.21
- *
- * @param bool|string $pageName
- *
- * @return string|null
- */
- public function getPageUrl( $pageName = false ) {
- $url = $this->getLinkPath();
- if ( $url === null ) {
- return null;
- }
- if ( $pageName !== false ) {
- $url = str_replace( '$1', rawurlencode( $pageName ), $url );
- }
- return $url;
- }
- /**
- * Attempt to normalize the page name in some fashion.
- * May return false to indicate various kinds of failure.
- *
- * This implementation returns $pageName without changes.
- *
- * @see Site::normalizePageName
- *
- * @since 1.21
- *
- * @param string $pageName
- *
- * @return string|false
- */
- public function normalizePageName( $pageName ) {
- return $pageName;
- }
- /**
- * Returns the type specific fields.
- *
- * @since 1.21
- *
- * @return array
- */
- public function getExtraData() {
- return $this->extraData;
- }
- /**
- * Sets the type specific fields.
- *
- * @since 1.21
- *
- * @param array $extraData
- */
- public function setExtraData( array $extraData ) {
- $this->extraData = $extraData;
- }
- /**
- * Returns the type specific config.
- *
- * @since 1.21
- *
- * @return array
- */
- public function getExtraConfig() {
- return $this->extraConfig;
- }
- /**
- * Sets the type specific config.
- *
- * @since 1.21
- *
- * @param array $extraConfig
- */
- public function setExtraConfig( array $extraConfig ) {
- $this->extraConfig = $extraConfig;
- }
- /**
- * Returns language code of the sites primary language.
- * Or null if it's not known.
- *
- * @since 1.21
- *
- * @return string|null
- */
- public function getLanguageCode() {
- return $this->languageCode;
- }
- /**
- * Sets language code of the sites primary language.
- *
- * @since 1.21
- *
- * @param string|null $languageCode
- */
- public function setLanguageCode( $languageCode ) {
- if ( $languageCode !== null && !Language::isValidCode( $languageCode ) ) {
- throw new InvalidArgumentException( "$languageCode is not a valid language code." );
- }
- $this->languageCode = $languageCode;
- }
- /**
- * Returns the set internal identifier for the site.
- *
- * @since 1.21
- *
- * @return string|null
- */
- public function getInternalId() {
- return $this->internalId;
- }
- /**
- * Sets the internal identifier for the site.
- * This typically is a primary key in a db table.
- *
- * @since 1.21
- *
- * @param int|null $internalId
- */
- public function setInternalId( $internalId = null ) {
- $this->internalId = $internalId;
- }
- /**
- * Adds a local identifier.
- *
- * @since 1.21
- *
- * @param string $type
- * @param string $identifier
- */
- public function addLocalId( $type, $identifier ) {
- if ( $this->localIds === false ) {
- $this->localIds = [];
- }
- if ( !array_key_exists( $type, $this->localIds ) ) {
- $this->localIds[$type] = [];
- }
- if ( !in_array( $identifier, $this->localIds[$type] ) ) {
- $this->localIds[$type][] = $identifier;
- }
- }
- /**
- * Adds an interwiki id to the site.
- *
- * @since 1.21
- *
- * @param string $identifier
- */
- public function addInterwikiId( $identifier ) {
- $this->addLocalId( self::ID_INTERWIKI, $identifier );
- }
- /**
- * Adds a navigation id to the site.
- *
- * @since 1.21
- *
- * @param string $identifier
- */
- public function addNavigationId( $identifier ) {
- $this->addLocalId( self::ID_EQUIVALENT, $identifier );
- }
- /**
- * Returns the interwiki link identifiers that can be used for this site.
- *
- * @since 1.21
- *
- * @return string[]
- */
- public function getInterwikiIds() {
- return array_key_exists( self::ID_INTERWIKI, $this->localIds )
- ? $this->localIds[self::ID_INTERWIKI]
- : [];
- }
- /**
- * Returns the equivalent link identifiers that can be used to make
- * the site show up in interfaces such as the "language links" section.
- *
- * @since 1.21
- *
- * @return string[]
- */
- public function getNavigationIds() {
- return array_key_exists( self::ID_EQUIVALENT, $this->localIds )
- ? $this->localIds[self::ID_EQUIVALENT] :
- [];
- }
- /**
- * Returns all local ids
- *
- * @since 1.21
- *
- * @return array[]
- */
- public function getLocalIds() {
- return $this->localIds;
- }
- /**
- * Sets the path used to construct links with.
- * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ).
- *
- * @since 1.21
- *
- * @param string $pathType
- * @param string $fullUrl
- *
- * @throws MWException
- */
- public function setPath( $pathType, $fullUrl ) {
- if ( !is_string( $fullUrl ) ) {
- throw new MWException( '$fullUrl needs to be a string' );
- }
- if ( !array_key_exists( 'paths', $this->extraData ) ) {
- $this->extraData['paths'] = [];
- }
- $this->extraData['paths'][$pathType] = $fullUrl;
- }
- /**
- * Returns the path of the provided type or false if there is no such path.
- *
- * @since 1.21
- *
- * @param string $pathType
- *
- * @return string|null
- */
- public function getPath( $pathType ) {
- $paths = $this->getAllPaths();
- return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : null;
- }
- /**
- * Returns the paths as associative array.
- * The keys are path types, the values are the path urls.
- *
- * @since 1.21
- *
- * @return string[]
- */
- public function getAllPaths() {
- return array_key_exists( 'paths', $this->extraData ) ? $this->extraData['paths'] : [];
- }
- /**
- * Removes the path of the provided type if it's set.
- *
- * @since 1.21
- *
- * @param string $pathType
- */
- public function removePath( $pathType ) {
- if ( array_key_exists( 'paths', $this->extraData ) ) {
- unset( $this->extraData['paths'][$pathType] );
- }
- }
- /**
- * @since 1.21
- *
- * @param string $siteType
- *
- * @return Site
- */
- public static function newForType( $siteType ) {
- global $wgSiteTypes;
- if ( array_key_exists( $siteType, $wgSiteTypes ) ) {
- return new $wgSiteTypes[$siteType]();
- }
- return new Site();
- }
- /**
- * @see Serializable::serialize
- *
- * @since 1.21
- *
- * @return string
- */
- public function serialize() {
- $fields = [
- 'globalid' => $this->globalId,
- 'type' => $this->type,
- 'group' => $this->group,
- 'source' => $this->source,
- 'language' => $this->languageCode,
- 'localids' => $this->localIds,
- 'config' => $this->extraConfig,
- 'data' => $this->extraData,
- 'forward' => $this->forward,
- 'internalid' => $this->internalId,
- ];
- return serialize( $fields );
- }
- /**
- * @see Serializable::unserialize
- *
- * @since 1.21
- *
- * @param string $serialized
- */
- public function unserialize( $serialized ) {
- $fields = unserialize( $serialized );
- $this->__construct( $fields['type'] );
- $this->setGlobalId( $fields['globalid'] );
- $this->setGroup( $fields['group'] );
- $this->setSource( $fields['source'] );
- $this->setLanguageCode( $fields['language'] );
- $this->localIds = $fields['localids'];
- $this->setExtraConfig( $fields['config'] );
- $this->setExtraData( $fields['data'] );
- $this->setForward( $fields['forward'] );
- $this->setInternalId( $fields['internalid'] );
- }
- }
|