Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
28 / 30
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslationStashStorage
93.33% covered (success)
93.33%
28 / 30
75.00% covered (warning)
75.00%
3 / 4
5.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTranslations
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 addTranslation
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 deleteTranslations
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorSandbox;
5
6use MediaWiki\Title\Title;
7use User;
8use Wikimedia\Rdbms\IDatabase;
9
10/**
11 * Storage class for stashed translations. This one uses sql tables as storage.
12 *
13 * @author Niklas Laxström
14 * @license GPL-2.0-or-later
15 * @since 2013.06 (namespaced in 2020.11)
16 */
17class TranslationStashStorage implements TranslationStashReader, TranslationStashWriter {
18    /** @var IDatabase */
19    protected $db;
20    /** @var string */
21    protected $dbTable;
22
23    public function __construct( IDatabase $db, string $table = 'translate_stash' ) {
24        $this->db = $db;
25        $this->dbTable = $table;
26    }
27
28    public function getTranslations( User $user ): array {
29        $res = $this->db->newSelectQueryBuilder()
30            ->select( [ 'ts_namespace', 'ts_title', 'ts_value', 'ts_metadata' ] )
31            ->from( $this->dbTable )
32            ->where( [ 'ts_user' => $user->getId() ] )
33            ->caller( __METHOD__ )
34            ->fetchResultSet();
35
36        $objects = [];
37        foreach ( $res as $row ) {
38            $objects[] = new StashedTranslation(
39                $user,
40                Title::makeTitle( (int)$row->ts_namespace, $row->ts_title ),
41                $row->ts_value,
42                unserialize( $row->ts_metadata )
43            );
44        }
45
46        return $objects;
47    }
48
49    public function addTranslation( StashedTranslation $item ): void {
50        $row = [
51            'ts_user' => $item->getUser()->getId(),
52            'ts_title' => $item->getTitle()->getDBkey(),
53            'ts_namespace' => $item->getTitle()->getNamespace(),
54            'ts_value' => $item->getValue(),
55            'ts_metadata' => serialize( $item->getMetadata() ),
56        ];
57
58        $indexes = [
59            [ 'ts_user', 'ts_namespace', 'ts_title' ],
60        ];
61
62        $this->db->replace( $this->dbTable, $indexes, $row, __METHOD__ );
63    }
64
65    public function deleteTranslations( User $user ): void {
66        $conds = [ 'ts_user' => $user->getId() ];
67        $this->db->delete( $this->dbTable, $conds, __METHOD__ );
68    }
69}