Hashtag handling with Diaspora improved

This commit is contained in:
Michael 2020-04-19 16:33:06 +00:00
parent 98b3058601
commit 538e212a84
4 changed files with 41 additions and 4 deletions

View file

@ -2100,7 +2100,7 @@ class BBCode
$ret = []; $ret = [];
// Convert hashtag links to hashtags // 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 // ignore anything in a code block
$string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string); $string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string);

View file

@ -2610,7 +2610,10 @@ class Item
// This sorting is important when there are hashtags that are part of other hashtags // 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 // 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 = "^\[\]"; $URLSearchString = "^\[\]";

View file

@ -21,7 +21,9 @@
namespace Friendica\Model; namespace Friendica\Model;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Util\Strings; use Friendica\Util\Strings;
@ -87,6 +89,7 @@ class Tag
} else { } else {
// The contact wasn't found in the system (most likely some dead account) // 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 // 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]); 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]; $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)) { if (!preg_match_all("/([" . $tags . "])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism", $body, $result, PREG_SET_ORDER)) {
return; return;
} }
Logger::info('Found tags', ['uri-id' => $uriid, 'hash' => $tags, 'result' => $result]);
foreach ($result as $tag) { foreach ($result as $tag) {
self::storeByHash($uriid, $tag[1], $tag[3], $tag[2]); 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 * Remove tag/mention
* *

View file

@ -1938,7 +1938,7 @@ class Diaspora
$datarray["body"] = self::replacePeopleGuid($body, $person["url"]); $datarray["body"] = self::replacePeopleGuid($body, $person["url"]);
self::storeMentions($datarray['uri-id'], $text); self::storeMentions($datarray['uri-id'], $text);
Tag::storeFromBody($datarray['uri-id'], $datarray["body"], '#'); Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray["body"]);
self::fetchGuid($datarray); self::fetchGuid($datarray);
@ -3015,7 +3015,7 @@ class Diaspora
$datarray["body"] = self::replacePeopleGuid($body, $contact["url"]); $datarray["body"] = self::replacePeopleGuid($body, $contact["url"]);
self::storeMentions($datarray['uri-id'], $text); self::storeMentions($datarray['uri-id'], $text);
Tag::storeFromBody($datarray['uri-id'], $datarray["body"], '#'); Tag::storeRawTagsFromBody($datarray['uri-id'], $datarray["body"]);
if ($provider_display_name != "") { if ($provider_display_name != "") {
$datarray["app"] = $provider_display_name; $datarray["app"] = $provider_display_name;