Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
HookDefinedMessageGroupFactory
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 7
110
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
 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 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 createGroups
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 appendAutoloader
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupConfiguration;
5
6use CacheDependency;
7use MediaWiki\Extension\Translate\HookRunner;
8use MediaWiki\Extension\Translate\MessageGroupProcessing\CachedMessageGroupFactory;
9use Wikimedia\Rdbms\IReadableDatabase;
10
11/**
12 * Creates message groups from the TranslatePostInitGroupsHook.
13 * @since 2024.06
14 * @author Niklas Laxström
15 * @license GPL-2.0-or-later
16 */
17final class HookDefinedMessageGroupFactory implements CachedMessageGroupFactory {
18    private HookRunner $hookRunner;
19    /** @var CacheDependency[] */
20    private array $deps;
21
22    public function __construct( HookRunner $hookRunner ) {
23        $this->hookRunner = $hookRunner;
24    }
25
26    public function getCacheKey(): string {
27        return 'hook-defined-groups';
28    }
29
30    public function getCacheVersion(): int {
31        return 1;
32    }
33
34    public function getDependencies(): array {
35        return $this->deps;
36    }
37
38    public function getData( IReadableDatabase $db ): array {
39        $groups = $deps = $autoload = [];
40        // When possible, a cache dependency is created to automatically recreate
41        // the cache when configuration changes. Currently used by other extensions
42        // such as Banner Messages and test cases to load message groups.
43        $this->hookRunner->onTranslatePostInitGroups( $groups, $deps, $autoload );
44
45        $value = [
46            'groups' => $groups,
47            'autoload' => $autoload
48        ];
49
50        // Not ideal, but getDependencies is currently called after getData()
51        $this->deps = $deps;
52
53        return $value;
54    }
55
56    public function createGroups( $data ): array {
57        global $wgAutoloadClasses;
58        self::appendAutoloader( $data['autoload'], $wgAutoloadClasses );
59
60        return $data['groups'];
61    }
62
63    /**
64     * Safely merges first array to second array, throwing warning on duplicates and removing
65     * duplicates from the first array.
66     * @param array $additions Things to append
67     * @param array &$to Where to append
68     */
69    private static function appendAutoloader( array $additions, array &$to ) {
70        foreach ( $additions as $class => $file ) {
71            if ( isset( $to[$class] ) && $to[$class] !== $file ) {
72                $msg = "Autoload conflict for $class$to[$class] !== $file";
73                trigger_error( $msg, E_USER_WARNING );
74                continue;
75            }
76
77            $to[$class] = $file;
78        }
79    }
80}