MediaWiki  master
LCStoreDB.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\ScopedCallback;
25 
30 class LCStoreDB implements LCStore {
32  private $code;
34  private $server;
35 
37  private $batch = [];
38 
40  private $dbw;
42  private $writesDone = false;
44  private $readOnly = false;
45 
46  public function __construct( $params ) {
47  $this->server = $params['server'] ?? [];
48  }
49 
50  public function get( $code, $key ) {
51  if ( $this->server || $this->writesDone ) {
52  // If a server configuration map is specified, always used that connection
53  // for reads and writes. Otherwise, if writes occurred in finishWrite(), make
54  // sure those changes are always visible.
55  $db = $this->getWriteConnection();
56  } else {
57  $db = wfGetDB( DB_REPLICA );
58  }
59 
60  $value = $db->selectField(
61  'l10n_cache',
62  'lc_value',
63  [ 'lc_lang' => $code, 'lc_key' => $key ],
64  __METHOD__
65  );
66 
67  return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null;
68  }
69 
70  public function startWrite( $code ) {
71  if ( $this->readOnly ) {
72  return;
73  } elseif ( !$code ) {
74  throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
75  }
76 
77  $dbw = $this->getWriteConnection();
78  $this->readOnly = $dbw->isReadOnly();
79 
80  $this->code = $code;
81  $this->batch = [];
82  }
83 
84  public function finishWrite() {
85  if ( $this->readOnly ) {
86  return;
87  } elseif ( $this->code === null ) {
88  throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
89  }
90 
91  $scope = Profiler::instance()->getTransactionProfiler()->silenceForScope();
92  $dbw = $this->getWriteConnection();
93  $dbw->startAtomic( __METHOD__ );
94  try {
95  $dbw->delete( 'l10n_cache', [ 'lc_lang' => $this->code ], __METHOD__ );
96  foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
97  $dbw->insert( 'l10n_cache', $rows, __METHOD__ );
98  }
99  $this->writesDone = true;
100  } catch ( DBQueryError $e ) {
101  if ( $dbw->wasReadOnlyError() ) {
102  $this->readOnly = true; // just avoid site down time
103  } else {
104  throw $e;
105  }
106  }
107  $dbw->endAtomic( __METHOD__ );
108  ScopedCallback::consume( $scope );
109 
110  $this->code = null;
111  $this->batch = [];
112  }
113 
114  public function set( $key, $value ) {
115  if ( $this->readOnly ) {
116  return;
117  } elseif ( $this->code === null ) {
118  throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
119  }
120 
121  $dbw = $this->getWriteConnection();
122 
123  $this->batch[] = [
124  'lc_lang' => $this->code,
125  'lc_key' => $key,
126  'lc_value' => $dbw->encodeBlob( serialize( $value ) )
127  ];
128  }
129 
133  private function getWriteConnection() {
134  if ( !$this->dbw ) {
135  if ( $this->server ) {
136  $this->dbw = Database::factory( $this->server['type'], $this->server );
137  if ( !$this->dbw ) {
138  throw new MWException( __CLASS__ . ': failed to obtain a DB connection' );
139  }
140  } else {
141  $this->dbw = wfGetDB( DB_PRIMARY );
142  }
143  }
144 
145  return $this->dbw;
146  }
147 }
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:30
array $server
Server configuration map.
Definition: LCStoreDB.php:34
bool $readOnly
Whether the DB is read-only or otherwise unavailable for writes.
Definition: LCStoreDB.php:44
string null $code
Language code.
Definition: LCStoreDB.php:32
finishWrite()
Finish a write transaction.
Definition: LCStoreDB.php:84
startWrite( $code)
Start a write transaction.
Definition: LCStoreDB.php:70
IDatabase null $dbw
Definition: LCStoreDB.php:40
array $batch
Rows buffered for insertion.
Definition: LCStoreDB.php:37
getWriteConnection()
Definition: LCStoreDB.php:133
__construct( $params)
Definition: LCStoreDB.php:46
bool $writesDone
Whether a batch of writes were recently written.
Definition: LCStoreDB.php:42
MediaWiki exception.
Definition: MWException.php:29
static instance()
Singleton.
Definition: Profiler.php:69
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: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:25
const DB_PRIMARY
Definition: defines.php:27