Move Echo hooks to new EchoHooks handler that has no dependencies

Echo hook BeforeCreateEchoEvent is triggered very early in
Setup.php. This causes all dependencies of Thanks/Hooks to be
initialized. One of the places that is affected is the `addWiki.php`
script that tries to redefine the RevisionStore service, but cannot
due to fact it's already initialized.

Both Echo hooks (BeforeCreateEchoEvent and EchoGetBundleRules) do
not require any dependencies, and due to fact that one of those is
initialized early, let's move them to a separate small EchoHooks
class that would handle Echo specific logic.

Bug: T358236
Change-Id: I0e348872ba5dc313325e3f4f296fd84bfb2c785b
This commit is contained in:
Piotr Miazga 2024-04-04 16:15:30 +02:00
parent 65b0f8b3b7
commit 7b750929a8
3 changed files with 118 additions and 102 deletions

View file

@ -164,12 +164,12 @@
},
"Hooks": {
"ApiMain::moduleManager": "main",
"BeforeCreateEchoEvent": "main",
"BeforeCreateEchoEvent": "echo",
"BeforePageDisplay": "main",
"BeforeSpecialMobileDiffDisplay": "mobile-frontend",
"DiffTools": "main",
"DifferenceEngineViewHeader": "main",
"EchoGetBundleRules": "main",
"EchoGetBundleRules": "echo",
"GetAllBlockActions": "main",
"GetLogTypesOnUser": "main",
"HistoryTools": "main",
@ -195,6 +195,9 @@
"MainConfig",
"UserFactory"
]
},
"echo": {
"class": "MediaWiki\\Extension\\Thanks\\EchoHooks"
}
},
"ServiceWiringFiles": [

113
includes/EchoHooks.php Normal file
View file

@ -0,0 +1,113 @@
<?php
namespace MediaWiki\Extension\Thanks;
use EchoAttributeManager;
use EchoUserLocator;
use ExtensionRegistry;
use MediaWiki\Extension\Notifications\Hooks\BeforeCreateEchoEventHook;
use MediaWiki\Extension\Notifications\Hooks\EchoGetBundleRulesHook;
use MediaWiki\Extension\Notifications\Model\Event;
/**
* Hooks for Thanks extension
*
* @file
* @ingroup Extensions
*/
class EchoHooks implements BeforeCreateEchoEventHook, EchoGetBundleRulesHook {
/**
* Add Thanks events to Echo
*
* @param array &$notifications array of Echo notifications
* @param array &$notificationCategories array of Echo notification categories
* @param array &$icons array of icon details
*/
public function onBeforeCreateEchoEvent(
array &$notifications, array &$notificationCategories, array &$icons
) {
$notificationCategories['edit-thank'] = [
'priority' => 3,
'tooltip' => 'echo-pref-tooltip-edit-thank',
];
$notifications['edit-thank'] = [
// The following message is generated by the category name:
// * echo-category-title-edit-thank
'category' => 'edit-thank',
'group' => 'positive',
'section' => 'message',
'presentation-model' => EchoCoreThanksPresentationModel::class,
'bundle' => [
'web' => true,
'expandable' => true,
],
EchoAttributeManager::ATTR_LOCATORS => [
[
[ EchoUserLocator::class, 'locateFromEventExtra' ],
[ 'thanked-user-id' ]
],
],
];
if ( ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) {
$notifications['flow-thank'] = [
'category' => 'edit-thank',
'group' => 'positive',
'section' => 'message',
'presentation-model' => EchoFlowThanksPresentationModel::class,
'bundle' => [
'web' => true,
'expandable' => true,
],
EchoAttributeManager::ATTR_LOCATORS => [
[
[ EchoUserLocator::class, 'locateFromEventExtra' ],
[ 'thanked-user-id' ]
],
],
];
}
$icons['thanks'] = [
'path' => [
'ltr' => 'Thanks/modules/userTalk-constructive-ltr.svg',
'rtl' => 'Thanks/modules/userTalk-constructive-rtl.svg'
]
];
}
/**
* Handler for EchoGetBundleRule hook, which defines the bundle rules for each notification.
*
* @param Event $event The event being notified.
* @param string &$bundleString Determines how the notification should be bundled.
*/
public function onEchoGetBundleRules( Event $event, string &$bundleString ) {
switch ( $event->getType() ) {
case 'edit-thank':
$bundleString = 'edit-thank';
// Try to get either the revid or logid parameter.
$revOrLogId = $event->getExtraParam( 'logid' );
if ( $revOrLogId ) {
// avoid collision with revision ids
$revOrLogId = 'log' . $revOrLogId;
} else {
$revOrLogId = $event->getExtraParam( 'revid' );
}
if ( $revOrLogId ) {
$bundleString .= $revOrLogId;
}
break;
case 'flow-thank':
$bundleString = 'flow-thank';
$postId = $event->getExtraParam( 'post-id' );
if ( $postId ) {
$bundleString .= $postId;
}
break;
}
}
}

View file

@ -8,8 +8,6 @@ use ApiModuleManager;
use Article;
use DatabaseLogEntry;
use DifferenceEngine;
use EchoAttributeManager;
use EchoUserLocator;
use ExtensionRegistry;
use GenderCache;
use IContextSource;
@ -22,9 +20,6 @@ use MediaWiki\Config\Config;
use MediaWiki\Config\ConfigException;
use MediaWiki\Diff\Hook\DifferenceEngineViewHeaderHook;
use MediaWiki\Diff\Hook\DiffToolsHook;
use MediaWiki\Extension\Notifications\Hooks\BeforeCreateEchoEventHook;
use MediaWiki\Extension\Notifications\Hooks\EchoGetBundleRulesHook;
use MediaWiki\Extension\Notifications\Model\Event;
use MediaWiki\Extension\Thanks\Api\ApiFlowThank;
use MediaWiki\Hook\BeforePageDisplayHook;
use MediaWiki\Hook\GetLogTypesOnUserHook;
@ -54,11 +49,9 @@ use Skin;
*/
class Hooks implements
ApiMain__moduleManagerHook,
BeforeCreateEchoEventHook,
BeforePageDisplayHook,
DiffToolsHook,
DifferenceEngineViewHeaderHook,
EchoGetBundleRulesHook,
GetAllBlockActionsHook,
GetLogTypesOnUserHook,
HistoryToolsHook,
@ -318,67 +311,6 @@ class Hooks implements
}
}
/**
* Add Thanks events to Echo
*
* @param array &$notifications array of Echo notifications
* @param array &$notificationCategories array of Echo notification categories
* @param array &$icons array of icon details
*/
public function onBeforeCreateEchoEvent(
array &$notifications, array &$notificationCategories, array &$icons
) {
$notificationCategories['edit-thank'] = [
'priority' => 3,
'tooltip' => 'echo-pref-tooltip-edit-thank',
];
$notifications['edit-thank'] = [
// The following message is generated by the category name:
// * echo-category-title-edit-thank
'category' => 'edit-thank',
'group' => 'positive',
'section' => 'message',
'presentation-model' => EchoCoreThanksPresentationModel::class,
'bundle' => [
'web' => true,
'expandable' => true,
],
EchoAttributeManager::ATTR_LOCATORS => [
[
[ EchoUserLocator::class, 'locateFromEventExtra' ],
[ 'thanked-user-id' ]
],
],
];
if ( ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) {
$notifications['flow-thank'] = [
'category' => 'edit-thank',
'group' => 'positive',
'section' => 'message',
'presentation-model' => EchoFlowThanksPresentationModel::class,
'bundle' => [
'web' => true,
'expandable' => true,
],
EchoAttributeManager::ATTR_LOCATORS => [
[
[ EchoUserLocator::class, 'locateFromEventExtra' ],
[ 'thanked-user-id' ]
],
],
];
}
$icons['thanks'] = [
'path' => [
'ltr' => 'Thanks/modules/userTalk-constructive-ltr.svg',
'rtl' => 'Thanks/modules/userTalk-constructive-rtl.svg'
]
];
}
/**
* Handler for LocalUserCreated hook
* @see https://www.mediawiki.org/wiki/Manual:Hooks/LocalUserCreated
@ -456,38 +388,6 @@ class Hooks implements
}
}
/**
* Handler for EchoGetBundleRule hook, which defines the bundle rules for each notification.
*
* @param Event $event The event being notified.
* @param string &$bundleString Determines how the notification should be bundled.
*/
public function onEchoGetBundleRules( Event $event, string &$bundleString ) {
switch ( $event->getType() ) {
case 'edit-thank':
$bundleString = 'edit-thank';
// Try to get either the revid or logid parameter.
$revOrLogId = $event->getExtraParam( 'logid' );
if ( $revOrLogId ) {
// avoid collision with revision ids
$revOrLogId = 'log' . $revOrLogId;
} else {
$revOrLogId = $event->getExtraParam( 'revid' );
}
if ( $revOrLogId ) {
$bundleString .= $revOrLogId;
}
break;
case 'flow-thank':
$bundleString = 'flow-thank';
$postId = $event->getExtraParam( 'post-id' );
if ( $postId ) {
$bundleString .= $postId;
}
break;
}
}
/**
* Insert a 'thank' link into the log interface, if the user is allowed to thank.
*