From 3911c9450b7cee5b3b77f9cfa45e36555745c231 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 24 Sep 2022 17:56:07 +0000 Subject: [PATCH] (Hopefully) SQL improvements --- mod/pubsub.php | 2 +- src/Core/Worker/Cron.php | 2 +- src/Model/Nodeinfo.php | 6 ++-- src/Module/Api/Mastodon/Notifications.php | 4 +-- src/Module/Api/Mastodon/Search.php | 2 +- .../Api/Mastodon/Timelines/PublicTimeline.php | 2 +- src/Module/Contact.php | 9 +++--- src/Module/Conversation/Community.php | 2 +- src/Module/Conversation/Network.php | 2 +- src/Module/Profile/Status.php | 6 ++-- src/Worker/ExpirePosts.php | 32 +++++++++---------- 11 files changed, 34 insertions(+), 35 deletions(-) diff --git a/mod/pubsub.php b/mod/pubsub.php index 918c55ee14..a740b7b81d 100644 --- a/mod/pubsub.php +++ b/mod/pubsub.php @@ -145,7 +145,7 @@ function pubsub_post(App $a) } // We only import feeds from OStatus here - if ($contact['network'] != Protocol::OSTATUS) { + if (!in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::OSTATUS])) { Logger::warning('Unexpected network', ['contact' => $contact]); hub_post_return(); } diff --git a/src/Core/Worker/Cron.php b/src/Core/Worker/Cron.php index 9db954298a..800f0c8f9a 100644 --- a/src/Core/Worker/Cron.php +++ b/src/Core/Worker/Cron.php @@ -189,7 +189,7 @@ class Cron */ private static function addIntros() { - $contacts = DBA::p("SELECT `uid`, `id`, `created` FROM `contact` WHERE `rel` = ? AND `pending` AND NOT EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id`)", Contact::FOLLOWER); + $contacts = DBA::p("SELECT `uid`, `id`, `created` FROM `contact` WHERE `rel` = ? AND `pending` AND NOT `id` IN (SELECT `contact-id` FROM `intro`)", Contact::FOLLOWER); while ($contact = DBA::fetch($contacts)) { $fields = [ 'uid' => $contact['uid'], diff --git a/src/Model/Nodeinfo.php b/src/Model/Nodeinfo.php index 84a14a294d..5ce7f89513 100644 --- a/src/Model/Nodeinfo.php +++ b/src/Model/Nodeinfo.php @@ -61,9 +61,9 @@ class Nodeinfo $logger->info('user statistics', $userStats); - $posts = DBA::count('post-thread', ["EXISTS(SELECT `uri-id` FROM `post-user` WHERE NOT `deleted` AND `origin` AND `uri-id` = `post-thread`.`uri-id`)"]); - $comments = DBA::count('post', ["NOT `deleted` AND `gravity` = ? AND EXISTS(SELECT `uri-id` FROM `post-user` WHERE `origin` AND `uri-id` = `post`.`uri-id`)", GRAVITY_COMMENT]); - $config->set('nodeinfo', 'local_posts', $posts); + $posts = DBA::count('post-thread', ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE NOT `deleted` AND `origin`)"]); + $comments = DBA::count('post', ["NOT `deleted` AND `gravity` = ? AND `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)", GRAVITY_COMMENT]); + $config->set('nodeinfo', 'local_posts', $posts); $config->set('nodeinfo', 'local_comments', $comments); $logger->info('User actitivy', ['posts' => $posts, 'comments' => $comments]); diff --git a/src/Module/Api/Mastodon/Notifications.php b/src/Module/Api/Mastodon/Notifications.php index 490f4f83d5..4ab71d88f9 100644 --- a/src/Module/Api/Mastodon/Notifications.php +++ b/src/Module/Api/Mastodon/Notifications.php @@ -79,13 +79,13 @@ class Notifications extends BaseApi if (in_array(Notification::TYPE_INTRODUCTION, $request['exclude_types'])) { $condition = DBA::mergeConditions($condition, - ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND `pending`))", + ["(`vid` != ? OR `type` != ? OR NOT `actor-id` IN (SELECT `id` FROM `contact` WHERE `pending`))", Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]); } if (in_array(Notification::TYPE_FOLLOW, $request['exclude_types'])) { $condition = DBA::mergeConditions($condition, - ["(`vid` != ? OR `type` != ? OR NOT EXISTS (SELECT `id` FROM `contact` WHERE `id` = `actor-id` AND NOT `pending`))", + ["(`vid` != ? OR `type` != ? OR NOT `actor-id` IN (SELECT `id` FROM `contact` WHERE NOT `pending`))", Verb::getID(Activity::FOLLOW), Post\UserNotification::TYPE_NONE]); } diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index e3f462325e..27f99f00bf 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -180,7 +180,7 @@ class Search extends BaseApi $params = ['order' => ['name'], 'limit' => [$offset, $limit]]; - $condition = ["EXISTS(SELECT `tid` FROM `post-tag` WHERE `type` = ? AND `tid` = `id`) AND `name` LIKE ?", Tag::HASHTAG, $q . '%']; + $condition = ["`id` IN (SELECT `tid` FROM `post-tag` WHERE `type` = ?) AND `name` LIKE ?", Tag::HASHTAG, $q . '%']; $tags = DBA::select('tag', ['name'], $condition, $params); diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index b851974c38..d34deac079 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -92,7 +92,7 @@ class PublicTimeline extends BaseApi if (!empty($uid)) { $condition = DBA::mergeConditions($condition, - ["NOT EXISTS (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `parent-author-id` AND (`blocked` OR `ignored`))", $uid]); + ["NOT `parent-author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`blocked` OR `ignored`))", $uid]); } $items = Post::selectPostsForUser($uid, ['uri-id'], $condition, $params); diff --git a/src/Module/Contact.php b/src/Module/Contact.php index ccc61d3438..05c4e05081 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -210,7 +210,7 @@ class Contact extends BaseModule switch ($type) { case 'blocked': - $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`blocked`)"; + $sql_extra = " AND `id` IN (SELECT `cid` FROM `user-contact` WHERE `user-contact`.`uid` = ? AND `user-contact`.`blocked`)"; // This makes the query look for contact.uid = 0 array_unshift($sql_values, 0); break; @@ -218,7 +218,7 @@ class Contact extends BaseModule $sql_extra = " AND `hidden` AND NOT `blocked` AND NOT `pending`"; break; case 'ignored': - $sql_extra = " AND EXISTS(SELECT `id` from `user-contact` WHERE `contact`.`id` = `user-contact`.`cid` and `user-contact`.`uid` = ? and `user-contact`.`ignored`)"; + $sql_extra = " AND `id` IN (SELECT `cid` FROM `user-contact` WHERE `user-contact`.`uid` = ? AND `user-contact`.`ignored`)"; // This makes the query look for contact.uid = 0 array_unshift($sql_values, 0); break; @@ -227,8 +227,9 @@ class Contact extends BaseModule break; case 'pending': $sql_extra = " AND `pending` AND NOT `archive` AND NOT `failed` AND ((`rel` = ?) - OR EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id` AND NOT `ignore`))"; + OR `id` IN (SELECT `contact-id` FROM `intro` WHERE `intro`.`uid` = ? AND NOT `ignore`))"; $sql_values[] = Model\Contact::SHARING; + $sql_values[] = local_user(); break; default: $sql_extra = " AND NOT `archive` AND NOT `blocked` AND NOT `pending`"; @@ -275,7 +276,7 @@ class Contact extends BaseModule } if ($group) { - $sql_extra .= " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)"; + $sql_extra .= " AND `id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)"; $sql_values[] = $group; } diff --git a/src/Module/Conversation/Community.php b/src/Module/Conversation/Community.php index 16e0f38afd..8134c070e3 100644 --- a/src/Module/Conversation/Community.php +++ b/src/Module/Conversation/Community.php @@ -323,7 +323,7 @@ class Community extends BaseModule $condition[] = $item_id; } else { if (local_user() && !empty($_REQUEST['no_sharer'])) { - $condition[0] .= " AND NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post-thread-user-view`.`uri-id` AND `post-user`.`uid` = ?)"; + $condition[0] .= " AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uid` = ?)"; $condition[] = local_user(); } diff --git a/src/Module/Conversation/Network.php b/src/Module/Conversation/Network.php index 848f36ce3e..c36876b347 100644 --- a/src/Module/Conversation/Network.php +++ b/src/Module/Conversation/Network.php @@ -413,7 +413,7 @@ class Network extends BaseModule $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]); } elseif (self::$forumContactId) { $conditionStrings = DBA::mergeConditions($conditionStrings, - ["((`contact-id` = ?) OR EXISTS(SELECT `uri-id` FROM `post-user-view` WHERE `post-user-view`.`parent-uri-id` = " . DBA::quoteIdentifier($table) . ".`uri-id` AND (`contact-id` = ? AND `gravity` = ? AND `vid` = ? AND `uid` = ?)))", + ["((`contact-id` = ?) OR `uri-id` IN (SELECT `parent-uri-id` FROM `post-user-view` WHERE (`contact-id` = ? AND `gravity` = ? AND `vid` = ? AND `uid` = ?)))", self::$forumContactId, self::$forumContactId, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), local_user()]); } diff --git a/src/Module/Profile/Status.php b/src/Module/Profile/Status.php index f3015692f3..d851d5f474 100644 --- a/src/Module/Profile/Status.php +++ b/src/Module/Profile/Status.php @@ -175,10 +175,8 @@ class Status extends BaseProfile $condition = DBA::mergeConditions($condition, ["((`gravity` = ? AND `wall`) OR (`gravity` = ? AND `vid` = ? AND `origin` - AND EXISTS(SELECT `uri-id` FROM `post` WHERE `gravity` = ? AND `network` IN (?, ?, ?, ?) - AND `uri-id` = `post-user-view`.`thr-parent-id`)))", - GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT, - Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::DIASPORA, Protocol::OSTATUS]); + AND `thr-parent-id` IN (SELECT `uri-id` FROM `post` WHERE `gravity` = ? AND `network` = ?)))", + GRAVITY_PARENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), GRAVITY_PARENT, Protocol::ACTIVITYPUB]); $condition = DBA::mergeConditions($condition, ['uid' => $profile['uid'], 'network' => Protocol::FEDERATED, 'visible' => true, 'deleted' => false]); diff --git a/src/Worker/ExpirePosts.php b/src/Worker/ExpirePosts.php index bbdfaa42c7..7372ddcc88 100644 --- a/src/Worker/ExpirePosts.php +++ b/src/Worker/ExpirePosts.php @@ -179,22 +179,22 @@ class ExpirePosts } Logger::notice('Start collecting orphaned URI-ID', ['last-id' => $item['uri-id']]); $uris = DBA::select('item-uri', ['id'], ["`id` < ? - AND NOT EXISTS(SELECT `uri-id` FROM `post-user` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `parent-uri-id` FROM `post-user` WHERE `parent-uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `thr-parent-id` FROM `post-user` WHERE `thr-parent-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `conversation-id` FROM `post-thread` WHERE `conversation-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `uri-id` FROM `event` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `uri-id` FROM `user-contact` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `uri-id` FROM `contact` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `uri-id` FROM `apcontact` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `uri-id` FROM `fcontact` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `uri-id` FROM `inbox-status` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `uri-id` FROM `post-delivery` WHERE `uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `uri-id` FROM `post-delivery` WHERE `inbox-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `parent-uri-id` FROM `mail` WHERE `parent-uri-id` = `item-uri`.`id`) - AND NOT EXISTS(SELECT `thr-parent-id` FROM `mail` WHERE `thr-parent-id` = `item-uri`.`id`)", $item['uri-id']]); + AND NOT `id` IN (SELECT `uri-id` FROM `post-user`) + AND NOT `id` IN (SELECT `parent-uri-id` FROM `post-user`) + AND NOT `id` IN (SELECT `thr-parent-id` FROM `post-user`) + AND NOT `id` IN (SELECT `external-id` FROM `post-user`) + AND NOT `id` IN (SELECT `conversation-id` FROM `post-thread`) + AND NOT `id` IN (SELECT `uri-id` FROM `mail`) + AND NOT `id` IN (SELECT `uri-id` FROM `event`) + AND NOT `id` IN (SELECT `uri-id` FROM `user-contact`) + AND NOT `id` IN (SELECT `uri-id` FROM `contact`) + AND NOT `id` IN (SELECT `uri-id` FROM `apcontact`) + AND NOT `id` IN (SELECT `uri-id` FROM `fcontact`) + AND NOT `id` IN (SELECT `uri-id` FROM `inbox-status`) + AND NOT `id` IN (SELECT `uri-id` FROM `post-delivery`) + AND NOT `id` IN (SELECT `uri-id` FROM `post-delivery`) + AND NOT `id` IN (SELECT `parent-uri-id` FROM `mail`) + AND NOT `id` IN (SELECT `thr-parent-id` FROM `mail`)", $item['uri-id']]); Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]); $affected_count = 0;