]> git.mxchange.org Git - friendica.git/commitdiff
Hashtag handling with Diaspora improved
authorMichael <heluecht@pirati.ca>
Sun, 19 Apr 2020 16:33:06 +0000 (16:33 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 19 Apr 2020 16:33:06 +0000 (16:33 +0000)
src/Content/Text/BBCode.php
src/Model/Item.php
src/Model/Tag.php
src/Protocol/Diaspora.php

index 334efb32a78dd4f7fc904b78320d20edf542b859..601bf63b25e23dc2ee1250fbe7473da736052424 100644 (file)
@@ -2100,7 +2100,7 @@ class BBCode
                $ret = [];
 
                // Convert hashtag links to hashtags
-               $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
+               $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2 ', $string);
 
                // ignore anything in a code block
                $string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string);
index 1f2faa2698293cb9f97804f56947d2fa0a5b8213..0e82189298b7a53412f7bed20fef5c5c945f21bf 100644 (file)
@@ -2610,7 +2610,10 @@ class Item
 
                // This sorting is important when there are hashtags that are part of other hashtags
                // Otherwise there could be problems with hashtags like #test and #test2
-               rsort($tags);
+               // Because of this we are sorting from the longest to the shortest tag.
+               usort($rawtags, function($a, $b) {
+                       return strlen($b) <=> strlen($a);
+               });
 
                $URLSearchString = "^\[\]";
 
index bd1945c4801dbc65908d3b0a75ebb453ff02af09..ac2600903391c1cd275bfa0ff417e5f5278862cf 100644 (file)
@@ -21,7 +21,9 @@
 
 namespace Friendica\Model;
 
+use Friendica\Content\Text\BBCode;
 use Friendica\Core\Logger;
+use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Util\Strings;
 
@@ -87,6 +89,7 @@ class Tag
                        } else {
                                // The contact wasn't found in the system (most likely some dead account)
                                // We ensure that we only store a single entry by overwriting the previous name
+                               Logger::info('Update tag', ['url' => $url, 'name' => $name]);
                                DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
                        }
                }
@@ -148,15 +151,46 @@ class Tag
                        $tags =  self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION];
                }
 
+               Logger::info('Check for tags', ['uri-id' => $uriid, 'hash' => $tags, 'callstack' => System::callstack()]);
+
                if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
                        return;
                }
 
+               Logger::info('Found tags', ['uri-id' => $uriid, 'hash' => $tags, 'result' => $result]);
+
                foreach ($result as $tag) {
                        self::storeByHash($uriid, $tag[1], $tag[3], $tag[2]);
                }
        }
 
+       /**
+        * Store raw tags (not encapsulated in links) from the body
+        * This function is needed in the intermediate phase.
+        * Later we can call item::setHashtags in advance to have all tags converted.
+        * 
+        * @param integer $uriid URI-Id
+        * @param string  $body   Body of the post
+        */
+       public static function storeRawTagsFromBody(int $uriid, string $body)
+       {
+               Logger::info('Check for tags', ['uri-id' => $uriid, 'callstack' => System::callstack()]);
+
+               $result = BBCode::getTags($body);
+               if (empty($result)) {
+                       return;
+               }
+
+               Logger::info('Found tags', ['uri-id' => $uriid, 'result' => $result]);
+
+               foreach ($result as $tag) {
+                       if (substr($tag, 0, 1) != self::TAG_CHARACTER[self::HASHTAG]) {
+                               continue;
+                       }
+                       self::storeByHash($uriid, substr($tag, 0, 1), substr($tag, 1));
+               }
+       }
+
        /**
         * Remove tag/mention
         *
index d7e8f60a2e5acb8567720648b7b3da7977bb3a3d..513cd0bcc038e0b3d92b13ac3a49f9f599b251ab 100644 (file)
@@ -1938,7 +1938,7 @@ class Diaspora
                $datarray["body"] = self::replacePeopleGuid($body, $person["url"]);
 
                self::storeMentions($datarray['uri-id'], $text);
-               Tag::storeFromBody($datarray['uri-id'], $datarray["body"], '#');
+               Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray["body"]);
 
                self::fetchGuid($datarray);
 
@@ -3015,7 +3015,7 @@ class Diaspora
                $datarray["body"] = self::replacePeopleGuid($body, $contact["url"]);
 
                self::storeMentions($datarray['uri-id'], $text);
-               Tag::storeFromBody($datarray['uri-id'], $datarray["body"], '#');
+               Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray["body"]);
 
                if ($provider_display_name != "") {
                        $datarray["app"] = $provider_display_name;