]> git.mxchange.org Git - friendica.git/commitdiff
Validate full search text
authorMichael <heluecht@pirati.ca>
Wed, 10 Jan 2024 20:17:44 +0000 (20:17 +0000)
committerRoland Häder <roland@mxchange.org>
Sun, 28 Jan 2024 15:37:43 +0000 (16:37 +0100)
database.sql
doc/database/db_channel.md
src/Content/Conversation/Entity/Timeline.php
src/Content/Conversation/Factory/UserDefinedChannel.php
src/Content/Conversation/Repository/UserDefinedChannel.php
static/dbstructure.config.php

index ecc295841fa8b09bf4ad4a742cb6bb26bf04ae45..b05696e7da805b6656f4df797a9281c9f4d3c260 100644 (file)
@@ -506,6 +506,7 @@ CREATE TABLE IF NOT EXISTS `channel` (
        `media-type` smallint unsigned COMMENT 'Filtered media types',
        `languages` mediumtext COMMENT 'Desired languages',
        `publish` boolean COMMENT 'publish channel content',
+       `valid` boolean COMMENT 'Set, when the full-text-search is valid',
         PRIMARY KEY(`id`),
         INDEX `uid` (`uid`),
        FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE
index afd65b867c08a41c91186f3617ab33d755ad133e..5b0636dc802464a3fe62f804f0d9bb23d694abbc 100644 (file)
@@ -20,6 +20,7 @@ Fields
 | media-type       | Filtered media types                                                                              | smallint unsigned  | YES  |     | NULL    |                |
 | languages        | Desired languages                                                                                 | mediumtext         | YES  |     | NULL    |                |
 | publish          | publish channel content                                                                           | boolean            | YES  |     | NULL    |                |
+| valid            | Set, when the full-text-search is valid                                                           | boolean            | YES  |     | NULL    |                |
 
 Indexes
 ------------
index 175971a5836738fc8ca925c7290413aac9daed4e..cbb2862eb5bc46e27b734803e433d25faa54fddd 100644 (file)
@@ -64,8 +64,10 @@ class Timeline extends \Friendica\BaseEntity
        protected $languages;
        /** @var bool */
        protected $publish;
+       /** @var bool */
+       protected $valid;
 
-       public function __construct(string $code = null, string $label = null, string $description = null, string $accessKey = null, string $path = null, int $uid = null, string $includeTags = null, string $excludeTags = null, string $fullTextSearch = null, int $mediaType = null, int $circle = null, array $languages = null, bool $publish = null)
+       public function __construct(string $code = null, string $label = null, string $description = null, string $accessKey = null, string $path = null, int $uid = null, string $includeTags = null, string $excludeTags = null, string $fullTextSearch = null, int $mediaType = null, int $circle = null, array $languages = null, bool $publish = null, bool $valid = null)
        {
                $this->code           = $code;
                $this->label          = $label;
@@ -80,5 +82,6 @@ class Timeline extends \Friendica\BaseEntity
                $this->circle         = $circle;
                $this->languages      = $languages;
                $this->publish        = $publish;
+               $this->valid          = $valid;
        }
 }
index d4798cd6c7b5f06f0c14cbd88cd989b032577f7b..08a092205c4c2b2bbc7436e6eec2140cf1adb69b 100644 (file)
@@ -51,6 +51,7 @@ final class UserDefinedChannel extends Timeline implements ICanCreateFromTableRo
                        $row['circle'] ?? null,
                        $row['languages'] ?? null,
                        $row['publish'] ?? null,
+                       $row['valid'] ?? null,
                );
        }
 }
index e7b32255feea16fce10c5abd67576656aa77d33a..bb8ad02f6b71d4346cb8a70cb38e8114b4b66c38 100644 (file)
@@ -134,6 +134,7 @@ class UserDefinedChannel extends \Friendica\BaseRepository
                        'media-type'       => $Channel->mediaType,
                        'languages'        => serialize($Channel->languages),
                        'publish'          => $Channel->publish,
+                       'valid'            => $this->isValid($Channel->fullTextSearch),
                ];
 
                if ($Channel->code) {
@@ -149,6 +150,18 @@ class UserDefinedChannel extends \Friendica\BaseRepository
                return $Channel;
        }
 
+       private function isValid(string $searchtext): bool
+       {
+               if ($searchtext == '') {
+                       return true;
+               }
+
+               $this->db->insert('check-full-text-search', ['pid' => getmypid(), 'searchtext' => $searchtext], Database::INSERT_UPDATE);
+               $result = $this->db->select('check-full-text-search', [], ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", getmypid(), $this->escapeKeywords($searchtext)]);
+               $this->db->delete('check-full-text-search', ['pid' => getmypid()]);
+               return $result !== false;
+       }
+
        /**
         * Checks, if one of the user defined channels matches with the given search text
         * @todo Combine all the full text statements in a single search text to improve the performance.
@@ -214,7 +227,7 @@ class UserDefinedChannel extends \Friendica\BaseRepository
 
                $uids = [];
 
-               $condition = ['uid' => $channelUids];
+               $condition = ['uid' => $channelUids, 'valid' => true];
                if (!$relayMode) {
                        $condition = DBA::mergeConditions($condition, ["`full-text-search` != ?", '']);
                } else {
@@ -298,11 +311,16 @@ class UserDefinedChannel extends \Friendica\BaseRepository
        }
 
        private function inFulltext(string $fullTextSearch): bool
+       {
+               return $this->db->exists('check-full-text-search', ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", getmypid(), $this->escapeKeywords($fullTextSearch)]);
+       }
+
+       private function escapeKeywords(string $fullTextSearch): string
        {
                foreach (Engagement::KEYWORDS as $keyword) {
                        $fullTextSearch = preg_replace('~(' . $keyword . ':.[\w@\.-]+)~', '"$1"', $fullTextSearch);
                }
-               return $this->db->exists('check-full-text-search', ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", getmypid(), $fullTextSearch]);
+               return $fullTextSearch;
        }
 
        private function getUserCondition()
index 80e7e6a6ea118196e9d690cbaca8e6a036e0491f..1b49cac74a8c5274ee6d013da8661f8f7fea0aa0 100644 (file)
@@ -564,6 +564,7 @@ return [
                        "media-type" => ["type" => "smallint unsigned", "comment" => "Filtered media types"],
                        "languages" => ["type" => "mediumtext", "comment" => "Desired languages"],
                        "publish" => ["type" => "boolean", "comment" => "publish channel content"],
+                       "valid" => ["type" => "boolean", "comment" => "Set, when the full-text-search is valid"],
                ],
                "indexes" => [
                        "PRIMARY" => ["id"],