Move IDatabase/IMaintainableDatabase to Rdbms namespace
[lhc/web/wiklou.git] / includes / cache / localisation / LCStoreDB.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 use Wikimedia\Rdbms\IDatabase;
22
23 /**
24 * LCStore implementation which uses the standard DB functions to store data.
25 * This will work on any MediaWiki installation.
26 */
27 class LCStoreDB implements LCStore {
28
29 /** @var string */
30 private $currentLang;
31 /** @var bool */
32 private $writesDone = false;
33 /** @var IDatabase */
34 private $dbw;
35 /** @var array */
36 private $batch = [];
37 /** @var bool */
38 private $readOnly = false;
39
40 public function get( $code, $key ) {
41 if ( $this->writesDone && $this->dbw ) {
42 $db = $this->dbw; // see the changes in finishWrite()
43 } else {
44 $db = wfGetDB( DB_REPLICA );
45 }
46
47 $value = $db->selectField(
48 'l10n_cache',
49 'lc_value',
50 [ 'lc_lang' => $code, 'lc_key' => $key ],
51 __METHOD__
52 );
53
54 return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null;
55 }
56
57 public function startWrite( $code ) {
58 if ( $this->readOnly ) {
59 return;
60 } elseif ( !$code ) {
61 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
62 }
63
64 $this->dbw = wfGetDB( DB_MASTER );
65 $this->readOnly = $this->dbw->isReadOnly();
66
67 $this->currentLang = $code;
68 $this->batch = [];
69 }
70
71 public function finishWrite() {
72 if ( $this->readOnly ) {
73 return;
74 } elseif ( is_null( $this->currentLang ) ) {
75 throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
76 }
77
78 $this->dbw->startAtomic( __METHOD__ );
79 try {
80 $this->dbw->delete(
81 'l10n_cache',
82 [ 'lc_lang' => $this->currentLang ],
83 __METHOD__
84 );
85 foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
86 $this->dbw->insert( 'l10n_cache', $rows, __METHOD__ );
87 }
88 $this->writesDone = true;
89 } catch ( DBQueryError $e ) {
90 if ( $this->dbw->wasReadOnlyError() ) {
91 $this->readOnly = true; // just avoid site down time
92 } else {
93 throw $e;
94 }
95 }
96 $this->dbw->endAtomic( __METHOD__ );
97
98 $this->currentLang = null;
99 $this->batch = [];
100 }
101
102 public function set( $key, $value ) {
103 if ( $this->readOnly ) {
104 return;
105 } elseif ( is_null( $this->currentLang ) ) {
106 throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
107 }
108
109 $this->batch[] = [
110 'lc_lang' => $this->currentLang,
111 'lc_key' => $key,
112 'lc_value' => $this->dbw->encodeBlob( serialize( $value ) )
113 ];
114 }
115
116 }