Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
42.42% covered (danger)
42.42%
14 / 33
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
AggregateGroupMessageGroupFactory
42.42% covered (danger)
42.42%
14 / 33
33.33% covered (danger)
33.33%
2 / 6
24.46
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 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 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 createGroups
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use AggregateMessageGroup;
7use MediaWiki\Extension\Translate\MessageProcessing\MessageGroupMetadata;
8use MessageGroupBase;
9use Wikimedia\Rdbms\IReadableDatabase;
10
11/**
12 * @since 2024.05
13 * @license GPL-2.0-or-later
14 * @author Niklas Laxström
15 */
16final class AggregateGroupMessageGroupFactory implements CachedMessageGroupFactory {
17    private MessageGroupMetadata $messageGroupMetadata;
18
19    public function __construct( MessageGroupMetadata $messageGroupMetadata ) {
20        $this->messageGroupMetadata = $messageGroupMetadata;
21    }
22
23    public function getCacheKey(): string {
24        return 'aggregate-groups';
25    }
26
27    public function getCacheVersion(): int {
28        return 1;
29    }
30
31    public function getDependencies(): array {
32        return [];
33    }
34
35    public function getData( IReadableDatabase $db ): array {
36        // TODO: Ideally messageGroupMetadata would use the provided connection
37        $groupIds = $this->messageGroupMetadata->getGroupsWithSubgroups();
38        $this->messageGroupMetadata->preloadGroups( $groupIds, __METHOD__ );
39
40        $groups = [];
41        foreach ( $groupIds as $id ) {
42            $conf = [];
43            $conf['BASIC'] = [
44                'id' => $id,
45                'label' => $this->messageGroupMetadata->get( $id, 'name' ),
46                'description' => $this->messageGroupMetadata->get( $id, 'description' ),
47            ];
48            $sourceLanguage = $this->messageGroupMetadata->get( $id, 'sourcelanguagecode' );
49            if ( $sourceLanguage ) {
50                $conf['BASIC']['sourcelanguage'] = $sourceLanguage;
51            }
52            $conf['GROUPS'] = $this->messageGroupMetadata->getSubgroups( $id );
53            $groups[$id] = $conf;
54        }
55
56        return $groups;
57    }
58
59    public function createGroups( $data ): array {
60        // Parts that do not vary per group and do not need to be stored in the cache
61        $template = [
62            'BASIC' => [
63                'meta' => 1,
64                'class' => AggregateMessageGroup::class,
65                'namespace' => NS_TRANSLATIONS,
66            ]
67        ];
68
69        $groups = [];
70        foreach ( $data as $groupId => $groupData ) {
71            $groups[$groupId] = MessageGroupBase::factory(
72                array_merge_recursive( $template, $groupData )
73            );
74        }
75
76        return $groups;
77    }
78}