Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MessageBundleMessageGroupFactory
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 6
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDependencies
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getData
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
 createGroups
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageBundleTranslation;
5
6use MainConfigDependency;
7use MediaWiki\Config\ServiceOptions;
8use MediaWiki\Extension\Translate\MessageGroupProcessing\CachedMessageGroupFactory;
9use MediaWiki\Extension\Translate\MessageGroupProcessing\RevTagStore;
10use MediaWiki\Extension\Translate\MessageProcessing\MessageGroupMetadata;
11use MediaWiki\Title\Title;
12use Wikimedia\Rdbms\IReadableDatabase;
13
14/**
15 * @since 2024.05
16 * @author Niklas Laxström
17 * @copyright GPL-2.0-or-later
18 */
19class MessageBundleMessageGroupFactory implements CachedMessageGroupFactory {
20    public const SERVICE_OPTIONS = [
21        'TranslateEnableMessageBundleIntegration'
22    ];
23    private MessageGroupMetadata $messageGroupMetadata;
24    private bool $enableIntegration;
25
26    public function __construct(
27        MessageGroupMetadata $messageGroupMetadata,
28        ServiceOptions $options
29    ) {
30        $this->messageGroupMetadata = $messageGroupMetadata;
31        $options->assertRequiredOptions( self::SERVICE_OPTIONS );
32        $this->enableIntegration = $options->get( 'TranslateEnableMessageBundleIntegration' );
33    }
34
35    public function getCacheKey(): string {
36        return 'message-bundles';
37    }
38
39    public function getCacheVersion(): int {
40        return 1;
41    }
42
43    public function getDependencies(): array {
44        return [ new MainConfigDependency( 'TranslateEnableMessageBundleIntegration' ) ];
45    }
46
47    public function getData( IReadableDatabase $db ) {
48        if ( !$this->enableIntegration ) {
49            return [];
50        }
51
52        $cacheData = [];
53        $res = $db->newSelectQueryBuilder()
54            ->select( [ 'page_id', 'page_namespace', 'page_title', 'rt_revision' => 'MAX(rt_revision)' ] )
55            ->from( 'page' )
56            ->join( 'revtag', null, [ 'page_id=rt_page', 'rt_type' => RevTagStore::MB_VALID_TAG ] )
57            ->groupBy( [ 'page_id', 'page_namespace', 'page_title' ] )
58            ->caller( __METHOD__ )
59            ->fetchResultSet();
60
61        foreach ( $res as $r ) {
62            $title = Title::newFromRow( $r );
63            $cacheData[] = [
64                $title->getPrefixedText(),
65                (int)$r->page_id,
66                (int)$r->rt_revision,
67            ];
68        }
69
70        return $cacheData;
71    }
72
73    public function createGroups( $data ): array {
74        $groups = [];
75        $groupIds = [];
76
77        // First get all the group ids
78        foreach ( $data as $conf ) {
79            $groupIds[] = MessageBundleMessageGroup::getGroupId( $conf[0] );
80        }
81
82        // Preload all the metadata
83        $this->messageGroupMetadata->preloadGroups( $groupIds, __METHOD__ );
84
85        // Loop over all the group ids and create the MessageBundleMessageGroup
86        foreach ( $groupIds as $index => $groupId ) {
87            $conf = $data[$index];
88            $description = $this->messageGroupMetadata->getWithDefaultValue( $groupId, 'description', null );
89            $label = $this->messageGroupMetadata->getWithDefaultValue( $groupId, 'label', null );
90            $groups[$groupId] = new MessageBundleMessageGroup(
91                $groupId,
92                $conf[0],
93                $conf[1],
94                $conf[2],
95                $description,
96                $label
97            );
98        }
99
100        return $groups;
101    }
102}