]> git.mxchange.org Git - friendica.git/blob - include/tags.php
Preparations for a new tag structure. Tags are now stored in a dedicated table.
[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(get_config('system','url'));
16 */
17
18 function create_tags_from_item($itemid) {
19         global $a;
20
21         $searchpath = $a->get_baseurl()."/search?tag=";
22
23         $messages = q("SELECT `uri`, `uid`, `id`, `deleted`, `title`, `body`, `tag` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
24
25         if (!$messages)
26                 return;
27
28         $message = $messages[0];
29
30         // Clean up all tags
31         q("DELETE FROM `tag` WHERE `iid` = %d", intval($itemid));
32
33         if ($message["deleted"])
34                 return;
35
36         $taglist = explode(",", $message["tag"]);
37
38         $tags = "";
39         foreach ($taglist as $tag)
40                 if ((substr(trim($tag), 0, 1) == "#") OR (substr(trim($tag), 0, 1) == "@"))
41                         $tags .= " ".trim($tag);
42                 else
43                         $tags .= " #".trim($tag);
44
45         $data = " ".$message["title"]." ".$message["body"]." ".$tags." ";
46
47         $tags = array();
48
49         $pattern = "/\W\#([^\[].*?)[\s'\".,:;\?!\[\]\/]/ism";
50         if (preg_match_all($pattern, $data, $matches))
51                 foreach ($matches[1] as $match)
52                         $tags["#".strtolower($match)] = $searchpath.strtolower($match);
53
54         $pattern = "/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism";
55         if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
56                 foreach ($matches as $match)
57                         $tags[$match[1].strtolower(trim($match[3], ',.:;[]/\"?!'))] = $match[2];
58         }
59
60         foreach ($tags as $tag=>$link)
61                 $r = q("INSERT INTO `tag` (`iid`, `tag`, `link`) VALUES (%d, '%s', '%s')",
62                         intval($itemid), dbesc($tag), dbesc($link));
63 }
64
65 function create_tags_from_itemuri($itemuri, $uid) {
66         $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid));
67
68         foreach ($messages as $message)
69                 create_tags_from_item($message["id"]);
70 }
71
72 function update_items() {
73         $messages = q("SELECT `id` FROM `item` where tag !='' ORDER BY `created` DESC LIMIT 100");
74
75         foreach ($messages as $message)
76                 create_tags_from_item($message["id"]);
77 }
78
79 //update_items();
80 //create_tags_from_item(265194);
81 //create_tags_from_itemuri("infoagent@diasp.org:cce94abd104c06e8", 2);
82 ?>