Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EchoHooks
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 onBeforeCreateEchoEvent
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
6
 onEchoGetBundleRules
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace MediaWiki\Extension\Thanks;
4
5use MediaWiki\Extension\Notifications\AttributeManager;
6use MediaWiki\Extension\Notifications\Hooks\BeforeCreateEchoEventHook;
7use MediaWiki\Extension\Notifications\Hooks\EchoGetBundleRulesHook;
8use MediaWiki\Extension\Notifications\Model\Event;
9use MediaWiki\Extension\Notifications\UserLocator;
10use MediaWiki\Registration\ExtensionRegistry;
11
12/**
13 * Hooks for Thanks extension
14 *
15 * @file
16 * @ingroup Extensions
17 */
18class EchoHooks implements BeforeCreateEchoEventHook, EchoGetBundleRulesHook {
19
20    /**
21     * Add Thanks events to Echo
22     *
23     * @param array &$notifications array of Echo notifications
24     * @param array &$notificationCategories array of Echo notification categories
25     * @param array &$icons array of icon details
26     */
27    public function onBeforeCreateEchoEvent(
28        array &$notifications, array &$notificationCategories, array &$icons
29    ) {
30        $notificationCategories['edit-thank'] = [
31            'priority' => 3,
32            'tooltip' => 'echo-pref-tooltip-edit-thank',
33        ];
34
35        $notifications['edit-thank'] = [
36            // The following message is generated by the category name:
37            // * echo-category-title-edit-thank
38            'category' => 'edit-thank',
39            'group' => 'positive',
40            'section' => 'message',
41            'presentation-model' => EchoCoreThanksPresentationModel::class,
42            'bundle' => [
43                'web' => true,
44                'expandable' => true,
45            ],
46            AttributeManager::ATTR_LOCATORS => [
47                [
48                    [ UserLocator::class, 'locateFromEventExtra' ],
49                    [ 'thanked-user-id' ]
50                ],
51            ],
52        ];
53
54        if ( ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) {
55            $notifications['flow-thank'] = [
56                'category' => 'edit-thank',
57                'group' => 'positive',
58                'section' => 'message',
59                'presentation-model' => EchoFlowThanksPresentationModel::class,
60                'bundle' => [
61                    'web' => true,
62                    'expandable' => true,
63                ],
64                AttributeManager::ATTR_LOCATORS => [
65                    [
66                        [ UserLocator::class, 'locateFromEventExtra' ],
67                        [ 'thanked-user-id' ]
68                    ],
69                ],
70            ];
71        }
72
73        $icons['thanks'] = [
74            'path' => [
75                'ltr' => 'Thanks/modules/userTalk-constructive-ltr.svg',
76                'rtl' => 'Thanks/modules/userTalk-constructive-rtl.svg'
77            ]
78        ];
79    }
80
81    /**
82     * Handler for EchoGetBundleRule hook, which defines the bundle rules for each notification.
83     *
84     * @param Event $event The event being notified.
85     * @param string &$bundleString Determines how the notification should be bundled.
86     */
87    public function onEchoGetBundleRules( Event $event, string &$bundleString ) {
88        switch ( $event->getType() ) {
89            case 'edit-thank':
90                $bundleString = 'edit-thank';
91                // Try to get either the revid or logid parameter.
92                $revOrLogId = $event->getExtraParam( 'logid' );
93                if ( $revOrLogId ) {
94                    // avoid collision with revision ids
95                    $revOrLogId = 'log' . $revOrLogId;
96                } else {
97                    $revOrLogId = $event->getExtraParam( 'revid' );
98                }
99                if ( $revOrLogId ) {
100                    $bundleString .= $revOrLogId;
101                }
102                break;
103            case 'flow-thank':
104                $bundleString = 'flow-thank';
105                $postId = $event->getExtraParam( 'post-id' );
106                if ( $postId ) {
107                    $bundleString .= $postId;
108                }
109                break;
110        }
111    }
112
113}