* @file
*/
+use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\DBQueryError;
* This will work on any MediaWiki installation.
*/
class LCStoreDB implements LCStore {
-
/** @var string */
private $currentLang;
/** @var bool */
private $writesDone = false;
- /** @var IDatabase */
+ /** @var IDatabase|null */
private $dbw;
/** @var array */
private $batch = [];
/** @var bool */
private $readOnly = false;
+ /** @var array Server configuration map */
+ private $server;
+
+ public function __construct( $params ) {
+ $this->server = $params['server'] ?? [];
+ }
public function get( $code, $key ) {
- if ( $this->writesDone && $this->dbw ) {
- $db = $this->dbw; // see the changes in finishWrite()
+ if ( $this->server || $this->writesDone ) {
+ // If a server configuration map is specified, always used that connection
+ // for reads and writes. Otherwise, if writes occurred in finishWrite(), make
+ // sure those changes are always visible.
+ $db = $this->getWriteConnection();
} else {
$db = wfGetDB( DB_REPLICA );
}
throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
}
- $this->dbw = wfGetDB( DB_MASTER );
- $this->readOnly = $this->dbw->isReadOnly();
+ $dbw = $this->getWriteConnection();
+ $this->readOnly = $dbw->isReadOnly();
$this->currentLang = $code;
$this->batch = [];
throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
}
- $this->dbw->startAtomic( __METHOD__ );
+ $trxProfiler = Profiler::instance()->getTransactionProfiler();
+ $oldSilenced = $trxProfiler->setSilenced( true );
try {
- $this->dbw->delete(
- 'l10n_cache',
- [ 'lc_lang' => $this->currentLang ],
- __METHOD__
- );
- foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
- $this->dbw->insert( 'l10n_cache', $rows, __METHOD__ );
- }
- $this->writesDone = true;
- } catch ( DBQueryError $e ) {
- if ( $this->dbw->wasReadOnlyError() ) {
- $this->readOnly = true; // just avoid site down time
- } else {
- throw $e;
+ $dbw = $this->getWriteConnection();
+ $dbw->startAtomic( __METHOD__ );
+ try {
+ $dbw->delete( 'l10n_cache', [ 'lc_lang' => $this->currentLang ], __METHOD__ );
+ foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
+ $dbw->insert( 'l10n_cache', $rows, __METHOD__ );
+ }
+ $this->writesDone = true;
+ } catch ( DBQueryError $e ) {
+ if ( $dbw->wasReadOnlyError() ) {
+ $this->readOnly = true; // just avoid site down time
+ } else {
+ throw $e;
+ }
}
+ $dbw->endAtomic( __METHOD__ );
+ } finally {
+ $trxProfiler->setSilenced( $oldSilenced );
}
- $this->dbw->endAtomic( __METHOD__ );
$this->currentLang = null;
$this->batch = [];
throw new MWException( __CLASS__ . ': must call startWrite() before set()' );
}
+ $dbw = $this->getWriteConnection();
+
$this->batch[] = [
'lc_lang' => $this->currentLang,
'lc_key' => $key,
- 'lc_value' => $this->dbw->encodeBlob( serialize( $value ) )
+ 'lc_value' => $dbw->encodeBlob( serialize( $value ) )
];
}
+ /**
+ * @return IDatabase
+ */
+ private function getWriteConnection() {
+ if ( !$this->dbw ) {
+ if ( $this->server ) {
+ $this->dbw = Database::factory( $this->server['type'], $this->server );
+ if ( !$this->dbw ) {
+ throw new MWException( __CLASS__ . ': failed to obtain a DB connection' );
+ }
+ } else {
+ $this->dbw = wfGetDB( DB_MASTER );
+ }
+ }
+
+ return $this->dbw;
+ }
}