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.

135 lines
4.1 KiB

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