Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CampaignChangeHooks | |
0.00% |
0 / 56 |
|
0.00% |
0 / 2 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onCentralNoticeCampaignChange | |
0.00% |
0 / 55 |
|
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 | |
22 | namespace MediaWiki\Extension\EventBus\HookHandlers\MediaWiki; |
23 | |
24 | use Campaign; |
25 | use CentralNoticeCampaignChangeHook; |
26 | use MediaWiki\Deferred\DeferredUpdates; |
27 | use MediaWiki\Extension\EventBus\EventBus; |
28 | use MediaWiki\Extension\EventBus\StreamNameMapper; |
29 | use MediaWiki\User\User; |
30 | use UnexpectedValueException; |
31 | |
32 | class CampaignChangeHooks implements CentralNoticeCampaignChangeHook { |
33 | private StreamNameMapper $streamNameMapper; |
34 | |
35 | public function __construct( StreamNameMapper $streamNameMapper ) { |
36 | $this->streamNameMapper = $streamNameMapper; |
37 | } |
38 | |
39 | /** |
40 | * Handle CentralNoticeCampaignChange hook. Send an event corresponding to the type |
41 | * of campaign change made (create, change or delete). |
42 | * |
43 | * This method is only expected to be called if CentralNotice is installed. |
44 | * |
45 | * @see https://www.mediawiki.org/wiki/Extension:CentralNotice/CentralNoticeCampaignChange |
46 | * |
47 | * @param string $changeType Type of change performed. Can be 'created', 'modified', |
48 | * or 'removed'. |
49 | * @param string $time The time of the change. This is the same time that will be |
50 | * recorded for the change in the cn_notice_log table. |
51 | * @param string $campaignName Name of the campaign created, modified or removed. |
52 | * @param User $user The user who performed the change. |
53 | * @param array|null $beginSettings Campaign settings before the change, if applicable. |
54 | * These will include start, end, enabled, archived and banners. If not applicable, |
55 | * this parameter will be null. |
56 | * @param array|null $endSettings Campaign settings after the change, if applicable. |
57 | * These will include start, end, enabled, archived and banners. If not applicable, |
58 | * this parameter will be null. |
59 | * @param string $summary Change summary provided by the user, or empty string if none |
60 | * was provided. |
61 | */ |
62 | public function onCentralNoticeCampaignChange( |
63 | $changeType, |
64 | $time, |
65 | $campaignName, |
66 | $user, |
67 | $beginSettings, |
68 | $endSettings, |
69 | $summary |
70 | ): void { |
71 | // Since we're running this hook, we'll assume that CentralNotice is installed. |
72 | $campaignUrl = Campaign::getCanonicalURL( $campaignName ); |
73 | |
74 | switch ( $changeType ) { |
75 | case 'created': |
76 | if ( !$endSettings ) { |
77 | return; |
78 | } |
79 | |
80 | $stream = $this->streamNameMapper->resolve( |
81 | 'mediawiki.centralnotice.campaign-create' ); |
82 | $eventBus = EventBus::getInstanceForStream( $stream ); |
83 | $eventFactory = $eventBus->getFactory(); |
84 | $event = $eventFactory->createCentralNoticeCampaignCreateEvent( |
85 | $stream, |
86 | $campaignName, |
87 | $user, |
88 | $endSettings, |
89 | $summary, |
90 | $campaignUrl |
91 | ); |
92 | break; |
93 | |
94 | case 'modified': |
95 | if ( !$endSettings ) { |
96 | return; |
97 | } |
98 | |
99 | $stream = $this->streamNameMapper->resolve( |
100 | 'mediawiki.centralnotice.campaign-change' ); |
101 | $eventBus = EventBus::getInstanceForStream( $stream ); |
102 | $eventFactory = $eventBus->getFactory(); |
103 | $event = $eventFactory->createCentralNoticeCampaignChangeEvent( |
104 | $stream, |
105 | $campaignName, |
106 | $user, |
107 | $endSettings, |
108 | $beginSettings ?: [], |
109 | $summary, |
110 | $campaignUrl |
111 | ); |
112 | break; |
113 | |
114 | case 'removed': |
115 | $stream = $this->streamNameMapper->resolve( |
116 | 'mediawiki.centralnotice.campaign-delete' ); |
117 | $eventBus = EventBus::getInstanceForStream( $stream ); |
118 | $eventFactory = $eventBus->getFactory(); |
119 | $event = $eventFactory->createCentralNoticeCampaignDeleteEvent( |
120 | $stream, |
121 | $campaignName, |
122 | $user, |
123 | $beginSettings ?: [], |
124 | $summary, |
125 | $campaignUrl |
126 | ); |
127 | break; |
128 | |
129 | default: |
130 | throw new UnexpectedValueException( |
131 | 'Bad CentralNotice change type: ' . $changeType ); |
132 | } |
133 | |
134 | DeferredUpdates::addCallableUpdate( |
135 | static function () use ( $eventBus, $event ) { |
136 | $eventBus->send( [ $event ] ); |
137 | } |
138 | ); |
139 | } |
140 | |
141 | } |