| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\MediaWikiServices; |
| 22 |
use Wikimedia\Rdbms\DBQueryError; |
| 23 |
use Wikimedia\Rdbms\IDatabase; |
| 24 |
use Wikimedia\ScopedCallback; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class LCStoreDB implements LCStore { |
| 32 |
|
| 33 |
private $code; |
| 34 |
|
| 35 |
private $server; |
| 36 |
|
| 37 |
|
| 38 |
private $batch = []; |
| 39 |
|
| 40 |
|
| 41 |
private $dbw; |
| 42 |
|
| 43 |
private $writesDone = false; |
| 44 |
|
| 45 |
private $readOnly = false; |
| 46 |
|
| 47 |
public function __construct( $params ) { |
| 48 |
$this->server = $params['server'] ?? []; |
| 49 |
} |
| 50 |
|
| 51 |
public function get( $code, $key ) { |
| 52 |
if ( $this->server || $this->writesDone ) { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
$db = $this->getWriteConnection(); |
| 57 |
} else { |
| 58 |
$db = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 59 |
} |
| 60 |
|
| 61 |
$value = $db->newSelectQueryBuilder() |
| 62 |
->select( 'lc_value' ) |
| 63 |
->from( 'l10n_cache' ) |
| 64 |
->where( [ 'lc_lang' => $code, 'lc_key' => $key ] ) |
| 65 |
->caller( __METHOD__ )->fetchField(); |
| 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 InvalidArgumentException( __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 LogicException( __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->newDeleteQueryBuilder() |
| 96 |
->deleteFrom( 'l10n_cache' ) |
| 97 |
->where( [ 'lc_lang' => $this->code ] ) |
| 98 |
->caller( __METHOD__ )->execute(); |
| 99 |
foreach ( array_chunk( $this->batch, 500 ) as $rows ) { |
| 100 |
$dbw->newInsertQueryBuilder() |
| 101 |
->insertInto( 'l10n_cache' ) |
| 102 |
->rows( $rows ) |
| 103 |
->caller( __METHOD__ )->execute(); |
| 104 |
} |
| 105 |
$this->writesDone = true; |
| 106 |
} catch ( DBQueryError $e ) { |
| 107 |
if ( $dbw->isReadOnly() ) { |
| 108 |
$this->readOnly = true; |
| 109 |
} else { |
| 110 |
throw $e; |
| 111 |
} |
| 112 |
} |
| 113 |
$dbw->endAtomic( __METHOD__ ); |
| 114 |
ScopedCallback::consume( $scope ); |
| 115 |
|
| 116 |
$this->code = null; |
| 117 |
$this->batch = []; |
| 118 |
} |
| 119 |
|
| 120 |
public function set( $key, $value ) { |
| 121 |
if ( $this->readOnly ) { |
| 122 |
return; |
| 123 |
} elseif ( $this->code === null ) { |
| 124 |
throw new LogicException( __CLASS__ . ': must call startWrite() before set()' ); |
| 125 |
} |
| 126 |
|
| 127 |
$dbw = $this->getWriteConnection(); |
| 128 |
|
| 129 |
$this->batch[] = [ |
| 130 |
'lc_lang' => $this->code, |
| 131 |
'lc_key' => $key, |
| 132 |
'lc_value' => $dbw->encodeBlob( serialize( $value ) ) |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
private function getWriteConnection() { |
| 140 |
if ( !$this->dbw ) { |
| 141 |
if ( $this->server ) { |
| 142 |
$dbFactory = MediaWikiServices::getInstance()->getDatabaseFactory(); |
| 143 |
$this->dbw = $dbFactory->create( $this->server['type'], $this->server ); |
| 144 |
if ( !$this->dbw ) { |
| 145 |
throw new RuntimeException( __CLASS__ . ': failed to obtain a DB connection' ); |
| 146 |
} |
| 147 |
} else { |
| 148 |
$this->dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $this->dbw; |
| 153 |
} |
| 154 |
} |