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;
40 protected $sitesTable;
45 * @param ORMTable|null $sitesTable
47 public function __construct( ORMTable
$sitesTable = null ) {
48 if ( $sitesTable === null ) {
49 $sitesTable = $this->newSitesTable();
52 $this->sitesTable
= $sitesTable;
56 * @see SiteStore::getSites
62 public function getSites() {
69 * Returns a new Site object constructed from the provided ORMRow.
73 * @param ORMRow $siteRow
77 protected function siteFromRow( ORMRow
$siteRow ) {
79 $site = Site
::newForType( $siteRow->getField( 'type', Site
::TYPE_UNKNOWN
) );
81 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
83 $site->setInternalId( $siteRow->getField( 'id' ) );
85 if ( $siteRow->hasField( 'forward' ) ) {
86 $site->setForward( $siteRow->getField( 'forward' ) );
89 if ( $siteRow->hasField( 'group' ) ) {
90 $site->setGroup( $siteRow->getField( 'group' ) );
93 if ( $siteRow->hasField( 'language' ) ) {
94 $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
96 : $siteRow->getField( 'language' )
100 if ( $siteRow->hasField( 'source' ) ) {
101 $site->setSource( $siteRow->getField( 'source' ) );
104 if ( $siteRow->hasField( 'data' ) ) {
105 $site->setExtraData( $siteRow->getField( 'data' ) );
108 if ( $siteRow->hasField( 'config' ) ) {
109 $site->setExtraConfig( $siteRow->getField( 'config' ) );
116 * Get a new ORMRow from a Site object
124 protected function getRowFromSite( Site
$site ) {
127 'global_key' => $site->getGlobalId(), // TODO: check not null
128 'type' => $site->getType(),
129 'group' => $site->getGroup(),
130 'source' => $site->getSource(),
131 'language' => $site->getLanguageCode() === null ?
'' : $site->getLanguageCode(),
132 'protocol' => $site->getProtocol(),
133 'domain' => strrev( $site->getDomain() ) . '.',
134 'data' => $site->getExtraData(),
137 'forward' => $site->shouldForward(),
138 'config' => $site->getExtraConfig(),
141 if ( $site->getInternalId() !== null ) {
142 $fields['id'] = $site->getInternalId();
145 return new ORMRow( $this->sitesTable
, $fields );
149 * Fetches the site from the database and loads them into the sites field.
153 protected function loadSites() {
154 $this->sites
= new SiteList();
156 foreach ( $this->sitesTable
->select() as $siteRow ) {
157 $this->sites
[] = $this->siteFromRow( $siteRow );
160 // Batch load the local site identifiers.
161 $ids = wfGetDB( $this->sitesTable
->getReadDb() )->select(
172 foreach ( $ids as $id ) {
173 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
174 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
175 $site->addLocalId( $id->si_type
, $id->si_key
);
176 $this->sites
->setSite( $site );
182 * @see SiteStore::getSite
186 * @param string $globalId
190 public function getSite( $globalId ) {
191 if ( $this->sites
=== null ) {
192 $this->sites
= $this->getSites();
195 return $this->sites
->hasSite( $globalId ) ?
$this->sites
->getSite( $globalId ) : null;
199 * @see SiteStore::saveSite
205 * @return bool Success indicator
207 public function saveSite( Site
$site ) {
208 return $this->saveSites( array( $site ) );
212 * @see SiteStore::saveSites
216 * @param Site[] $sites
218 * @return bool Success indicator
220 public function saveSites( array $sites ) {
221 if ( empty( $sites ) ) {
225 $dbw = $this->sitesTable
->getWriteDbConnection();
227 $dbw->startAtomic( __METHOD__
);
231 $internalIds = array();
234 foreach ( $sites as $site ) {
235 if ( $site->getInternalId() !== null ) {
236 $internalIds[] = $site->getInternalId();
239 $siteRow = $this->getRowFromSite( $site );
240 $success = $siteRow->save( __METHOD__
) && $success;
242 foreach ( $site->getLocalIds() as $idType => $ids ) {
243 foreach ( $ids as $id ) {
244 $localIds[] = array( $siteRow->getId(), $idType, $id );
249 if ( $internalIds !== array() ) {
252 array( 'si_site' => $internalIds ),
257 foreach ( $localIds as $localId ) {
261 'si_site' => $localId[0],
262 'si_type' => $localId[1],
263 'si_key' => $localId[2],
269 $dbw->endAtomic( __METHOD__
);
277 * Resets the SiteList
281 public function reset() {
286 * Clears the list of sites stored in the database.
288 * @see SiteStore::clear()
290 * @return bool Success
292 public function clear() {
293 $dbw = $this->sitesTable
->getWriteDbConnection();
295 $dbw->startAtomic( __METHOD__
);
296 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
297 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
298 $dbw->endAtomic( __METHOD__
);
310 protected function newSitesTable() {
317 'global_key' => 'str',
331 'type' => Site
::TYPE_UNKNOWN
,
332 'group' => Site
::GROUP_NONE
,
333 'source' => Site
::SOURCE_LOCAL
,