c1c10c2136b936ff1157a6249bee888f954f95fa
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 >
29 * @author Daniel Kinzler
31 class DBSiteStore
implements SiteStore
{
36 protected $sites = null;
41 private $dbLoadBalancer;
46 private $languageCodeMapping = [];
51 * @todo: inject some kind of connection manager that is aware of the target wiki,
52 * instead of injecting a LoadBalancer.
54 * @param LoadBalancer $dbLoadBalancer
56 public function __construct( LoadBalancer
$dbLoadBalancer ) {
57 $this->dbLoadBalancer
= $dbLoadBalancer;
61 * @see SiteStore::getSites
67 public function getSites() {
74 * Fetches the site from the database and loads them into the sites field.
78 protected function loadSites() {
79 $this->sites
= new SiteList();
81 $dbr = $this->dbLoadBalancer
->getConnection( DB_SLAVE
);
100 [ 'ORDER BY' => 'site_global_key' ]
103 foreach ( $res as $row ) {
104 $languageCode = $row->site_language
=== '' ?
null : $row->site_language
;
105 if ( isset( $this->languageCodeMapping
[ $languageCode ] ) ) {
106 $languageCode = $this->languageCodeMapping
[ $languageCode ];
109 $site = Site
::newForType( $row->site_type
);
110 $site->setGlobalId( $row->site_global_key
);
111 $site->setInternalId( (int)$row->site_id
);
112 $site->setForward( (bool)$row->site_forward
);
113 $site->setGroup( $row->site_group
);
114 $site->setLanguageCode( $languageCode );
115 $site->setSource( $row->site_source
);
116 $site->setExtraData( unserialize( $row->site_data
) );
117 $site->setExtraConfig( unserialize( $row->site_config
) );
118 $this->sites
[] = $site;
121 // Batch load the local site identifiers.
133 foreach ( $ids as $id ) {
134 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
135 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
136 $site->addLocalId( $id->si_type
, $id->si_key
);
137 $this->sites
->setSite( $site );
141 $this->dbLoadBalancer
->reuseConnection( $dbr );
145 * @see SiteStore::getSite
149 * @param string $globalId
153 public function getSite( $globalId ) {
154 if ( $this->sites
=== null ) {
155 $this->sites
= $this->getSites();
158 return $this->sites
->hasSite( $globalId ) ?
$this->sites
->getSite( $globalId ) : null;
162 * @see SiteStore::saveSite
168 * @return bool Success indicator
170 public function saveSite( Site
$site ) {
171 return $this->saveSites( [ $site ] );
175 * @see SiteStore::saveSites
179 * @param Site[] $sites
181 * @return bool Success indicator
183 public function saveSites( array $sites ) {
184 if ( empty( $sites ) ) {
188 $dbw = $this->dbLoadBalancer
->getConnection( DB_MASTER
);
190 $dbw->startAtomic( __METHOD__
);
197 foreach ( $sites as $site ) {
198 if ( $site->getInternalId() !== null ) {
199 $internalIds[] = $site->getInternalId();
204 'site_global_key' => $site->getGlobalId(), // TODO: check not null
205 'site_type' => $site->getType(),
206 'site_group' => $site->getGroup(),
207 'site_source' => $site->getSource(),
208 'site_language' => $site->getLanguageCode() === null ?
'' : $site->getLanguageCode(),
209 'site_protocol' => $site->getProtocol(),
210 'site_domain' => strrev( $site->getDomain() ) . '.',
211 'site_data' => serialize( $site->getExtraData() ),
214 'site_forward' => $site->shouldForward() ?
1 : 0,
215 'site_config' => serialize( $site->getExtraConfig() ),
218 $rowId = $site->getInternalId();
219 if ( $rowId !== null ) {
220 $success = $dbw->update(
221 'sites', $fields, [ 'site_id' => $rowId ], __METHOD__
224 $rowId = $dbw->nextSequenceValue( 'sites_site_id_seq' );
225 $fields['site_id'] = $rowId;
226 $success = $dbw->insert( 'sites', $fields, __METHOD__
) && $success;
227 $rowId = $dbw->insertId();
230 foreach ( $site->getLocalIds() as $idType => $ids ) {
231 foreach ( $ids as $id ) {
232 $localIds[] = [ $rowId, $idType, $id ];
237 if ( $internalIds !== [] ) {
240 [ 'si_site' => $internalIds ],
245 foreach ( $localIds as $localId ) {
249 'si_site' => $localId[0],
250 'si_type' => $localId[1],
251 'si_key' => $localId[2],
257 $dbw->endAtomic( __METHOD__
);
259 $this->dbLoadBalancer
->reuseConnection( $dbw );
267 * Resets the SiteList
271 public function reset() {
276 * Clears the list of sites stored in the database.
278 * @see SiteStore::clear()
280 * @return bool Success
282 public function clear() {
283 $dbw = $this->dbLoadBalancer
->getConnection( DB_MASTER
);
285 $dbw->startAtomic( __METHOD__
);
286 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
287 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
288 $dbw->endAtomic( __METHOD__
);
290 $this->dbLoadBalancer
->reuseConnection( $dbw );
298 * Provide an array that maps language codes
300 * @param string[] $newMapping
302 public function setLanguageCodeMapping( array $newMapping ) {
303 $this->languageCodeMapping
= $newMapping;