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
{
33 private $writesDone = false;
34 /** @var IDatabase|null */
39 private $readOnly = false;
40 /** @var array Server configuration map */
43 public function __construct( $params ) {
44 $this->server
= $params['server'] ??
[];
47 public function get( $code, $key ) {
48 if ( $this->server ||
$this->writesDone
) {
49 // If a server configuration map is specified, always used that connection
50 // for reads and writes. Otherwise, if writes occurred in finishWrite(), make
51 // sure those changes are always visible.
52 $db = $this->getWriteConnection();
54 $db = wfGetDB( DB_REPLICA
);
57 $value = $db->selectField(
60 [ 'lc_lang' => $code, 'lc_key' => $key ],
64 return ( $value !== false ) ?
unserialize( $db->decodeBlob( $value ) ) : null;
67 public function startWrite( $code ) {
68 if ( $this->readOnly
) {
71 throw new MWException( __METHOD__
. ": Invalid language \"$code\"" );
74 $dbw = $this->getWriteConnection();
75 $this->readOnly
= $dbw->isReadOnly();
77 $this->currentLang
= $code;
81 public function finishWrite() {
82 if ( $this->readOnly
) {
84 } elseif ( is_null( $this->currentLang
) ) {
85 throw new MWException( __CLASS__
. ': must call startWrite() before finishWrite()' );
88 $trxProfiler = Profiler
::instance()->getTransactionProfiler();
89 $oldSilenced = $trxProfiler->setSilenced( true );
91 $dbw = $this->getWriteConnection();
92 $dbw->startAtomic( __METHOD__
);
94 $dbw->delete( 'l10n_cache', [ 'lc_lang' => $this->currentLang
], __METHOD__
);
95 foreach ( array_chunk( $this->batch
, 500 ) as $rows ) {
96 $dbw->insert( 'l10n_cache', $rows, __METHOD__
);
98 $this->writesDone
= true;
99 } catch ( DBQueryError
$e ) {
100 if ( $dbw->wasReadOnlyError() ) {
101 $this->readOnly
= true; // just avoid site down time
106 $dbw->endAtomic( __METHOD__
);
108 $trxProfiler->setSilenced( $oldSilenced );
111 $this->currentLang
= null;
115 public function set( $key, $value ) {
116 if ( $this->readOnly
) {
118 } elseif ( is_null( $this->currentLang
) ) {
119 throw new MWException( __CLASS__
. ': must call startWrite() before set()' );
122 $dbw = $this->getWriteConnection();
125 'lc_lang' => $this->currentLang
,
127 'lc_value' => $dbw->encodeBlob( serialize( $value ) )
134 private function getWriteConnection() {
136 if ( $this->server
) {
137 $this->dbw
= Database
::factory( $this->server
['type'], $this->server
);
139 throw new MWException( __CLASS__
. ': failed to obtain a DB connection' );
142 $this->dbw
= wfGetDB( DB_MASTER
);