Display error if Installer->execute() doesn't return a good status object
[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 $trxProfiler = Profiler::instance()->getTransactionProfiler();
89 $oldSilenced = $trxProfiler->setSilenced( true );
90 try {
91 $dbw = $this->getWriteConnection();
92 $dbw->startAtomic( __METHOD__ );
93 try {
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__ );
97 }
98 $this->writesDone = true;
99 } catch ( DBQueryError $e ) {
100 if ( $dbw->wasReadOnlyError() ) {
101 $this->readOnly = true; // just avoid site down time
102 } else {
103 throw $e;
104 }
105 }
106 $dbw->endAtomic( __METHOD__ );
107 } finally {
108 $trxProfiler->setSilenced( $oldSilenced );
109 }
110
111 $this->currentLang = null;
112 $this->batch = [];
113 }
114
115 public function set( $key, $value ) {
116 if ( $this->readOnly ) {
117 return;
118 } elseif ( is_null( $this->currentLang ) ) {
119 throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
120 }
121
122 $dbw = $this->getWriteConnection();
123
124 $this->batch[] = [
125 'lc_lang' => $this->currentLang,
126 'lc_key' => $key,
127 'lc_value' => $dbw->encodeBlob( serialize( $value ) )
128 ];
129 }
130
131 /**
132 * @return IDatabase
133 */
134 private function getWriteConnection() {
135 if ( !$this->dbw ) {
136 if ( $this->server ) {
137 $this->dbw = Database::factory( $this->server['type'], $this->server );
138 if ( !$this->dbw ) {
139 throw new MWException( __CLASS__ . ': failed to obtain a DB connection' );
140 }
141 } else {
142 $this->dbw = wfGetDB( DB_MASTER );
143 }
144 }
145
146 return $this->dbw;
147 }
148 }