MediaWiki REL1_35
LCStoreDB.php
Go to the documentation of this file.
1<?php
24
29class LCStoreDB implements LCStore {
31 private $code;
33 private $server;
34
36 private $batch = [];
37
39 private $dbw;
41 private $writesDone = false;
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 ( $this->code === null ) {
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 ( $this->code === null ) {
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
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}
serialize()
unserialize( $serialized)
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
LCStore implementation which uses the standard DB functions to store data.
Definition LCStoreDB.php:29
array $server
Server configuration map.
Definition LCStoreDB.php:33
bool $readOnly
Whether the DB is read-only or otherwise unavailable for writes.
Definition LCStoreDB.php:43
finishWrite()
Finish a write transaction.
Definition LCStoreDB.php:83
startWrite( $code)
Start a write transaction.
Definition LCStoreDB.php:69
IDatabase null $dbw
Definition LCStoreDB.php:39
string $code
Language code.
Definition LCStoreDB.php:31
array $batch
Rows buffered for insertion.
Definition LCStoreDB.php:36
getWriteConnection()
__construct( $params)
Definition LCStoreDB.php:45
bool $writesDone
Whether a batch of writes were recently written.
Definition LCStoreDB.php:41
MediaWiki exception.
@newable Stable to extend
Relational database abstraction object.
Definition Database.php:50
Interface for the persistence layer of LocalisationCache.
Definition LCStore.php:38
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
endAtomic( $fname=__METHOD__)
Ends an atomic section of SQL statements.
delete( $table, $conds, $fname=__METHOD__)
Delete all rows in a table that match a condition.
encodeBlob( $b)
Some DBMSs have a special format for inserting into blob fields, they don't allow simple quoted strin...
insert( $table, $rows, $fname=__METHOD__, $options=[])
Insert the given row(s) into a table.
wasReadOnlyError()
Determines if the last failure was due to the database being read-only.
startAtomic( $fname=__METHOD__, $cancelable=self::ATOMIC_NOT_CANCELABLE)
Begin an atomic section of SQL statements.
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29