1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Xomw: Add XomwSiteList and XomwGenericArrayObject

This commit is contained in:
gnosygnu
2017-03-18 09:49:11 -04:00
parent 5967c75433
commit a1d2e69211
12 changed files with 9211 additions and 357 deletions

View File

@@ -83,7 +83,7 @@ public class XomwSite {
*
* @var array[]
*/
private Hash_adp localIds;
private Ordered_hash localIds;
/**
* @since 1.21
@@ -492,12 +492,12 @@ public class XomwSite {
*/
public void addLocalId(String type, String identifier) {
if (this.localIds == null) {
this.localIds = Hash_adp_.New();
this.localIds = Ordered_hash_.New();
}
Hash_adp typeHash = (Hash_adp)this.localIds.Get_by(type);
Ordered_hash typeHash = (Ordered_hash)this.localIds.Get_by(type);
if (typeHash == null) {
typeHash = Hash_adp_.New();
typeHash = Ordered_hash_.New();
this.localIds.Add(type, typeHash);
}
@@ -535,8 +535,8 @@ public class XomwSite {
*
* @return String[]
*/
public Hash_adp getInterwikiIds() {
return (Hash_adp)this.localIds.Get_by(XomwSite.ID_INTERWIKI);
public Ordered_hash getInterwikiIds() {
return (Ordered_hash)this.localIds.Get_by(XomwSite.ID_INTERWIKI);
}
/**
@@ -547,8 +547,8 @@ public class XomwSite {
*
* @return String[]
*/
public Hash_adp getNavigationIds() {
return (Hash_adp)this.localIds.Get_by(XomwSite.ID_EQUIVALENT);
public Ordered_hash getNavigationIds() {
return (Ordered_hash)this.localIds.Get_by(XomwSite.ID_EQUIVALENT);
}
/**
@@ -558,7 +558,7 @@ public class XomwSite {
*
* @return array[]
*/
public Hash_adp getLocalIds() {
public Ordered_hash getLocalIds() {
return this.localIds;
}
@@ -635,20 +635,10 @@ public class XomwSite {
* @return Site
*/
public static XomwSite newForType(String siteType) {
// global $wgSiteTypes;
/*
//$wgSiteTypes = [
// 'mediawiki' => 'MediaWikiSite',
//];
Object o = XomwDefaultSettings.wgSiteTypes;
if (o != null) {
return new XomwMediaWikiSite()
String type = (String)XomwDefaultSettings.wgSiteTypes.Get_by(siteType);
if (String_.Eq(type, XomwDefaultSettings.wgSiteTypes__MediaWikiSite)) {
return new XomwMediaWikiSite();
}
*/
// if (array_key_exists($siteType, $wgSiteTypes)) {
// return new $wgSiteTypes[$siteType]();
// }
return new XomwSite(siteType);
}

View File

@@ -14,332 +14,340 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.xowa.mediawiki.includes.site; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*;
import gplx.xowa.mediawiki.includes.libs.*;
/**
* Collection of Site objects.
*/
public class XomwSiteList {
public int Len() {return 0;}
public class XomwSiteList extends XomwGenericArrayObject { public int Len() {return 0;}
public XomwSite GetAt(int idx) {return null;}
// /**
// * Internal site identifiers pointing to their sites offset value.
// *
// * @since 1.21
// *
// * @var array Array of integer
// */
// protected $byInternalId = [];
//
// /**
// * Global site identifiers pointing to their sites offset value.
// *
// * @since 1.21
// *
// * @var array Array of String
// */
// protected $byGlobalId = [];
//
// /**
// * Navigational site identifiers alias inter-language prefixes
// * pointing to their sites offset value.
// *
// * @since 1.23
// *
// * @var array Array of String
// */
// protected $byNavigationId = [];
//
// /**
// * @see GenericArrayObject::getObjectType
// *
// * @since 1.21
// *
// * @return String
// */
// public function getObjectType() {
// return 'Site';
// }
//
// /**
// * @see GenericArrayObject::preSetElement
// *
// * @since 1.21
// *
// * @param int|String $index
// * @param Site $site
// *
// * @return boolean
// */
// protected function preSetElement( $index, $site ) {
// if ( $this->hasSite( $site->getGlobalId() ) ) {
// $this->removeSite( $site->getGlobalId() );
// }
//
// $this->byGlobalId[$site->getGlobalId()] = $index;
// $this->byInternalId[$site->getInternalId()] = $index;
//
// $ids = $site->getNavigationIds();
// foreach ( $ids as $navId ) {
// $this->byNavigationId[$navId] = $index;
// }
//
// return true;
// }
//
// /**
// * @see ArrayObject::offsetUnset()
// *
// * @since 1.21
// *
// * @param mixed $index
// */
// public function offsetUnset( $index ) {
// if ( $this->offsetExists( $index ) ) {
// /**
// * @var Site $site
// */
// $site = $this->offsetGet( $index );
//
// unset( $this->byGlobalId[$site->getGlobalId()] );
// unset( $this->byInternalId[$site->getInternalId()] );
//
// $ids = $site->getNavigationIds();
// foreach ( $ids as $navId ) {
// unset( $this->byNavigationId[$navId] );
// }
// }
//
// parent::offsetUnset( $index );
// }
//
// /**
// * Returns all the global site identifiers.
// * Optionally only those belonging to the specified group.
// *
// * @since 1.21
// *
// * @return array
// */
// public function getGlobalIdentifiers() {
// return array_keys( $this->byGlobalId );
// }
//
// /**
// * Returns if the list contains the site with the provided global site identifier.
// *
// * @param String $globalSiteId
// *
// * @return boolean
// */
// public function hasSite( $globalSiteId ) {
// return array_key_exists( $globalSiteId, $this->byGlobalId );
// }
//
// /**
// * Returns the Site with the provided global site identifier.
// * The site needs to exist, so if not sure, call hasGlobalId first.
// *
// * @since 1.21
// *
// * @param String $globalSiteId
// *
// * @return Site
// */
// public function getSite( $globalSiteId ) {
// return $this->offsetGet( $this->byGlobalId[$globalSiteId] );
// }
//
// /**
// * Removes the site with the specified global site identifier.
// * The site needs to exist, so if not sure, call hasGlobalId first.
// *
// * @since 1.21
// *
// * @param String $globalSiteId
// */
// public function removeSite( $globalSiteId ) {
// $this->offsetUnset( $this->byGlobalId[$globalSiteId] );
// }
//
// /**
// * Returns if the list contains no sites.
// *
// * @since 1.21
// *
// * @return boolean
// */
// public function isEmpty() {
// return $this->byGlobalId === [];
// }
//
// /**
// * Returns if the list contains the site with the provided site id.
// *
// * @param int $id
// *
// * @return boolean
// */
// public function hasInternalId( $id ) {
// return array_key_exists( $id, $this->byInternalId );
// }
//
// /**
// * Returns the Site with the provided site id.
// * The site needs to exist, so if not sure, call has first.
// *
// * @since 1.21
// *
// * @param int $id
// *
// * @return Site
// */
// public function getSiteByInternalId( $id ) {
// return $this->offsetGet( $this->byInternalId[$id] );
// }
//
// /**
// * Removes the site with the specified site id.
// * The site needs to exist, so if not sure, call has first.
// *
// * @since 1.21
// *
// * @param int $id
// */
// public function removeSiteByInternalId( $id ) {
// $this->offsetUnset( $this->byInternalId[$id] );
// }
//
// /**
// * Returns if the list contains the site with the provided navigational site id.
// *
// * @param String $id
// *
// * @return boolean
// */
// public function hasNavigationId( $id ) {
// return array_key_exists( $id, $this->byNavigationId );
// }
//
// /**
// * Returns the Site with the provided navigational site id.
// * The site needs to exist, so if not sure, call has first.
// *
// * @since 1.23
// *
// * @param String $id
// *
// * @return Site
// */
// public function getSiteByNavigationId( $id ) {
// return $this->offsetGet( $this->byNavigationId[$id] );
// }
//
// /**
// * Removes the site with the specified navigational site id.
// * The site needs to exist, so if not sure, call has first.
// *
// * @since 1.23
// *
// * @param String $id
// */
// public function removeSiteByNavigationId( $id ) {
// $this->offsetUnset( $this->byNavigationId[$id] );
// }
//
// /**
// * Sets a site in the list. If the site was not there,
// * it will be added. If it was, it will be updated.
// *
// * @since 1.21
// *
// * @param Site $site
// */
// public function setSite( Site $site ) {
// $this[] = $site;
// }
//
// /**
// * Returns the sites that are in the provided group.
// *
// * @since 1.21
// *
// * @param String $groupName
// *
// * @return SiteList
// */
// public function getGroup( $groupName ) {
// $group = new self();
//
// /**
// * @var Site $site
// */
// foreach ( $this as $site ) {
// if ( $site->getGroup() === $groupName ) {
// $group[] = $site;
// }
// }
//
// return $group;
// }
//
// /**
// * 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,
// * not including any sub-structures.
// */
// static final SERIAL_VERSION_ID = '2014-03-17';
//
// /**
// * Returns the version ID that identifies the serialization structure used by
// * getSerializationData() and unserialize(), including the structure of any nested structures.
// * This is useful for constructing cache keys in cases where the cache relies
// * on serialization for storing the SiteList.
// *
// * @return String A String uniquely identifying the version of the serialization structure,
// * including any sub-structures.
// */
// public static function getSerialVersionId() {
// return self::SERIAL_VERSION_ID . '+Site:' . Site::SERIAL_VERSION_ID;
// }
//
// /**
// * @see GenericArrayObject::getSerializationData
// *
// * @since 1.21
// *
// * @return array
// */
// protected function getSerializationData() {
// // NOTE: When changing the structure, either implement unserialize() to handle the
// // old structure too, or update SERIAL_VERSION_ID to kill any caches.
// return array_merge(
// parent::getSerializationData(),
// [
// 'internalIds' => $this->byInternalId,
// 'globalIds' => $this->byGlobalId,
// 'navigationIds' => $this->byNavigationId
// ]
// );
// }
//
// /**
// * @see GenericArrayObject::unserialize
// *
// * @since 1.21
// *
// * @param String $serialization
// *
// * @return array
// */
// public function unserialize( $serialization ) {
// $serializationData = parent::unserialize( $serialization );
//
// $this->byInternalId = $serializationData['internalIds'];
// $this->byGlobalId = $serializationData['globalIds'];
// $this->byNavigationId = $serializationData['navigationIds'];
//
// return $serializationData;
// }
public XomwSiteList() {super();
}
/**
* Internal site identifiers pointing to their sites offset value.
*
* @since 1.21
*
* @var array Array of integer
*/
private final Ordered_hash byInternalId = Ordered_hash_.New();
/**
* Global site identifiers pointing to their sites offset value.
*
* @since 1.21
*
* @var array Array of String
*/
private final Ordered_hash byGlobalId = Ordered_hash_.New();
/**
* Navigational site identifiers alias inter-language prefixes
* pointing to their sites offset value.
*
* @since 1.23
*
* @var array Array of String
*/
private final Ordered_hash byNavigationId = Ordered_hash_.New();
/**
* @see GenericArrayObject::getObjectType
*
* @since 1.21
*
* @return String
*/
@Override public Class<?> getObjectType() {
return XomwSite.class;
}
/**
* @see GenericArrayObject::preSetElement
*
* @since 1.21
*
* @param int|String index
* @param Site site
*
* @return boolean
*/
public boolean preSetElement(int index, XomwSite site) {
if (this.hasSite(site.getGlobalId())) {
this.removeSite(site.getGlobalId());
}
this.byGlobalId.Add(site.getGlobalId(), index);
this.byInternalId.Add(site.getInternalId(), index);
Ordered_hash ids = site.getNavigationIds();
int len = ids.Len();
for (int i = 0; i < len; i++) {
int navId = Int_.cast(ids.Get_at(i));
this.byNavigationId.Add(navId, index);
}
return true;
}
/**
* @see ArrayObject::offsetUnset()
*
* @since 1.21
*
* @param mixed index
*/
public void offsetUnset(int index) {
if (this.offsetExists(index)) {
/**
* @var Site site
*/
XomwSite site = (XomwSite)this.offsetGet(index);
XophpArray.unset(this.byGlobalId, site.getGlobalId());
XophpArray.unset(this.byInternalId, site.getInternalId());
Ordered_hash ids = site.getNavigationIds();
int len = ids.Len();
for (int i = 0; i < len; i++) {
int navId = Int_.cast(ids.Get_at(i));
XophpArray.unset(this.byNavigationId, navId);
}
}
super.offsetUnset(index);
}
/**
* Returns all the global site identifiers.
* Optionally only those belonging to the specified group.
*
* @since 1.21
*
* @return array
*/
public String[] getGlobalIdentifiers() {
return XophpArray.array_keys_str(this.byGlobalId);
}
/**
* Returns if the list contains the site with the provided global site identifier.
*
* @param String globalSiteId
*
* @return boolean
*/
public boolean hasSite(String globalSiteId) {
return XophpArray.array_key_exists(globalSiteId, this.byGlobalId);
}
/**
* Returns the Site with the provided global site identifier.
* The site needs to exist, so if not sure, call hasGlobalId first.
*
* @since 1.21
*
* @param String globalSiteId
*
* @return Site
*/
public XomwSite getSite(String globalSiteId) {
return (XomwSite)this.offsetGet(this.byGlobalId.Get_by(globalSiteId));
}
/**
* Removes the site with the specified global site identifier.
* The site needs to exist, so if not sure, call hasGlobalId first.
*
* @since 1.21
*
* @param String globalSiteId
*/
public void removeSite(String globalSiteId) {
this.offsetUnset(this.byGlobalId.Get_by(globalSiteId));
}
/**
* Returns if the list contains no sites.
*
* @since 1.21
*
* @return boolean
*/
@Override public boolean isEmpty() {
return XophpArray.array_is_empty(this.byGlobalId);
}
/**
* Returns if the list contains the site with the provided site id.
*
* @param int id
*
* @return boolean
*/
public boolean hasInternalId(int id) {
return XophpArray.array_key_exists(id, this.byInternalId);
}
/**
* Returns the Site with the provided site id.
* The site needs to exist, so if not sure, call has first.
*
* @since 1.21
*
* @param int id
*
* @return Site
*/
public XomwSite getSiteByInternalId(int id) {
return (XomwSite)this.offsetGet(this.byInternalId.Get_by(id));
}
/**
* Removes the site with the specified site id.
* The site needs to exist, so if not sure, call has first.
*
* @since 1.21
*
* @param int id
*/
public void removeSiteByInternalId(int id) {
this.offsetUnset(this.byInternalId.Get_by(id));
}
/**
* Returns if the list contains the site with the provided navigational site id.
*
* @param String id
*
* @return boolean
*/
public boolean hasNavigationId(String id) {
return XophpArray.array_key_exists(id, this.byNavigationId);
}
/**
* Returns the Site with the provided navigational site id.
* The site needs to exist, so if not sure, call has first.
*
* @since 1.23
*
* @param String id
*
* @return Site
*/
public XomwSite getSiteByNavigationId(String id) {
return (XomwSite)this.offsetGet(this.byNavigationId.Get_by(id));
}
/**
* Removes the site with the specified navigational site id.
* The site needs to exist, so if not sure, call has first.
*
* @since 1.23
*
* @param String id
*/
public void removeSiteByNavigationId(String id) {
this.offsetUnset(this.byNavigationId.Get_by(id));
}
/**
* Sets a site in the list. If the site was not there,
* it will be added. If it was, it will be updated.
*
* @since 1.21
*
* @param Site site
*/
public void setSite(XomwSite site) {
this.Add_or_update(site);
}
/**
* Returns the sites that are in the provided group.
*
* @since 1.21
*
* @param String groupName
*
* @return SiteList
*/
public XomwSiteList getGroup(String groupName) {
XomwSiteList group = new XomwSiteList();
/**
* @var Site site
*/
int len = this.count();
for (int i = 0; i < len; i++) {
XomwSite site = (XomwSite)this.Get_at(i);
if (String_.Eq(site.getGroup(), groupName)) {
group.Add_or_update(site);
}
}
return group;
}
// /**
// * 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,
// * not including any sub-structures.
// */
// static final SERIAL_VERSION_ID = '2014-03-17';
//
// /**
// * Returns the version ID that identifies the serialization structure used by
// * getSerializationData() and unserialize(), including the structure of any nested structures.
// * This is useful for constructing cache keys in cases where the cache relies
// * on serialization for storing the SiteList.
// *
// * @return String A String uniquely identifying the version of the serialization structure,
// * including any sub-structures.
// */
// public static function getSerialVersionId() {
// return self::SERIAL_VERSION_ID . '+Site:' . Site::SERIAL_VERSION_ID;
// }
//
// /**
// * @see GenericArrayObject::getSerializationData
// *
// * @since 1.21
// *
// * @return array
// */
// protected function getSerializationData() {
// // NOTE: When changing the structure, either implement unserialize() to handle the
// // old structure too, or update SERIAL_VERSION_ID to kill any caches.
// return array_merge(
// super.getSerializationData(),
// [
// 'internalIds' => this.byInternalId,
// 'globalIds' => this.byGlobalId,
// 'navigationIds' => this.byNavigationId
// ]
// );
// }
//
// /**
// * @see GenericArrayObject::unserialize
// *
// * @since 1.21
// *
// * @param String serialization
// *
// * @return array
// */
// public function unserialize(serialization) {
// serializationData = super.unserialize(serialization);
//
// this.byInternalId = serializationData['internalIds'];
// this.byGlobalId = serializationData['globalIds'];
// this.byNavigationId = serializationData['navigationIds'];
//
// return serializationData;
// }
}