19 private ?array $index =
null;
20 private ILoadBalancer $loadBalancer;
22 public function __construct() {
23 $this->loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
26 public function retrieve(
bool $readLatest =
false ): array {
27 if ( $this->index !==
null && !$readLatest ) {
31 $dbr = $this->loadBalancer->getConnection( $readLatest ? DB_PRIMARY : DB_REPLICA );
32 $res = $dbr->newSelectQueryBuilder()
34 ->from(
'translate_messageindex' )
35 ->caller( __METHOD__ )
38 foreach ( $res as $row ) {
39 $this->index[$row->tmi_key] = $this->unserialize( $row->tmi_value );
46 public function get(
string $key ) {
47 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
48 $value = $dbr->newSelectQueryBuilder()
49 ->select(
'tmi_value' )
50 ->from(
'translate_messageindex' )
51 ->where( [
'tmi_key' => $key ] )
52 ->caller( __METHOD__ )
55 return is_string( $value ) ? $this->unserialize( $value ) :
null;
58 public function store( array $array, array $diff ): void {
61 foreach ( [ $diff[
'add'], $diff[
'mod'] ] as $changes ) {
62 foreach ( $changes as $key => $data ) {
71 $index = [
'tmi_key' ];
72 $deletions = array_keys( $diff[
'del'] );
74 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
75 $dbw->startAtomic( __METHOD__ );
77 if ( $updates !== [] ) {
78 $dbw->replace(
'translate_messageindex', [ $index ], $updates, __METHOD__ );
81 if ( $deletions !== [] ) {
82 $dbw->delete(
'translate_messageindex', [
'tmi_key' => $deletions ], __METHOD__ );
85 $dbw->endAtomic( __METHOD__ );
87 $this->index = $array;