]> git.mxchange.org Git - friendica.git/commitdiff
Add throwaway full-text search feature in a dedicated Database class
authorHypolite Petovan <hypolite@mrpetovan.com>
Wed, 17 Jan 2024 03:24:52 +0000 (22:24 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Wed, 24 Jan 2024 19:42:45 +0000 (14:42 -0500)
- Add explicit return type to UserDefinedChannels->current() to help IDE auto-completion

src/Content/Conversation/Collection/UserDefinedChannels.php
src/Database/DisposableFullTextSearch.php [new file with mode: 0644]

index ea5a68f48a82f00997e11088740dbdd0511a27c7..203a67dafe716b2cdf06c56bf06429d0da33ed2e 100644 (file)
 
 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 (file)
index 0000000..9cf6337
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2024, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+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]);
+       }
+}