MediaWiki  1.34.0
LCStoreDB.php
Go to the documentation of this file.
1 <?php
24 
29 class 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 ( is_null( $this->code ) ) {
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 ( is_null( $this->code ) ) {
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 }
LCStoreDB\$writesDone
bool $writesDone
Whether a batch of writes were recently written.
Definition: LCStoreDB.php:41
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:49
LCStoreDB\$dbw
IDatabase null $dbw
Definition: LCStoreDB.php:39
LCStoreDB\getWriteConnection
getWriteConnection()
Definition: LCStoreDB.php:136
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:63
Wikimedia\Rdbms\IDatabase\endAtomic
endAtomic( $fname=__METHOD__)
Ends an atomic section of SQL statements.
LCStore
Interface for the persistence layer of LocalisationCache.
Definition: LCStore.php:38
Wikimedia\Rdbms\IDatabase\isReadOnly
isReadOnly()
serialize
serialize()
Definition: ApiMessageTrait.php:138
LCStoreDB\$code
string $code
Language code.
Definition: LCStoreDB.php:31
Wikimedia\Rdbms\IDatabase\insert
insert( $table, $a, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
LCStoreDB\$batch
array $batch
Rows buffered for insertion.
Definition: LCStoreDB.php:36
MWException
MediaWiki exception.
Definition: MWException.php:26
Wikimedia\Rdbms\IDatabase\encodeBlob
encodeBlob( $b)
Some DBMSs have a special format for inserting into blob fields, they don't allow simple quoted strin...
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Wikimedia\Rdbms\DBQueryError
Definition: DBQueryError.php:27
LCStoreDB
LCStore implementation which uses the standard DB functions to store data.
Definition: LCStoreDB.php:29
LCStoreDB\startWrite
startWrite( $code)
Start a write transaction.
Definition: LCStoreDB.php:69
LCStoreDB\__construct
__construct( $params)
Definition: LCStoreDB.php:45
Wikimedia\Rdbms\IDatabase\wasReadOnlyError
wasReadOnlyError()
Determines if the last failure was due to the database being read-only.
LCStoreDB\$server
array $server
Server configuration map.
Definition: LCStoreDB.php:33
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
LCStoreDB\finishWrite
finishWrite()
Finish a write transaction.
Definition: LCStoreDB.php:83
LCStoreDB\$readOnly
bool $readOnly
Whether the DB is read-only or otherwise unavailable for writes.
Definition: LCStoreDB.php:43
Wikimedia\Rdbms\IDatabase\delete
delete( $table, $conds, $fname=__METHOD__)
DELETE query wrapper.
Wikimedia\Rdbms\IDatabase\startAtomic
startAtomic( $fname=__METHOD__, $cancelable=self::ATOMIC_NOT_CANCELABLE)
Begin an atomic section of SQL statements.