]> git.somenet.org - pub/jan/mw_mm_webhook_extension.git/blob - MMNotifyCore.php
ac05afa351fe1c1f8fff8bf3d2cd85cdd2bd335c
[pub/jan/mw_mm_webhook_extension.git] / MMNotifyCore.php
1 <?php
2
3 /**
4  * MMNotify extension
5  *
6  * @file
7  * @ingroup Extensions
8  * @copyright 2019-2020 Someone <someone@somenet.org>
9  * @license MIT
10  */
11
12 class MMNotify{
13     /**
14     * Occurs after a new article has been created.
15     * @see http://www.mediawiki.org/wiki/Manual:Hooks/ArticleInsertComplete
16     */
17     static function onPageContentInsertComplete(WikiPage $wikiPage, $user, $text, $summary, $isminor, $iswatch, $section, $flags, $revision){
18         if (stripos($wikiPage->getTitle()->getText(), 'FSPlenum') === false) return;
19
20         $text = ContentHandler::getContentText($wikiPage->getRevision()->getContent( Revision::RAW));
21         preg_match_all('/^={1,4}[^=].*$/m', $text, $out);
22
23         $post = '\n';
24         foreach ($out[0] as $heading) {
25             if((stripos($heading, 'outcome') !== false))continue;
26
27             $heading = preg_replace('/\s*=+\s*$/', '', $heading);
28             $heading = preg_replace('/^(=+)\s*/', '\1+ ', $heading);
29             $heading = explode ('+ ', $heading, 2);
30
31             $post .= str_replace("=", " ", $heading[0]).'+ '.$heading[1].'\n';
32         }
33
34         $post = wfMessage('mmmotify-onPageContentInsertComplete')->plain().'\n'.$wikiPage->getTitle()->getFullURL().'\n\n----'.self::unindent($post);
35
36         self::send_curl_request('{"text": "'.str_replace('"', '\"', $post).'"}');
37         return true;
38     }
39
40
41     // stolen from Indentation.class.php
42     private static function lines($lines) {
43         if(is_string($lines)) $lines = explode("\n",str_replace("\r\n","\n",$lines));
44         return $lines;
45     }
46
47     // stolen from Indentation.class.php
48     private static function unindent($lines) {
49         $lines = self::lines($lines);
50         if(!$lines) return '';
51
52         $first = array_shift($lines);
53         $min = PHP_INT_MAX;
54
55         foreach($lines as $l) { # find smallest indentation
56             $ind = strlen($l) - strlen(ltrim($l));
57             if($ind < $min)$min = $ind;
58          }
59         foreach($lines as $idx=>$l) $lines[$idx] = substr($l,$min);
60         return trim($first."\n".implode("\n",$lines));
61     }
62
63     private static function send_curl_request($postData) {
64         global $MMIncomingWebhookUrl;
65
66         $h = curl_init();
67         curl_setopt($h, CURLOPT_URL, $MMIncomingWebhookUrl);
68         curl_setopt($h, CURLOPT_POST, 1);
69         curl_setopt($h, CURLOPT_POSTFIELDS, $postData);
70         curl_setopt($h, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: '.strlen($postData)));
71         curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
72         $curl_output = curl_exec($h);
73         curl_close($h);
74     }
75 }