Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseMessageIndex
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 retrieve
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 get
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 store
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2declare ( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageLoading;
5
6use MediaWiki\MediaWikiServices;
7use Wikimedia\Rdbms\ILoadBalancer;
8
9/**
10 * Storage on the database itself.
11 *
12 * This is likely to be the slowest backend. However, it scales okay
13 * and provides random access. It also doesn't need any special setup,
14 * the database table is added with update.php together with other tables,
15 * which is the reason this is the default backend. It also works well
16 * on multi-server setup without needing for shared file storage.
17 */
18class DatabaseMessageIndex extends MessageIndexStore {
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
45    /** @inheritDoc */
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        $deletions = array_keys( $diff['del'] );
72
73        $dbw = $this->loadBalancer->getConnection( DB_PRIMARY );
74        $dbw->startAtomic( __METHOD__ );
75
76        if ( $updates !== [] ) {
77            $dbw->newReplaceQueryBuilder()
78                ->replaceInto( 'translate_messageindex' )
79                ->uniqueIndexFields( [ 'tmi_key' ] )
80                ->rows( $updates )
81                ->caller( __METHOD__ )
82                ->execute();
83        }
84
85        if ( $deletions !== [] ) {
86            $dbw->newDeleteQueryBuilder()
87                ->deleteFrom( 'translate_messageindex' )
88                ->where( [ 'tmi_key' => $deletions ] )
89                ->caller( __METHOD__ )
90                ->execute();
91        }
92
93        $dbw->endAtomic( __METHOD__ );
94
95        $this->index = $array;
96    }
97}