Merge "Add attributes parameter to ShowSearchHitTitle"
[lhc/web/wiklou.git] / includes / site / HashSiteStore.php
1 <?php
2 /**
3 * In-memory implementation of SiteStore.
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 * @file
21 */
22
23 /**
24 * In-memory SiteStore implementation, storing sites in an associative array.
25 *
26 * @author Daniel Kinzler
27 * @author Katie Filbert < aude.wiki@gmail.com >
28 *
29 * @since 1.25
30 * @ingroup Site
31 */
32 class HashSiteStore implements SiteStore {
33
34 /**
35 * @var Site[]
36 */
37 private $sites = [];
38
39 /**
40 * @param Site[] $sites
41 */
42 public function __construct( $sites = [] ) {
43 $this->saveSites( $sites );
44 }
45
46 /**
47 * Saves the provided site.
48 *
49 * @since 1.25
50 *
51 * @param Site $site
52 *
53 * @return bool Success indicator
54 */
55 public function saveSite( Site $site ) {
56 $this->sites[$site->getGlobalId()] = $site;
57
58 return true;
59 }
60
61 /**
62 * Saves the provided sites.
63 *
64 * @since 1.25
65 *
66 * @param Site[] $sites
67 *
68 * @return bool Success indicator
69 */
70 public function saveSites( array $sites ) {
71 foreach ( $sites as $site ) {
72 $this->saveSite( $site );
73 }
74
75 return true;
76 }
77
78 /**
79 * Returns the site with provided global id, or null if there is no such site.
80 *
81 * @since 1.25
82 *
83 * @param string $globalId
84 * @param string $source either 'cache' or 'recache'.
85 * If 'cache', the values can (but not obliged) come from a cache.
86 *
87 * @return Site|null
88 */
89 public function getSite( $globalId, $source = 'cache' ) {
90 if ( isset( $this->sites[$globalId] ) ) {
91 return $this->sites[$globalId];
92 } else {
93 return null;
94 }
95 }
96
97 /**
98 * Returns a list of all sites. By default this site is
99 * fetched from the cache, which can be changed to loading
100 * the list from the database using the $useCache parameter.
101 *
102 * @since 1.25
103 *
104 * @param string $source either 'cache' or 'recache'.
105 * If 'cache', the values can (but not obliged) come from a cache.
106 *
107 * @return SiteList
108 */
109 public function getSites( $source = 'cache' ) {
110 return new SiteList( $this->sites );
111 }
112
113 /**
114 * Deletes all sites from the database. After calling clear(), getSites() will return an empty
115 * list and getSite() will return null until saveSite() or saveSites() is called.
116 * @return bool
117 */
118 public function clear() {
119 $this->sites = [];
120
121 return true;
122 }
123
124 }