SiteList.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /**
  3. * Collection of Site objects.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @since 1.21
  21. *
  22. * @file
  23. * @ingroup Site
  24. *
  25. * @license GPL-2.0-or-later
  26. * @author Jeroen De Dauw < jeroendedauw@gmail.com >
  27. */
  28. class SiteList extends GenericArrayObject {
  29. /**
  30. * Internal site identifiers pointing to their sites offset value.
  31. *
  32. * @since 1.21
  33. *
  34. * @var array Array of integer
  35. */
  36. protected $byInternalId = [];
  37. /**
  38. * Global site identifiers pointing to their sites offset value.
  39. *
  40. * @since 1.21
  41. *
  42. * @var array Array of string
  43. */
  44. protected $byGlobalId = [];
  45. /**
  46. * Navigational site identifiers alias inter-language prefixes
  47. * pointing to their sites offset value.
  48. *
  49. * @since 1.23
  50. *
  51. * @var array Array of string
  52. */
  53. protected $byNavigationId = [];
  54. /**
  55. * @see GenericArrayObject::getObjectType
  56. *
  57. * @since 1.21
  58. *
  59. * @return string
  60. */
  61. public function getObjectType() {
  62. return Site::class;
  63. }
  64. /**
  65. * @see GenericArrayObject::preSetElement
  66. *
  67. * @since 1.21
  68. *
  69. * @param int|string $index
  70. * @param Site $site
  71. *
  72. * @return bool
  73. */
  74. protected function preSetElement( $index, $site ) {
  75. if ( $this->hasSite( $site->getGlobalId() ) ) {
  76. $this->removeSite( $site->getGlobalId() );
  77. }
  78. $this->byGlobalId[$site->getGlobalId()] = $index;
  79. $this->byInternalId[$site->getInternalId()] = $index;
  80. $ids = $site->getNavigationIds();
  81. foreach ( $ids as $navId ) {
  82. $this->byNavigationId[$navId] = $index;
  83. }
  84. return true;
  85. }
  86. /**
  87. * @see ArrayObject::offsetUnset()
  88. *
  89. * @since 1.21
  90. *
  91. * @param mixed $index
  92. */
  93. public function offsetUnset( $index ) {
  94. if ( $this->offsetExists( $index ) ) {
  95. /**
  96. * @var Site $site
  97. */
  98. $site = $this->offsetGet( $index );
  99. unset( $this->byGlobalId[$site->getGlobalId()] );
  100. unset( $this->byInternalId[$site->getInternalId()] );
  101. $ids = $site->getNavigationIds();
  102. foreach ( $ids as $navId ) {
  103. unset( $this->byNavigationId[$navId] );
  104. }
  105. }
  106. parent::offsetUnset( $index );
  107. }
  108. /**
  109. * Returns all the global site identifiers.
  110. * Optionally only those belonging to the specified group.
  111. *
  112. * @since 1.21
  113. *
  114. * @return array
  115. */
  116. public function getGlobalIdentifiers() {
  117. return array_keys( $this->byGlobalId );
  118. }
  119. /**
  120. * Returns if the list contains the site with the provided global site identifier.
  121. *
  122. * @param string $globalSiteId
  123. *
  124. * @return bool
  125. */
  126. public function hasSite( $globalSiteId ) {
  127. return array_key_exists( $globalSiteId, $this->byGlobalId );
  128. }
  129. /**
  130. * Returns the Site with the provided global site identifier.
  131. * The site needs to exist, so if not sure, call hasGlobalId first.
  132. *
  133. * @since 1.21
  134. *
  135. * @param string $globalSiteId
  136. *
  137. * @return Site
  138. */
  139. public function getSite( $globalSiteId ) {
  140. return $this->offsetGet( $this->byGlobalId[$globalSiteId] );
  141. }
  142. /**
  143. * Removes the site with the specified global site identifier.
  144. * The site needs to exist, so if not sure, call hasGlobalId first.
  145. *
  146. * @since 1.21
  147. *
  148. * @param string $globalSiteId
  149. */
  150. public function removeSite( $globalSiteId ) {
  151. $this->offsetUnset( $this->byGlobalId[$globalSiteId] );
  152. }
  153. /**
  154. * Returns if the list contains no sites.
  155. *
  156. * @since 1.21
  157. *
  158. * @return bool
  159. */
  160. public function isEmpty() {
  161. return $this->byGlobalId === [];
  162. }
  163. /**
  164. * Returns if the list contains the site with the provided site id.
  165. *
  166. * @param int $id
  167. *
  168. * @return bool
  169. */
  170. public function hasInternalId( $id ) {
  171. return array_key_exists( $id, $this->byInternalId );
  172. }
  173. /**
  174. * Returns the Site with the provided site id.
  175. * The site needs to exist, so if not sure, call has first.
  176. *
  177. * @since 1.21
  178. *
  179. * @param int $id
  180. *
  181. * @return Site
  182. */
  183. public function getSiteByInternalId( $id ) {
  184. return $this->offsetGet( $this->byInternalId[$id] );
  185. }
  186. /**
  187. * Removes the site with the specified site id.
  188. * The site needs to exist, so if not sure, call has first.
  189. *
  190. * @since 1.21
  191. *
  192. * @param int $id
  193. */
  194. public function removeSiteByInternalId( $id ) {
  195. $this->offsetUnset( $this->byInternalId[$id] );
  196. }
  197. /**
  198. * Returns if the list contains the site with the provided navigational site id.
  199. *
  200. * @param string $id
  201. *
  202. * @return bool
  203. */
  204. public function hasNavigationId( $id ) {
  205. return array_key_exists( $id, $this->byNavigationId );
  206. }
  207. /**
  208. * Returns the Site with the provided navigational site id.
  209. * The site needs to exist, so if not sure, call has first.
  210. *
  211. * @since 1.23
  212. *
  213. * @param string $id
  214. *
  215. * @return Site
  216. */
  217. public function getSiteByNavigationId( $id ) {
  218. return $this->offsetGet( $this->byNavigationId[$id] );
  219. }
  220. /**
  221. * Removes the site with the specified navigational site id.
  222. * The site needs to exist, so if not sure, call has first.
  223. *
  224. * @since 1.23
  225. *
  226. * @param string $id
  227. */
  228. public function removeSiteByNavigationId( $id ) {
  229. $this->offsetUnset( $this->byNavigationId[$id] );
  230. }
  231. /**
  232. * Sets a site in the list. If the site was not there,
  233. * it will be added. If it was, it will be updated.
  234. *
  235. * @since 1.21
  236. *
  237. * @param Site $site
  238. */
  239. public function setSite( Site $site ) {
  240. $this[] = $site;
  241. }
  242. /**
  243. * Returns the sites that are in the provided group.
  244. *
  245. * @since 1.21
  246. *
  247. * @param string $groupName
  248. *
  249. * @return SiteList
  250. */
  251. public function getGroup( $groupName ) {
  252. $group = new self();
  253. /**
  254. * @var Site $site
  255. */
  256. foreach ( $this as $site ) {
  257. if ( $site->getGroup() === $groupName ) {
  258. $group[] = $site;
  259. }
  260. }
  261. return $group;
  262. }
  263. /**
  264. * A version ID that identifies the serialization structure used by getSerializationData()
  265. * and unserialize(). This is useful for constructing cache keys in cases where the cache relies
  266. * on serialization for storing the SiteList.
  267. *
  268. * @var string A string uniquely identifying the version of the serialization structure,
  269. * not including any sub-structures.
  270. */
  271. const SERIAL_VERSION_ID = '2014-03-17';
  272. /**
  273. * Returns the version ID that identifies the serialization structure used by
  274. * getSerializationData() and unserialize(), including the structure of any nested structures.
  275. * This is useful for constructing cache keys in cases where the cache relies
  276. * on serialization for storing the SiteList.
  277. *
  278. * @return string A string uniquely identifying the version of the serialization structure,
  279. * including any sub-structures.
  280. */
  281. public static function getSerialVersionId() {
  282. return self::SERIAL_VERSION_ID . '+Site:' . Site::SERIAL_VERSION_ID;
  283. }
  284. /**
  285. * @see GenericArrayObject::getSerializationData
  286. *
  287. * @since 1.21
  288. *
  289. * @return array
  290. */
  291. protected function getSerializationData() {
  292. // NOTE: When changing the structure, either implement unserialize() to handle the
  293. // old structure too, or update SERIAL_VERSION_ID to kill any caches.
  294. return array_merge(
  295. parent::getSerializationData(),
  296. [
  297. 'internalIds' => $this->byInternalId,
  298. 'globalIds' => $this->byGlobalId,
  299. 'navigationIds' => $this->byNavigationId
  300. ]
  301. );
  302. }
  303. /**
  304. * @see GenericArrayObject::unserialize
  305. *
  306. * @since 1.21
  307. *
  308. * @param string $serialization
  309. *
  310. * @return array
  311. */
  312. public function unserialize( $serialization ) {
  313. $serializationData = parent::unserialize( $serialization );
  314. $this->byInternalId = $serializationData['internalIds'];
  315. $this->byGlobalId = $serializationData['globalIds'];
  316. $this->byNavigationId = $serializationData['navigationIds'];
  317. return $serializationData;
  318. }
  319. }