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
22 * LCStore implementation which uses the standard DB functions to store data.
23 * This will work on any MediaWiki installation.
25 class LCStoreDB
implements LCStore
{
30 private $writesDone = false;
36 private $readOnly = false;
38 public function get( $code, $key ) {
39 if ( $this->writesDone
&& $this->dbw
) {
40 $db = $this->dbw
; // see the changes in finishWrite()
42 $db = wfGetDB( DB_REPLICA
);
45 $value = $db->selectField(
48 [ 'lc_lang' => $code, 'lc_key' => $key ],
52 return ( $value !== false ) ?
unserialize( $db->decodeBlob( $value ) ) : null;
55 public function startWrite( $code ) {
56 if ( $this->readOnly
) {
59 throw new MWException( __METHOD__
. ": Invalid language \"$code\"" );
62 $this->dbw
= wfGetDB( DB_MASTER
);
63 $this->readOnly
= $this->dbw
->isReadOnly();
65 $this->currentLang
= $code;
69 public function finishWrite() {
70 if ( $this->readOnly
) {
72 } elseif ( is_null( $this->currentLang
) ) {
73 throw new MWException( __CLASS__
. ': must call startWrite() before finishWrite()' );
76 $this->dbw
->startAtomic( __METHOD__
);
80 [ 'lc_lang' => $this->currentLang
],
83 foreach ( array_chunk( $this->batch
, 500 ) as $rows ) {
84 $this->dbw
->insert( 'l10n_cache', $rows, __METHOD__
);
86 $this->writesDone
= true;
87 } catch ( DBQueryError
$e ) {
88 if ( $this->dbw
->wasReadOnlyError() ) {
89 $this->readOnly
= true; // just avoid site down time
94 $this->dbw
->endAtomic( __METHOD__
);
96 $this->currentLang
= null;
100 public function set( $key, $value ) {
101 if ( $this->readOnly
) {
103 } elseif ( is_null( $this->currentLang
) ) {
104 throw new MWException( __CLASS__
. ': must call startWrite() before set()' );
108 'lc_lang' => $this->currentLang
,
110 'lc_value' => $this->dbw
->encodeBlob( serialize( $value ) )