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 }
LCStoreDB\$writesDone
bool $writesDone
Whether a batch of writes were recently written.
Definition: LCStoreDB.php:42
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:52
LCStoreDB\$dbw
IDatabase null $dbw
Definition: LCStoreDB.php:40
LCStoreDB\getWriteConnection
getWriteConnection()
Definition: LCStoreDB.php:133
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:69
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:32
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:37
MWException
MediaWiki exception.
Definition: MWException.php:29
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:2200
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Wikimedia\Rdbms\DBQueryError
@newable
Definition: DBQueryError.php:29
LCStoreDB
LCStore implementation which uses the standard DB functions to store data.
Definition: LCStoreDB.php:30
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
LCStoreDB\startWrite
startWrite( $code)
Start a write transaction.
Definition: LCStoreDB.php:70
LCStoreDB\__construct
__construct( $params)
Definition: LCStoreDB.php:46
Wikimedia\Rdbms\IDatabase\wasReadOnlyError
wasReadOnlyError()
Determines if the last failure was due to the database being read-only.
Wikimedia\Rdbms\IDatabase\insert
insert( $table, $rows, $fname=__METHOD__, $options=[])
Insert the given row(s) into a table.
LCStoreDB\$server
array $server
Server configuration map.
Definition: LCStoreDB.php:34
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
LCStoreDB\finishWrite
finishWrite()
Finish a write transaction.
Definition: LCStoreDB.php:84
LCStoreDB\$readOnly
bool $readOnly
Whether the DB is read-only or otherwise unavailable for writes.
Definition: LCStoreDB.php:44
Wikimedia\Rdbms\IDatabase\delete
delete( $table, $conds, $fname=__METHOD__)
Delete all rows in a table that match a condition.
Wikimedia\Rdbms\IDatabase\startAtomic
startAtomic( $fname=__METHOD__, $cancelable=self::ATOMIC_NOT_CANCELABLE)
Begin an atomic section of SQL statements.