Merge "DefaultPreferencesFactory: Remove fallback for null PermissionManager"
[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 Language code */
31 private $code;
32 /** @var array Server configuration map */
33 private $server;
34
35 /** @var array Rows buffered for insertion */
36 private $batch = [];
37
38 /** @var IDatabase|null */
39 private $dbw;
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;
44
45 public function __construct( $params ) {
46 $this->server = $params['server'] ?? [];
47 }
48
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();
55 } else {
56 $db = wfGetDB( DB_REPLICA );
57 }
58
59 $value = $db->selectField(
60 'l10n_cache',
61 'lc_value',
62 [ 'lc_lang' => $code, 'lc_key' => $key ],
63 __METHOD__
64 );
65
66 return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null;
67 }
68
69 public function startWrite( $code ) {
70 if ( $this->readOnly ) {
71 return;
72 } elseif ( !$code ) {
73 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
74 }
75
76 $dbw = $this->getWriteConnection();
77 $this->readOnly = $dbw->isReadOnly();
78
79 $this->code = $code;
80 $this->batch = [];
81 }
82
83 public function finishWrite() {
84 if ( $this->readOnly ) {
85 return;
86 } elseif ( is_null( $this->code ) ) {
87 throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
88 }
89
90 $trxProfiler = Profiler::instance()->getTransactionProfiler();
91 $oldSilenced = $trxProfiler->setSilenced( true );
92 try {
93 $dbw = $this->getWriteConnection();
94 $dbw->startAtomic( __METHOD__ );
95 try {
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__ );
99 }
100 $this->writesDone = true;
101 } catch ( DBQueryError $e ) {
102 if ( $dbw->wasReadOnlyError() ) {
103 $this->readOnly = true; // just avoid site down time
104 } else {
105 throw $e;
106 }
107 }
108 $dbw->endAtomic( __METHOD__ );
109 } finally {
110 $trxProfiler->setSilenced( $oldSilenced );
111 }
112
113 $this->code = null;
114 $this->batch = [];
115 }
116
117 public function set( $key, $value ) {
118 if ( $this->readOnly ) {
119 return;
120 } elseif ( is_null( $this->code ) ) {
121 throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
122 }
123
124 $dbw = $this->getWriteConnection();
125
126 $this->batch[] = [
127 'lc_lang' => $this->code,
128 'lc_key' => $key,
129 'lc_value' => $dbw->encodeBlob( serialize( $value ) )
130 ];
131 }
132
133 /**
134 * @return IDatabase
135 */
136 private function getWriteConnection() {
137 if ( !$this->dbw ) {
138 if ( $this->server ) {
139 $this->dbw = Database::factory( $this->server['type'], $this->server );
140 if ( !$this->dbw ) {
141 throw new MWException( __CLASS__ . ': failed to obtain a DB connection' );
142 }
143 } else {
144 $this->dbw = wfGetDB( DB_MASTER );
145 }
146 }
147
148 return $this->dbw;
149 }
150 }