Search for tags and media in full text when doing full text searches

This commit is contained in:
Michael 2024-02-02 16:05:24 +00:00
parent fc05daefb5
commit 885b3a12b9

View File

@ -412,19 +412,50 @@ class Timeline extends BaseModule
} }
if (!empty($channel->fullTextSearch)) { if (!empty($channel->fullTextSearch)) {
$condition = DBA::mergeConditions($condition, ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", Engagement::escapeKeywords($channel->fullTextSearch)]); if (!empty($channel->includeTags)) {
} $additional = self:: addIncludeTags($channel->includeTags);
} else {
$additional = '';
}
if (!empty($channel->includeTags)) { if (!empty($channel->excludeTags)) {
$search = explode(',', mb_strtolower($channel->includeTags)); foreach (explode(',', mb_strtolower($channel->excludeTags)) as $tag) {
$placeholders = substr(str_repeat("?, ", count($search)), 0, -2); $additional .= ' -tag:' . $tag;
$condition = DBA::mergeConditions($condition, array_merge(["`uri-id` IN (SELECT `uri-id` FROM `post-tag` INNER JOIN `tag` ON `tag`.`id` = `post-tag`.`tid` WHERE `post-tag`.`type` = 1 AND `name` IN (" . $placeholders . "))"], $search)); }
} }
if (!empty($channel->excludeTags)) { if (!empty($channel->mediaType)) {
$search = explode(',', mb_strtolower($channel->excludeTags)); $additional .= self::addMediaTerms($channel->mediaType);
$placeholders = substr(str_repeat("?, ", count($search)), 0, -2); }
$condition = DBA::mergeConditions($condition, array_merge(["NOT `uri-id` IN (SELECT `uri-id` FROM `post-tag` INNER JOIN `tag` ON `tag`.`id` = `post-tag`.`tid` WHERE `post-tag`.`type` = 1 AND `name` IN (" . $placeholders . "))"], $search));
$additional .= self::addLanguageSearchTerms($uid, $channel->languages);
if ($additional) {
$searchterms = '+(' . trim($channel->fullTextSearch) . ')' . $additional;
} else {
$searchterms = $channel->fullTextSearch;
}
$condition = DBA::mergeConditions($condition, ["MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", Engagement::escapeKeywords($searchterms)]);
} else {
if (!empty($channel->includeTags)) {
$search = explode(',', mb_strtolower($channel->includeTags));
$placeholders = substr(str_repeat("?, ", count($search)), 0, -2);
$condition = DBA::mergeConditions($condition, array_merge(["`uri-id` IN (SELECT `uri-id` FROM `post-tag` INNER JOIN `tag` ON `tag`.`id` = `post-tag`.`tid` WHERE `post-tag`.`type` = 1 AND `name` IN (" . $placeholders . "))"], $search));
}
if (!empty($channel->excludeTags)) {
$search = explode(',', mb_strtolower($channel->excludeTags));
$placeholders = substr(str_repeat("?, ", count($search)), 0, -2);
$condition = DBA::mergeConditions($condition, array_merge(["NOT `uri-id` IN (SELECT `uri-id` FROM `post-tag` INNER JOIN `tag` ON `tag`.`id` = `post-tag`.`tid` WHERE `post-tag`.`type` = 1 AND `name` IN (" . $placeholders . "))"], $search));
}
if (!empty($channel->mediaType)) {
$condition = DBA::mergeConditions($condition, ["`media-type` & ?", $channel->mediaType]);
}
// For "addLanguageCondition" to work, the condition must not be empty
$condition = $this->addLanguageCondition($uid, $condition ?: ["true"], $channel->languages);
} }
if (!is_null($channel->minSize)) { if (!is_null($channel->minSize)) {
@ -435,24 +466,67 @@ class Timeline extends BaseModule
$condition = DBA::mergeConditions($condition, ["`size` <= ?", $channel->maxSize]); $condition = DBA::mergeConditions($condition, ["`size` <= ?", $channel->maxSize]);
} }
if (!empty($channel->mediaType)) { return $condition;
$condition = DBA::mergeConditions($condition, ["`media-type` & ?", $channel->mediaType]); }
private function addIncludeTags(string $includeTags): string
{
$tagterms = '';
foreach (explode(',', mb_strtolower($includeTags)) as $tag) {
$tagterms .= ' tag:' . $tag;
} }
// For "addLanguageCondition" to work, the condition must not be empty if ($tagterms) {
$condition = $this->addLanguageCondition($uid, $condition ?: ["true"], $channel->languages); return ' +(' . trim($tagterms) . ')';
} else {
return '';
}
}
return $condition; private function addMediaTerms(int $mediaType): string
{
$mediaterms = '';
if ($mediaType & 1) {
$mediaterms .= ' media:image';
}
if ($mediaType & 2) {
$mediaterms .= ' media:video';
}
if ($mediaType & 4) {
$mediaterms .= ' media:audio';
}
if ($mediaterms) {
return ' +(' . trim($mediaterms) . ')';
} else {
return '';
}
}
private function addLanguageSearchTerms(int $uid, $languages = null): string
{
$langterms = '';
foreach ($languages ?: User::getWantedLanguages($uid) as $language) {
$langterms .= ' language:' . $language;
}
if ($langterms) {
return ' +(' . trim($langterms) . ')';
} else {
return '';
}
} }
private function addLanguageCondition(int $uid, array $condition, $languages = null): array private function addLanguageCondition(int $uid, array $condition, $languages = null): array
{ {
$conditions = []; $conditions = [];
$languages = $languages ?: User::getWantedLanguages($uid); foreach ($languages ?: User::getWantedLanguages($uid) as $language) {
foreach ($languages as $language) {
$conditions[] = "`language` = ?"; $conditions[] = "`language` = ?";
$condition[] = $language; $condition[] = $language;
} }
if (!empty($conditions)) { if (!empty($conditions)) {
$condition[0] .= " AND (" . implode(' OR ', $conditions) . ")"; $condition[0] .= " AND (" . implode(' OR ', $conditions) . ")";
} }