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.
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.
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
21 namespace MediaWiki\Site
;
24 * In-memory SiteStore implementation, stored in an associative array.
28 * @author Daniel Kinzler
29 * @author Katie Filbert < aude.wiki@gmail.com >
31 class HashSiteStore
implements SiteStore
{
36 * @param Site[] $sites
38 public function __construct( array $sites = [] ) {
39 $this->saveSites( $sites );
43 * Save the provided site.
47 * @return bool Success indicator
49 public function saveSite( Site
$site ) {
50 $this->sites
[$site->getGlobalId()] = $site;
56 * Save the provided sites.
59 * @param Site[] $sites
60 * @return bool Success indicator
62 public function saveSites( array $sites ) {
63 foreach ( $sites as $site ) {
64 $this->saveSite( $site );
71 * Return the site with provided global ID, or null if there is no such site.
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.
79 public function getSite( $globalId, $source = 'cache' ) {
80 return $this->sites
[$globalId] ??
null;
84 * Return a list of all sites.
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.
90 * @param string $source either 'cache' or 'recache'.
91 * If 'cache', the values can (but not obliged) come from a cache.
94 public function getSites( $source = 'cache' ) {
95 return new SiteList( $this->sites
);
99 * Delete all sites from the database.
101 * After calling clear(), getSites() will return an empty list and getSite() will
102 * return null until saveSite() or saveSites() is called.
106 public function clear() {
113 /** @deprecated class alias since 1.42 */
114 class_alias( HashSiteStore
::class, 'HashSiteStore' );