Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Hooks
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 onBeforePageDisplay
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 onBeforeCreateEchoEvent
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 onEchoGetBundleRules
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * @file
18 */
19
20namespace MediaWiki\Extension\ImageSuggestions;
21
22use MediaWiki\Extension\Notifications\AttributeManager;
23use MediaWiki\Extension\Notifications\Hooks\BeforeCreateEchoEventHook;
24use MediaWiki\Extension\Notifications\Hooks\EchoGetBundleRulesHook;
25use MediaWiki\Extension\Notifications\Model\Event;
26use MediaWiki\Extension\Notifications\UserLocator;
27use MediaWiki\Output\Hook\BeforePageDisplayHook;
28use MediaWiki\Output\OutputPage;
29use Skin;
30
31class Hooks implements
32    BeforeCreateEchoEventHook,
33    BeforePageDisplayHook,
34    EchoGetBundleRulesHook
35{
36    public const EVENT_CATEGORY = 'image-suggestions';
37    public const EVENT_NAME = 'image-suggestions';
38
39    /**
40     * @param OutputPage $output
41     * @param Skin $skin
42     */
43    public function onBeforePageDisplay( $output, $skin ): void {
44        $output->addModules( [
45            'oojs-ui.styles.icons-media',
46            'oojs-ui-core.icons'
47        ] );
48    }
49
50    /**
51     * Add Image Suggestions events to Echo
52     *
53     * @param array &$notifications array of Echo notifications
54     * @param array &$notificationCategories array of Echo notification categories
55     * @param array &$icons array of icon details
56     */
57    public function onBeforeCreateEchoEvent(
58        array &$notifications,
59        array &$notificationCategories,
60        array &$icons
61    ) {
62        // Define the category this event belongs to
63        // (this will appear in Special:Preferences)
64        $notificationCategories[static::EVENT_CATEGORY] = [
65            'priority' => 3,
66            'tooltip' => 'echo-pref-tooltip-image-suggestions',
67        ];
68
69        $notifications[static::EVENT_NAME] = [
70            'category' => static::EVENT_CATEGORY,
71            'group' => 'positive',
72            'section' => 'message',
73            AttributeManager::ATTR_LOCATORS => [
74                [ [ UserLocator::class, 'locateEventAgent' ] ],
75            ],
76            'canNotifyAgent' => true,
77            'presentation-model' => ImageSuggestionsPresentationModel::class,
78            'bundle' => [
79                'web' => true,
80                'email' => true,
81                'expandable' => true,
82            ]
83        ];
84
85        // Define the icon to use for this notification
86        $icons[ 'image-suggestions-blue' ] = [
87            'path' => 'ImageSuggestions/modules/ImageSuggestions-placeholder-icon-blue.svg'
88        ];
89    }
90
91    /** @inheritDoc */
92    public function onEchoGetBundleRules( Event $event, string &$bundleString ) {
93        if ( $event->getType() === static::EVENT_NAME ) {
94            $bundleString = static::EVENT_NAME . '-' .
95                $event->getTitle()->getNamespace() . '-' . $event->getTitle()->getDBkey();
96        }
97        return true;
98    }
99}