Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.33% covered (warning)
73.33%
11 / 15
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SidebarHookHandler
73.33% covered (warning)
73.33%
11 / 15
66.67% covered (warning)
66.67%
2 / 3
6.68
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onSidebarBeforeOutput
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 buildConceptUriLink
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare( strict_types = 1 );
4
5namespace EntitySchema\MediaWiki\Hooks;
6
7use MediaWiki\Hook\SidebarBeforeOutputHook;
8use Skin;
9use Wikibase\DataAccess\EntitySource;
10
11/**
12 * @license GPL-2.0-or-later
13 */
14class 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}