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 use Wikimedia\Rdbms\IDatabase;
22 use Wikimedia\Rdbms\DBQueryError;
25 * LCStore implementation which uses the standard DB functions to store data.
26 * This will work on any MediaWiki installation.
28 class LCStoreDB implements LCStore {
33 private $writesDone = false;
39 private $readOnly = false;
41 public function get( $code, $key ) {
42 if ( $this->writesDone && $this->dbw ) {
43 $db = $this->dbw; // see the changes in finishWrite()
45 $db = wfGetDB( DB_REPLICA );
48 $value = $db->selectField(
51 [ 'lc_lang' => $code, 'lc_key' => $key ],
55 return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null;
58 public function startWrite( $code ) {
59 if ( $this->readOnly ) {
62 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
65 $this->dbw = wfGetDB( DB_MASTER );
66 $this->readOnly = $this->dbw->isReadOnly();
68 $this->currentLang = $code;
72 public function finishWrite() {
73 if ( $this->readOnly ) {
75 } elseif ( is_null( $this->currentLang ) ) {
76 throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
79 $this->dbw->startAtomic( __METHOD__ );
83 [ 'lc_lang' => $this->currentLang ],
86 foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
87 $this->dbw->insert( 'l10n_cache', $rows, __METHOD__ );
89 $this->writesDone = true;
90 } catch ( DBQueryError $e ) {
91 if ( $this->dbw->wasReadOnlyError() ) {
92 $this->readOnly = true; // just avoid site down time
97 $this->dbw->endAtomic( __METHOD__ );
99 $this->currentLang = null;
103 public function set( $key, $value ) {
104 if ( $this->readOnly ) {
106 } elseif ( is_null( $this->currentLang ) ) {
107 throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
111 'lc_lang' => $this->currentLang,
113 'lc_value' => $this->dbw->encodeBlob( serialize( $value ) )