Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ToolLinksHandler
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
4 / 4
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
 onContributionsToolLinks
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 onHistoryPageToolLinks
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 onUndeletePageToolLinks
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getSpecialPageTitle
n/a
0 / 0
n/a
0 / 0
2
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Hooks\Handlers;
4
5use IContextSource;
6use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
7use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseLog;
8use MediaWiki\Linker\LinkRenderer;
9use MediaWiki\Linker\LinkTarget;
10use MediaWiki\SpecialPage\SpecialPage;
11use MediaWiki\Title\Title;
12use MediaWiki\Title\TitleValue;
13use Wikimedia\IPUtils;
14
15class ToolLinksHandler implements
16    \MediaWiki\Hook\ContributionsToolLinksHook,
17    \MediaWiki\Hook\HistoryPageToolLinksHook,
18    \MediaWiki\Hook\UndeletePageToolLinksHook
19{
20
21    /** @var AbuseFilterPermissionManager */
22    private $afPermManager;
23
24    /**
25     * ToolLinksHandler constructor.
26     * @param AbuseFilterPermissionManager $afPermManager
27     */
28    public function __construct( AbuseFilterPermissionManager $afPermManager ) {
29        $this->afPermManager = $afPermManager;
30    }
31
32    /**
33     * @param int $id
34     * @param Title $nt
35     * @param array &$tools
36     * @param SpecialPage $sp for context
37     */
38    public function onContributionsToolLinks( $id, Title $nt, array &$tools, SpecialPage $sp ) {
39        $username = $nt->getText();
40        if ( $this->afPermManager->canViewAbuseLog( $sp->getAuthority() )
41            && !IPUtils::isValidRange( $username )
42        ) {
43            $linkRenderer = $sp->getLinkRenderer();
44            $tools['abuselog'] = $linkRenderer->makeLink(
45                $this->getSpecialPageTitle(),
46                $sp->msg( 'abusefilter-log-linkoncontribs' )->text(),
47                [ 'title' => $sp->msg( 'abusefilter-log-linkoncontribs-text',
48                    $username )->text(), 'class' => 'mw-contributions-link-abuse-log' ],
49                [ 'wpSearchUser' => $username ]
50            );
51        }
52    }
53
54    /**
55     * @param IContextSource $context
56     * @param LinkRenderer $linkRenderer
57     * @param string[] &$links
58     */
59    public function onHistoryPageToolLinks( IContextSource $context, LinkRenderer $linkRenderer, array &$links ) {
60        if ( $this->afPermManager->canViewAbuseLog( $context->getAuthority() ) ) {
61            $links[] = $linkRenderer->makeLink(
62                $this->getSpecialPageTitle(),
63                $context->msg( 'abusefilter-log-linkonhistory' )->text(),
64                [ 'title' => $context->msg( 'abusefilter-log-linkonhistory-text' )->text() ],
65                [ 'wpSearchTitle' => $context->getTitle()->getPrefixedText() ]
66            );
67        }
68    }
69
70    /**
71     * @param IContextSource $context
72     * @param LinkRenderer $linkRenderer
73     * @param string[] &$links
74     */
75    public function onUndeletePageToolLinks( IContextSource $context, LinkRenderer $linkRenderer, array &$links ) {
76        $show = $this->afPermManager->canViewAbuseLog( $context->getAuthority() );
77        $action = $context->getRequest()->getVal( 'action', 'view' );
78
79        // For 'history action', the link would be added by HistoryPageToolLinks hook.
80        if ( $show && $action !== 'history' ) {
81            $links[] = $linkRenderer->makeLink(
82                $this->getSpecialPageTitle(),
83                $context->msg( 'abusefilter-log-linkonundelete' )->text(),
84                [ 'title' => $context->msg( 'abusefilter-log-linkonundelete-text' )->text() ],
85                [ 'wpSearchTitle' => $context->getTitle()->getPrefixedText() ]
86            );
87        }
88    }
89
90    /**
91     * @codeCoverageIgnore Helper for tests
92     * @return LinkTarget
93     */
94    private function getSpecialPageTitle(): LinkTarget {
95        return defined( 'MW_PHPUNIT_TEST' )
96            ? new TitleValue( NS_SPECIAL, SpecialAbuseLog::PAGE_NAME )
97            : SpecialPage::getTitleFor( SpecialAbuseLog::PAGE_NAME );
98    }
99}