forked from kulttuuri/SlackNotifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlackNotificationsCore.php
435 lines (398 loc) · 16.1 KB
/
SlackNotificationsCore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
use MediaWiki\MediaWikiServices;
class SlackNotifications
{
/**
* Gets nice HTML text for user containing the link to user page
* and also links to user site, groups editing, talk and contribs pages.
*/
static function getSlackUserText($user)
{
global $wgSlackNotificationWikiUrl, $wgSlackNotificationWikiUrlEnding, $wgSlackNotificationWikiUrlEndingUserPage,
$wgSlackNotificationWikiUrlEndingBlockUser, $wgSlackNotificationWikiUrlEndingUserRights,
$wgSlackNotificationWikiUrlEndingUserTalkPage, $wgSlackNotificationWikiUrlEndingUserContributions,
$wgSlackIncludeUserUrls;
if ($wgSlackIncludeUserUrls)
{
return sprintf(
"%s (%s | %s | %s | %s)",
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$wgSlackNotificationWikiUrlEndingUserPage.$user."|$user>",
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$wgSlackNotificationWikiUrlEndingBlockUser.$user."|block>",
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$wgSlackNotificationWikiUrlEndingUserRights.$user."|groups>",
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$wgSlackNotificationWikiUrlEndingUserTalkPage.$user."|talk>",
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$wgSlackNotificationWikiUrlEndingUserContributions.$user."|contribs>");
}
else
{
return "<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$wgSlackNotificationWikiUrlEndingUserPage.$user."|$user>";
}
}
/**
* Gets nice HTML text for article containing the link to article page
* and also into edit, delete and article history pages.
*/
static function getSlackArticleText(WikiPage $article, $diff = false)
{
global $wgSlackNotificationWikiUrl, $wgSlackNotificationWikiUrlEnding, $wgSlackNotificationWikiUrlEndingEditArticle,
$wgSlackNotificationWikiUrlEndingDeleteArticle, $wgSlackNotificationWikiUrlEndingHistory,
$wgSlackNotificationWikiUrlEndingDiff, $wgSlackIncludePageUrls;
$prefix = "<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.str_replace(" ", "_", $article->getTitle()->getFullText());
if ($wgSlackIncludePageUrls)
{
$out = sprintf(
"%s (%s | %s | %s",
$prefix."|".$article->getTitle()->getFullText().">",
$prefix."&".$wgSlackNotificationWikiUrlEndingEditArticle."|edit>",
$prefix."&".$wgSlackNotificationWikiUrlEndingDeleteArticle."|delete>",
$prefix."&".$wgSlackNotificationWikiUrlEndingHistory."|history>"/*,
"move",
"protect",
"watch"*/);
if ($diff)
{
$out .= " | ".$prefix."&".$wgSlackNotificationWikiUrlEndingDiff.$article->getRevisionRecord()->getID()."|diff>)";
}
else
{
$out .= ")";
}
return $out;
}
else
{
return $prefix."|".$article->getTitle()->getFullText().">";
}
}
/**
* Gets nice HTML text for title object containing the link to article page
* and also into edit, delete and article history pages.
*/
static function getSlackTitleText(Title $title)
{
global $wgSlackNotificationWikiUrl, $wgSlackNotificationWikiUrlEnding, $wgSlackNotificationWikiUrlEndingEditArticle,
$wgSlackNotificationWikiUrlEndingDeleteArticle, $wgSlackNotificationWikiUrlEndingHistory,
$wgSlackIncludePageUrls;
$titleName = $title->getFullText();
if ($wgSlackIncludePageUrls)
{
return sprintf(
"%s (%s | %s | %s)",
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$titleName."|".$titleName.">",
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$titleName."&".$wgSlackNotificationWikiUrlEndingEditArticle."|edit>",
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$titleName."&".$wgSlackNotificationWikiUrlEndingDeleteArticle."|delete>",
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$titleName."&".$wgSlackNotificationWikiUrlEndingHistory."|history>"/*,
"move",
"protect",
"watch"*/);
}
else
{
return "<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$titleName."|".$titleName.">";
}
}
/**
* Returns whether the given title should be excluded
*/
private static function titleIsExcluded( $title ) {
global $wgSlackExcludeNotificationsFrom;
if ( is_array( $wgSlackExcludeNotificationsFrom ) && count( $wgSlackExcludeNotificationsFrom ) > 0 ) {
foreach ( $wgSlackExcludeNotificationsFrom as &$currentExclude ) {
if ( 0 === strpos( $title, $currentExclude ) ) return true;
}
}
return false;
}
/**
* Occurs after an article has been created or edited.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/PageSaveComplete
*/
static function slack_article_saved($wikiPage, $user, $summary, $flags, $revisionRecord, $editResult)
{
global $wgSlackNotificationEditedArticle, $wgSlackIgnoreMinorEdits,
$wgSlackNotificationAddedArticle, $wgSlackIncludeDiffSize;
$isNew = (bool)( $flags & EDIT_NEW );
if ( !$wgSlackNotificationEditedArticle && !$isNew ) return true;
if ( !$wgSlackNotificationAddedArticle && $isNew ) return true;
if ( self::titleIsExcluded( $wikiPage->getTitle() ) ) return true;
// Ignore null / empty edits (https://en.wikipedia.org/wiki/Wikipedia:Purge#Null_edit)
if ($editResult->isNullEdit() == 1) return true;
// Do not announce newly added file uploads as articles...
if ( $wikiPage->getTitle()->getNsText() && $wikiPage->getTitle()->getNsText() == 'File' ) return true;
if ( $isNew ) {
$message = sprintf(
"%s has %s article %s %s",
self::getSlackUserText( $user ),
"created",
self::getSlackArticleText( $wikiPage ),
$summary == "" ? "" : "Summary: $summary"
);
if ( $wgSlackIncludeDiffSize ) {
$message .= sprintf(
" (%+d bytes)",
$revisionRecord->getSize());
}
self::push_slack_notify($message, "yellow", $user);
} else {
$isMinor = (bool)( $flags & EDIT_MINOR );
// Skip minor edits if user wanted to ignore them
if ( $isMinor && $wgSlackIgnoreMinorEdits ) return true;
$message = sprintf(
'%s has %s article %s %s',
self::getSlackUserText( $user ),
$isMinor == true ? "made minor edit to" : "edited",
self::getSlackArticleText( $wikiPage, true ),
$summary == "" ? "" : "Summary: $summary"
);
if ( $wgSlackIncludeDiffSize ) {
$message .= sprintf(
" (%+d bytes)",
$revisionRecord->getSize() - MediaWikiServices::getInstance()->getRevisionLookup()->getPreviousRevision($revisionRecord)->getSize());
}
self::push_slack_notify($message, "yellow", $user);
}
return true;
}
/**
* Occurs after the delete article request has been processed.
* @see http://www.mediawiki.org/wiki/Manual:Hooks/ArticleDeleteComplete
*/
static function slack_article_deleted(WikiPage $article, $user, $reason, $id)
{
global $wgSlackNotificationRemovedArticle;
if (!$wgSlackNotificationRemovedArticle) return;
// Discard notifications from excluded pages
global $wgSlackExcludeNotificationsFrom;
if (count($wgSlackExcludeNotificationsFrom) > 0) {
foreach ($wgSlackExcludeNotificationsFrom as &$currentExclude) {
if (0 === strpos($article->getTitle(), $currentExclude)) return;
}
}
// Discard notifications from non-included pages
global $wgSlackIncludeNotificationsFrom;
if (count($wgSlackIncludeNotificationsFrom) > 0) {
foreach ($wgSlackIncludeNotificationsFrom as &$currentInclude) {
if (0 !== strpos($article->getTitle(), $currentInclude)) return;
}
}
$message = sprintf(
"%s has deleted article %s Reason: %s",
self::getSlackUserText($user),
self::getSlackArticleText($article),
$reason);
self::push_slack_notify($message, "red", $user);
return true;
}
/**
* Occurs after a page has been moved.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/TitleMoveComplete
*/
static function slack_article_moved($title, $newtitle, $user, $oldid, $newid, $reason = null)
{
global $wgSlackNotificationMovedArticle;
if (!$wgSlackNotificationMovedArticle) return;
// Discard notifications from excluded pages
global $wgSlackExcludeNotificationsFrom;
if (count($wgSlackExcludeNotificationsFrom) > 0) {
foreach ($wgSlackExcludeNotificationsFrom as &$currentExclude) {
if (0 === strpos($title, $currentExclude)) return;
if (0 === strpos($newtitle, $currentExclude)) return;
}
}
// Discard notifications from non-included pages
global $wgSlackIncludeNotificationsFrom;
if (count($wgSlackIncludeNotificationsFrom) > 0) {
foreach ($wgSlackIncludeNotificationsFrom as &$currentInclude) {
if (0 !== strpos($title, $currentInclude)) return;
if (0 !== strpos($newtitle, $currentInclude)) return;
}
}
$message = sprintf(
"%s has moved article %s to %s. Reason: %s",
self::getSlackUserText($user),
self::getSlackTitleText($title),
self::getSlackTitleText($newtitle),
$reason);
self::push_slack_notify($message, "green", $user);
return true;
}
/**
* Occurs after the protect article request has been processed.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticleProtectComplete
*/
static function slack_article_protected($article, $user, $protect, $reason, $moveonly = false)
{
global $wgSlackNotificationProtectedArticle;
if (!$wgSlackNotificationProtectedArticle) return;
$message = sprintf(
"%s has %s article %s. Reason: %s",
self::getSlackUserText($user),
$protect ? "changed protection of" : "removed protection of",
self::getSlackArticleText($article),
$reason);
self::push_slack_notify($message, "yellow", $user);
return true;
}
/**
* Called after a user account is created.
* @see http://www.mediawiki.org/wiki/Manual:Hooks/AddNewAccount
*/
static function slack_new_user_account($user, $byEmail)
{
global $wgSlackNotificationNewUser, $wgSlackShowNewUserEmail, $wgSlackShowNewUserFullName, $wgSlackShowNewUserIP;
if (!$wgSlackNotificationNewUser) return;
$email = "";
$realname = "";
$ipaddress = "";
try { $email = $user->getEmail(); } catch (Exception $e) {}
try { $realname = $user->getRealName(); } catch (Exception $e) {}
try { $ipaddress = $user->getRequest()->getIP(); } catch (Exception $e) {}
$messageExtra = "";
if ($wgSlackShowNewUserEmail || $wgSlackShowNewUserFullName || $wgSlackShowNewUserIP) {
$messageExtra = "(";
if ($wgSlackShowNewUserEmail) $messageExtra .= $email . ", ";
if ($wgSlackShowNewUserFullName) $messageExtra .= $realname . ", ";
if ($wgSlackShowNewUserIP) $messageExtra .= $ipaddress . ", ";
$messageExtra = substr($messageExtra, 0, -2); // Remove trailing ,
$messageExtra .= ")";
}
$message = sprintf(
"New user account %s was just created %s",
self::getSlackUserText($user),
$messageExtra);
self::push_slack_notify($message, "green", $user);
return true;
}
/**
* Called when a file upload has completed.
* @see http://www.mediawiki.org/wiki/Manual:Hooks/UploadComplete
*/
static function slack_file_uploaded($image)
{
global $wgSlackNotificationFileUpload;
if (!$wgSlackNotificationFileUpload) return;
global $wgSlackNotificationWikiUrl, $wgSlackNotificationWikiUrlEnding, $wgUser;
$message = sprintf(
"%s has uploaded file <%s|%s> (format: %s, size: %s MB, summary: %s)",
self::getSlackUserText($wgUser->mName),
$wgSlackNotificationWikiUrl . $wgSlackNotificationWikiUrlEnding . $image->getLocalFile()->getTitle(),
$image->getLocalFile()->getTitle(),
$image->getLocalFile()->getMimeType(),
round($image->getLocalFile()->size / 1024 / 1024, 3),
$image->getLocalFile()->getDescription());
self::push_slack_notify($message, "green", $wgUser);
return true;
}
/**
* Occurs after the request to block an IP or user has been processed
* @see http://www.mediawiki.org/wiki/Manual:MediaWiki_hooks/BlockIpComplete
*/
static function slack_user_blocked(Block $block, $user)
{
global $wgSlackNotificationBlockedUser;
if (!$wgSlackNotificationBlockedUser) return;
global $wgSlackNotificationWikiUrl, $wgSlackNotificationWikiUrlEnding, $wgSlackNotificationWikiUrlEndingBlockList;
if (defined('MW_VERSION') && version_compare(MW_VERSION, '1.35', '>=')) { //DatabaseBlock::$mReason was made protected in MW 1.35
$mReason = $block->getReasonComment()->text;
} else {
$mReason = $block->mReason;
}
$message = sprintf(
"%s has blocked %s %s Block expiration: %s. %s",
self::getSlackUserText($user),
self::getSlackUserText($block->getTargetName()),
$mReason == "" ? "" : "with reason '".$mReason."'.",
$block->mExpiry,
"<".$wgSlackNotificationWikiUrl.$wgSlackNotificationWikiUrlEnding.$wgSlackNotificationWikiUrlEndingBlockList."|List of all blocks>.");
self::push_slack_notify($message, "red", $user);
return true;
}
/**
* Occurs after the user groups (rights) have been changed
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UserGroupsChanged
*/
static function slack_user_groups_changed(User $user, array $added, array $removed, $performer, $reason, $oldUGMs, $newUGMs)
{
global $wgSlackNotificationUserGroupsChanged;
if (!$wgSlackNotificationUserGroupsChanged) return;
global $wgSlackNotificationWikiUrl, $wgSlackNotificationWikiUrlEnding, $wgSlackNotificationWikiUrlEndingUserRights;
$message = sprintf(
"%s has changed user groups for %s. New groups: %s",
self::getSlackUserText($performer),
self::getSlackUserText($user),
implode(", ", $user->getGroups()));
self::push_slack_notify($message, "green", $user);
return true;
}
/**
* Sends the message into Slack room.
* @param message Message to be sent.
* @param color Background color for the message. One of "green", "yellow" or "red". (default: yellow)
* @see https://api.slack.com/incoming-webhooks
*/
static function push_slack_notify($message, $bgColor, $user)
{
global $wgSlackIncomingWebhookUrl, $wgSlackFromName, $wgSlackRoomName, $wgSlackSendMethod, $wgSlackExcludedPermission, $wgSitename, $wgSlackEmoji, $wgHTTPProxy;
if ( $wgSlackExcludedPermission != "" ) {
if ( $user->isAllowed( $wgSlackExcludedPermission ) )
{
return; // Users with the permission suppress notifications
}
}
$slackColor = "warning";
if ($bgColor == "green") $slackColor = "good";
else if ($bgColor == "red") $slackColor = "danger";
$optionalChannel = "";
if (!empty($wgSlackRoomName)) {
$optionalChannel = ' "channel": "'.$wgSlackRoomName.'", ';
}
// Convert " to ' in the message to be sent as otherwise JSON formatting would break.
$message = str_replace('"', "'", $message);
//Ensure a random \ is not read as the beginning of an escape character. Windows users may be authenticated with a username containing domain\user, so to ensure \ is encoded as \\...
$message = str_replace('\\', "\\\\", $message);
$slackFromName = $wgSlackFromName;
if ( $slackFromName == "" )
{
$slackFromName = $wgSitename;
}
$post = sprintf('payload={"username": "%s",'.$optionalChannel.' "attachments": [ { "text": "%s", "color": "%s" } ]',
urlencode($slackFromName),
urlencode($message),
urlencode($slackColor));
if ( $wgSlackEmoji != "" )
{
$post .= sprintf( ', "icon_emoji": "%s"', $wgSlackEmoji );
}
$post .= '}';
// Use file_get_contents to send the data. Note that you will need to have allow_url_fopen enabled in php.ini for this to work.
if ($wgSlackSendMethod == "file_get_contents") {
$extradata = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $post,
),
);
$context = stream_context_create($extradata);
$result = file_get_contents($wgSlackIncomingWebhookUrl, false, $context);
}
// Call the Slack API through cURL (default way). Note that you will need to have cURL enabled for this to work.
else {
$h = curl_init();
curl_setopt($h, CURLOPT_URL, $wgSlackIncomingWebhookUrl);
curl_setopt($h, CURLOPT_POST, 1);
curl_setopt($h, CURLOPT_POSTFIELDS, $post);
curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
// Commented out lines below. Using default curl settings for host and peer verification.
//curl_setopt ($h, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt ($h, CURLOPT_SSL_VERIFYPEER, 0);
// Set proxy for the request if user had proxy URL set
if ($wgHTTPProxy) {
curl_setopt($h, CURLOPT_PROXY, $wgHTTPProxy);
}
// ... Aaand execute the curl script!
$ok = curl_exec($h);
curl_close($h);
}
}
}
?>