-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSlack.hooks.php
149 lines (116 loc) · 4.26 KB
/
Slack.hooks.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
<?php
/**
* Mediawiki Slack integration extension.
* @version 1.0.1
*
* Copyright (C) 2014-2015 George Goldberg <[email protected]>
* @author George Goldberg <[email protected]>
*
* @license MIT
*
* @file The hooks of the Mediawiki Slack integration extension.
* For more information on Slack, see http://www.slack.com.
* @ingroup Extensions
*/
class SlackHooks {
public static function isCreate() {
global $wgSlackIsCreate;
if ($wgSlackIsCreate === true) {
return true;
}
$wgSlackIsCreate = true;
return false;
}
public static function encodeSlackChars($in) {
// This function encodes chars that the Slack API expects to be encoded in the JSON values.
// See https://api.slack.com/docs/formatting for details.
$o = str_replace("&", "&", $in);
$o = str_replace("<", "<", $o);
$o = str_replace(">", ">", $o);
$o = str_replace('"', """, $o);
return $o;
}
public static function sendToSlack($payload) {
global $wgSlackWebhookURL;
wfDebug("Slack URL: ".$wgSlackWebhookURL."\n");
wfDebug("Slack Payload: ".$payload."\n");
$post = "payload=".urlencode($payload);
// POST it to Slack.
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $post,
),
);
$context = stream_context_create($options);
$result = file_get_contents($wgSlackWebhookURL, false, $context);
wfDebug("Slack Result: ".$result."\n");
}
public static function buildMessage($wikiPage, $user, $summary, $verb) {
global $wgSlackLinkUsers;
// Build the message we're going to post to Slack.
$message = '*<'.SlackHooks::encodeSlackChars($wikiPage->getTitle()->getFullURL())
.'|'.SlackHooks::encodeSlackChars($wikiPage->getTitle()).'>* '
.$verb.' by *';
if ($wgSlackLinkUsers) {
$message .= '@';
}
$message .= SlackHooks::encodeSlackChars(strtolower($user->getName())).'*';
if (!empty($summary)) {
$message .= ': '.SlackHooks::encodeSlackChars($summary);
}
$message .= '.';
return $message;
}
public static function buildPayload($message) {
global $wgSlackChannel, $wgSlackLinkUsers, $wgSlackUserName;
// Build the WebHook Payload.
// NB: The Slack parser chokes if there is a trailing , at the end of the list of items
// in the payload. Make sure any optional items are in the middle to avoid this.
$payload = '{"channel": "'.$wgSlackChannel.'",';
if ($wgSlackLinkUsers) {
$payload .= '"link_names": "1",';
}
$payload .= '"username": "'.$wgSlackUserName.'",'
.'"text": "'.$message.'"'
.'}';
return $payload;
}
public static function onPageContentSaveComplete($wikiPage, $user, $content, $summary, $isMinor,
$isWatch, $section, $flags, $revision, $status, $baseRevId) {
global $wgSlackIgnoreMinor;
// If this is a minor edit and we want to ignore minor edits, return now.
if ($wgSlackIgnoreMinor && $isMinor) {
return true;
}
// If this is a page creation, don't notify it as being modified too.
if (true === SlackHooks::isCreate()) {
return true;
}
// Build the Slack Message.
$message = SlackHooks::buildMessage($wikiPage, $user, $summary, "modified");
// Build the Slack Payload.
$payload = SlackHooks::buildPayload($message);
// Send the message to Slack.
SlackHooks::sendToSlack($payload);
return true;
}
public static function onPageContentInsertComplete($wikiPage, $user, $content, $summary,
$isMinor, $isWatch, $section, $flags, $revision) {
global $wgSlackIsCreate, $wgSlackIgnoreMinor;
// If this is a minor edit and we want to ignore minor edits, return now.
if ($wgSlackIgnoreMinor && $isMinor) {
return true;
}
// Flag this as a page creation so we don't notify it's been modified as well.
$wgSlackIsCreate = true;
// Build the Slack Message.
$message = SlackHooks::buildMessage($wikiPage, $user, $summary, "created");
// Build the Slack Payload.
$payload = SlackHooks::buildPayload($message);
// Send the message to Slack.
SlackHooks::sendToSlack($payload);
return true;
}
}