Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
DatabaseMessageIndex.php
1<?php
2declare ( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageLoading;
5
6use MediaWiki\MediaWikiServices;
7use Wikimedia\Rdbms\IConnectionProvider;
8
19 private ?array $index = null;
20 private IConnectionProvider $dbProvider;
21
22 public function __construct() {
23 $this->dbProvider = MediaWikiServices::getInstance()->getConnectionProvider();
24 }
25
26 public function retrieve( bool $readLatest = false ): array {
27 if ( $this->index !== null && !$readLatest ) {
28 return $this->index;
29 }
30
31 $dbr = $readLatest ? $this->dbProvider->getPrimaryDatabase() :
32 $this->dbProvider->getReplicaDatabase();
33 $res = $dbr->newSelectQueryBuilder()
34 ->select( '*' )
35 ->from( 'translate_messageindex' )
36 ->caller( __METHOD__ )
37 ->fetchResultSet();
38 $this->index = [];
39 foreach ( $res as $row ) {
40 $this->index[$row->tmi_key] = $this->unserialize( $row->tmi_value );
41 }
42
43 return $this->index;
44 }
45
47 public function get( string $key ) {
48 $dbr = $this->dbProvider->getReplicaDatabase();
49 $value = $dbr->newSelectQueryBuilder()
50 ->select( 'tmi_value' )
51 ->from( 'translate_messageindex' )
52 ->where( [ 'tmi_key' => $key ] )
53 ->caller( __METHOD__ )
54 ->fetchField();
55
56 return is_string( $value ) ? $this->unserialize( $value ) : null;
57 }
58
59 public function store( array $array, array $diff ): void {
60 $updates = [];
61
62 foreach ( [ $diff['add'], $diff['mod'] ] as $changes ) {
63 foreach ( $changes as $key => $data ) {
64 [ , $new ] = $data;
65 $updates[] = [
66 'tmi_key' => $key,
67 'tmi_value' => $this->serialize( $new ),
68 ];
69 }
70 }
71
72 $deletions = array_keys( $diff['del'] );
73
74 $dbw = $this->dbProvider->getPrimaryDatabase();
75 $dbw->startAtomic( __METHOD__ );
76
77 if ( $updates !== [] ) {
78 $dbw->newReplaceQueryBuilder()
79 ->replaceInto( 'translate_messageindex' )
80 ->uniqueIndexFields( [ 'tmi_key' ] )
81 ->rows( $updates )
82 ->caller( __METHOD__ )
83 ->execute();
84 }
85
86 if ( $deletions !== [] ) {
87 $dbw->newDeleteQueryBuilder()
88 ->deleteFrom( 'translate_messageindex' )
89 ->where( [ 'tmi_key' => $deletions ] )
90 ->caller( __METHOD__ )
91 ->execute();
92 }
93
94 $dbw->endAtomic( __METHOD__ );
95
96 $this->index = $array;
97 }
98}
serialize( $data)
These are probably slower than serialize and unserialize, but they are more space efficient because w...