Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ReportIncidentController
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
6 / 6
11
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
 shouldShowButtonForNamespace
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shouldShowButtonForUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isButtonEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shouldAddMenuItem
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 addModulesAndConfigVars
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace MediaWiki\Extension\ReportIncident\Services;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Context\IContextSource;
7use MediaWiki\Output\OutputPage;
8use MediaWiki\User\User;
9
10/**
11 * Controls whether the reporting links and dialog should be shown.
12 */
13class ReportIncidentController {
14
15    private Config $config;
16
17    public function __construct( Config $config ) {
18        $this->config = $config;
19    }
20
21    /**
22     * Should the reporting link / button be shown in the current namespace
23     *
24     * @param int $namespace
25     * @return bool
26     */
27    private function shouldShowButtonForNamespace( int $namespace ): bool {
28        return in_array( $namespace, $this->config->get( 'ReportIncidentEnabledNamespaces' ) );
29    }
30
31    /**
32     * Should the reporting link / button be shown for the current user
33     *
34     * @param User $user
35     * @return bool
36     */
37    private function shouldShowButtonForUser( User $user ): bool {
38        return $user->isNamed();
39    }
40
41    /**
42     * Is the reporting button / link (and reporting dialog) enabled.
43     *
44     * @return bool
45     */
46    private function isButtonEnabled(): bool {
47        return $this->config->get( 'ReportIncidentReportButtonEnabled' );
48    }
49
50    /**
51     * Should the reporting link / button be shown for the current
52     * namespace and user.
53     *
54     * This can also be used to determine whether to add the HTML
55     * for the reporting dialog in a given request.
56     *
57     * @param IContextSource $context The context associated with the current request.
58     * @return bool Whether the button / link should be shown.
59     */
60    public function shouldAddMenuItem( IContextSource $context ): bool {
61        return $this->isButtonEnabled() &&
62            $this->shouldShowButtonForNamespace( $context->getTitle()->getNamespace() ) &&
63            $this->shouldShowButtonForUser( $context->getUser() );
64    }
65
66    /**
67     * Load the modules and associated JS configuration variables
68     * to allow use of the ReportIncident dialog.
69     *
70     * Should only be called after the other methods in this
71     * service to check if the button should be shown are
72     * consulted.
73     *
74     * @param OutputPage $output The OutputPage object associated with the current response
75     * @return void
76     */
77    public function addModulesAndConfigVars( OutputPage $output ): void {
78        $user = $output->getUser();
79        $isDeveloperMode = $this->config->get( 'ReportIncidentDeveloperMode' );
80        $pretendUserHasConfirmedEmail = $isDeveloperMode && $output->getRequest()->getBool( 'withconfirmedemail' );
81        $output->addJsConfigVars( [
82            // Add the link to the administrators page for use by the dialog.
83            'wgReportIncidentAdministratorsPage' => $this->config->get( 'ReportIncidentAdministratorsPage' ),
84            // If in developer mode, pretend the user has a confirmed email if the query parameter is set to
85            // 'withconfirmedemail=1', otherwise use DB value.
86            'wgReportIncidentUserHasConfirmedEmail' => $pretendUserHasConfirmedEmail ?: $user->isEmailConfirmed(),
87        ] );
88        // Add the ReportIncident module, including the JS and Vue code for the dialog.
89        $output->addModules( 'ext.reportIncident' );
90        if ( $output->getSkin()->getSkinName() === 'minerva' ) {
91            // Load custom menu styles for Minerva; see the 'skinStyles' property of the RL module
92            // loaded below.
93            $output->addModuleStyles( 'ext.reportIncident.menuStyles' );
94        }
95    }
96}