Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.82% |
9 / 11 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
HashSiteStore | |
90.00% |
9 / 10 |
|
83.33% |
5 / 6 |
7.05 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
saveSite | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
saveSites | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getSite | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSites | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
clear | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
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 | |
21 | namespace MediaWiki\Site; |
22 | |
23 | /** |
24 | * In-memory SiteStore implementation, stored in an associative array. |
25 | * |
26 | * @since 1.25 |
27 | * @ingroup Site |
28 | * @author Daniel Kinzler |
29 | * @author Katie Filbert < aude.wiki@gmail.com > |
30 | */ |
31 | class HashSiteStore implements SiteStore { |
32 | /** @var Site[] */ |
33 | private $sites = []; |
34 | |
35 | /** |
36 | * @param Site[] $sites |
37 | */ |
38 | public function __construct( array $sites = [] ) { |
39 | $this->saveSites( $sites ); |
40 | } |
41 | |
42 | /** |
43 | * Save the provided site. |
44 | * |
45 | * @since 1.25 |
46 | * @param Site $site |
47 | * @return bool Success indicator |
48 | */ |
49 | public function saveSite( Site $site ) { |
50 | $this->sites[$site->getGlobalId()] = $site; |
51 | |
52 | return true; |
53 | } |
54 | |
55 | /** |
56 | * Save the provided sites. |
57 | * |
58 | * @since 1.25 |
59 | * @param Site[] $sites |
60 | * @return bool Success indicator |
61 | */ |
62 | public function saveSites( array $sites ) { |
63 | foreach ( $sites as $site ) { |
64 | $this->saveSite( $site ); |
65 | } |
66 | |
67 | return true; |
68 | } |
69 | |
70 | /** |
71 | * Return the site with provided global ID, or null if there is no such site. |
72 | * |
73 | * @since 1.25 |
74 | * @param string $globalId |
75 | * @param string $source either 'cache' or 'recache'. |
76 | * If 'cache', the values can (but not obliged) come from a cache. |
77 | * @return Site|null |
78 | */ |
79 | public function getSite( $globalId, $source = 'cache' ) { |
80 | return $this->sites[$globalId] ?? null; |
81 | } |
82 | |
83 | /** |
84 | * Return a list of all sites. |
85 | * |
86 | * By default this list is fetched from the cache, which can be changed to loading |
87 | * the list from the database using the $useCache parameter. |
88 | * |
89 | * @since 1.25 |
90 | * @param string $source either 'cache' or 'recache'. |
91 | * If 'cache', the values can (but not obliged) come from a cache. |
92 | * @return SiteList |
93 | */ |
94 | public function getSites( $source = 'cache' ) { |
95 | return new SiteList( $this->sites ); |
96 | } |
97 | |
98 | /** |
99 | * Delete all sites from the database. |
100 | * |
101 | * After calling clear(), getSites() will return an empty list and getSite() will |
102 | * return null until saveSite() or saveSites() is called. |
103 | * |
104 | * @return bool |
105 | */ |
106 | public function clear() { |
107 | $this->sites = []; |
108 | return true; |
109 | } |
110 | |
111 | } |
112 | |
113 | /** @deprecated class alias since 1.42 */ |
114 | class_alias( HashSiteStore::class, 'HashSiteStore' ); |