Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.7 KiB

  1. <?php
  2. function create_tags_from_item($itemid) {
  3. global $a;
  4. $profile_base = $a->get_baseurl();
  5. $profile_data = parse_url($profile_base);
  6. $profile_base_friendica = $profile_data['host'].$profile_data['path']."/profile/";
  7. $profile_base_diaspora = $profile_data['host'].$profile_data['path']."/u/";
  8. $searchpath = $a->get_baseurl()."/search?tag=";
  9. $messages = q("SELECT `guid`, `uid`, `id`, `edited`, `deleted`, `title`, `body`, `tag`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
  10. if (!$messages)
  11. return;
  12. $message = $messages[0];
  13. // Clean up all tags
  14. q("DELETE FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d)",
  15. intval(TERM_OBJ_POST),
  16. intval($itemid),
  17. intval(TERM_HASHTAG),
  18. intval(TERM_MENTION));
  19. if ($message["deleted"])
  20. return;
  21. $cachefile = get_cachefile($message["guid"]."-".hash("md5", $message['body']));
  22. if (($cachefile != '') AND !file_exists($cachefile)) {
  23. $s = prepare_text($message['body']);
  24. $stamp1 = microtime(true);
  25. file_put_contents($cachefile, $s);
  26. $a->save_timestamp($stamp1, "file");
  27. logger('create_tags_from_item: put item '.$message["id"].' into cachefile '.$cachefile);
  28. }
  29. $taglist = explode(",", $message["tag"]);
  30. $tags = "";
  31. foreach ($taglist as $tag)
  32. if ((substr(trim($tag), 0, 1) == "#") OR (substr(trim($tag), 0, 1) == "@"))
  33. $tags .= " ".trim($tag);
  34. else
  35. $tags .= " #".trim($tag);
  36. $data = " ".$message["title"]." ".$message["body"]." ".$tags." ";
  37. $tags = array();
  38. $pattern = "/\W\#([^\[].*?)[\s'\".,:;\?!\[\]\/]/ism";
  39. if (preg_match_all($pattern, $data, $matches))
  40. foreach ($matches[1] as $match)
  41. $tags["#".strtolower($match)] = ""; // $searchpath.strtolower($match);
  42. $pattern = "/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism";
  43. if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
  44. foreach ($matches as $match)
  45. $tags[$match[1].strtolower(trim($match[3], ',.:;[]/\"?!'))] = $match[2];
  46. }
  47. foreach ($tags as $tag=>$link) {
  48. if (substr(trim($tag), 0, 1) == "#") {
  49. // try to ignore #039 or #1 or anything like that
  50. if(ctype_digit(substr(trim($tag),1)))
  51. continue;
  52. // try to ignore html hex escapes, e.g. #x2317
  53. if((substr(trim($tag),1,1) == 'x' || substr(trim($tag),1,1) == 'X') && ctype_digit(substr(trim($tag),2)))
  54. continue;
  55. $type = TERM_HASHTAG;
  56. $term = substr($tag, 1);
  57. } elseif (substr(trim($tag), 0, 1) == "@") {
  58. $type = TERM_MENTION;
  59. $term = substr($tag, 1);
  60. } else { // This shouldn't happen
  61. $type = TERM_HASHTAG;
  62. $term = $tag;
  63. }
  64. $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
  65. intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval($type), dbesc($term), dbesc($link));
  66. // Search for mentions
  67. if ((substr($tag, 0, 1) == '@') AND (strpos($link, $profile_base_friendica) OR strpos($link, $profile_base_diaspora))) {
  68. $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
  69. foreach ($users AS $user) {
  70. if ($user["uid"] == $message["uid"]) {
  71. q("UPDATE `item` SET `mention` = 1 WHERE `id` = %d", intval($itemid));
  72. q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($message["parent"]));
  73. }
  74. }
  75. }
  76. }
  77. }
  78. function create_tags_from_itemuri($itemuri, $uid) {
  79. $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
  80. if(count($messages)) {
  81. foreach ($messages as $message)
  82. create_tags_from_item($message["id"]);
  83. }
  84. }
  85. function update_items() {
  86. //$messages = q("SELECT `id` FROM `item` where tag !='' ORDER BY `created` DESC limit 10");
  87. $messages = q("SELECT `id` FROM `item` where tag !=''");
  88. foreach ($messages as $message)
  89. create_tags_from_item($message["id"]);
  90. }
  91. ?>