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 $siteRows = $this->sitesTable
->select( null, array(), array(
157 'ORDER BY' => 'site_global_key'
160 foreach ( $siteRows as $siteRow ) {
161 $this->sites
[] = $this->siteFromRow( $siteRow );
164 // Batch load the local site identifiers.
165 $ids = wfGetDB( $this->sitesTable
->getReadDb() )->select(
176 foreach ( $ids as $id ) {
177 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
178 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
179 $site->addLocalId( $id->si_type
, $id->si_key
);
180 $this->sites
->setSite( $site );
186 * @see SiteStore::getSite
190 * @param string $globalId
194 public function getSite( $globalId ) {
195 if ( $this->sites
=== null ) {
196 $this->sites
= $this->getSites();
199 return $this->sites
->hasSite( $globalId ) ?
$this->sites
->getSite( $globalId ) : null;
203 * @see SiteStore::saveSite
209 * @return bool Success indicator
211 public function saveSite( Site
$site ) {
212 return $this->saveSites( array( $site ) );
216 * @see SiteStore::saveSites
220 * @param Site[] $sites
222 * @return bool Success indicator
224 public function saveSites( array $sites ) {
225 if ( empty( $sites ) ) {
229 $dbw = $this->sitesTable
->getWriteDbConnection();
231 $dbw->startAtomic( __METHOD__
);
235 $internalIds = array();
238 foreach ( $sites as $site ) {
239 if ( $site->getInternalId() !== null ) {
240 $internalIds[] = $site->getInternalId();
243 $siteRow = $this->getRowFromSite( $site );
244 $success = $siteRow->save( __METHOD__
) && $success;
246 foreach ( $site->getLocalIds() as $idType => $ids ) {
247 foreach ( $ids as $id ) {
248 $localIds[] = array( $siteRow->getId(), $idType, $id );
253 if ( $internalIds !== array() ) {
256 array( 'si_site' => $internalIds ),
261 foreach ( $localIds as $localId ) {
265 'si_site' => $localId[0],
266 'si_type' => $localId[1],
267 'si_key' => $localId[2],
273 $dbw->endAtomic( __METHOD__
);
281 * Resets the SiteList
285 public function reset() {
290 * Clears the list of sites stored in the database.
292 * @see SiteStore::clear()
294 * @return bool Success
296 public function clear() {
297 $dbw = $this->sitesTable
->getWriteDbConnection();
299 $dbw->startAtomic( __METHOD__
);
300 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
301 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
302 $dbw->endAtomic( __METHOD__
);
314 protected function newSitesTable() {
321 'global_key' => 'str',
335 'type' => Site
::TYPE_UNKNOWN
,
336 'group' => Site
::GROUP_NONE
,
337 'source' => Site
::SOURCE_LOCAL
,