MediaWiki  master
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 }
array $batch
Rows buffered for insertion.
Definition: LCStoreDB.php:36
serialize()
getWriteConnection()
Definition: LCStoreDB.php:136
static instance()
Singleton.
Definition: Profiler.php:63
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
IDatabase null $dbw
Definition: LCStoreDB.php:39
endAtomic( $fname=__METHOD__)
Ends an atomic section of SQL statements.
encodeBlob( $b)
Some DBMSs have a special format for inserting into blob fields, they don&#39;t allow simple quoted strin...
const DB_MASTER
Definition: defines.php:26
string $code
Language code.
Definition: LCStoreDB.php:31
finishWrite()
Finish a write transaction.
Definition: LCStoreDB.php:83
wasReadOnlyError()
Determines if the last failure was due to the database being read-only.
unserialize( $serialized)
__construct( $params)
Definition: LCStoreDB.php:45
bool $writesDone
Whether a batch of writes were recently written.
Definition: LCStoreDB.php:41
Interface for the persistence layer of LocalisationCache.
Definition: LCStore.php:38
bool $readOnly
Whether the DB is read-only or otherwise unavailable for writes.
Definition: LCStoreDB.php:43
startAtomic( $fname=__METHOD__, $cancelable=self::ATOMIC_NOT_CANCELABLE)
Begin an atomic section of SQL statements.
insert( $table, $rows, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
array $server
Server configuration map.
Definition: LCStoreDB.php:33
const DB_REPLICA
Definition: defines.php:25
LCStore implementation which uses the standard DB functions to store data.
Definition: LCStoreDB.php:29
delete( $table, $conds, $fname=__METHOD__)
DELETE query wrapper.
startWrite( $code)
Start a write transaction.
Definition: LCStoreDB.php:69