Merge "Migrate a few hook handlers to DI"

This commit is contained in:
jenkins-bot 2020-10-13 08:36:27 +00:00 committed by Gerrit Code Review
commit 95766762c4
3 changed files with 95 additions and 74 deletions

View file

@ -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",

View file

@ -1,14 +1,11 @@
<?php
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
use MediaWiki\Extension\AbuseFilter\VariableGenerator\RunVariableGenerator;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\User\UserIdentity;
use Wikimedia\IPUtils;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\IMaintainableDatabase;
@ -659,74 +656,6 @@ class AbuseFilterHooks {
}
}
/**
* @param int $id
* @param Title $nt
* @param array &$tools
* @param SpecialPage $sp for context
*/
public static function onContributionsToolLinks( $id, Title $nt, array &$tools, SpecialPage $sp ) {
$afPermManager = AbuseFilterServices::getPermissionManager();
$username = $nt->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.
*

View file

@ -0,0 +1,86 @@
<?php
namespace MediaWiki\Extension\AbuseFilter\Hooks\Handlers;
use IContextSource;
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
use MediaWiki\Linker\LinkRenderer;
use SpecialPage;
use Title;
use Wikimedia\IPUtils;
class ToolLinksHandler implements
\MediaWiki\Hook\ContributionsToolLinksHook,
\MediaWiki\Hook\HistoryPageToolLinksHook,
\MediaWiki\Hook\UndeletePageToolLinksHook
{
/** @var AbuseFilterPermissionManager */
private $afPermManager;
/**
* ToolLinksHandler constructor.
* @param AbuseFilterPermissionManager $afPermManager
*/
public function __construct( AbuseFilterPermissionManager $afPermManager ) {
$this->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() ]
);
}
}
}