]> git.mxchange.org Git - friendica.git/commitdiff
Check for hashtags when storing them and eventually adding hashtag links
authorMichael Vogel <icarus@dabo.de>
Sun, 1 Mar 2015 16:33:55 +0000 (17:33 +0100)
committerMichael Vogel <icarus@dabo.de>
Sun, 1 Mar 2015 16:33:55 +0000 (17:33 +0100)
include/items.php
include/text.php

index dd4021775380dd2121ceec673dbf97b202e09b81..faeea1eafeaeeb12a9806921c4a76d0638df57fa 100644 (file)
@@ -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("#", "&num;", $match[2])."[/url]");
+               },$item["body"]);
+
+       $item["body"] = preg_replace_callback("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism",
+               function ($match){
+                       return("[bookmark=".$match[1]."]".str_replace("#", "&num;", $match[2])."[/bookmark]");
+               },$item["body"]);
+
+       $item["body"] = preg_replace_callback("/\[attachment (.*)\](.*?)\[\/attachment\]/ism",
+               function ($match){
+                       return("[attachment ".str_replace("#", "&num;", $match[1])."]".$match[2]."[/attachment]");
+               },$item["body"]);
+
+       // Repair recursive urls
+       $item["body"] = preg_replace("/&num;\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
+                       "&num;$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("&num;", "#", $item["body"]);
+}
+
 function get_item_guid($id) {
        $r = q("SELECT `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($id));
        if (count($r))
index c33dd79952d8015dc38583e495ef93c9a89583de..be74756cd447ffe18e8a854cec15ac6b13d17bea 100644 (file)
@@ -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);