Update docs/hooks.txt for ShowSearchHitTitle
[mediawiki.git] / includes / cache / localisation / LCStoreDB.php
blobe7e2d10308f8d94433acb0fec0634b833237786d
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.
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.
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
18 * @file
21 /**
22 * LCStore implementation which uses the standard DB functions to store data.
23 * This will work on any MediaWiki installation.
25 class LCStoreDB implements LCStore {
27 /** @var string */
28 private $currentLang;
29 /** @var bool */
30 private $writesDone = false;
31 /** @var IDatabase */
32 private $dbw;
33 /** @var array */
34 private $batch = [];
35 /** @var bool */
36 private $readOnly = false;
38 public function get( $code, $key ) {
39 if ( $this->writesDone && $this->dbw ) {
40 $db = $this->dbw; // see the changes in finishWrite()
41 } else {
42 $db = wfGetDB( DB_REPLICA );
45 $value = $db->selectField(
46 'l10n_cache',
47 'lc_value',
48 [ 'lc_lang' => $code, 'lc_key' => $key ],
49 __METHOD__
52 return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null;
55 public function startWrite( $code ) {
56 if ( $this->readOnly ) {
57 return;
58 } elseif ( !$code ) {
59 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
62 $this->dbw = wfGetDB( DB_MASTER );
63 $this->readOnly = $this->dbw->isReadOnly();
65 $this->currentLang = $code;
66 $this->batch = [];
69 public function finishWrite() {
70 if ( $this->readOnly ) {
71 return;
72 } elseif ( is_null( $this->currentLang ) ) {
73 throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
76 $this->dbw->startAtomic( __METHOD__ );
77 try {
78 $this->dbw->delete(
79 'l10n_cache',
80 [ 'lc_lang' => $this->currentLang ],
81 __METHOD__
83 foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
84 $this->dbw->insert( 'l10n_cache', $rows, __METHOD__ );
86 $this->writesDone = true;
87 } catch ( DBQueryError $e ) {
88 if ( $this->dbw->wasReadOnlyError() ) {
89 $this->readOnly = true; // just avoid site down time
90 } else {
91 throw $e;
94 $this->dbw->endAtomic( __METHOD__ );
96 $this->currentLang = null;
97 $this->batch = [];
100 public function set( $key, $value ) {
101 if ( $this->readOnly ) {
102 return;
103 } elseif ( is_null( $this->currentLang ) ) {
104 throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
107 $this->batch[] = [
108 'lc_lang' => $this->currentLang,
109 'lc_key' => $key,
110 'lc_value' => $this->dbw->encodeBlob( serialize( $value ) )