From 1191024609ac5262dc5d0acd96b1311df4311c45 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 16 Jan 2024 22:24:52 -0500 Subject: [PATCH] Add throwaway full-text search feature in a dedicated Database class - Add explicit return type to UserDefinedChannels->current() to help IDE auto-completion --- .../Collection/UserDefinedChannels.php | 6 ++ src/Database/DisposableFullTextSearch.php | 59 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/Database/DisposableFullTextSearch.php diff --git a/src/Content/Conversation/Collection/UserDefinedChannels.php b/src/Content/Conversation/Collection/UserDefinedChannels.php index ea5a68f48a..203a67dafe 100644 --- a/src/Content/Conversation/Collection/UserDefinedChannels.php +++ b/src/Content/Conversation/Collection/UserDefinedChannels.php @@ -21,6 +21,12 @@ namespace Friendica\Content\Conversation\Collection; +use Friendica\Content\Conversation\Entity; + class UserDefinedChannels extends Timelines { + public function current(): Entity\UserDefinedChannel + { + return parent::current(); + } } diff --git a/src/Database/DisposableFullTextSearch.php b/src/Database/DisposableFullTextSearch.php new file mode 100644 index 0000000000..9cf6337936 --- /dev/null +++ b/src/Database/DisposableFullTextSearch.php @@ -0,0 +1,59 @@ +. + * + */ + +namespace Friendica\Database; + +/** + * Full-text search on a haystack string that isn't present in the database. + * The haystack is inserted in a temporary table with a FULLTEXT index, then any number of + * matches can be performed on it before the row is deleted when the class instance is destroyed, + * either manually or at the end of the script at the latest. + */ +class DisposableFullTextSearch +{ + private Database $db; + private int $identifier; + + public function __construct(Database $database, string $haystack) + { + $this->db = $database; + + // Maximum value is indicated by the INT UNSIGNED type of the check-full-text-search.pid field + $this->identifier = random_int(0, pow(2, 32) - 1); + + $this->db->insert('check-full-text-search', ['pid' => $this->identifier, 'searchtext' => $haystack], Database::INSERT_UPDATE); + } + + public function __destruct() + { + $this->db->delete('check-full-text-search', ['pid' => $this->identifier]); + } + + /** + * @param string $needle Boolean mode search string + * @return bool + * @throws \Exception + */ + public function match(string $needle): bool + { + return $this->db->exists('check-full-text-search', ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", $this->identifier, $needle]); + } +}