(Bug 40860) make purgeRedundantText not fail on pre MW1.5 records
[lhc/web/wiklou.git] / includes / site / Sites.php
1 <?php
2
3 /**
4 * Represents the site configuration of a wiki.
5 * Holds a list of sites (ie SiteList) and takes care
6 * of retrieving and caching site information when appropriate.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @since 1.21
24 *
25 * @file
26 * @ingroup Site
27 *
28 * @license GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 */
31 class Sites {
32
33 /**
34 * @since 1.21
35 * @var SiteList|null
36 */
37 protected $sites = null;
38
39 /**
40 * Constructor.
41 *
42 * @since 1.21
43 */
44 protected function __construct() {}
45
46 /**
47 * Returns an instance of Sites.
48 *
49 * @since 1.21
50 *
51 * @return Sites
52 */
53 public static function singleton() {
54 static $instance = false;
55
56 if ( $instance === false ) {
57 $instance = new static();
58 }
59
60 return $instance;
61 }
62
63 /**
64 * Factory for creating new site objects.
65 *
66 * @since 1.21
67 *
68 * @param string|boolean false $globalId
69 *
70 * @return Site
71 */
72 public static function newSite( $globalId = false ) {
73 /**
74 * @var Site $site
75 */
76 $site = SitesTable::singleton()->newRow( array(), true );
77
78 if ( $globalId !== false ) {
79 $site->setGlobalId( $globalId );
80 }
81
82 return $site;
83 }
84
85 /**
86 * Returns a list of all sites. By default this site is
87 * fetched from the cache, which can be changed to loading
88 * the list from the database using the $useCache parameter.
89 *
90 * @since 1.21
91 *
92 * @param string $source either 'cache' or 'recache'
93 *
94 * @return SiteList
95 */
96 public function getSites( $source = 'cache' ) {
97 if ( $source === 'cache' ) {
98 if ( $this->sites === null ) {
99 $cache = wfGetMainCache();
100 $sites = $cache->get( wfMemcKey( 'SiteList' ) );
101
102 if ( is_object( $sites ) && isset( $sites->cacheVersion ) && $sites->cacheVersion === SiteArray::CACHE_VERSION ) {
103 $this->sites = $sites;
104 } else {
105 $this->loadSites();
106 }
107 }
108 }
109 else {
110 $this->loadSites();
111 }
112
113 return $this->sites;
114 }
115
116 /**
117 * Returns a list of sites in the given group. Calling getGroup() on any of
118 * the sites in the resulting SiteList shall return $group.
119 *
120 * @since 1.21
121 *
122 * @param string $group th group to get.
123 *
124 * @return SiteList
125 */
126 public function getSiteGroup( $group ) {
127 $sites = self::getSites();
128
129 $siteGroup = new SiteArray();
130
131 /* @var Site $site */
132 foreach ( $sites as $site ) {
133 if ( $site->getGroup() == $group ) {
134 $siteGroup->append( $site );
135 }
136 }
137
138 return $siteGroup;
139 }
140
141 /**
142 * Fetches the site from the database and loads them into the sites field.
143 *
144 * @since 1.21
145 */
146 protected function loadSites() {
147 $this->sites = new SiteArray( SitesTable::singleton()->select() );
148
149 // Batch load the local site identifiers.
150 $dbr = wfGetDB( SitesTable::singleton()->getReadDb() );
151
152 $ids = $dbr->select(
153 'site_identifiers',
154 array(
155 'si_site',
156 'si_type',
157 'si_key',
158 ),
159 array(),
160 __METHOD__
161 );
162
163 foreach ( $ids as $id ) {
164 if ( $this->sites->hasInternalId( $id->si_site ) ) {
165 $site = $this->sites->getSiteByInternalId( $id->si_site );
166 $site->addLocalId( $id->si_type, $id->si_key );
167 $this->sites->setSite( $site );
168 }
169 }
170
171 $cache = wfGetMainCache();
172 $cache->set( wfMemcKey( 'SiteList' ), $this->sites );
173 }
174
175 /**
176 * Returns the site with provided global id, or false if there is no such site.
177 *
178 * @since 1.21
179 *
180 * @param string $globalId
181 * @param string $source
182 *
183 * @return Site|false
184 */
185 public function getSite( $globalId, $source = 'cache' ) {
186 $sites = $this->getSites( $source );
187
188 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : false;
189 }
190
191 }