1
1
Fork 0

Preparations to not store the tags in the item table anymore

This commit is contained in:
Michael 2018-06-30 13:54:01 +00:00
parent a8a189eec4
commit 0ab9f2e265
5 changed files with 67 additions and 32 deletions

View file

@ -987,7 +987,6 @@ CREATE TABLE IF NOT EXISTS `term` (
`created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`received` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', `received` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`global` boolean NOT NULL DEFAULT '0' COMMENT '', `global` boolean NOT NULL DEFAULT '0' COMMENT '',
`aid` int unsigned NOT NULL DEFAULT 0 COMMENT '',
`uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'User id', `uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'User id',
PRIMARY KEY(`tid`), PRIMARY KEY(`tid`),
INDEX `oid_otype_type_term` (`oid`,`otype`,`type`,`term`(32)), INDEX `oid_otype_type_term` (`oid`,`otype`,`type`,`term`(32)),

View file

@ -738,18 +738,13 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
// Only act if it is a "real" post // Only act if it is a "real" post
// We need the additional check for the "local_profile" because of mixed situations on connector networks // We need the additional check for the "local_profile" because of mixed situations on connector networks
$item = q("SELECT `id`, `mention`, `tag`,`parent`, `title`, `body`, `author-id`, `guid`, $fields = ['id', 'mention', 'tag', 'parent', 'title', 'body',
`parent-uri`, `uri`, `contact-id`, `network` 'author-link', 'author-name', 'author-avatar', 'author-id',
FROM `item` WHERE `id` = %d AND `gravity` IN (%d, %d) AND 'guid', 'parent-uri', 'uri', 'contact-id', 'network'];
NOT (`author-id` IN ($contact_list)) LIMIT 1", $condition = ['id' => $itemid, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
intval($itemid), intval(GRAVITY_PARENT), intval(GRAVITY_COMMENT)); $item = Item::selectFirst($fields, $condition);
if (!$item) if (!DBM::is_result($item) || in_array($item['author-id'], $contacts)) {
return false; return;
if ($item[0]['network'] != NETWORK_FEED) {
$author = dba::selectFirst('contact', ['name', 'thumb', 'url'], ['id' => $item[0]['author-id']]);
} else {
$author = dba::selectFirst('contact', ['name', 'thumb', 'url'], ['id' => $item[0]['contact-id']]);
} }
// Generate the notification array // Generate the notification array
@ -759,17 +754,17 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
$params["language"] = $user["language"]; $params["language"] = $user["language"];
$params["to_name"] = $user["username"]; $params["to_name"] = $user["username"];
$params["to_email"] = $user["email"]; $params["to_email"] = $user["email"];
$params["item"] = $item[0]; $params["item"] = $item;
$params["parent"] = $item[0]["parent"]; $params["parent"] = $item["parent"];
$params["link"] = System::baseUrl().'/display/'.urlencode($item[0]["guid"]); $params["link"] = System::baseUrl().'/display/'.urlencode($item["guid"]);
$params["otype"] = 'item'; $params["otype"] = 'item';
$params["source_name"] = $author["name"]; $params["source_name"] = $item["author-name"];
$params["source_link"] = $author["url"]; $params["source_link"] = $item["author-link"];
$params["source_photo"] = $author["thumb"]; $params["source_photo"] = $item["author-avatar"];
if ($item[0]["parent-uri"] === $item[0]["uri"]) { if ($item["parent-uri"] === $item["uri"]) {
// Send a notification for every new post? // Send a notification for every new post?
$send_notification = dba::exists('contact', ['id' => $item[0]['contact-id'], 'notify_new_posts' => true]); $send_notification = dba::exists('contact', ['id' => $item['contact-id'], 'notify_new_posts' => true]);
if (!$send_notification) { if (!$send_notification) {
$tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d", $tags = q("SELECT `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` = %d AND `uid` = %d",
@ -796,11 +791,11 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
$tagged = false; $tagged = false;
foreach ($profiles AS $profile) { foreach ($profiles AS $profile) {
if (strpos($item[0]["tag"], "=".$profile."]") || strpos($item[0]["body"], "=".$profile."]")) if (strpos($item["tag"], "=".$profile."]") || strpos($item["body"], "=".$profile."]"))
$tagged = true; $tagged = true;
} }
if ($item[0]["mention"] || $tagged || ($defaulttype == NOTIFY_TAGSELF)) { if ($item["mention"] || $tagged || ($defaulttype == NOTIFY_TAGSELF)) {
$params["type"] = NOTIFY_TAGSELF; $params["type"] = NOTIFY_TAGSELF;
$params["verb"] = ACTIVITY_TAG; $params["verb"] = ACTIVITY_TAG;
} }
@ -810,7 +805,7 @@ function check_item_notification($itemid, $uid, $defaulttype = "") {
WHERE `thread`.`iid` = %d AND NOT `thread`.`ignored` AND WHERE `thread`.`iid` = %d AND NOT `thread`.`ignored` AND
(`thread`.`mention` OR `item`.`author-id` IN ($contact_list)) (`thread`.`mention` OR `item`.`author-id` IN ($contact_list))
LIMIT 1", LIMIT 1",
intval($item[0]["parent"])); intval($item["parent"]));
if ($parent && !isset($params["type"])) { if ($parent && !isset($params["type"])) {
$params["type"] = NOTIFY_COMMENT; $params["type"] = NOTIFY_COMMENT;

View file

@ -1716,7 +1716,6 @@ class DBStructure
"created" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => ""], "created" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => ""],
"received" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => ""], "received" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => ""],
"global" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], "global" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"aid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "comment" => ""],
"uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "relation" => ["user" => "uid"], "comment" => "User id"], "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "relation" => ["user" => "uid"], "comment" => "User id"],
], ],
"indexes" => [ "indexes" => [

View file

@ -117,6 +117,11 @@ class Item extends BaseObject
} }
} }
// Build the tag string out of the term entries
if (isset($row['id']) && isset($row['tag'])) {
$row['tag'] = Term::tagTextFromItemId($row['id']);
}
// We can always comment on posts from these networks // We can always comment on posts from these networks
if (isset($row['writable']) && !empty($row['network']) && if (isset($row['writable']) && !empty($row['network']) &&
in_array($row['network'], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])) { in_array($row['network'], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])) {
@ -525,6 +530,11 @@ class Item extends BaseObject
*/ */
private static function constructSelectFields($fields, $selected) private static function constructSelectFields($fields, $selected)
{ {
// To be able to fetch the tags we need the item id
if (in_array('tag', $selected) && !in_array('id', $selected)) {
$selected[] = 'id';
}
$selection = []; $selection = [];
foreach ($fields as $table => $table_fields) { foreach ($fields as $table => $table_fields) {
foreach ($table_fields as $field => $select) { foreach ($table_fields as $field => $select) {
@ -622,8 +632,15 @@ class Item extends BaseObject
$content_fields['plink'] = $item['plink']; $content_fields['plink'] = $item['plink'];
} }
self::updateContent($content_fields, ['uri' => $item['uri']]); self::updateContent($content_fields, ['uri' => $item['uri']]);
if (array_key_exists('tag', $fields)) {
Term::insertFromTagFieldByItemId($item['id']); Term::insertFromTagFieldByItemId($item['id']);
}
if (array_key_exists('file', $fields)) {
Term::insertFromFileFieldByItemId($item['id']); Term::insertFromFileFieldByItemId($item['id']);
}
self::updateThread($item['id']); self::updateThread($item['id']);
// We only need to notfiy others when it is an original entry from us. // We only need to notfiy others when it is an original entry from us.
@ -1471,8 +1488,13 @@ class Item extends BaseObject
* Due to deadlock issues with the "term" table we are doing these steps after the commit. * Due to deadlock issues with the "term" table we are doing these steps after the commit.
* This is not perfect - but a workable solution until we found the reason for the problem. * This is not perfect - but a workable solution until we found the reason for the problem.
*/ */
if (array_key_exists('tag', $item)) {
Term::insertFromTagFieldByItemId($current_post); Term::insertFromTagFieldByItemId($current_post);
}
if (array_key_exists('file', $item)) {
Term::insertFromFileFieldByItemId($current_post); Term::insertFromFileFieldByItemId($current_post);
}
if ($item['parent-uri'] === $item['uri']) { if ($item['parent-uri'] === $item['uri']) {
self::addShadow($current_post); self::addShadow($current_post);

View file

@ -15,6 +15,26 @@ require_once 'include/dba.php';
class Term class Term
{ {
public static function tagTextFromItemId($itemid)
{
$tag_text = '';
$condition = ['otype' => TERM_OBJ_POST, 'oid' => $itemid, 'type' => [TERM_HASHTAG, TERM_MENTION]];
$tags = dba::select('term', [], $condition);
while ($tag = dba::fetch($tags)) {
if ($tag_text != '') {
$tag_text .= ',';
}
if ($tag['type'] == 1) {
$tag_text .= '#';
} else {
$tag_text .= '@';
}
$tag_text .= '[url=' . $tag['url'] . ']' . $tag['term'] . '[/url]';
}
return $tag_text;
}
public static function insertFromTagFieldByItemId($itemid) public static function insertFromTagFieldByItemId($itemid)
{ {
$profile_base = System::baseUrl(); $profile_base = System::baseUrl();
@ -57,14 +77,14 @@ class Term
$pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism'; $pattern = '/\W\#([^\[].*?)[\s\'".,:;\?!\[\]\/]/ism';
if (preg_match_all($pattern, $data, $matches)) { if (preg_match_all($pattern, $data, $matches)) {
foreach ($matches[1] as $match) { foreach ($matches[1] as $match) {
$tags['#' . strtolower($match)] = ''; $tags['#' . $match] = '';
} }
} }
$pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism'; $pattern = '/\W([\#@])\[url\=(.*?)\](.*?)\[\/url\]/ism';
if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) { if (preg_match_all($pattern, $data, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) { foreach ($matches as $match) {
$tags[$match[1] . strtolower(trim($match[3], ',.:;[]/\"?!'))] = $match[2]; $tags[$match[1] . trim($match[3], ',.:;[]/\"?!')] = $match[2];
} }
} }
@ -193,7 +213,7 @@ class Term
while ($tag = dba::fetch($taglist)) { while ($tag = dba::fetch($taglist)) {
if ($tag["url"] == "") { if ($tag["url"] == "") {
$tag["url"] = $searchpath . strtolower($tag["term"]); $tag["url"] = $searchpath . $tag["term"];
} }
$orig_tag = $tag["url"]; $orig_tag = $tag["url"];