3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Site
;
23 use Wikimedia\Rdbms\IConnectionProvider
;
24 use Wikimedia\Rdbms\IDatabase
;
27 * Holds a list of sites stored in the database.
31 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
32 * @author Daniel Kinzler
34 class DBSiteStore
implements SiteStore
{
35 /** @var SiteList|null */
36 protected $sites = null;
37 /** @var IConnectionProvider */
42 * @param IConnectionProvider $dbProvider
44 public function __construct( IConnectionProvider
$dbProvider ) {
45 $this->dbProvider
= $dbProvider;
49 * @see SiteStore::getSites
54 public function getSites() {
61 * Fetches the site from the database and loads them into the sites field.
65 protected function loadSites() {
66 $this->sites
= new SiteList();
68 $dbr = $this->dbProvider
->getReplicaDatabase();
70 $res = $dbr->newSelectQueryBuilder()
85 ->orderBy( 'site_global_key' )
86 ->caller( __METHOD__
)->fetchResultSet();
88 foreach ( $res as $row ) {
89 $site = Site
::newForType( $row->site_type
);
90 $site->setGlobalId( $row->site_global_key
);
91 $site->setInternalId( (int)$row->site_id
);
92 $site->setForward( (bool)$row->site_forward
);
93 $site->setGroup( $row->site_group
);
94 $site->setLanguageCode( $row->site_language
=== ''
98 $site->setSource( $row->site_source
);
99 $site->setExtraData( unserialize( $row->site_data
) );
100 $site->setExtraConfig( unserialize( $row->site_config
) );
101 $this->sites
[] = $site;
104 // Batch load the local site identifiers.
105 $ids = $dbr->newSelectQueryBuilder()
106 ->select( [ 'si_site', 'si_type', 'si_key', ] )
107 ->from( 'site_identifiers' )
108 ->caller( __METHOD__
)->fetchResultSet();
110 foreach ( $ids as $id ) {
111 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
112 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
113 $site->addLocalId( $id->si_type
, $id->si_key
);
114 $this->sites
->setSite( $site );
120 * @see SiteStore::getSite
123 * @param string $globalId
126 public function getSite( $globalId ) {
127 if ( $this->sites
=== null ) {
128 $this->sites
= $this->getSites();
131 return $this->sites
->hasSite( $globalId ) ?
$this->sites
->getSite( $globalId ) : null;
135 * @see SiteStore::saveSite
139 * @return bool Success indicator
141 public function saveSite( Site
$site ) {
142 return $this->saveSites( [ $site ] );
146 * @see SiteStore::saveSites
149 * @param Site[] $sites
150 * @return bool Success indicator
152 public function saveSites( array $sites ) {
157 $dbw = $this->dbProvider
->getPrimaryDatabase();
159 $dbw->startAtomic( __METHOD__
);
164 foreach ( $sites as $site ) {
165 if ( $site->getInternalId() !== null ) {
166 $internalIds[] = $site->getInternalId();
171 'site_global_key' => $site->getGlobalId(), // TODO: check not null
172 'site_type' => $site->getType(),
173 'site_group' => $site->getGroup(),
174 'site_source' => $site->getSource(),
175 'site_language' => $site->getLanguageCode() ??
'',
176 'site_protocol' => $site->getProtocol(),
177 'site_domain' => strrev( $site->getDomain() ??
'' ) . '.',
178 'site_data' => serialize( $site->getExtraData() ),
181 'site_forward' => $site->shouldForward() ?
1 : 0,
182 'site_config' => serialize( $site->getExtraConfig() ),
185 $rowId = $site->getInternalId();
186 if ( $rowId !== null ) {
187 $dbw->newUpdateQueryBuilder()
190 ->where( [ 'site_id' => $rowId ] )
191 ->caller( __METHOD__
)->execute();
193 $dbw->newInsertQueryBuilder()
194 ->insertInto( 'sites' )
196 ->caller( __METHOD__
)->execute();
197 $rowId = $dbw->insertId();
200 foreach ( $site->getLocalIds() as $idType => $ids ) {
201 foreach ( $ids as $id ) {
202 $localIds[] = [ $rowId, $idType, $id ];
207 if ( $internalIds !== [] ) {
208 $dbw->newDeleteQueryBuilder()
209 ->deleteFrom( 'site_identifiers' )
210 ->where( [ 'si_site' => $internalIds ] )
211 ->caller( __METHOD__
)->execute();
214 foreach ( $localIds as $localId ) {
215 $dbw->newInsertQueryBuilder()
216 ->insertInto( 'site_identifiers' )
217 ->row( [ 'si_site' => $localId[0], 'si_type' => $localId[1], 'si_key' => $localId[2] ] )
218 ->caller( __METHOD__
)->execute();
221 $dbw->endAtomic( __METHOD__
);
229 * Resets the SiteList
233 public function reset() {
238 * Clears the list of sites stored in the database.
240 * @see SiteStore::clear()
242 public function clear() {
243 $dbw = $this->dbProvider
->getPrimaryDatabase();
245 $dbw->startAtomic( __METHOD__
);
246 $dbw->newDeleteQueryBuilder()
247 ->deleteFrom( 'sites' )
248 ->where( IDatabase
::ALL_ROWS
)
249 ->caller( __METHOD__
)->execute();
250 $dbw->newDeleteQueryBuilder()
251 ->deleteFrom( 'site_identifiers' )
252 ->where( IDatabase
::ALL_ROWS
)
253 ->caller( __METHOD__
)->execute();
254 $dbw->endAtomic( __METHOD__
);
261 /** @deprecated class alias since 1.42 */
262 class_alias( DBSiteStore
::class, 'DBSiteStore' );