From ac3a4824df86795cc7d5f08a3014cf84ac4b327c Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 1 Mar 2015 17:33:55 +0100 Subject: [PATCH] Check for hashtags when storing them and eventually adding hashtag links --- include/items.php | 61 +++++++++++++++++++++++++++++++++++++++++++++++ include/text.php | 3 +++ 2 files changed, 64 insertions(+) diff --git a/include/items.php b/include/items.php index dd40217753..faeea1eafe 100644 --- a/include/items.php +++ b/include/items.php @@ -1239,6 +1239,9 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa logger("item_store: Set network to ".$arr["network"]." for ".$arr["uri"], LOGGER_DEBUG); } + // Check for hashtags in the body and repair or add hashtag links + item_body_set_hashtags($arr); + $arr['thr-parent'] = $arr['parent-uri']; if($arr['parent-uri'] === $arr['uri']) { $parent_id = 0; @@ -1556,6 +1559,64 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa return $current_post; } +function item_body_set_hashtags(&$item) { + + $tags = get_tags($item["body"]); + + // No hashtags? + if(!count($tags)) + return(false); + + // 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); + + $a = get_app(); + + $URLSearchString = "^\[\]"; + + // All hashtags should point to the home server + $item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", + "#[url=".$a->get_baseurl()."/search?tag=$2]$2[/url]", $item["body"]); + + $item["tag"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", + "#[url=".$a->get_baseurl()."/search?tag=$2]$2[/url]", $item["tag"]); + + // mask hashtags inside of url, bookmarks and attachments to avoid urls in urls + $item["body"] = preg_replace_callback("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", + function ($match){ + return("[url=".$match[1]."]".str_replace("#", "#", $match[2])."[/url]"); + },$item["body"]); + + $item["body"] = preg_replace_callback("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", + function ($match){ + return("[bookmark=".$match[1]."]".str_replace("#", "#", $match[2])."[/bookmark]"); + },$item["body"]); + + $item["body"] = preg_replace_callback("/\[attachment (.*)\](.*?)\[\/attachment\]/ism", + function ($match){ + return("[attachment ".str_replace("#", "#", $match[1])."]".$match[2]."[/attachment]"); + },$item["body"]); + + // Repair recursive urls + $item["body"] = preg_replace("/#\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", + "#$2", $item["body"]); + + foreach($tags as $tag) { + if(strpos($tag,'#') !== 0) + continue; + + if(strpos($tag,'[url=')) + continue; + + $basetag = str_replace('_',' ',substr($tag,1)); + $item["body"] = str_replace($tag,'#[url='.$a->get_baseurl().'/search?tag='.rawurlencode($basetag).']'.$basetag.'[/url]', $item["body"]); + } + + // Convert back the masked hashtags + $item["body"] = str_replace("#", "#", $item["body"]); +} + function get_item_guid($id) { $r = q("SELECT `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($id)); if (count($r)) diff --git a/include/text.php b/include/text.php index c33dd79952..be74756cd4 100644 --- a/include/text.php +++ b/include/text.php @@ -752,6 +752,9 @@ if(! function_exists('get_tags')) { function get_tags($s) { $ret = array(); + // Convert hashtag links to hashtags + $s = preg_replace("/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism", "#$2", $s); + // ignore anything in a code block $s = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$s); -- 2.39.5