Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
22 / 44
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslatableBundleStatusStore
50.00% covered (danger)
50.00%
22 / 44
33.33% covered (danger)
33.33%
2 / 6
34.12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setStatus
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
2.00
 getAllWithStatus
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 removeStatus
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getBundleTypeId
40.00% covered (danger)
40.00%
2 / 5
0.00% covered (danger)
0.00%
0 / 1
4.94
 doesTableExist
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use Collation;
7use InvalidArgumentException;
8use MediaWiki\Extension\Translate\MessageBundleTranslation\MessageBundle;
9use MediaWiki\Extension\Translate\PageTranslation\TranslatablePage;
10use MediaWiki\Title\Title;
11use Wikimedia\Rdbms\IDatabase;
12use Wikimedia\Rdbms\IMaintainableDatabase;
13
14/**
15 * Store service for looking up and storing status for translatable bundle status.
16 * @author Abijeet Patro
17 * @license GPL-2.0-or-later
18 * @since 2022.09
19 */
20class TranslatableBundleStatusStore {
21    private const TABLE_NAME = 'translate_translatable_bundles';
22    private IDatabase $database;
23    private Collation $collation;
24    private IMaintainableDatabase $dbMaintenance;
25    private ?bool $tableExists = null;
26
27    public function __construct(
28        IDatabase $database,
29        Collation $collation,
30        IMaintainableDatabase $dbMaintenance
31    ) {
32        $this->database = $database;
33        $this->collation = $collation;
34        $this->dbMaintenance = $dbMaintenance;
35    }
36
37    public function setStatus( Title $title, TranslatableBundleStatus $status, string $bundleType ): void {
38        if ( !$this->doesTableExist() ) {
39            return;
40        }
41
42        $sortKey = substr( $this->collation->getSortKey( $title->getPrefixedDBkey() ), 0, 255 );
43        $bundleTypeId = $this->getBundleTypeId( $bundleType );
44        $this->database->replace(
45            self::TABLE_NAME,
46            'ttb_page_id',
47            [
48                'ttb_page_id' => $title->getArticleID(),
49                'ttb_type' => $bundleTypeId,
50                'ttb_status' => $status->getId(),
51                'ttb_sortkey' => $sortKey
52            ],
53            __METHOD__
54        );
55    }
56
57    /** Return all bundles in an array with key being page id, value being status */
58    public function getAllWithStatus(): array {
59        if ( !$this->doesTableExist() ) {
60            return [];
61        }
62
63        $resultSet = $this->database->newSelectQueryBuilder()
64            ->select( [ 'ttb_page_id', 'ttb_status' ] )
65            ->from( self::TABLE_NAME )
66            ->caller( __METHOD__ )
67            ->fetchResultSet();
68
69        $result = [];
70        foreach ( $resultSet as $row ) {
71            $result[$row->ttb_page_id] = (int)$row->ttb_status;
72        }
73
74        return $result;
75    }
76
77    public function removeStatus( int ...$pageIds ): void {
78        if ( !$this->doesTableExist() ) {
79            return;
80        }
81
82        $this->database->delete(
83            self::TABLE_NAME,
84            [ 'ttb_page_id' => $pageIds ],
85            __METHOD__
86        );
87    }
88
89    private function getBundleTypeId( string $bundle ): int {
90        if ( $bundle === TranslatablePage::class ) {
91            return 1;
92        } elseif ( $bundle === MessageBundle::class ) {
93            return 2;
94        }
95
96        throw new InvalidArgumentException( "Unknown translatable bundle type: $bundle" );
97    }
98
99    /** TODO: Remove this check once table is available on Wikimedia sites that use Translate */
100    private function doesTableExist(): bool {
101        if ( $this->tableExists === null ) {
102            $this->tableExists = $this->dbMaintenance->tableExists( self::TABLE_NAME, __METHOD__ );
103        }
104
105        return $this->tableExists;
106    }
107}