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\Database
;
22 use Wikimedia\Rdbms\IDatabase
;
23 use Wikimedia\Rdbms\DBQueryError
;
26 * LCStore implementation which uses the standard DB functions to store data.
27 * This will work on any MediaWiki installation.
29 class LCStoreDB
implements LCStore
{
30 /** @var string Language code */
32 /** @var array Server configuration map */
35 /** @var array Rows buffered for insertion */
38 /** @var IDatabase|null */
40 /** @var bool Whether a batch of writes were recently written */
41 private $writesDone = false;
42 /** @var bool Whether the DB is read-only or otherwise unavailable for writes */
43 private $readOnly = false;
45 public function __construct( $params ) {
46 $this->server
= $params['server'] ??
[];
49 public function get( $code, $key ) {
50 if ( $this->server ||
$this->writesDone
) {
51 // If a server configuration map is specified, always used that connection
52 // for reads and writes. Otherwise, if writes occurred in finishWrite(), make
53 // sure those changes are always visible.
54 $db = $this->getWriteConnection();
56 $db = wfGetDB( DB_REPLICA
);
59 $value = $db->selectField(
62 [ 'lc_lang' => $code, 'lc_key' => $key ],
66 return ( $value !== false ) ?
unserialize( $db->decodeBlob( $value ) ) : null;
69 public function startWrite( $code ) {
70 if ( $this->readOnly
) {
73 throw new MWException( __METHOD__
. ": Invalid language \"$code\"" );
76 $dbw = $this->getWriteConnection();
77 $this->readOnly
= $dbw->isReadOnly();
83 public function finishWrite() {
84 if ( $this->readOnly
) {
86 } elseif ( is_null( $this->code
) ) {
87 throw new MWException( __CLASS__
. ': must call startWrite() before finishWrite()' );
90 $trxProfiler = Profiler
::instance()->getTransactionProfiler();
91 $oldSilenced = $trxProfiler->setSilenced( true );
93 $dbw = $this->getWriteConnection();
94 $dbw->startAtomic( __METHOD__
);
96 $dbw->delete( 'l10n_cache', [ 'lc_lang' => $this->code
], __METHOD__
);
97 foreach ( array_chunk( $this->batch
, 500 ) as $rows ) {
98 $dbw->insert( 'l10n_cache', $rows, __METHOD__
);
100 $this->writesDone
= true;
101 } catch ( DBQueryError
$e ) {
102 if ( $dbw->wasReadOnlyError() ) {
103 $this->readOnly
= true; // just avoid site down time
108 $dbw->endAtomic( __METHOD__
);
110 $trxProfiler->setSilenced( $oldSilenced );
117 public function set( $key, $value ) {
118 if ( $this->readOnly
) {
120 } elseif ( is_null( $this->code
) ) {
121 throw new MWException( __CLASS__
. ': must call startWrite() before set()' );
124 $dbw = $this->getWriteConnection();
127 'lc_lang' => $this->code
,
129 'lc_value' => $dbw->encodeBlob( serialize( $value ) )
136 private function getWriteConnection() {
138 if ( $this->server
) {
139 $this->dbw
= Database
::factory( $this->server
['type'], $this->server
);
141 throw new MWException( __CLASS__
. ': failed to obtain a DB connection' );
144 $this->dbw
= wfGetDB( DB_MASTER
);