Updated API for mentioned users/quotes
authorAlexander Ebert <ebert@woltlab.com>
Wed, 20 Jul 2016 14:01:50 +0000 (16:01 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 20 Jul 2016 14:02:07 +0000 (16:02 +0200)
wcfsetup/install/files/lib/util/MessageUtil.class.php

index 7f1f234670ea5de804b7aeb37916ffec955eddb1..dd0b3f87243fead177298648b8b903e272b7fb0a 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\util;
 use wcf\system\Callback;
+use wcf\system\html\input\HtmlInputProcessor;
 use wcf\system\Regex;
 
 /**
@@ -43,63 +44,40 @@ class MessageUtil {
        /**
         * Returns the mentioned users in the given text.
         * 
-        * @param       string          $text
-        * @return      string[]
+        * @param       HtmlInputProcessor      $htmlInputProcessor     html input processor instance
+        * @return      string[]                mentioned usernames
         */
-       public static function getMentionedUsers($text) {
-               // remove quotes
-               $newText = $text;
-               if (preg_match_all("~(?:\[quote|\[/quote\])~i", $text, $matches)) {
-                       $newText = '';
-                       $substrings = preg_split("~(?:\[quote|\[/quote\])~i", $text);
-                       
-                       $inQuote = 0;
-                       foreach ($matches[0] as $i => $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;