4 * Represents the site configuration of a wiki.
5 * Holds a list of sites (ie SiteList), stored in the database.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
27 * @license GNU GPL v2+
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 class DBSiteStore
implements SiteStore
{
35 protected $sites = null;
39 * @param null $sitesTable Unused since 1.27
41 public function __construct( $sitesTable = null ) {
42 if ( $sitesTable !== null ) {
43 throw new InvalidArgumentException(
44 __METHOD__
. ': $sitesTable parameter must be null'
50 * @see SiteStore::getSites
56 public function getSites() {
63 * Fetches the site from the database and loads them into the sites field.
67 protected function loadSites() {
68 $this->sites
= new SiteList();
70 $dbr = wfGetDB( DB_SLAVE
);
89 array( 'ORDER BY' => 'site_global_key' )
92 foreach ( $res as $row ) {
93 $site = Site
::newForType( $row->site_type
);
94 $site->setGlobalId( $row->site_global_key
);
95 $site->setInternalId( (int)$row->site_id
);
96 $site->setForward( (bool)$row->site_forward
);
97 $site->setGroup( $row->site_group
);
98 $site->setLanguageCode( $row->site_language
=== ''
100 : $row->site_language
102 $site->setSource( $row->site_source
);
103 $site->setExtraData( unserialize( $row->site_data
) );
104 $site->setExtraConfig( unserialize( $row->site_config
) );
105 $this->sites
[] = $site;
108 // Batch load the local site identifiers.
120 foreach ( $ids as $id ) {
121 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
122 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
123 $site->addLocalId( $id->si_type
, $id->si_key
);
124 $this->sites
->setSite( $site );
130 * @see SiteStore::getSite
134 * @param string $globalId
138 public function getSite( $globalId ) {
139 if ( $this->sites
=== null ) {
140 $this->sites
= $this->getSites();
143 return $this->sites
->hasSite( $globalId ) ?
$this->sites
->getSite( $globalId ) : null;
147 * @see SiteStore::saveSite
153 * @return bool Success indicator
155 public function saveSite( Site
$site ) {
156 return $this->saveSites( array( $site ) );
160 * @see SiteStore::saveSites
164 * @param Site[] $sites
166 * @return bool Success indicator
168 public function saveSites( array $sites ) {
169 if ( empty( $sites ) ) {
173 $dbw = wfGetDB( DB_MASTER
);
175 $dbw->startAtomic( __METHOD__
);
179 $internalIds = array();
182 foreach ( $sites as $site ) {
183 if ( $site->getInternalId() !== null ) {
184 $internalIds[] = $site->getInternalId();
189 'site_global_key' => $site->getGlobalId(), // TODO: check not null
190 'site_type' => $site->getType(),
191 'site_group' => $site->getGroup(),
192 'site_source' => $site->getSource(),
193 'site_language' => $site->getLanguageCode() === null ?
'' : $site->getLanguageCode(),
194 'site_protocol' => $site->getProtocol(),
195 'site_domain' => strrev( $site->getDomain() ) . '.',
196 'site_data' => serialize( $site->getExtraData() ),
199 'site_forward' => $site->shouldForward() ?
1 : 0,
200 'site_config' => serialize( $site->getExtraConfig() ),
203 $rowId = $site->getInternalId();
204 if ( $rowId !== null ) {
205 $success = $dbw->update(
206 'sites', $fields, array( 'site_id' => $rowId ), __METHOD__
209 $rowId = $dbw->nextSequenceValue( 'sites_site_id_seq' );
210 $fields['site_id'] = $rowId;
211 $success = $dbw->insert( 'sites', $fields, __METHOD__
) && $success;
212 $rowId = $dbw->insertId();
215 foreach ( $site->getLocalIds() as $idType => $ids ) {
216 foreach ( $ids as $id ) {
217 $localIds[] = array( $rowId, $idType, $id );
222 if ( $internalIds !== array() ) {
225 array( 'si_site' => $internalIds ),
230 foreach ( $localIds as $localId ) {
234 'si_site' => $localId[0],
235 'si_type' => $localId[1],
236 'si_key' => $localId[2],
242 $dbw->endAtomic( __METHOD__
);
250 * Resets the SiteList
254 public function reset() {
259 * Clears the list of sites stored in the database.
261 * @see SiteStore::clear()
263 * @return bool Success
265 public function clear() {
266 $dbw = wfGetDB( DB_MASTER
);
268 $dbw->startAtomic( __METHOD__
);
269 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
270 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
271 $dbw->endAtomic( __METHOD__
);