Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
CampaignChangeHooks
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 1
 onCentralNoticeCampaignChange
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22namespace MediaWiki\Extension\EventBus\HookHandlers\MediaWiki;
23
24use Campaign;
25use CentralNoticeCampaignChangeHook;
26use MediaWiki\Deferred\DeferredUpdates;
27use MediaWiki\Extension\EventBus\EventBus;
28use MediaWiki\User\User;
29use UnexpectedValueException;
30
31class CampaignChangeHooks implements CentralNoticeCampaignChangeHook {
32
33    /**
34     * Handle CentralNoticeCampaignChange hook. Send an event corresponding to the type
35     * of campaign change made (create, change or delete).
36     *
37     * This method is only expected to be called if CentralNotice is installed.
38     *
39     * @see https://www.mediawiki.org/wiki/Extension:CentralNotice/CentralNoticeCampaignChange
40     *
41     * @param string $changeType Type of change performed. Can be 'created', 'modified',
42     *   or 'removed'.
43     * @param string $time The time of the change. This is the same time that will be
44     *   recorded for the change in the cn_notice_log table.
45     * @param string $campaignName Name of the campaign created, modified or removed.
46     * @param User $user The user who performed the change.
47     * @param array|null $beginSettings Campaign settings before the change, if applicable.
48     *   These will include start, end, enabled, archived and banners. If not applicable,
49     *   this parameter will be null.
50     * @param array|null $endSettings Campaign settings after the change, if applicable.
51     *   These will include start, end, enabled, archived and banners. If not applicable,
52     *   this parameter will be null.
53     * @param string $summary Change summary provided by the user, or empty string if none
54     *   was provided.
55     */
56    public function onCentralNoticeCampaignChange(
57        $changeType,
58        $time,
59        $campaignName,
60        $user,
61        $beginSettings,
62        $endSettings,
63        $summary
64    ): void {
65        // Since we're running this hook, we'll assume that CentralNotice is installed.
66        $campaignUrl = Campaign::getCanonicalURL( $campaignName );
67
68        switch ( $changeType ) {
69            case 'created':
70                if ( !$endSettings ) {
71                    return;
72                }
73
74                $stream = 'mediawiki.centralnotice.campaign-create';
75                $eventBus = EventBus::getInstanceForStream( $stream );
76                $eventFactory = $eventBus->getFactory();
77                $event = $eventFactory->createCentralNoticeCampaignCreateEvent(
78                    $stream,
79                    $campaignName,
80                    $user,
81                    $endSettings,
82                    $summary,
83                    $campaignUrl
84                );
85                break;
86
87            case 'modified':
88                if ( !$endSettings ) {
89                    return;
90                }
91
92                $stream = 'mediawiki.centralnotice.campaign-change';
93                $eventBus = EventBus::getInstanceForStream( $stream );
94                $eventFactory = $eventBus->getFactory();
95                $event = $eventFactory->createCentralNoticeCampaignChangeEvent(
96                    $stream,
97                    $campaignName,
98                    $user,
99                    $endSettings,
100                    $beginSettings ?: [],
101                    $summary,
102                    $campaignUrl
103                );
104                break;
105
106            case 'removed':
107                $stream = 'mediawiki.centralnotice.campaign-delete';
108                $eventBus = EventBus::getInstanceForStream( $stream );
109                $eventFactory = $eventBus->getFactory();
110                $event = $eventFactory->createCentralNoticeCampaignDeleteEvent(
111                    $stream,
112                    $campaignName,
113                    $user,
114                    $beginSettings ?: [],
115                    $summary,
116                    $campaignUrl
117                );
118                break;
119
120            default:
121                throw new UnexpectedValueException(
122                    'Bad CentralNotice change type: ' . $changeType );
123        }
124
125        DeferredUpdates::addCallableUpdate(
126            static function () use ( $eventBus, $event ) {
127                $eventBus->send( [ $event ] );
128            }
129        );
130    }
131
132}