Merge "ContribsPager: Factor revision check out of formatRow"
[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\Database;
22 use Wikimedia\Rdbms\IDatabase;
23 use Wikimedia\Rdbms\DBQueryError;
24
25 /**
26 * LCStore implementation which uses the standard DB functions to store data.
27 * This will work on any MediaWiki installation.
28 */
29 class LCStoreDB implements LCStore {
30 /** @var string */
31 private $currentLang;
32 /** @var bool */
33 private $writesDone = false;
34 /** @var IDatabase|null */
35 private $dbw;
36 /** @var array */
37 private $batch = [];
38 /** @var bool */
39 private $readOnly = false;
40 /** @var array Server configuration map */
41 private $server;
42
43 public function __construct( $params ) {
44 $this->server = $params['server'] ?? [];
45 }
46
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();
53 } else {
54 $db = wfGetDB( DB_REPLICA );
55 }
56
57 $value = $db->selectField(
58 'l10n_cache',
59 'lc_value',
60 [ 'lc_lang' => $code, 'lc_key' => $key ],
61 __METHOD__
62 );
63
64 return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null;
65 }
66
67 public function startWrite( $code ) {
68 if ( $this->readOnly ) {
69 return;
70 } elseif ( !$code ) {
71 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
72 }
73
74 $dbw = $this->getWriteConnection();
75 $this->readOnly = $dbw->isReadOnly();
76
77 $this->currentLang = $code;
78 $this->batch = [];
79 }
80
81 public function finishWrite() {
82 if ( $this->readOnly ) {
83 return;
84 } elseif ( is_null( $this->currentLang ) ) {
85 throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
86 }
87
88 $dbw = $this->getWriteConnection();
89 $dbw->startAtomic( __METHOD__ );
90 try {
91 $dbw->delete( 'l10n_cache', [ 'lc_lang' => $this->currentLang ], __METHOD__ );
92 foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
93 $dbw->insert( 'l10n_cache', $rows, __METHOD__ );
94 }
95 $this->writesDone = true;
96 } catch ( DBQueryError $e ) {
97 if ( $dbw->wasReadOnlyError() ) {
98 $this->readOnly = true; // just avoid site down time
99 } else {
100 throw $e;
101 }
102 }
103 $dbw->endAtomic( __METHOD__ );
104
105 $this->currentLang = null;
106 $this->batch = [];
107 }
108
109 public function set( $key, $value ) {
110 if ( $this->readOnly ) {
111 return;
112 } elseif ( is_null( $this->currentLang ) ) {
113 throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
114 }
115
116 $dbw = $this->getWriteConnection();
117
118 $this->batch[] = [
119 'lc_lang' => $this->currentLang,
120 'lc_key' => $key,
121 'lc_value' => $dbw->encodeBlob( serialize( $value ) )
122 ];
123 }
124
125 /**
126 * @return IDatabase
127 */
128 private function getWriteConnection() {
129 if ( !$this->dbw ) {
130 if ( $this->server ) {
131 $this->dbw = Database::factory( $this->server['type'], $this->server );
132 if ( !$this->dbw ) {
133 throw new MWException( __CLASS__ . ': failed to obtain a DB connection' );
134 }
135 } else {
136 $this->dbw = wfGetDB( DB_MASTER );
137 }
138 }
139
140 return $this->dbw;
141 }
142 }