From 7ef2259228d1b8b695ceb523e3ae2747ea7ba57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Such=C3=A1nek?= Date: Sun, 4 Oct 2020 13:29:06 +0200 Subject: [PATCH] Migrate a few hook handlers to DI Bug: T261067 Change-Id: If699917c3d2e9e22525c7d0495554e25f6b45125 --- extension.json | 12 ++- includes/AbuseFilterHooks.php | 71 ---------------- includes/Hooks/Handlers/ToolLinksHandler.php | 86 ++++++++++++++++++++ 3 files changed, 95 insertions(+), 74 deletions(-) create mode 100644 includes/Hooks/Handlers/ToolLinksHandler.php diff --git a/extension.json b/extension.json index cbce1b9ba..7980fcd4d 100644 --- a/extension.json +++ b/extension.json @@ -271,6 +271,12 @@ "remoteExtPath": "AbuseFilter/modules" }, "callback": "AbuseFilterHooks::onRegistration", + "HookHandlers": { + "ToolLinks": { + "class": "MediaWiki\\Extension\\AbuseFilter\\Hooks\\Handlers\\ToolLinksHandler", + "services": [ "AbuseFilterPermissionManager" ] + } + }, "Hooks": { "EditFilterMergedContent": "AbuseFilterHooks::onEditFilterMergedContent", "GetAutoPromoteGroups": "AbuseFilterHooks::onGetAutoPromoteGroups", @@ -280,9 +286,9 @@ "ListDefinedTags": "AbuseFilterHooks::onListDefinedTags", "ChangeTagsListActive": "AbuseFilterHooks::onChangeTagsListActive", "LoadExtensionSchemaUpdates": "AbuseFilterHooks::onLoadExtensionSchemaUpdates", - "ContributionsToolLinks": "AbuseFilterHooks::onContributionsToolLinks", - "HistoryPageToolLinks": "AbuseFilterHooks::onHistoryPageToolLinks", - "UndeletePageToolLinks": "AbuseFilterHooks::onUndeletePageToolLinks", + "ContributionsToolLinks": "ToolLinks", + "HistoryPageToolLinks": "ToolLinks", + "UndeletePageToolLinks": "ToolLinks", "UploadVerifyUpload": "AbuseFilterHooks::onUploadVerifyUpload", "UploadStashFile": "AbuseFilterHooks::onUploadStashFile", "PageSaveComplete": "AbuseFilterHooks::onPageSaveComplete", diff --git a/includes/AbuseFilterHooks.php b/includes/AbuseFilterHooks.php index b203116e9..3a54733f2 100644 --- a/includes/AbuseFilterHooks.php +++ b/includes/AbuseFilterHooks.php @@ -1,14 +1,11 @@ getText(); - if ( $afPermManager->canViewAbuseLog( $sp->getUser() ) && !IPUtils::isValidRange( $username ) ) { - $linkRenderer = $sp->getLinkRenderer(); - $tools['abuselog'] = $linkRenderer->makeLink( - SpecialPage::getTitleFor( 'AbuseLog' ), - $sp->msg( 'abusefilter-log-linkoncontribs' )->text(), - [ 'title' => $sp->msg( 'abusefilter-log-linkoncontribs-text', - $username )->text() ], - [ 'wpSearchUser' => $username ] - ); - } - } - - /** - * @param IContextSource $context - * @param LinkRenderer $linkRenderer - * @param string[] &$links - */ - public static function onHistoryPageToolLinks( - IContextSource $context, - LinkRenderer $linkRenderer, - array &$links - ) { - $afPermManager = AbuseFilterServices::getPermissionManager(); - $user = $context->getUser(); - if ( $afPermManager->canViewAbuseLog( $user ) ) { - $links[] = $linkRenderer->makeLink( - SpecialPage::getTitleFor( 'AbuseLog' ), - $context->msg( 'abusefilter-log-linkonhistory' )->text(), - [ 'title' => $context->msg( 'abusefilter-log-linkonhistory-text' )->text() ], - [ 'wpSearchTitle' => $context->getTitle()->getPrefixedText() ] - ); - } - } - - /** - * @param IContextSource $context - * @param LinkRenderer $linkRenderer - * @param string[] &$links - */ - public static function onUndeletePageToolLinks( - IContextSource $context, - LinkRenderer $linkRenderer, - array &$links - ) { - $afPermManager = AbuseFilterServices::getPermissionManager(); - $show = $afPermManager->canViewAbuseLog( $context->getUser() ); - $action = $context->getRequest()->getVal( 'action', 'view' ); - - // For 'history action', the link would be added by HistoryPageToolLinks hook. - if ( $show && $action !== 'history' ) { - $links[] = $linkRenderer->makeLink( - SpecialPage::getTitleFor( 'AbuseLog' ), - $context->msg( 'abusefilter-log-linkonundelete' )->text(), - [ 'title' => $context->msg( 'abusefilter-log-linkonundelete-text' )->text() ], - [ 'wpSearchTitle' => $context->getTitle()->getPrefixedText() ] - ); - } - } - /** * Filter an upload. * diff --git a/includes/Hooks/Handlers/ToolLinksHandler.php b/includes/Hooks/Handlers/ToolLinksHandler.php new file mode 100644 index 000000000..42628db64 --- /dev/null +++ b/includes/Hooks/Handlers/ToolLinksHandler.php @@ -0,0 +1,86 @@ +afPermManager = $afPermManager; + } + + /** + * @param int $id + * @param Title $nt + * @param array &$tools + * @param SpecialPage $sp for context + */ + public function onContributionsToolLinks( $id, $nt, &$tools, $sp ) { + $username = $nt->getText(); + if ( $this->afPermManager->canViewAbuseLog( $sp->getUser() ) + && !IPUtils::isValidRange( $username ) + ) { + $linkRenderer = $sp->getLinkRenderer(); + $tools['abuselog'] = $linkRenderer->makeLink( + SpecialPage::getTitleFor( 'AbuseLog' ), + $sp->msg( 'abusefilter-log-linkoncontribs' )->text(), + [ 'title' => $sp->msg( 'abusefilter-log-linkoncontribs-text', + $username )->text() ], + [ 'wpSearchUser' => $username ] + ); + } + } + + /** + * @param IContextSource $context + * @param LinkRenderer $linkRenderer + * @param string[] &$links + */ + public function onHistoryPageToolLinks( $context, $linkRenderer, &$links ) { + if ( $this->afPermManager->canViewAbuseLog( $context->getUser() ) ) { + $links[] = $linkRenderer->makeLink( + SpecialPage::getTitleFor( 'AbuseLog' ), + $context->msg( 'abusefilter-log-linkonhistory' )->text(), + [ 'title' => $context->msg( 'abusefilter-log-linkonhistory-text' )->text() ], + [ 'wpSearchTitle' => $context->getTitle()->getPrefixedText() ] + ); + } + } + + /** + * @param IContextSource $context + * @param LinkRenderer $linkRenderer + * @param string[] &$links + */ + public function onUndeletePageToolLinks( $context, $linkRenderer, &$links ) { + $show = $this->afPermManager->canViewAbuseLog( $context->getUser() ); + $action = $context->getRequest()->getVal( 'action', 'view' ); + + // For 'history action', the link would be added by HistoryPageToolLinks hook. + if ( $show && $action !== 'history' ) { + $links[] = $linkRenderer->makeLink( + SpecialPage::getTitleFor( 'AbuseLog' ), + $context->msg( 'abusefilter-log-linkonundelete' )->text(), + [ 'title' => $context->msg( 'abusefilter-log-linkonundelete-text' )->text() ], + [ 'wpSearchTitle' => $context->getTitle()->getPrefixedText() ] + ); + } + } +}