Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
73.33% |
11 / 15 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
SidebarHookHandler | |
73.33% |
11 / 15 |
|
66.67% |
2 / 3 |
6.68 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
onSidebarBeforeOutput | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
buildConceptUriLink | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace EntitySchema\MediaWiki\Hooks; |
6 | |
7 | use MediaWiki\Hook\SidebarBeforeOutputHook; |
8 | use Skin; |
9 | use Wikibase\DataAccess\EntitySource; |
10 | |
11 | /** |
12 | * @license GPL-2.0-or-later |
13 | */ |
14 | class SidebarHookHandler implements SidebarBeforeOutputHook { |
15 | |
16 | /** @var EntitySource */ |
17 | private EntitySource $localEntitySource; |
18 | |
19 | public function __construct( |
20 | EntitySource $localEntitySource |
21 | ) { |
22 | $this->localEntitySource = $localEntitySource; |
23 | } |
24 | |
25 | /** |
26 | * Add Concept URI link to the toolbox section of the sidebar. |
27 | * |
28 | * @param Skin $skin |
29 | * @param string[] &$sidebar |
30 | * @return void |
31 | */ |
32 | public function onSidebarBeforeOutput( $skin, &$sidebar ): void { |
33 | |
34 | $conceptUriLink = $this->buildConceptUriLink( $skin ); |
35 | |
36 | if ( $conceptUriLink === null ) { |
37 | return; |
38 | } |
39 | |
40 | $sidebar['TOOLBOX']['wb-concept-uri'] = $conceptUriLink; |
41 | } |
42 | |
43 | /** |
44 | * Build concept URI link for the sidebar toolbox. |
45 | * |
46 | * @param Skin $skin |
47 | * @return string[]|null Array of link elements or Null if link cannot be created. |
48 | */ |
49 | public function buildConceptUriLink( Skin $skin ): ?array { |
50 | $title = $skin->getTitle(); |
51 | |
52 | $baseConceptUri = $this->localEntitySource->getConceptBaseUri(); |
53 | |
54 | if ( $title === null || !$title->inNamespace( NS_ENTITYSCHEMA_JSON ) ) { |
55 | return null; |
56 | } |
57 | |
58 | return [ |
59 | 'id' => 't-wb-concept-uri', |
60 | 'text' => $skin->msg( 'wikibase-concept-uri' )->text(), |
61 | 'href' => $baseConceptUri . $title->getText(), |
62 | 'title' => $skin->msg( 'wikibase-concept-uri-tooltip' )->text(), |
63 | ]; |
64 | } |
65 | |
66 | } |