Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
NavigationHooks
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onSkinTemplateNavigation__Universal
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
90
 arrayInsertAfterView
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\CommunityConfiguration\Hooks;
4
5use MediaWiki\Extension\CommunityConfiguration\Provider\ConfigurationProviderFactory;
6use MediaWiki\Extension\CommunityConfiguration\Store\WikiPageStore;
7use MediaWiki\Hook\SkinTemplateNavigation__UniversalHook;
8use MediaWiki\SpecialPage\SpecialPage;
9use MediaWiki\SpecialPage\SpecialPageFactory;
10use MediaWiki\Title\MalformedTitleException;
11use SkinTemplate;
12
13class NavigationHooks implements SkinTemplateNavigation__UniversalHook {
14    private ConfigurationProviderFactory $providerFactory;
15    private SpecialPageFactory $specialPageFactory;
16
17    public function __construct(
18        ConfigurationProviderFactory $providerFactory, SpecialPageFactory $specialPageFactory ) {
19        $this->providerFactory = $providerFactory;
20        $this->specialPageFactory = $specialPageFactory;
21    }
22
23    /**
24     * Adds the `View Form` and `View History` tab.
25     *
26     * This is attached to the MediaWiki 'SkinTemplateNavigation::Universal' hook.
27     *
28     * @param SkinTemplate $sktemplate
29     * @param array &$links Navigation links.
30     * @throws MalformedTitleException
31     */
32    public function onSkinTemplateNavigation__Universal( $sktemplate, &$links ): void {
33        $title = $sktemplate->getTitle();
34        if ( $title->getContentModel() === CONTENT_MODEL_JSON ) {
35            foreach ( $this->providerFactory->getSupportedKeys() as $providerKey ) {
36                $provider = $this->providerFactory->newProvider( $providerKey );
37                $store = $provider->getStore();
38                if ( $store instanceof WikiPageStore &&
39                    $store->getConfigurationTitle()->equals( $title ) ) {
40                    $specialPageTitle = SpecialPage::getTitleFor( 'CommunityConfiguration', $providerKey );
41                    $links['views'] = self::arrayInsertAfterView( $links['views'], [ 'viewform' => [
42                        'class' => '',
43                        'href' => $specialPageTitle->getLocalURL(),
44                        'text' => $sktemplate->msg(
45                            'communityconfiguration-editor-navigation-tab-viewform' )->text()
46                    ] ] );
47                    break;
48                }
49            }
50        }
51
52        [ $specialPageCanonicalName, $providerId ] = $this->specialPageFactory->resolveAlias( $title->getText() );
53        if ( $specialPageCanonicalName === 'CommunityConfiguration'
54            && $providerId && in_array(
55                $providerId, $this->providerFactory->getSupportedKeys() ) && $title->getNamespace() === NS_SPECIAL ) {
56            // Unset Message and Discussion
57            $links['associated-pages'] = [];
58            // Unset Actions 'Move' and 'Delete'
59            unset( $links['actions']['delete'] );
60            unset( $links['actions']['move'] );
61            unset( $links['actions']['protect'] );
62            // Unset Views 'Edit' and 'Read'
63            unset( $links['views']['view'] );
64            unset( $links['views']['viewsource'] );
65            unset( $links['views']['edit'] );
66            $links['views'] = array_merge( [ 'viewform' => [
67                'class' => 'selected',
68                'href'  => $title->getLocalURL(),
69                'text'  => $sktemplate->msg( 'communityconfiguration-editor-navigation-tab-viewform' )->text()
70            ] ], $links['views'] );
71        }
72    }
73
74    /**
75     * @param array $array
76     * @param mixed $insert
77     * @return array
78     */
79    private static function arrayInsertAfterView( array $array, $insert ): array {
80        $pos = array_search( 'view', array_keys( $array ) );
81        return array_merge(
82            array_slice( $array, 0, $pos + 1 ),
83            $insert,
84            array_slice( $array, $pos )
85        );
86    }
87}