Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
SpecialCommunityConfiguration
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 showErrorMessage
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 isProviderSupported
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\CommunityConfiguration\Specials;
4
5use MediaWiki\Extension\CommunityConfiguration\EditorCapabilities\EditorCapabilityFactory;
6use MediaWiki\Extension\CommunityConfiguration\Provider\ConfigurationProviderFactory;
7use MediaWiki\Extension\CommunityConfiguration\Provider\IConfigurationProvider;
8use MediaWiki\Html\Html;
9use MediaWiki\Output\OutputPage;
10use MediaWiki\SpecialPage\SpecialPage;
11
12class SpecialCommunityConfiguration extends SpecialPage {
13
14    private EditorCapabilityFactory $editorCapabilityFactory;
15    private ConfigurationProviderFactory $providerFactory;
16
17    private const CAPABILITY_DASHBOARD = 'dashboard';
18    private const CAPABILITY_EDITOR = 'generic-editor';
19
20    public function __construct(
21        EditorCapabilityFactory $editorCapabilityFactory,
22        ConfigurationProviderFactory $providerFactory
23    ) {
24        parent::__construct( 'CommunityConfiguration' );
25        $this->editorCapabilityFactory = $editorCapabilityFactory;
26        $this->providerFactory = $providerFactory;
27    }
28
29    /**
30     * @param string|null $subPage
31     * @return void
32     */
33    public function execute( $subPage ) {
34        parent::execute( $subPage );
35        $out = $this->getContext()->getOutput();
36
37        if ( $subPage === null ) {
38            $capabilityName = self::CAPABILITY_DASHBOARD;
39        } else {
40            if ( !$this->isProviderSupported( $subPage ) ) {
41                $this->showErrorMessage( $out, 'communityconfiguration-provider-not-found', $subPage );
42                return;
43            }
44
45            $provider = $this->providerFactory->newProvider( $subPage );
46
47            // If not displayed on the dashboard, it doesn't necessarily mean it's not supported.
48            if ( $provider->getOptionValue( IConfigurationProvider::OPTION_SKIP_DASHBOARD_LISTING ) ) {
49                $this->showErrorMessage( $out, 'communityconfiguration-provider-not-found', $subPage );
50                return;
51            }
52
53            $capabilityName = $provider->getOptionValue( IConfigurationProvider::OPTION_EDITOR_CAPABILITY )
54                ?? self::CAPABILITY_EDITOR;
55        }
56
57        $this->editorCapabilityFactory
58            ->newCapability( $capabilityName, $this->getContext(), $this->getPageTitle() )
59            ->execute( $subPage );
60    }
61
62    /**
63     * Show an error message on the output page
64     *
65     * @param OutputPage $out
66     * @param string $messageKey
67     * @param string $subPage
68     * @return void
69     */
70    private function showErrorMessage( OutputPage $out, string $messageKey, $subPage ) {
71        $out->addHTML( Html::rawElement( 'p', [ 'class' => 'error' ], $this->msg(
72            $messageKey,
73            $subPage
74        )->parse() ) );
75    }
76
77    /**
78     * @param string $providerName The name of the provider as registered in extension.json
79     * @return bool
80     */
81    private function isProviderSupported( string $providerName ): bool {
82        return in_array( $providerName, $this->providerFactory->getSupportedKeys() );
83    }
84}