Deprecate setting Parser::mTitle to null
[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 return $this->sites[$globalId] ?? null;
91 }
92
93 /**
94 * Returns a list of all sites. By default this site is
95 * fetched from the cache, which can be changed to loading
96 * the list from the database using the $useCache parameter.
97 *
98 * @since 1.25
99 *
100 * @param string $source either 'cache' or 'recache'.
101 * If 'cache', the values can (but not obliged) come from a cache.
102 *
103 * @return SiteList
104 */
105 public function getSites( $source = 'cache' ) {
106 return new SiteList( $this->sites );
107 }
108
109 /**
110 * Deletes all sites from the database. After calling clear(), getSites() will return an empty
111 * list and getSite() will return null until saveSite() or saveSites() is called.
112 * @return bool
113 */
114 public function clear() {
115 $this->sites = [];
116
117 return true;
118 }
119
120 }