From 60a6dfa23c7a501143ce89d309a44473572dfd79 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 16 Feb 2021 22:04:03 +0000 Subject: [PATCH] Fix several database issues --- database.sql | 9 +++----- mod/editpost.php | 2 +- src/Model/Post.php | 2 +- src/Module/Update/Profile.php | 9 +++++--- src/Worker/RemoveContact.php | 41 ++++++++++++++++++++++++----------- static/dbstructure.config.php | 9 ++++---- static/dbview.config.php | 1 - 7 files changed, 43 insertions(+), 30 deletions(-) diff --git a/database.sql b/database.sql index 3e1c44f99a..db1d90420b 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.03-dev (Red Hot Poker) --- DB_UPDATE_VERSION 1400 +-- DB_UPDATE_VERSION 1401 -- ------------------------------------------ @@ -889,7 +889,7 @@ CREATE TABLE IF NOT EXISTS `notify` ( `msg` mediumtext COMMENT '', `uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'Owner User id', `link` varchar(255) NOT NULL DEFAULT '' COMMENT '', - `iid` int unsigned COMMENT 'item.id', + `iid` int unsigned COMMENT '', `parent` int unsigned COMMENT '', `uri-id` int unsigned COMMENT 'Item-uri id of the related post', `parent-uri-id` int unsigned COMMENT 'Item-uri id of the parent of the related post', @@ -915,17 +915,15 @@ CREATE TABLE IF NOT EXISTS `notify` ( CREATE TABLE IF NOT EXISTS `notify-threads` ( `id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID', `notify-id` int unsigned NOT NULL DEFAULT 0 COMMENT '', - `master-parent-item` int unsigned COMMENT '', + `master-parent-item` int unsigned COMMENT 'Deprecated', `master-parent-uri-id` int unsigned COMMENT 'Item-uri id of the parent of the related post', `parent-item` int unsigned NOT NULL DEFAULT 0 COMMENT '', `receiver-uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'User id', PRIMARY KEY(`id`), - INDEX `master-parent-item` (`master-parent-item`), INDEX `master-parent-uri-id` (`master-parent-uri-id`), INDEX `receiver-uid` (`receiver-uid`), INDEX `notify-id` (`notify-id`), FOREIGN KEY (`notify-id`) REFERENCES `notify` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, - FOREIGN KEY (`master-parent-item`) REFERENCES `item` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, FOREIGN KEY (`master-parent-uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, FOREIGN KEY (`receiver-uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE ) DEFAULT COLLATE utf8mb4_general_ci COMMENT=''; @@ -1748,7 +1746,6 @@ DROP VIEW IF EXISTS `post-thread-view`; CREATE VIEW `post-thread-view` AS SELECT `post-user`.`id` AS `id`, `item`.`id` AS `item-id`, - `post-user`.`id` AS `iid`, `post-user`.`id` AS `post-user-id`, `post-thread-user`.`uid` AS `uid`, `parent-post`.`id` AS `parent`, diff --git a/mod/editpost.php b/mod/editpost.php index 31f121bd54..8ef1520845 100644 --- a/mod/editpost.php +++ b/mod/editpost.php @@ -46,7 +46,7 @@ function editpost_content(App $a) } $fields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', - 'type', 'body', 'title', 'uri-id', 'wall', 'post-type', 'guid']; + 'body', 'title', 'uri-id', 'wall', 'post-type', 'guid']; $item = Post::selectFirstForUser(local_user(), $fields, ['id' => $post_id, 'uid' => local_user()]); diff --git a/src/Model/Post.php b/src/Model/Post.php index ff86dc6f2d..e3114ca909 100644 --- a/src/Model/Post.php +++ b/src/Model/Post.php @@ -224,7 +224,7 @@ class Post $selected = array_merge(Item::DISPLAY_FIELDLIST, Item::ITEM_FIELDLIST); if ($view == 'post-thread-view') { - $selected = array_merge($selected, ['ignored', 'iid']); + $selected = array_merge($selected, ['ignored']); } } diff --git a/src/Module/Update/Profile.php b/src/Module/Update/Profile.php index 93a3752a4f..bab1a63a34 100644 --- a/src/Module/Update/Profile.php +++ b/src/Module/Update/Profile.php @@ -68,9 +68,12 @@ class Profile extends BaseModule $last_updated = $last_updated_array[$last_updated_key] ?? 0; - // If the page user is the owner of the page we should query for unseen - // items. Otherwise use a timestamp of the last succesful update request. - if ($is_owner || !$last_updated) { + if ($_GET['force'] && !empty($_GET['item'])) { + // When the parent is provided, we only fetch this + $sql_extra4 = " AND `parent` = " . intval($_GET['item']); + } elseif ($is_owner || !$last_updated) { + // If the page user is the owner of the page we should query for unseen + // items. Otherwise use a timestamp of the last succesful update request. $sql_extra4 = " AND `unseen`"; } else { $gmupdate = gmdate(DateTimeFormat::MYSQL, $last_updated); diff --git a/src/Worker/RemoveContact.php b/src/Worker/RemoveContact.php index d07780edf1..ecfe498ffc 100644 --- a/src/Worker/RemoveContact.php +++ b/src/Worker/RemoveContact.php @@ -32,6 +32,10 @@ use Friendica\Model\Post; */ class RemoveContact { public static function execute($id) { + if (empty($id)) { + return; + } + // Only delete if the contact is to be deleted $contact = DBA::selectFirst('contact', ['uid'], ['deleted' => true, 'id' => $id]); if (!DBA::isResult($contact)) { @@ -47,20 +51,31 @@ class RemoveContact { } else { $condition = ['uid' => $contact['uid'], 'contact-id' => $id]; } - do { - $items = Post::select(['item-id', 'post-user-id', 'uri-id', 'guid'], $condition, ['limit' => 100]); - while ($item = Post::fetch($items)) { - Logger::info('Delete removed contact item', ['id' => $item['item-id'], 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]); - if (DBStructure::existsTable('item')) { - DBA::delete('item', ['id' => $item['item-id']]); + if (DBStructure::existsTable('item')) { + do { + $items = Post::select(['item-id', 'post-user-id', 'uri-id', 'guid'], $condition, ['limit' => 100]); + while ($item = Post::fetch($items)) { + Logger::info('Delete removed contact item', ['id' => $item['item-id'], 'uri-id' => $item['uri-id'], 'guid' => $item['guid']]); + DBA::delete('item', ['id' => $item['item-id']]); } - Post::delete(['uri-id' => $item['uri-id']]); - Post\ThreadUser::delete(['post-user-id' => $item['post-user-id']]); - Post\Thread::delete(['uri-id' => $item['uri-id']]); - Post\User::delete(['id' => $item['post-user-id']]); - } - DBA::close($items); - } while (Post::exists($condition)); + DBA::close($items); + } while (Post::exists($condition)); + } + + Post\User::delete(['author-id' => $id]); + Post\User::delete(['owner-id' => $id]); + Post\User::delete(['causer-id' => $id]); + Post\User::delete(['contact-id' => $id]); + Post\ThreadUser::delete(['author-id' => $id]); + Post\ThreadUser::delete(['owner-id' => $id]); + Post\ThreadUser::delete(['causer-id' => $id]); + Post\ThreadUser::delete(['contact-id' => $id]); + Post\Thread::delete(['author-id' => $id]); + Post\Thread::delete(['owner-id' => $id]); + Post\Thread::delete(['causer-id' => $id]); + Post::delete(['author-id' => $id]); + Post::delete(['owner-id' => $id]); + Post::delete(['causer-id' => $id]); Photo::delete(['contact-id' => $id]); $ret = DBA::delete('contact', ['id' => $id]); diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index f612621056..d7408c11b6 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1400); + define('DB_UPDATE_VERSION', 1401); } return [ @@ -951,8 +951,8 @@ return [ "msg" => ["type" => "mediumtext", "comment" => ""], "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "foreign" => ["user" => "uid"], "comment" => "Owner User id"], "link" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], - "iid" => ["type" => "int unsigned", "relation" => ["item" => "id"], "comment" => "item.id"], - "parent" => ["type" => "int unsigned", "relation" => ["item" => "id"], "comment" => ""], + "iid" => ["type" => "int unsigned", "comment" => ""], + "parent" => ["type" => "int unsigned", "comment" => ""], "uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Item-uri id of the related post"], "parent-uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Item-uri id of the parent of the related post"], "seen" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], @@ -975,7 +975,7 @@ return [ "fields" => [ "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], "notify-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["notify" => "id"], "comment" => ""], - "master-parent-item" => ["type" => "int unsigned", "foreign" => ["item" => "id"], "comment" => ""], + "master-parent-item" => ["type" => "int unsigned", "comment" => "Deprecated"], "master-parent-uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Item-uri id of the parent of the related post"], "parent-item" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "comment" => ""], "receiver-uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "foreign" => ["user" => "uid"], @@ -983,7 +983,6 @@ return [ ], "indexes" => [ "PRIMARY" => ["id"], - "master-parent-item" => ["master-parent-item"], "master-parent-uri-id" => ["master-parent-uri-id"], "receiver-uid" => ["receiver-uid"], "notify-id" => ["notify-id"], diff --git a/static/dbview.config.php b/static/dbview.config.php index 3751e671c9..fcd04c9682 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -197,7 +197,6 @@ "fields" => [ "id" => ["post-user", "id"], "item-id" => ["item", "id"], - "iid" => ["post-user", "id"], "post-user-id" => ["post-user", "id"], "uid" => ["post-thread-user", "uid"], "parent" => ["parent-post", "id"],