3 use Wikimedia\Rdbms\LoadBalancer
;
6 * Represents the site configuration of a wiki.
7 * Holds a list of sites (ie SiteList), stored in the database.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
29 * @license GNU GPL v2+
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 * @author Daniel Kinzler
33 class DBSiteStore
implements SiteStore
{
38 protected $sites = null;
43 private $dbLoadBalancer;
48 * @todo: inject some kind of connection manager that is aware of the target wiki,
49 * instead of injecting a LoadBalancer.
51 * @param LoadBalancer $dbLoadBalancer
53 public function __construct( LoadBalancer
$dbLoadBalancer ) {
54 $this->dbLoadBalancer
= $dbLoadBalancer;
58 * @see SiteStore::getSites
64 public function getSites() {
71 * Fetches the site from the database and loads them into the sites field.
75 protected function loadSites() {
76 $this->sites
= new SiteList();
78 $dbr = $this->dbLoadBalancer
->getConnection( DB_REPLICA
);
97 [ 'ORDER BY' => 'site_global_key' ]
100 foreach ( $res as $row ) {
101 $site = Site
::newForType( $row->site_type
);
102 $site->setGlobalId( $row->site_global_key
);
103 $site->setInternalId( (int)$row->site_id
);
104 $site->setForward( (bool)$row->site_forward
);
105 $site->setGroup( $row->site_group
);
106 $site->setLanguageCode( $row->site_language
=== ''
108 : $row->site_language
110 $site->setSource( $row->site_source
);
111 $site->setExtraData( unserialize( $row->site_data
) );
112 $site->setExtraConfig( unserialize( $row->site_config
) );
113 $this->sites
[] = $site;
116 // Batch load the local site identifiers.
128 foreach ( $ids as $id ) {
129 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
130 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
131 $site->addLocalId( $id->si_type
, $id->si_key
);
132 $this->sites
->setSite( $site );
138 * @see SiteStore::getSite
142 * @param string $globalId
146 public function getSite( $globalId ) {
147 if ( $this->sites
=== null ) {
148 $this->sites
= $this->getSites();
151 return $this->sites
->hasSite( $globalId ) ?
$this->sites
->getSite( $globalId ) : null;
155 * @see SiteStore::saveSite
161 * @return bool Success indicator
163 public function saveSite( Site
$site ) {
164 return $this->saveSites( [ $site ] );
168 * @see SiteStore::saveSites
172 * @param Site[] $sites
174 * @return bool Success indicator
176 public function saveSites( array $sites ) {
177 if ( empty( $sites ) ) {
181 $dbw = $this->dbLoadBalancer
->getConnection( DB_MASTER
);
183 $dbw->startAtomic( __METHOD__
);
190 foreach ( $sites as $site ) {
191 if ( $site->getInternalId() !== null ) {
192 $internalIds[] = $site->getInternalId();
197 'site_global_key' => $site->getGlobalId(), // TODO: check not null
198 'site_type' => $site->getType(),
199 'site_group' => $site->getGroup(),
200 'site_source' => $site->getSource(),
201 'site_language' => $site->getLanguageCode() === null ?
'' : $site->getLanguageCode(),
202 'site_protocol' => $site->getProtocol(),
203 'site_domain' => strrev( $site->getDomain() ) . '.',
204 'site_data' => serialize( $site->getExtraData() ),
207 'site_forward' => $site->shouldForward() ?
1 : 0,
208 'site_config' => serialize( $site->getExtraConfig() ),
211 $rowId = $site->getInternalId();
212 if ( $rowId !== null ) {
213 $success = $dbw->update(
214 'sites', $fields, [ 'site_id' => $rowId ], __METHOD__
217 $rowId = $dbw->nextSequenceValue( 'sites_site_id_seq' );
218 $fields['site_id'] = $rowId;
219 $success = $dbw->insert( 'sites', $fields, __METHOD__
) && $success;
220 $rowId = $dbw->insertId();
223 foreach ( $site->getLocalIds() as $idType => $ids ) {
224 foreach ( $ids as $id ) {
225 $localIds[] = [ $rowId, $idType, $id ];
230 if ( $internalIds !== [] ) {
233 [ 'si_site' => $internalIds ],
238 foreach ( $localIds as $localId ) {
242 'si_site' => $localId[0],
243 'si_type' => $localId[1],
244 'si_key' => $localId[2],
250 $dbw->endAtomic( __METHOD__
);
258 * Resets the SiteList
262 public function reset() {
267 * Clears the list of sites stored in the database.
269 * @see SiteStore::clear()
271 * @return bool Success
273 public function clear() {
274 $dbw = $this->dbLoadBalancer
->getConnection( DB_MASTER
);
276 $dbw->startAtomic( __METHOD__
);
277 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
278 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
279 $dbw->endAtomic( __METHOD__
);