Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| LCStoreDB | |
0.00% |
0 / 62 |
|
0.00% |
0 / 7 |
506 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| startWrite | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| finishWrite | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
42 | |||
| set | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getWriteConnection | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| lateFallback | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Language; |
| 8 | |
| 9 | use InvalidArgumentException; |
| 10 | use LogicException; |
| 11 | use MediaWiki\MediaWikiServices; |
| 12 | use MediaWiki\Profiler\Profiler; |
| 13 | use RuntimeException; |
| 14 | use Wikimedia\Rdbms\DBQueryError; |
| 15 | use Wikimedia\Rdbms\IDatabase; |
| 16 | use Wikimedia\ScopedCallback; |
| 17 | |
| 18 | /** |
| 19 | * LCStore implementation which uses the standard DB functions to store data. |
| 20 | * |
| 21 | * @ingroup Language |
| 22 | */ |
| 23 | class LCStoreDB implements LCStore { |
| 24 | /** @var string|null Language code */ |
| 25 | private $code; |
| 26 | /** @var array Server configuration map */ |
| 27 | private $server; |
| 28 | |
| 29 | /** @var array Rows buffered for insertion */ |
| 30 | private $batch = []; |
| 31 | |
| 32 | /** @var IDatabase|null */ |
| 33 | private $dbw; |
| 34 | /** @var bool Whether write batch was recently written */ |
| 35 | private $writesDone = false; |
| 36 | /** @var bool Whether the DB is read-only or otherwise unavailable for writing */ |
| 37 | private $readOnly = false; |
| 38 | |
| 39 | public function __construct( array $params ) { |
| 40 | $this->server = $params['server'] ?? []; |
| 41 | } |
| 42 | |
| 43 | /** @inheritDoc */ |
| 44 | public function get( $code, $key ) { |
| 45 | if ( $this->server || $this->writesDone ) { |
| 46 | // If a server configuration map is specified, always used that connection |
| 47 | // for reads and writes. Otherwise, if writes occurred in finishWrite(), make |
| 48 | // sure those changes are always visible. |
| 49 | $db = $this->getWriteConnection(); |
| 50 | } else { |
| 51 | $db = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 52 | } |
| 53 | |
| 54 | $value = $db->newSelectQueryBuilder() |
| 55 | ->select( 'lc_value' ) |
| 56 | ->from( 'l10n_cache' ) |
| 57 | ->where( [ 'lc_lang' => $code, 'lc_key' => $key ] ) |
| 58 | ->caller( __METHOD__ )->fetchField(); |
| 59 | |
| 60 | return ( $value !== false ) ? unserialize( $db->decodeBlob( $value ) ) : null; |
| 61 | } |
| 62 | |
| 63 | /** @inheritDoc */ |
| 64 | public function startWrite( $code ) { |
| 65 | if ( $this->readOnly ) { |
| 66 | return; |
| 67 | } elseif ( !$code ) { |
| 68 | throw new InvalidArgumentException( __METHOD__ . ": Invalid language \"$code\"" ); |
| 69 | } |
| 70 | |
| 71 | $dbw = $this->getWriteConnection(); |
| 72 | $this->readOnly = $dbw->isReadOnly(); |
| 73 | |
| 74 | $this->code = $code; |
| 75 | $this->batch = []; |
| 76 | } |
| 77 | |
| 78 | public function finishWrite() { |
| 79 | if ( $this->readOnly ) { |
| 80 | return; |
| 81 | } elseif ( $this->code === null ) { |
| 82 | throw new LogicException( __CLASS__ . ': must call startWrite() before finishWrite()' ); |
| 83 | } |
| 84 | |
| 85 | $scope = Profiler::instance()->getTransactionProfiler()->silenceForScope(); |
| 86 | $dbw = $this->getWriteConnection(); |
| 87 | $dbw->startAtomic( __METHOD__ ); |
| 88 | try { |
| 89 | $dbw->newDeleteQueryBuilder() |
| 90 | ->deleteFrom( 'l10n_cache' ) |
| 91 | ->where( [ 'lc_lang' => $this->code ] ) |
| 92 | ->caller( __METHOD__ )->execute(); |
| 93 | foreach ( array_chunk( $this->batch, 500 ) as $rows ) { |
| 94 | $dbw->newInsertQueryBuilder() |
| 95 | ->insertInto( 'l10n_cache' ) |
| 96 | ->rows( $rows ) |
| 97 | ->caller( __METHOD__ )->execute(); |
| 98 | } |
| 99 | $this->writesDone = true; |
| 100 | } catch ( DBQueryError $e ) { |
| 101 | if ( $dbw->isReadOnly() ) { |
| 102 | $this->readOnly = true; // just avoid site downtime |
| 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 | /** @inheritDoc */ |
| 115 | public function set( $key, $value ) { |
| 116 | if ( $this->readOnly ) { |
| 117 | return; |
| 118 | } elseif ( $this->code === null ) { |
| 119 | throw new LogicException( __CLASS__ . ': must call startWrite() before set()' ); |
| 120 | } |
| 121 | |
| 122 | $dbw = $this->getWriteConnection(); |
| 123 | |
| 124 | $this->batch[] = [ |
| 125 | 'lc_lang' => $this->code, |
| 126 | 'lc_key' => $key, |
| 127 | 'lc_value' => $dbw->encodeBlob( serialize( $value ) ) |
| 128 | ]; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return IDatabase |
| 133 | */ |
| 134 | private function getWriteConnection() { |
| 135 | if ( !$this->dbw ) { |
| 136 | if ( $this->server ) { |
| 137 | $dbFactory = MediaWikiServices::getInstance()->getDatabaseFactory(); |
| 138 | $this->dbw = $dbFactory->create( $this->server['type'], $this->server ); |
| 139 | if ( !$this->dbw ) { |
| 140 | throw new RuntimeException( __CLASS__ . ': failed to obtain a DB connection' ); |
| 141 | } |
| 142 | } else { |
| 143 | $this->dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return $this->dbw; |
| 148 | } |
| 149 | |
| 150 | /** @inheritDoc */ |
| 151 | public function lateFallback(): bool { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | |
| 157 | /** @deprecated class alias since 1.46 */ |
| 158 | class_alias( LCStoreDB::class, 'LCStoreDB' ); |