Validate full search text

This commit is contained in:
Michael 2024-01-10 20:17:44 +00:00
parent b2dd95affa
commit b48467c3f8
6 changed files with 28 additions and 3 deletions

View File

@ -506,6 +506,7 @@ CREATE TABLE IF NOT EXISTS `channel` (
`media-type` smallint unsigned COMMENT 'Filtered media types', `media-type` smallint unsigned COMMENT 'Filtered media types',
`languages` mediumtext COMMENT 'Desired languages', `languages` mediumtext COMMENT 'Desired languages',
`publish` boolean COMMENT 'publish channel content', `publish` boolean COMMENT 'publish channel content',
`valid` boolean COMMENT 'Set, when the full-text-search is valid',
PRIMARY KEY(`id`), PRIMARY KEY(`id`),
INDEX `uid` (`uid`), INDEX `uid` (`uid`),
FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE

View File

@ -20,6 +20,7 @@ Fields
| media-type | Filtered media types | smallint unsigned | YES | | NULL | | | media-type | Filtered media types | smallint unsigned | YES | | NULL | |
| languages | Desired languages | mediumtext | YES | | NULL | | | languages | Desired languages | mediumtext | YES | | NULL | |
| publish | publish channel content | boolean | YES | | NULL | | | publish | publish channel content | boolean | YES | | NULL | |
| valid | Set, when the full-text-search is valid | boolean | YES | | NULL | |
Indexes Indexes
------------ ------------

View File

@ -64,8 +64,10 @@ class Timeline extends \Friendica\BaseEntity
protected $languages; protected $languages;
/** @var bool */ /** @var bool */
protected $publish; 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->code = $code;
$this->label = $label; $this->label = $label;
@ -80,5 +82,6 @@ class Timeline extends \Friendica\BaseEntity
$this->circle = $circle; $this->circle = $circle;
$this->languages = $languages; $this->languages = $languages;
$this->publish = $publish; $this->publish = $publish;
$this->valid = $valid;
} }
} }

View File

@ -51,6 +51,7 @@ final class UserDefinedChannel extends Timeline implements ICanCreateFromTableRo
$row['circle'] ?? null, $row['circle'] ?? null,
$row['languages'] ?? null, $row['languages'] ?? null,
$row['publish'] ?? null, $row['publish'] ?? null,
$row['valid'] ?? null,
); );
} }
} }

View File

@ -134,6 +134,7 @@ class UserDefinedChannel extends \Friendica\BaseRepository
'media-type' => $Channel->mediaType, 'media-type' => $Channel->mediaType,
'languages' => serialize($Channel->languages), 'languages' => serialize($Channel->languages),
'publish' => $Channel->publish, 'publish' => $Channel->publish,
'valid' => $this->isValid($Channel->fullTextSearch),
]; ];
if ($Channel->code) { if ($Channel->code) {
@ -149,6 +150,18 @@ class UserDefinedChannel extends \Friendica\BaseRepository
return $Channel; 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 * 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. * @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 = []; $uids = [];
$condition = ['uid' => $channelUids]; $condition = ['uid' => $channelUids, 'valid' => true];
if (!$relayMode) { if (!$relayMode) {
$condition = DBA::mergeConditions($condition, ["`full-text-search` != ?", '']); $condition = DBA::mergeConditions($condition, ["`full-text-search` != ?", '']);
} else { } else {
@ -298,11 +311,16 @@ class UserDefinedChannel extends \Friendica\BaseRepository
} }
private function inFulltext(string $fullTextSearch): bool 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) { foreach (Engagement::KEYWORDS as $keyword) {
$fullTextSearch = preg_replace('~(' . $keyword . ':.[\w@\.-]+)~', '"$1"', $fullTextSearch); $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() private function getUserCondition()

View File

@ -564,6 +564,7 @@ return [
"media-type" => ["type" => "smallint unsigned", "comment" => "Filtered media types"], "media-type" => ["type" => "smallint unsigned", "comment" => "Filtered media types"],
"languages" => ["type" => "mediumtext", "comment" => "Desired languages"], "languages" => ["type" => "mediumtext", "comment" => "Desired languages"],
"publish" => ["type" => "boolean", "comment" => "publish channel content"], "publish" => ["type" => "boolean", "comment" => "publish channel content"],
"valid" => ["type" => "boolean", "comment" => "Set, when the full-text-search is valid"],
], ],
"indexes" => [ "indexes" => [
"PRIMARY" => ["id"], "PRIMARY" => ["id"],