X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fsite%2FSite.php;h=5394860e02863b4d48c46e17fb806504b6caa841;hb=ec493cc7508e4741e370cbcd10b59ba636c13113;hp=200a006608e76932a2836700b019fe22093fb081;hpb=83783cd93399fd7526aacf629e448ad3fa80e23c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/site/Site.php b/includes/site/Site.php index 200a006608..5394860e02 100644 --- a/includes/site/Site.php +++ b/includes/site/Site.php @@ -1,7 +1,7 @@ */ -interface Site { +class Site implements Serializable { const TYPE_UNKNOWN = 'unknown'; const TYPE_MEDIAWIKI = 'mediawiki'; @@ -38,42 +38,140 @@ interface Site { const SOURCE_LOCAL = 'local'; + const PATH_LINK = 'link'; + + /** - * Returns the global site identifier (ie enwiktionary). + * 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 * - * @return string + * @var string|null */ - public function getGlobalId(); + protected $globalId = null; /** - * Sets the global site identifier (ie enwiktionary). + * @since 1.21 * + * @var string + */ + protected $type = self::TYPE_UNKNOWN; + + /** * @since 1.21 * - * @param string $globalId + * @var string */ - public function setGlobalId( $globalId ); + protected $group = self::GROUP_NONE; /** - * Returns the type of the site (ie mediawiki). + * @since 1.21 * + * @var string + */ + protected $source = self::SOURCE_LOCAL; + + /** * @since 1.21 * - * @return string + * @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[] + */ + protected $localIds = array(); + + /** + * @since 1.21 + * + * @var array + */ + protected $extraData = array(); + + /** + * @since 1.21 + * + * @var array + */ + protected $extraConfig = array(); + + /** + * @since 1.21 + * + * @var bool */ - public function getType(); + protected $forward = false; /** - * Sets the type of the site (ie mediawiki). - * TODO: remove, we cannot change this after instantiation + * @since 1.21 + * + * @var int|null + */ + protected $internalId = null; + + /** + * Constructor. * * @since 1.21 * * @param string $type */ - public function setType( $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 type of the site (ie wikipedia). @@ -82,7 +180,9 @@ interface Site { * * @return string */ - public function getGroup(); + public function getGroup() { + return $this->group; + } /** * Sets the type of the site (ie wikipedia). @@ -90,8 +190,16 @@ interface Site { * @since 1.21 * * @param string $group + * + * @throws MWException */ - public function setGroup( $group ); + 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'). @@ -100,7 +208,9 @@ interface Site { * * @return string */ - public function getSource(); + public function getSource() { + return $this->source; + } /** * Sets the source of the site data (ie 'local', 'wikidata', 'my-magical-repo'). @@ -108,18 +218,46 @@ interface Site { * @since 1.21 * * @param string $source + * + * @throws MWException */ - public function setSource( $source ); + public function setSource( $source ) { + if ( !is_string( $source ) ) { + throw new MWException( '$source needs to be a string' ); + } + + $this->source = $source; + } /** - * Returns the protocol of the site, ie 'http://', 'irc://', '//' - * Or false if it's not known. + * 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 string|false + * @return boolean */ - public function getProtocol(); + 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 boolean $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 @@ -127,9 +265,95 @@ interface Site { * * @since 1.21 * - * @return string|false + * @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 getDomain(); + public function getLinkPathType() { + return self::PATH_LINK; + } /** * Returns the full URL for the given page on the site. @@ -138,79 +362,155 @@ interface Site { * 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 - * @see Site::getLinkPath() * - * @param bool|String $page + * @param bool|String $pageName * - * @return string|false + * @return string|boolean false */ - public function getPageUrl( $page = false ); + public function getPageUrl( $pageName = false ) { + $url = $this->getLinkPath(); + + if ( $url === false ) { + return false; + } + + if ( $pageName !== false ) { + $url = str_replace( '$1', rawurlencode( $pageName ), $url ) ; + } + + return $url; + } /** - * Returns language code of the sites primary language. - * Or false if it's not known. + * Returns $pageName without changes. + * Subclasses may override this to apply some kind of normalization. + * + * @see Site::normalizePageName * * @since 1.21 * - * @return string|false + * @param string $pageName + * + * @return string */ - public function getLanguageCode(); + public function normalizePageName( $pageName ) { + return $pageName; + } /** - * Sets language code of the sites primary language. + * Returns the type specific fields. * * @since 1.21 * - * @param string $languageCode + * @return array */ - public function setLanguageCode( $languageCode ); + public function getExtraData() { + return $this->extraData; + } /** - * Returns the normalized, canonical form of the given page name. - * How normalization is performed or what the properties of a normalized name are depends on the site. - * The general contract of this method is that the normalized form shall refer to the same content - * as the original form, and any other page name referring to the same content will have the same normalized form. + * Sets the type specific fields. * - * Note that this method may call out to the target site to perform the normalization, so it may be slow - * and fail due to IO errors. + * @since 1.21 + * + * @param array $extraData + */ + public function setExtraData( array $extraData ) { + $this->extraData = $extraData; + } + + /** + * Returns the type specific config. * * @since 1.21 * - * @param string $pageName + * @return array + */ + public function getExtraConfig() { + return $this->extraConfig; + } + + /** + * Sets the type specific config. * - * @return string the normalized page name + * @since 1.21 + * + * @param array $extraConfig */ - public function normalizePageName( $pageName ); + public function setExtraConfig( array $extraConfig ) { + $this->extraConfig = $extraConfig; + } /** - * Returns the interwiki link identifiers that can be used for this site. + * Returns language code of the sites primary language. + * Or null if it's not known. * * @since 1.21 * - * @return array of string + * @return string|null */ - public function getInterwikiIds(); + public function getLanguageCode() { + return $this->languageCode; + } /** - * Returns the equivalent link identifiers that can be used to make - * the site show up in interfaces such as the "language links" section. + * Sets language code of the sites primary language. * * @since 1.21 * - * @return array of string + * @param string $languageCode */ - public function getNavigationIds(); + public function setLanguageCode( $languageCode ) { + $this->languageCode = $languageCode; + } /** - * Adds an local identifier to the site. + * Returns the set internal identifier for the site. * * @since 1.21 * - * @param string $type The type of the identifier, element of the Site::ID_ enum + * @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 ); + public function addLocalId( $type, $identifier ) { + if ( $this->localIds === false ) { + $this->localIds = array(); + } + + if ( !array_key_exists( $type, $this->localIds ) ) { + $this->localIds[$type] = array(); + } + + if ( !in_array( $identifier, $this->localIds[$type] ) ) { + $this->localIds[$type][] = $identifier; + } + } /** * Adds an interwiki id to the site. @@ -219,7 +519,9 @@ interface Site { * * @param string $identifier */ - public function addInterwikiId( $identifier ); + public function addInterwikiId( $identifier ) { + $this->addLocalId( self::ID_INTERWIKI, $identifier ); + } /** * Adds a navigation id to the site. @@ -228,35 +530,66 @@ interface Site { * * @param string $identifier */ - public function addNavigationId( $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] : array(); + } /** - * Saves the site. + * 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 * - * @param string|null $functionName + * @return string[] */ - public function save( $functionName = null ); + public function getNavigationIds() { + return array_key_exists( self::ID_EQUIVALENT, $this->localIds ) ? $this->localIds[self::ID_EQUIVALENT] : array(); + } /** - * Returns the internal ID of the site. + * Returns all local ids * * @since 1.21 * - * @return integer + * @return array[] */ - public function getInternalId(); + public function getLocalIds() { + return $this->localIds; + } /** - * Sets the provided url as path of the specified type. + * 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 ); + 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'] = array(); + } + + $this->extraData['paths'][$pathType] = $fullUrl; + } /** * Returns the path of the provided type or false if there is no such path. @@ -265,52 +598,106 @@ interface Site { * * @param string $pathType * - * @return string|false + * @return string|null */ - public function getPath( $pathType ); + public function getPath( $pathType ) { + $paths = $this->getAllPaths(); + return array_key_exists( $pathType, $paths ) ? $paths[$pathType] : null; + } /** - * Sets the path used to construct links with. - * Shall be equivalent to setPath( getLinkPathType(), $fullUrl ). - * - * @param string $fullUrl + * Returns the paths as associative array. + * The keys are path types, the values are the path urls. * * @since 1.21 + * + * @return string[] */ - public function setLinkPath( $fullUrl ); + public function getAllPaths() { + return array_key_exists( 'paths', $this->extraData ) ? $this->extraData['paths'] : array(); + } /** - * Returns the path used to construct links with or false if there is no such path. - * Shall be equivalent to getPath( getLinkPathType() ). + * Removes the path of the provided type if it's set. + * + * @since 1.21 * - * @return string|false + * @param string $pathType */ - public function getLinkPath(); + public function removePath( $pathType ) { + if ( array_key_exists( 'paths', $this->extraData ) ) { + unset( $this->extraData['paths'][$pathType] ); + } + } /** - * Returns the path type used to construct links with. + * @since 1.21 + * + * @param string $siteType * - * @return string|false + * @return Site */ - public function getLinkPathType(); + public static function newForType( $siteType ) { + global $wgSiteTypes; + + if ( array_key_exists( $siteType, $wgSiteTypes ) ) { + return new $wgSiteTypes[$siteType](); + } + + return new Site(); + } /** - * Returns the paths as associative array. - * The keys are path types, the values are the path urls. + * @see Serializable::serialize * * @since 1.21 * - * @return array of string + * @return string */ - public function getAllPaths(); + public function serialize() { + $fields = array( + '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 ); + } /** - * Removes the path of the provided type if it's set. + * @see Serializable::unserialize * * @since 1.21 * - * @param string $pathType + * @param string $serialized */ - public function removePath( $pathType ); + 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'] ); + } -} \ No newline at end of file +} + +/** + * @deprecated + */ +class SiteObject extends Site {}