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