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.
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.
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.
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
28 * @license GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 class SiteSQLStore
implements SiteStore
{
38 protected $sites = null;
43 protected $sitesTable;
48 * @param ORMTable|null $sitesTable
52 public static function newInstance( ORMTable
$sitesTable = null ) {
53 return new static( $sitesTable );
61 * @param ORMTable|null $sitesTable
63 protected function __construct( ORMTable
$sitesTable = null ) {
64 if ( $sitesTable === null ) {
65 $sitesTable = $this->newSitesTable();
68 $this->sitesTable
= $sitesTable;
72 * @see SiteStore::getSites
76 * @param string $source either 'cache' or 'recache'
80 public function getSites( $source = 'cache' ) {
81 if ( $source === 'cache' ) {
82 if ( $this->sites
=== null ) {
83 $cache = wfGetMainCache();
84 $sites = $cache->get( wfMemcKey( 'SiteList' ) );
86 if ( is_object( $sites ) ) {
87 $this->sites
= $sites;
101 * Returns a new Site object constructed from the provided ORMRow.
105 * @param ORMRow $siteRow
109 protected function siteFromRow( ORMRow
$siteRow ) {
110 $site = Site
::newForType( $siteRow->getField( 'type', Site
::TYPE_UNKNOWN
) );
112 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
114 if ( $siteRow->hasField( 'forward' ) ) {
115 $site->setForward( $siteRow->getField( 'forward' ) );
118 if ( $siteRow->hasField( 'group' ) ) {
119 $site->setGroup( $siteRow->getField( 'group' ) );
122 if ( $siteRow->hasField( 'language' ) ) {
123 $site->setLanguageCode( $siteRow->getField( 'language' ) === '' ?
null : $siteRow->getField( 'language' ) );
126 if ( $siteRow->hasField( 'source' ) ) {
127 $site->setSource( $siteRow->getField( 'source' ) );
130 if ( $siteRow->hasField( 'data' ) ) {
131 $site->setExtraData( $siteRow->getField( 'data' ) );
134 if ( $siteRow->hasField( 'config' ) ) {
135 $site->setExtraConfig( $siteRow->getField( 'config' ) );
142 * Fetches the site from the database and loads them into the sites field.
146 protected function loadSites() {
147 $this->sites
= new SiteList();
149 foreach ( $this->sitesTable
->select() as $siteRow ) {
150 $this->sites
[] = $this->siteFromRow( $siteRow );
153 // Batch load the local site identifiers.
154 $ids = wfGetDB( $this->sitesTable
->getReadDb() )->select(
165 foreach ( $ids as $id ) {
166 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
167 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
168 $site->addLocalId( $id->si_type
, $id->si_key
);
169 $this->sites
->setSite( $site );
173 $cache = wfGetMainCache();
174 $cache->set( wfMemcKey( 'SiteList' ), $this->sites
);
178 * @see SiteStore::getSite
182 * @param string $globalId
183 * @param string $source
187 public function getSite( $globalId, $source = 'cache' ) {
188 $sites = $this->getSites( $source );
190 return $sites->hasSite( $globalId ) ?
$sites->getSite( $globalId ) : null;
194 * @see SiteStore::saveSite
200 * @return boolean Success indicator
202 public function saveSite( Site
$site ) {
203 return $this->saveSites( array( $site ) );
207 * @see SiteStore::saveSites
211 * @param Site[] $sites
213 * @return boolean Success indicator
215 public function saveSites( array $sites ) {
216 if ( empty( $sites ) ) {
220 $dbw = $this->sitesTable
->getWriteDbConnection();
222 $trx = $dbw->trxLevel();
225 $dbw->begin( __METHOD__
);
230 $internalIds = array();
233 foreach ( $sites as $site ) {
236 'global_key' => $site->getGlobalId(), // TODO: check not null
237 'type' => $site->getType(),
238 'group' => $site->getGroup(),
239 'source' => $site->getSource(),
240 'language' => $site->getLanguageCode() === null ?
'' : $site->getLanguageCode(),
241 'protocol' => $site->getProtocol(),
242 'domain' => strrev( $site->getDomain() ) . '.',
243 'data' => $site->getExtraData(),
246 'forward' => $site->shouldForward(),
247 'config' => $site->getExtraConfig(),
250 if ( $site->getInternalId() !== null ) {
251 $fields['id'] = $site->getInternalId();
252 $internalIds[] = $site->getInternalId();
255 $siteRow = new ORMRow( $this->sitesTable
, $fields );
256 $success = $siteRow->save( __METHOD__
) && $success;
258 foreach ( $site->getLocalIds() as $idType => $ids ) {
259 foreach ( $ids as $id ) {
260 $localIds[] = array( $siteRow->getId(), $idType, $id );
265 if ( $internalIds !== array() ) {
268 array( 'si_site' => $internalIds ),
273 foreach ( $localIds as $localId ) {
277 'si_site' => $localId[0],
278 'si_type' => $localId[1],
279 'si_key' => $localId[2],
286 $dbw->commit( __METHOD__
);
297 protected function newSitesTable() {
304 'global_key' => 'str',
318 'type' => Site
::TYPE_UNKNOWN
,
319 'group' => Site
::GROUP_NONE
,
320 'source' => Site
::SOURCE_LOCAL
,
337 class Sites
extends SiteSQLStore
{
340 * Factory for creating new site objects.
345 * @param string|boolean false $globalId
349 public static function newSite( $globalId = false ) {
352 if ( $globalId !== false ) {
353 $site->setGlobalId( $globalId );
363 public static function singleton() {
371 public function getSiteGroup( $group ) {
372 return $this->getSites()->getGroup( $group );