Advertisement
Guest User

Untitled

a guest
Jun 20th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.30 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Wikibase;
  4. use MWException;
  5.  
  6. /**
  7.  * A collection of Site objects.
  8.  *
  9.  * TODO: ensure append works
  10.  * TODO: ensure unset works
  11.  *
  12.  * @since 0.1
  13.  *
  14.  * @file
  15.  * @ingroup Wikibase
  16.  * @ingroup Sites
  17.  *
  18.  * @licence GNU GPL v2+
  19.  * @author Jeroen De Dauw < jeroendedauw@gmail.com >
  20.  */
  21. class SiteList extends \ArrayObject /* implements ORMIterator */ {
  22.  
  23.     /**
  24.      * Holds the group names (keys) pointing to an arrays
  25.      * consisting of offset value pointing to their sites global identifier.
  26.      * @since 0.1
  27.      * @var array
  28.      */
  29.     protected $groups = array();
  30.  
  31.     /**
  32.      * Local site identifiers pointing to their sites offset value.
  33.      * @since 0.1
  34.      * @var array
  35.      */
  36.     protected $byLocalId = array();
  37.  
  38.     /**
  39.      * Global site identifiers pointing to their sites offset value.
  40.      * @since 0.1
  41.      * @var array
  42.      */
  43.     protected $byGlobalId = array();
  44.  
  45.     /**
  46.      * @see SiteList::getNewOffset()
  47.      * @since 0.1
  48.      * @var integer
  49.      */
  50.     protected $indexOffset = 0;
  51.  
  52.     /**
  53.      * Finds a new offset for when appending an element.
  54.      * TODO: the base class does this, so it would be better to integrate,
  55.      * but there does not appear to be any way to do this...
  56.      *
  57.      * @since 0.1
  58.      *
  59.      * @return integer
  60.      */
  61.     protected function getNewOffset() {
  62.         while ( true ) {
  63.             if ( !$this->offsetExists( $this->indexOffset ) ) {
  64.                 return $this->indexOffset;
  65.             }
  66.  
  67.             $this->indexOffset++;
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * @see ArrayObject::offsetSet()
  73.      *
  74.      * @since 0.1
  75.      *
  76.      * @param mixed $index
  77.      * @param Site $site
  78.      */
  79.     public function offsetSet( $index, $site ) {
  80.         if ( !$site instanceof Site ) {
  81.             throw new MWException( 'Can only add Site implementing objects to SiteList.' );
  82.         }
  83.  
  84.         if ( is_null( $index ) ) {
  85.             $index = $this->getNewOffset();
  86.         }
  87.  
  88.         $this->byGlobalId[$site->getField( 'global_key' )] = $index;
  89.         $this->byLocalId[$site->getField( 'local_key' )] = $index;
  90.  
  91.         $group = $site->getField( 'group' );
  92.  
  93.         if ( !array_key_exists( $group, $this->groups ) ) {
  94.             $this->groups[$group] = array();
  95.         }
  96.  
  97.         $this->groups[$group][$index] = $site->getField( 'global_key' );
  98.  
  99.         parent::offsetSet( $index, $site );
  100.     }
  101.  
  102.     /**
  103.      * @see ArrayObject::offsetUnset()
  104.      *
  105.      * @since 0.1
  106.      *
  107.      * @param mixed $index
  108.      */
  109.     public function offsetUnset( $index ) {
  110.         $site = $this->offsetGet( $index );
  111.  
  112.         if ( $site !== false ) {
  113.             unset( $this->byGlobalId[$site->getField( 'global_key' )] );
  114.             unset( $this->byLocalId[$site->getField( 'local_key' )] );
  115.             unset( $this->groups[$site->getField( 'group' )][$index] );
  116.         }
  117.  
  118.         parent::offsetUnset( $index );
  119.     }
  120.  
  121.     /**
  122.      * Returns all the global site identifiers.
  123.      * Optionally only those belonging to the specified group.
  124.      *
  125.      * @since 0.1
  126.      *
  127.      * @param string|null $groupName
  128.      *
  129.      * @return array
  130.      * @throws MWException
  131.      */
  132.     public function getGlobalIdentifiers( $groupName = null ) {
  133.         if ( is_null( $groupName ) ) {
  134.             return array_keys( $this->byGlobalId );
  135.         }
  136.         else {
  137.             if ( !array_key_exists( $groupName, $this->groups ) ) {
  138.                 throw new MWException( "No site group with name '$groupName' exists" );
  139.             }
  140.  
  141.             return $this->groups[$groupName];
  142.         }
  143.     }
  144.  
  145.     /**
  146.      * Returns the local identifiers.
  147.      *
  148.      * @since 0.1
  149.      *
  150.      * @return array
  151.      */
  152.     public function getLocalIdentifiers() {
  153.         return array_keys( $this->byLocalId );
  154.     }
  155.  
  156.     /**
  157.      * @return array
  158.      */
  159.     public function getGroupNames() {
  160.         return array_keys( $this->groups );
  161.     }
  162.  
  163.     /**
  164.      * Returns a Sites containing only the sites of the specified group.
  165.      *
  166.      * @since 0.1
  167.      *
  168.      * @param string $groupName
  169.      *
  170.      * @return SiteList
  171.      */
  172.     public function getGroup( $groupName ) {
  173.         if ( array_key_exists( $groupName, $this->groups ) ) {
  174.             $sites = array();
  175.  
  176.             foreach ( array_keys( $this->groups[$groupName] ) as $offset ) {
  177.                 $sites[$offset] = $this->offsetGet( $offset );
  178.             }
  179.         }
  180.         else {
  181.             $sites = array();
  182.         }
  183.  
  184.         return new static( $sites );
  185.     }
  186.  
  187.     /**
  188.      * Returns if the list contains the site with the provided local site identifier.
  189.      *
  190.      * @param string $localSiteId
  191.      *
  192.      * @return boolean
  193.      */
  194.     public function hasLocalId( $localSiteId ) {
  195.         return array_key_exists( $localSiteId, $this->byLocalId );
  196.     }
  197.  
  198.     /**
  199.      * Returns the Site with the provided local site id.
  200.      * The site needs to exist, so if not sure, call hasLocalId first.
  201.      *
  202.      * @since 0.1
  203.      *
  204.      * @param string $localSiteId
  205.      *
  206.      * @return Site
  207.      */
  208.     public function getSiteByLocalId( $localSiteId ) {
  209.         return $this->offsetGet( $this->byLocalId[$localSiteId] );
  210.     }
  211.  
  212.     /**
  213.      * Returns if the list contains the site with the provided global site identifier.
  214.      *
  215.      * @param string $globalSiteId
  216.      *
  217.      * @return boolean
  218.      */
  219.     public function hasGlobalId( $globalSiteId ) {
  220.         return array_key_exists( $globalSiteId, $this->byGlobalId );
  221.     }
  222.  
  223.     /**
  224.      * Returns the Site with the provided global site id.
  225.      * The site needs to exist, so if not sure, call hasGlobalId first.
  226.      *
  227.      * @since 0.1
  228.      *
  229.      * @param string $globalSiteId
  230.      *
  231.      * @return Site
  232.      */
  233.     public function getSiteByGlobalId( $globalSiteId ) {
  234.         return $this->offsetGet( $this->byGlobalId[$globalSiteId] );
  235.     }
  236.  
  237.     /**
  238.      * Returns if the site list contains no sites.
  239.      *
  240.      * @since 0.1
  241.      *
  242.      * @return boolean
  243.      */
  244.     public function isEmpty() {
  245.         return $this->count() === 0;
  246.     }
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement