Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
18 / 21 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ExternalLinksProvider | |
85.71% |
18 / 21 |
|
0.00% |
0 / 1 |
8.19 | |
0.00% |
0 / 1 |
| getData | |
85.71% |
18 / 21 |
|
0.00% |
0 / 1 |
8.19 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Provides external links information to ResourceLoader module ext.kartographer.dialog.sidebar |
| 4 | * |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | */ |
| 8 | |
| 9 | namespace Kartographer\Modules; |
| 10 | |
| 11 | use MediaWiki\Json\FormatJson; |
| 12 | use MediaWiki\ResourceLoader\Context; |
| 13 | use RuntimeException; |
| 14 | use stdClass; |
| 15 | |
| 16 | /** |
| 17 | * @license MIT |
| 18 | */ |
| 19 | class 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 | // Names that don't need localization can be hard-coded in the .json file |
| 43 | if ( !isset( $service->name ) ) { |
| 44 | $msg = $context->msg( 'kartographer-link-' . $service->id ); |
| 45 | if ( !$msg->isDisabled() ) { |
| 46 | $service->name = $msg->plain(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | foreach ( $service->links as $link ) { |
| 51 | $usedTypes[$link->type] = true; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | $data->types = array_keys( array_merge( array_flip( $data->types ), $usedTypes ) ); |
| 56 | |
| 57 | $data->localization = []; |
| 58 | foreach ( $usedTypes as $type => $_ ) { |
| 59 | $data->localization[$type] = $context->msg( 'kartographer-linktype-' . $type )->plain(); |
| 60 | } |
| 61 | |
| 62 | return $data; |
| 63 | } |
| 64 | } |