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

pull/13869/head
Michael 2024-02-02 16:05:24 +00:00
parent fc05daefb5
commit 885b3a12b9
1 changed files with 92 additions and 18 deletions

View File

@ -412,9 +412,32 @@ 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->excludeTags)) {
foreach (explode(',', mb_strtolower($channel->excludeTags)) as $tag) {
$additional .= ' -tag:' . $tag;
}
}
if (!empty($channel->mediaType)) {
$additional .= self::addMediaTerms($channel->mediaType);
}
$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)) { if (!empty($channel->includeTags)) {
$search = explode(',', mb_strtolower($channel->includeTags)); $search = explode(',', mb_strtolower($channel->includeTags));
$placeholders = substr(str_repeat("?, ", count($search)), 0, -2); $placeholders = substr(str_repeat("?, ", count($search)), 0, -2);
@ -427,6 +450,14 @@ class Timeline extends BaseModule
$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)); $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)) {
$condition = DBA::mergeConditions($condition, ["`size` >= ?", $channel->minSize]); $condition = DBA::mergeConditions($condition, ["`size` >= ?", $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]);
} }
// For "addLanguageCondition" to work, the condition must not be empty private function addIncludeTags(string $includeTags): string
$condition = $this->addLanguageCondition($uid, $condition ?: ["true"], $channel->languages); {
$tagterms = '';
foreach (explode(',', mb_strtolower($includeTags)) as $tag) {
$tagterms .= ' tag:' . $tag;
}
return $condition; if ($tagterms) {
return ' +(' . trim($tagterms) . ')';
} else {
return '';
}
}
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) . ")";
} }