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