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\ILoadBalancer;
8
19 private ?array $index = null;
20 private ILoadBalancer $loadBalancer;
21
22 public function __construct() {
23 $this->loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
24 }
25
26 public function retrieve( bool $readLatest = false ): array {
27 if ( $this->index !== null && !$readLatest ) {
28 return $this->index;
29 }
30
31 $dbr = $this->loadBalancer->getConnection( $readLatest ? DB_PRIMARY : DB_REPLICA );
32 $res = $dbr->newSelectQueryBuilder()
33 ->select( '*' )
34 ->from( 'translate_messageindex' )
35 ->caller( __METHOD__ )
36 ->fetchResultSet();
37 $this->index = [];
38 foreach ( $res as $row ) {
39 $this->index[$row->tmi_key] = $this->unserialize( $row->tmi_value );
40 }
41
42 return $this->index;
43 }
44
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__ )
53 ->fetchField();
54
55 return is_string( $value ) ? $this->unserialize( $value ) : null;
56 }
57
58 public function store( array $array, array $diff ): void {
59 $updates = [];
60
61 foreach ( [ $diff['add'], $diff['mod'] ] as $changes ) {
62 foreach ( $changes as $key => $data ) {
63 [ , $new ] = $data;
64 $updates[] = [
65 'tmi_key' => $key,
66 'tmi_value' => $this->serialize( $new ),
67 ];
68 }
69 }
70
71 $index = [ 'tmi_key' ];
72 $deletions = array_keys( $diff['del'] );
73
74 $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
75 $dbw->startAtomic( __METHOD__ );
76
77 if ( $updates !== [] ) {
78 $dbw->replace( 'translate_messageindex', [ $index ], $updates, __METHOD__ );
79 }
80
81 if ( $deletions !== [] ) {
82 $dbw->delete( 'translate_messageindex', [ 'tmi_key' => $deletions ], __METHOD__ );
83 }
84
85 $dbw->endAtomic( __METHOD__ );
86
87 $this->index = $array;
88 }
89}
serialize( $data)
These are probably slower than serialize and unserialize, but they are more space efficient because w...