From 657e0c2307397c61805517d8340bb11d48cebf37 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Wed, 20 Jul 2016 16:01:50 +0200 Subject: [PATCH] Updated API for mentioned users/quotes --- .../files/lib/util/MessageUtil.class.php | 70 +++++++------------ 1 file changed, 24 insertions(+), 46 deletions(-) diff --git a/wcfsetup/install/files/lib/util/MessageUtil.class.php b/wcfsetup/install/files/lib/util/MessageUtil.class.php index 7f1f234670..dd0b3f8724 100644 --- a/wcfsetup/install/files/lib/util/MessageUtil.class.php +++ b/wcfsetup/install/files/lib/util/MessageUtil.class.php @@ -1,6 +1,7 @@ $tag) { - if (!$inQuote) { - $newText .= $substrings[$i]; - } - - if ($tag == '[quote') { - $inQuote++; - } - else { - $inQuote--; - } + public static function getMentionedUsers(HtmlInputProcessor $htmlInputProcessor) { + $usernames = []; + + $elements = $htmlInputProcessor->getHtmlInputNodeProcessor()->getDocument()->getElementsByTagName('woltlab-mention'); + /** @var \DOMElement $element */ + foreach ($elements as $element) { + if (DOMUtil::hasParent($element, 'blockquote')) { + // ignore mentions within quotes + continue; } - if (!$inQuote) $newText .= $substrings[count($substrings) - 1]; - } - - // check for mentions - if (preg_match_all("~\[url='[^']+'\]@(.+?)\[/url\]~", $newText, $matches)) { - return $matches[1]; + $usernames[] = $element->getAttribute('data-username'); } - return []; + return $usernames; } /** * Returns the quoted users in the given text. - * - * @param string $text - * @return string[] + * + * @param HtmlInputProcessor $htmlInputProcessor html input processor instance + * @return string[] quoted usernames */ - public static function getQuotedUsers($text) { + public static function getQuotedUsers(HtmlInputProcessor $htmlInputProcessor) { $usernames = []; - if (preg_match_all("~(?:\[(quote)=(?:')?(.+?)(?:')?(?:,[^\]]*)?\]|\[/quote\])~i", $text, $matches)) { - $level = 0; - - foreach ($matches[1] as $i => $tag) { - if ($tag == 'quote') { - if (!$level) { - $usernames[] = $matches[2][$i]; - } - $level++; - } - else { - $level--; - } - } + + $elements = $htmlInputProcessor->getHtmlInputNodeProcessor()->getDocument()->getElementsByTagName('blockquote'); + /** @var \DOMElement $element */ + foreach ($elements as $element) { + $username = $element->getAttribute('data-author'); + if (!empty($username)) $usernames[] = $username; } return $usernames; -- 2.20.1