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