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