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 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfigUtils
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 computeInterwikiMap
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Utils;
5
6/**
7 * This refactors common code in Api and Mock based config computation
8 */
9class ConfigUtils {
10    /**
11     * Compute the interwiki map based on raw data (either manually
12     * configured or obtaianed via an API)
13     *
14     * @param array $iwData
15     *
16     * @return array<array>
17     */
18    public static function computeInterwikiMap( array $iwData ): array {
19        $interwikiMap = [];
20        $keys = [
21            'prefix' => true,
22            'url' => true,
23            'protorel' => true,
24            'local' => true,
25            'localinterwiki' => true,
26            'language' => true,
27            'extralanglink' => true,
28            'linktext' => true,
29        ];
30        foreach ( $iwData as $iwEntry ) {
31            $iwEntry['language'] = isset( $iwEntry['language'] );
32            // Fix up broken interwiki hrefs that are missing a $1 placeholder
33            // Just append the placeholder at the end.
34            // This makes sure that the interwikiMatcher adds one match
35            // group per URI, and that interwiki links work as expected.
36
37            // Not sure why Phan thinks $iwEntry['url'] is a bool
38            // @phan-suppress-next-line PhanTypeMismatchArgumentInternal
39            if ( !str_contains( $iwEntry['url'], '$1' ) ) {
40                $iwEntry['url'] .= '$1';
41            }
42            $iwEntry = array_intersect_key( $iwEntry, $keys );
43            $interwikiMap[$iwEntry['prefix']] = array_filter(
44                $iwEntry, static fn ( $v ) => $v !== false
45            );
46        }
47
48        return $interwikiMap;
49    }
50}