From 4d565a5b5f3a90706059917d3e0f911088653ff1 Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 22 Apr 2020 04:27:43 +0200 Subject: [PATCH] Dont send out mediawiki syntax, but markdown notifications. --- MMNotifyCore.php | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/MMNotifyCore.php b/MMNotifyCore.php index 5b90428..ac05afa 100644 --- a/MMNotifyCore.php +++ b/MMNotifyCore.php @@ -5,7 +5,7 @@ * * @file * @ingroup Extensions - * @copyright 2019 Someone + * @copyright 2019-2020 Someone * @license MIT */ @@ -17,20 +17,49 @@ class MMNotify{ static function onPageContentInsertComplete(WikiPage $wikiPage, $user, $text, $summary, $isminor, $iswatch, $section, $flags, $revision){ if (stripos($wikiPage->getTitle()->getText(), 'FSPlenum') === false) return; - $post = wfMessage('mmmotify-onPageContentInsertComplete')->plain().'\n'.$wikiPage->getTitle()->getFullURL().'\n\n----\n'; - $text = ContentHandler::getContentText($wikiPage->getRevision()->getContent( Revision::RAW)); - preg_match_all('/^={1,2}[^=].*$/m', $text, $out); + preg_match_all('/^={1,4}[^=].*$/m', $text, $out); + $post = '\n'; foreach ($out[0] as $heading) { - $post .= $heading.'\n'; + if((stripos($heading, 'outcome') !== false))continue; + + $heading = preg_replace('/\s*=+\s*$/', '', $heading); + $heading = preg_replace('/^(=+)\s*/', '\1+ ', $heading); + $heading = explode ('+ ', $heading, 2); + + $post .= str_replace("=", " ", $heading[0]).'+ '.$heading[1].'\n'; } + $post = wfMessage('mmmotify-onPageContentInsertComplete')->plain().'\n'.$wikiPage->getTitle()->getFullURL().'\n\n----'.self::unindent($post); + self::send_curl_request('{"text": "'.str_replace('"', '\"', $post).'"}'); return true; } + // stolen from Indentation.class.php + private static function lines($lines) { + if(is_string($lines)) $lines = explode("\n",str_replace("\r\n","\n",$lines)); + return $lines; + } + + // stolen from Indentation.class.php + private static function unindent($lines) { + $lines = self::lines($lines); + if(!$lines) return ''; + + $first = array_shift($lines); + $min = PHP_INT_MAX; + + foreach($lines as $l) { # find smallest indentation + $ind = strlen($l) - strlen(ltrim($l)); + if($ind < $min)$min = $ind; + } + foreach($lines as $idx=>$l) $lines[$idx] = substr($l,$min); + return trim($first."\n".implode("\n",$lines)); + } + private static function send_curl_request($postData) { global $MMIncomingWebhookUrl; -- 2.43.0