Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.16% covered (danger)
8.16%
4 / 49
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbuseFilterSpecialPage
8.16% covered (danger)
8.16%
4 / 49
20.00% covered (danger)
20.00%
1 / 5
87.45
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getShortDescription
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getNavigationLinksInternal
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
 getAssociatedNavigationLinks
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 addNavigationLinks
10.53% covered (danger)
10.53%
2 / 19
0.00% covered (danger)
0.00%
0 / 1
15.46
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Special;
4
5use HtmlArmor;
6use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
7use MediaWiki\Html\Html;
8use MediaWiki\SpecialPage\SpecialPage;
9use MediaWiki\Title\TitleValue;
10
11/**
12 * Parent class for AbuseFilter special pages.
13 */
14abstract class AbuseFilterSpecialPage extends SpecialPage {
15
16    /** @var AbuseFilterPermissionManager */
17    protected $afPermissionManager;
18
19    /**
20     * @param string $name
21     * @param string $restriction
22     * @param AbuseFilterPermissionManager $afPermissionManager
23     */
24    public function __construct(
25        $name,
26        $restriction,
27        AbuseFilterPermissionManager $afPermissionManager
28    ) {
29        parent::__construct( $name, $restriction );
30        $this->afPermissionManager = $afPermissionManager;
31    }
32
33    /**
34     * @inheritDoc
35     */
36    public function getShortDescription( string $path = '' ): string {
37        return match ( $path ) {
38            'AbuseFilter' => $this->msg( 'abusefilter-topnav-home' )->text(),
39            'AbuseFilter/history' => $this->msg( 'abusefilter-topnav-recentchanges' )->text(),
40            'AbuseFilter/examine' => $this->msg( 'abusefilter-topnav-examine' )->text(),
41            'AbuseFilter/test' => $this->msg( 'abusefilter-topnav-test' )->text(),
42            'AbuseFilter/tools' => $this->msg( 'abusefilter-topnav-tools' )->text(),
43            default => parent::getShortDescription( $path ),
44        };
45    }
46
47    /**
48     * Get topbar navigation links definitions
49     */
50    private function getNavigationLinksInternal(): array {
51        $performer = $this->getAuthority();
52
53        $linkDefs = [
54            'home' => 'AbuseFilter',
55            'recentchanges' => 'AbuseFilter/history',
56            'examine' => 'AbuseFilter/examine',
57        ];
58
59        if ( $this->afPermissionManager->canViewAbuseLog( $performer ) ) {
60            $linkDefs += [
61                'log' => 'AbuseLog'
62            ];
63        }
64
65        if ( $this->afPermissionManager->canUseTestTools( $performer ) ) {
66            $linkDefs += [
67                'test' => 'AbuseFilter/test',
68                'tools' => 'AbuseFilter/tools'
69            ];
70        }
71
72        return $linkDefs;
73    }
74
75    /**
76     * Return an array of strings representing page titles that are discoverable to end users via UI.
77     *
78     * @inheritDoc
79     */
80    public function getAssociatedNavigationLinks(): array {
81        $links = $this->getNavigationLinksInternal();
82        return array_map( static function ( $name ) {
83            return 'Special:' . $name;
84        }, array_values( $links ) );
85    }
86
87    /**
88     * Add topbar navigation links
89     *
90     * @param string $pageType
91     */
92    protected function addNavigationLinks( $pageType ) {
93        // If the current skin supports sub menus nothing to do here.
94        if ( $this->getSkin()->supportsMenu( 'associated-pages' ) ) {
95            return;
96        }
97        $linkDefs = $this->getNavigationLinksInternal();
98        $links = [];
99        foreach ( $linkDefs as $name => $page ) {
100            // Give grep a chance to find the usages:
101            // abusefilter-topnav-home, abusefilter-topnav-recentchanges, abusefilter-topnav-test,
102            // abusefilter-topnav-log, abusefilter-topnav-tools, abusefilter-topnav-examine
103            $msgName = "abusefilter-topnav-$name";
104
105            $msg = $this->msg( $msgName )->parse();
106
107            if ( $name === $pageType ) {
108                $links[] = Html::rawElement( 'strong', [], $msg );
109            } else {
110                $links[] = $this->getLinkRenderer()->makeLink(
111                    new TitleValue( NS_SPECIAL, $page ),
112                    new HtmlArmor( $msg )
113                );
114            }
115        }
116
117        $linkStr = $this->msg( 'parentheses' )
118            ->rawParams( $this->getLanguage()->pipeList( $links ) )
119            ->escaped();
120        $linkStr = $this->msg( 'abusefilter-topnav' )->parse() . " $linkStr";
121
122        $linkStr = Html::rawElement( 'div', [ 'class' => 'mw-abusefilter-navigation' ], $linkStr );
123
124        $this->getOutput()->setSubtitle( $linkStr );
125    }
126}