Merge "Use a fixed marker prefix string in the Parser and MWTidy"
[mediawiki.git] / includes / site / DBSiteStore.php
blobf167584e70ba35c0d65bf4ad5ec64f989c9a31bf
1 <?php
3 /**
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
22 * @since 1.25
24 * @file
25 * @ingroup Site
27 * @license GNU GPL v2+
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 class DBSiteStore implements SiteStore {
32 /**
33 * @var SiteList|null
35 protected $sites = null;
37 /**
38 * @var ORMTable
40 protected $sitesTable;
42 /**
43 * @since 1.25
45 * @param ORMTable|null $sitesTable
47 public function __construct( ORMTable $sitesTable = null ) {
48 if ( $sitesTable === null ) {
49 $sitesTable = $this->newSitesTable();
52 $this->sitesTable = $sitesTable;
55 /**
56 * @see SiteStore::getSites
58 * @since 1.25
60 * @return SiteList
62 public function getSites() {
63 $this->loadSites();
65 return $this->sites;
68 /**
69 * Returns a new Site object constructed from the provided ORMRow.
71 * @since 1.25
73 * @param ORMRow $siteRow
75 * @return Site
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' ) === ''
95 ? null
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' ) );
112 return $site;
116 * Get a new ORMRow from a Site object
118 * @since 1.25
120 * @param Site $site
122 * @return ORMRow
124 protected function getRowFromSite( Site $site ) {
125 $fields = array(
126 // Site data
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(),
136 // Site config
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.
151 * @since 1.25
153 protected function loadSites() {
154 $this->sites = new SiteList();
156 foreach ( $this->sitesTable->select() as $siteRow ) {
157 $this->sites[] = $this->siteFromRow( $siteRow );
160 // Batch load the local site identifiers.
161 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
162 'site_identifiers',
163 array(
164 'si_site',
165 'si_type',
166 'si_key',
168 array(),
169 __METHOD__
172 foreach ( $ids as $id ) {
173 if ( $this->sites->hasInternalId( $id->si_site ) ) {
174 $site = $this->sites->getSiteByInternalId( $id->si_site );
175 $site->addLocalId( $id->si_type, $id->si_key );
176 $this->sites->setSite( $site );
182 * @see SiteStore::getSite
184 * @since 1.25
186 * @param string $globalId
188 * @return Site|null
190 public function getSite( $globalId ) {
191 if ( $this->sites === null ) {
192 $this->sites = $this->getSites();
195 return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
199 * @see SiteStore::saveSite
201 * @since 1.25
203 * @param Site $site
205 * @return bool Success indicator
207 public function saveSite( Site $site ) {
208 return $this->saveSites( array( $site ) );
212 * @see SiteStore::saveSites
214 * @since 1.25
216 * @param Site[] $sites
218 * @return bool Success indicator
220 public function saveSites( array $sites ) {
221 if ( empty( $sites ) ) {
222 return true;
225 $dbw = $this->sitesTable->getWriteDbConnection();
227 $dbw->startAtomic( __METHOD__ );
229 $success = true;
231 $internalIds = array();
232 $localIds = array();
234 foreach ( $sites as $site ) {
235 if ( $site->getInternalId() !== null ) {
236 $internalIds[] = $site->getInternalId();
239 $siteRow = $this->getRowFromSite( $site );
240 $success = $siteRow->save( __METHOD__ ) && $success;
242 foreach ( $site->getLocalIds() as $idType => $ids ) {
243 foreach ( $ids as $id ) {
244 $localIds[] = array( $siteRow->getId(), $idType, $id );
249 if ( $internalIds !== array() ) {
250 $dbw->delete(
251 'site_identifiers',
252 array( 'si_site' => $internalIds ),
253 __METHOD__
257 foreach ( $localIds as $localId ) {
258 $dbw->insert(
259 'site_identifiers',
260 array(
261 'si_site' => $localId[0],
262 'si_type' => $localId[1],
263 'si_key' => $localId[2],
265 __METHOD__
269 $dbw->endAtomic( __METHOD__ );
271 $this->reset();
273 return $success;
277 * Resets the SiteList
279 * @since 1.25
281 public function reset() {
282 $this->sites = null;
286 * Clears the list of sites stored in the database.
288 * @see SiteStore::clear()
290 * @return bool Success
292 public function clear() {
293 $dbw = $this->sitesTable->getWriteDbConnection();
295 $dbw->startAtomic( __METHOD__ );
296 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
297 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
298 $dbw->endAtomic( __METHOD__ );
300 $this->reset();
302 return $ok;
306 * @since 1.25
308 * @return ORMTable
310 protected function newSitesTable() {
311 return new ORMTable(
312 'sites',
313 array(
314 'id' => 'id',
316 // Site data
317 'global_key' => 'str',
318 'type' => 'str',
319 'group' => 'str',
320 'source' => 'str',
321 'language' => 'str',
322 'protocol' => 'str',
323 'domain' => 'str',
324 'data' => 'array',
326 // Site config
327 'forward' => 'bool',
328 'config' => 'array',
330 array(
331 'type' => Site::TYPE_UNKNOWN,
332 'group' => Site::GROUP_NONE,
333 'source' => Site::SOURCE_LOCAL,
334 'data' => array(),
336 'forward' => false,
337 'config' => array(),
338 'language' => '',
340 'ORMRow',
341 'site_'