Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
MainHooksHandler
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onBeforePageDisplay
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 onSidebarBeforeOutput
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 onSkinTemplateNavigation__Universal
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * @file
18 */
19
20namespace MediaWiki\Extension\ReportIncident\Hooks\Handlers;
21
22use MediaWiki\Extension\ReportIncident\Services\ReportIncidentController;
23use MediaWiki\Hook\SidebarBeforeOutputHook;
24use MediaWiki\Hook\SkinTemplateNavigation__UniversalHook;
25use MediaWiki\Output\Hook\BeforePageDisplayHook;
26
27class MainHooksHandler implements
28    BeforePageDisplayHook,
29    SidebarBeforeOutputHook,
30    SkinTemplateNavigation__UniversalHook
31{
32
33    private ReportIncidentController $controller;
34
35    public function __construct( ReportIncidentController $controller ) {
36        $this->controller = $controller;
37    }
38
39    /** @inheritDoc */
40    public function onBeforePageDisplay( $out, $skin ): void {
41        // Only add HTML if the:
42        // * page is in a supported namespace,
43        // * skin is minerva
44        // * link is to be shown to the current user, and
45        // * feature flag is enabled.
46        if ( $this->controller->shouldAddMenuItem( $out->getContext() ) ) {
47            $out->addHtml( '<div id="ext-reportincident-app"></div>' );
48        }
49    }
50
51    /** @inheritDoc */
52    public function onSidebarBeforeOutput( $skin, &$sidebar ): void {
53        // Show the link to report in minerva overflow menu if the:
54        // * page is in a supported namespace,
55        // * skin is minerva
56        // * link is to be shown to the current user, and
57        // * feature flag is enabled.
58        if (
59            $skin->getSkinName() === 'minerva' &&
60            $this->controller->shouldAddMenuItem( $skin->getContext() )
61        ) {
62            $this->controller->addModulesAndConfigVars( $skin->getOutput() );
63            $sidebar['TOOLBOX']['reportincident'] = [
64                'class' => 'ext-reportincident-link',
65                'text' => $skin->msg( 'reportincident-report-btn-label' )->text(),
66                'href' => '#',
67                'icon' => 'flag',
68            ];
69        }
70    }
71
72    /**
73     * @inheritDoc
74     * @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
75     */
76    public function onSkinTemplateNavigation__Universal( $sktemplate, &$links ): void {
77        // Show the reporting link in the "Tools" menu if the:
78        // * page is in a supported namespace,
79        // * link is to be shown to the current user, and
80        // * feature flag is enabled.
81        if ( $this->controller->shouldAddMenuItem( $sktemplate->getContext() ) ) {
82            $this->controller->addModulesAndConfigVars( $sktemplate->getOutput() );
83            $links['actions']['reportincident'] = [
84                'class' => 'ext-reportincident-link',
85                'text' => $sktemplate->msg( 'reportincident-report-btn-label' )->text(),
86                'href' => '#',
87                'icon' => 'flag',
88            ];
89        }
90    }
91}