Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
15 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExternalLinksProvider
83.33% covered (warning)
83.33%
15 / 18
0.00% covered (danger)
0.00%
0 / 1
6.17
0.00% covered (danger)
0.00%
0 / 1
 getData
83.33% covered (warning)
83.33%
15 / 18
0.00% covered (danger)
0.00%
0 / 1
6.17
1<?php
2/**
3 * Provides external links information to ResourceLoader module ext.kartographer.dialog.sidebar
4 *
5 * @file
6 * @ingroup Extensions
7 */
8
9namespace Kartographer\Modules;
10
11use FormatJson;
12use MediaWiki\ResourceLoader\Context;
13use RuntimeException;
14use stdClass;
15
16/**
17 * @license MIT
18 */
19class ExternalLinksProvider {
20
21    /**
22     * @param Context $context
23     *
24     * @return stdClass
25     */
26    public static function getData( Context $context ) {
27        $json = file_get_contents( __DIR__ . '/../../externalLinks.json' );
28        if ( !$json ) {
29            throw new RuntimeException( 'Error reading externalLinks.json' );
30        }
31        $status = FormatJson::parse( $json, FormatJson::TRY_FIXING | FormatJson::STRIP_COMMENTS );
32
33        if ( !$status->isOK() ) {
34            $message = $status->getMessage( false, false, 'en' )->text();
35            throw new RuntimeException( "Unable to parse externalLinks.json: $message" );
36        }
37
38        $data = $status->getValue();
39        $usedTypes = [];
40
41        foreach ( $data->services as $service ) {
42            $service->name = $context->msg( 'kartographer-link-' . $service->id )->plain();
43
44            foreach ( $service->links as $link ) {
45                $usedTypes[$link->type] = true;
46            }
47        }
48
49        $data->types = array_keys( array_merge( array_flip( $data->types ), $usedTypes ) );
50
51        $data->localization = [];
52        foreach ( $usedTypes as $type => $_ ) {
53            $data->localization[$type] = $context->msg( 'kartographer-linktype-' . $type )->plain();
54        }
55
56        return $data;
57    }
58}