Make mediawiki.special.pageLanguage work again
[mediawiki.git] / includes / site / DBSiteStore.php
blob17764a1985be10cca6c5ddfb9501d8f0437f990f
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 * @since 1.25
39 * @param null $sitesTable Unused since 1.27
41 public function __construct( $sitesTable = null ) {
42 if ( $sitesTable !== null ) {
43 throw new InvalidArgumentException(
44 __METHOD__ . ': $sitesTable parameter must be null'
49 /**
50 * @see SiteStore::getSites
52 * @since 1.25
54 * @return SiteList
56 public function getSites() {
57 $this->loadSites();
59 return $this->sites;
62 /**
63 * Fetches the site from the database and loads them into the sites field.
65 * @since 1.25
67 protected function loadSites() {
68 $this->sites = new SiteList();
70 $dbr = wfGetDB( DB_SLAVE );
72 $res = $dbr->select(
73 'sites',
74 array(
75 'site_id',
76 'site_global_key',
77 'site_type',
78 'site_group',
79 'site_source',
80 'site_language',
81 'site_protocol',
82 'site_domain',
83 'site_data',
84 'site_forward',
85 'site_config',
87 '',
88 __METHOD__,
89 array( 'ORDER BY' => 'site_global_key' )
92 foreach ( $res as $row ) {
93 $site = Site::newForType( $row->site_type );
94 $site->setGlobalId( $row->site_global_key );
95 $site->setInternalId( (int)$row->site_id );
96 $site->setForward( (bool)$row->site_forward );
97 $site->setGroup( $row->site_group );
98 $site->setLanguageCode( $row->site_language === ''
99 ? null
100 : $row->site_language
102 $site->setSource( $row->site_source );
103 $site->setExtraData( unserialize( $row->site_data ) );
104 $site->setExtraConfig( unserialize( $row->site_config ) );
105 $this->sites[] = $site;
108 // Batch load the local site identifiers.
109 $ids = $dbr->select(
110 'site_identifiers',
111 array(
112 'si_site',
113 'si_type',
114 'si_key',
116 array(),
117 __METHOD__
120 foreach ( $ids as $id ) {
121 if ( $this->sites->hasInternalId( $id->si_site ) ) {
122 $site = $this->sites->getSiteByInternalId( $id->si_site );
123 $site->addLocalId( $id->si_type, $id->si_key );
124 $this->sites->setSite( $site );
130 * @see SiteStore::getSite
132 * @since 1.25
134 * @param string $globalId
136 * @return Site|null
138 public function getSite( $globalId ) {
139 if ( $this->sites === null ) {
140 $this->sites = $this->getSites();
143 return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
147 * @see SiteStore::saveSite
149 * @since 1.25
151 * @param Site $site
153 * @return bool Success indicator
155 public function saveSite( Site $site ) {
156 return $this->saveSites( array( $site ) );
160 * @see SiteStore::saveSites
162 * @since 1.25
164 * @param Site[] $sites
166 * @return bool Success indicator
168 public function saveSites( array $sites ) {
169 if ( empty( $sites ) ) {
170 return true;
173 $dbw = wfGetDB( DB_MASTER );
175 $dbw->startAtomic( __METHOD__ );
177 $success = true;
179 $internalIds = array();
180 $localIds = array();
182 foreach ( $sites as $site ) {
183 if ( $site->getInternalId() !== null ) {
184 $internalIds[] = $site->getInternalId();
187 $fields = array(
188 // Site data
189 'site_global_key' => $site->getGlobalId(), // TODO: check not null
190 'site_type' => $site->getType(),
191 'site_group' => $site->getGroup(),
192 'site_source' => $site->getSource(),
193 'site_language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
194 'site_protocol' => $site->getProtocol(),
195 'site_domain' => strrev( $site->getDomain() ) . '.',
196 'site_data' => serialize( $site->getExtraData() ),
198 // Site config
199 'site_forward' => $site->shouldForward() ? 1 : 0,
200 'site_config' => serialize( $site->getExtraConfig() ),
203 $rowId = $site->getInternalId();
204 if ( $rowId !== null ) {
205 $success = $dbw->update(
206 'sites', $fields, array( 'site_id' => $rowId ), __METHOD__
207 ) && $success;
208 } else {
209 $rowId = $dbw->nextSequenceValue( 'sites_site_id_seq' );
210 $fields['site_id'] = $rowId;
211 $success = $dbw->insert( 'sites', $fields, __METHOD__ ) && $success;
212 $rowId = $dbw->insertId();
215 foreach ( $site->getLocalIds() as $idType => $ids ) {
216 foreach ( $ids as $id ) {
217 $localIds[] = array( $rowId, $idType, $id );
222 if ( $internalIds !== array() ) {
223 $dbw->delete(
224 'site_identifiers',
225 array( 'si_site' => $internalIds ),
226 __METHOD__
230 foreach ( $localIds as $localId ) {
231 $dbw->insert(
232 'site_identifiers',
233 array(
234 'si_site' => $localId[0],
235 'si_type' => $localId[1],
236 'si_key' => $localId[2],
238 __METHOD__
242 $dbw->endAtomic( __METHOD__ );
244 $this->reset();
246 return $success;
250 * Resets the SiteList
252 * @since 1.25
254 public function reset() {
255 $this->sites = null;
259 * Clears the list of sites stored in the database.
261 * @see SiteStore::clear()
263 * @return bool Success
265 public function clear() {
266 $dbw = wfGetDB( DB_MASTER );
268 $dbw->startAtomic( __METHOD__ );
269 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
270 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
271 $dbw->endAtomic( __METHOD__ );
273 $this->reset();
275 return $ok;