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.

152 lines
5.0KB

  1. <?php
  2. function create_tags_from_item($itemid) {
  3. $a = get_app();
  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`, `created`, `received`, `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. $taglist = explode(",", $message["tag"]);
  22. $tags = "";
  23. foreach ($taglist as $tag)
  24. if ((substr(trim($tag), 0, 1) == "#") OR (substr(trim($tag), 0, 1) == "@"))
  25. $tags .= " ".trim($tag);
  26. else
  27. $tags .= " #".trim($tag);
  28. $data = " ".$message["title"]." ".$message["body"]." ".$tags." ";
  29. // ignore anything in a code block
  30. $data = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$data);
  31. $tags = array();
  32. $pattern = "/\W\#([^\[].*?)[\s'\".,:;\?!\[\]\/]/ism";
  33. if (preg_match_all($pattern, $data, $matches))
  34. foreach ($matches[1] as $match)
  35. $tags["#".strtolower($match)] = ""; // $searchpath.strtolower($match);
  36. $pattern = "/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism";
  37. if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
  38. foreach ($matches as $match)
  39. $tags[$match[1].strtolower(trim($match[3], ',.:;[]/\"?!'))] = $match[2];
  40. }
  41. foreach ($tags as $tag=>$link) {
  42. if (substr(trim($tag), 0, 1) == "#") {
  43. // try to ignore #039 or #1 or anything like that
  44. if(ctype_digit(substr(trim($tag),1)))
  45. continue;
  46. // try to ignore html hex escapes, e.g. #x2317
  47. if((substr(trim($tag),1,1) == 'x' || substr(trim($tag),1,1) == 'X') && ctype_digit(substr(trim($tag),2)))
  48. continue;
  49. $type = TERM_HASHTAG;
  50. $term = substr($tag, 1);
  51. } elseif (substr(trim($tag), 0, 1) == "@") {
  52. $type = TERM_MENTION;
  53. $term = substr($tag, 1);
  54. } else { // This shouldn't happen
  55. $type = TERM_HASHTAG;
  56. $term = $tag;
  57. }
  58. if ($message["uid"] == 0) {
  59. $global = true;
  60. q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
  61. intval(TERM_OBJ_POST), dbesc($message["guid"]));
  62. } else {
  63. $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
  64. intval(TERM_OBJ_POST), dbesc($message["guid"]));
  65. $global = (count($isglobal) > 0);
  66. }
  67. $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `guid`, `created`, `received`, `global`)
  68. VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', %d)",
  69. intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval($type), dbesc($term),
  70. dbesc($link), dbesc($message["guid"]), dbesc($message["created"]), dbesc($message["received"]), intval($global));
  71. // Search for mentions
  72. if ((substr($tag, 0, 1) == '@') AND (strpos($link, $profile_base_friendica) OR strpos($link, $profile_base_diaspora))) {
  73. $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
  74. foreach ($users AS $user) {
  75. if ($user["uid"] == $message["uid"]) {
  76. q("UPDATE `item` SET `mention` = 1 WHERE `id` = %d", intval($itemid));
  77. q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($message["parent"]));
  78. }
  79. }
  80. }
  81. }
  82. }
  83. function create_tags_from_itemuri($itemuri, $uid) {
  84. $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
  85. if(count($messages)) {
  86. foreach ($messages as $message)
  87. create_tags_from_item($message["id"]);
  88. }
  89. }
  90. function update_items() {
  91. global $db;
  92. $messages = $db->q("SELECT `oid`,`item`.`guid`, `item`.`created`, `item`.`received` FROM `term` INNER JOIN `item` ON `item`.`id`=`term`.`oid` WHERE `term`.`otype` = 1 AND `term`.`guid` = ''", true);
  93. logger("fetched messages: ".count($messages));
  94. while ($message = $db->qfetch()) {
  95. if ($message["uid"] == 0) {
  96. $global = true;
  97. q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
  98. intval(TERM_OBJ_POST), dbesc($message["guid"]));
  99. } else {
  100. $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
  101. intval(TERM_OBJ_POST), dbesc($message["guid"]));
  102. $global = (count($isglobal) > 0);
  103. }
  104. q("UPDATE `term` SET `guid` = '%s', `created` = '%s', `received` = '%s', `global` = %d WHERE `otype` = %d AND `oid` = %d",
  105. dbesc($message["guid"]), dbesc($message["created"]), dbesc($message["received"]),
  106. intval($global), intval(TERM_OBJ_POST), intval($message["oid"]));
  107. }
  108. $db->qclose();
  109. $messages = $db->q("SELECT `guid` FROM `item` WHERE `uid` = 0", true);
  110. logger("fetched messages: ".count($messages));
  111. while ($message = $db->qfetch()) {
  112. q("UPDATE `item` SET `global` = 1 WHERE `guid` = '%s'", dbesc($message["guid"]));
  113. }
  114. $db->qclose();
  115. }
  116. ?>