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