FileBasedSiteLookup.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. *
  20. * @license GPL-2.0-or-later
  21. */
  22. /**
  23. * Provides a file-based cache of a SiteStore, using a json file.
  24. *
  25. * @since 1.25
  26. * @deprecated since 1.33 Use CachingSiteStore instead.
  27. */
  28. class FileBasedSiteLookup implements SiteLookup {
  29. /**
  30. * @var SiteList
  31. */
  32. private $sites = null;
  33. /**
  34. * @var string
  35. */
  36. private $cacheFile;
  37. /**
  38. * @param string $cacheFile
  39. */
  40. public function __construct( $cacheFile ) {
  41. wfDeprecated( __CLASS__, '1.33' );
  42. $this->cacheFile = $cacheFile;
  43. }
  44. /**
  45. * @since 1.25
  46. *
  47. * @return SiteList
  48. */
  49. public function getSites() {
  50. if ( $this->sites === null ) {
  51. $this->sites = $this->loadSitesFromCache();
  52. }
  53. return $this->sites;
  54. }
  55. /**
  56. * @param string $globalId
  57. *
  58. * @since 1.25
  59. *
  60. * @return Site|null
  61. */
  62. public function getSite( $globalId ) {
  63. $sites = $this->getSites();
  64. return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
  65. }
  66. /**
  67. * @return SiteList
  68. */
  69. private function loadSitesFromCache() {
  70. $data = $this->loadJsonFile();
  71. $sites = new SiteList();
  72. // @todo lazy initialize the site objects in the site list (e.g. only when needed to access)
  73. foreach ( $data['sites'] as $siteArray ) {
  74. $sites[] = $this->newSiteFromArray( $siteArray );
  75. }
  76. return $sites;
  77. }
  78. /**
  79. * @throws MWException
  80. * @return array see docs/sitescache.txt for format of the array.
  81. */
  82. private function loadJsonFile() {
  83. if ( !is_readable( $this->cacheFile ) ) {
  84. throw new MWException( 'SiteList cache file not found.' );
  85. }
  86. $contents = file_get_contents( $this->cacheFile );
  87. $data = json_decode( $contents, true );
  88. if ( !is_array( $data ) || !is_array( $data['sites'] )
  89. || !array_key_exists( 'sites', $data )
  90. ) {
  91. throw new MWException( 'SiteStore json cache data is invalid.' );
  92. }
  93. return $data;
  94. }
  95. /**
  96. * @param array $data
  97. *
  98. * @return Site
  99. */
  100. private function newSiteFromArray( array $data ) {
  101. $siteType = array_key_exists( 'type', $data ) ? $data['type'] : Site::TYPE_UNKNOWN;
  102. $site = Site::newForType( $siteType );
  103. $site->setGlobalId( $data['globalid'] );
  104. $site->setForward( $data['forward'] );
  105. $site->setGroup( $data['group'] );
  106. $site->setLanguageCode( $data['language'] );
  107. $site->setSource( $data['source'] );
  108. $site->setExtraData( $data['data'] );
  109. $site->setExtraConfig( $data['config'] );
  110. foreach ( $data['identifiers'] as $identifier ) {
  111. $site->addLocalId( $identifier['type'], $identifier['key'] );
  112. }
  113. return $site;
  114. }
  115. }