MediaWiki REL1_39
LCStoreDB.php
Go to the documentation of this file.
1<?php
24use Wikimedia\ScopedCallback;
25
31class LCStoreDB implements LCStore {
33 private $code;
35 private $server;
36
38 private $batch = [];
39
41 private $dbw;
43 private $writesDone = false;
45 private $readOnly = false;
46
47 public function __construct( $params ) {
48 $this->server = $params['server'] ?? [];
49 }
50
51 public function get( $code, $key ) {
52 if ( $this->server || $this->writesDone ) {
53 // If a server configuration map is specified, always used that connection
54 // for reads and writes. Otherwise, if writes occurred in finishWrite(), make
55 // sure those changes are always visible.
56 $db = $this->getWriteConnection();
57 } else {
58 $db = wfGetDB( DB_REPLICA );
59 }
60
61 $value = $db->selectField(
62 'l10n_cache',
63 'lc_value',
64 [ 'lc_lang' => $code, 'lc_key' => $key ],
65 __METHOD__
66 );
67
68 return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null;
69 }
70
71 public function startWrite( $code ) {
72 if ( $this->readOnly ) {
73 return;
74 } elseif ( !$code ) {
75 throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
76 }
77
78 $dbw = $this->getWriteConnection();
79 $this->readOnly = $dbw->isReadOnly();
80
81 $this->code = $code;
82 $this->batch = [];
83 }
84
85 public function finishWrite() {
86 if ( $this->readOnly ) {
87 return;
88 } elseif ( $this->code === null ) {
89 throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
90 }
91
92 $scope = Profiler::instance()->getTransactionProfiler()->silenceForScope();
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 ScopedCallback::consume( $scope );
110
111 $this->code = null;
112 $this->batch = [];
113 }
114
115 public function set( $key, $value ) {
116 if ( $this->readOnly ) {
117 return;
118 } elseif ( $this->code === null ) {
119 throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
120 }
121
122 $dbw = $this->getWriteConnection();
123
124 $this->batch[] = [
125 'lc_lang' => $this->code,
126 'lc_key' => $key,
127 'lc_value' => $dbw->encodeBlob( serialize( $value ) )
128 ];
129 }
130
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_PRIMARY );
143 }
144 }
145
146 return $this->dbw;
147 }
148}
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:31
finishWrite()
Finish a write transaction.
Definition LCStoreDB.php:85
startWrite( $code)
Start a write transaction.
Definition LCStoreDB.php:71
__construct( $params)
Definition LCStoreDB.php:47
MediaWiki exception.
Interface for the persistence layer of LocalisationCache.
Definition LCStore.php:40
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
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 row(s) into a table, in the provided order.
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:26
const DB_PRIMARY
Definition defines.php:28