From f9fedfb131769b792365e9e9003dade6d912ff1d Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Tue, 7 Dec 2021 20:25:28 +0000 Subject: [PATCH] [COMPONENT][Search] Fix search --- components/Search/Controller/Search.php | 12 +++++++++--- components/Search/Util/Parser.php | 6 +++--- components/Tag/Tag.php | 21 ++++++++++----------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/components/Search/Controller/Search.php b/components/Search/Controller/Search.php index f49ed3bce8..60c9d94687 100644 --- a/components/Search/Controller/Search.php +++ b/components/Search/Controller/Search.php @@ -26,20 +26,26 @@ namespace Component\Search\Controller; use App\Core\Controller; use App\Core\DB\DB; use App\Core\Event; +use App\Util\Common; use Component\Search\Util\Parser; use Symfony\Component\HttpFoundation\Request; class Search extends Controller { + /** + * Handle a search query + */ public function handle(Request $request) { + $actor = Common::actor(); + $language = !\is_null($actor) ? $actor->getTopLanguage()->getLocale() : null; $q = $this->string('q'); - [$note_criteria, $actor_criteria] = Parser::parse($q); + [$note_criteria, $actor_criteria] = Parser::parse($q, $language); $note_qb = DB::createQueryBuilder(); $actor_qb = DB::createQueryBuilder(); - $note_qb->select('note')->from('App\Entity\Note', 'note'); - $actor_qb->select('actor')->from('App\Entity\Actor', 'actor'); + $note_qb->select('note')->from('App\Entity\Note', 'note')->orderBy('note.created', 'DESC'); + $actor_qb->select('actor')->from('App\Entity\Actor', 'actor')->orderBy('actor.created', 'DESC'); Event::handle('SearchQueryAddJoins', [&$note_qb, &$actor_qb]); $notes = $actors = []; if (!\is_null($note_criteria)) { diff --git a/components/Search/Util/Parser.php b/components/Search/Util/Parser.php index 88a35decbd..d4c1f8446a 100644 --- a/components/Search/Util/Parser.php +++ b/components/Search/Util/Parser.php @@ -53,7 +53,7 @@ abstract class Parser * * @return Criteria[] */ - public static function parse(string $input, int $level = 0): array + public static function parse(string $input, ?string $language = null, int $level = 0): array { if ($level === 0) { $input = trim(preg_replace(['/\s+/', '/\s+AND\s+/', '/\s+OR\s+/'], [' ', '&', '|'], $input), ' |&'); @@ -77,8 +77,8 @@ abstract class Parser if ($input[$index] === $delimiter || $end = ($index === $lenght - 1)) { $term = mb_substr($input, $left, $end ? null : $right - $left); $note_res = $actor_res = null; - $ret = Event::handle('SearchCreateExpression', [$eb, $term, &$note_res, &$actor_res]); - if ((\is_null($note_res) && \is_null($actor_res)) || $ret == Event::next) { + $ret = Event::handle('SearchCreateExpression', [$eb, $term, $language, &$note_res, &$actor_res]); + if (\is_null($note_res) && \is_null($actor_res)) { throw new ServerException("No one claimed responsibility for a match term: {$term}"); } elseif (!\is_null($note_res)) { $note_parts[] = $note_res; diff --git a/components/Tag/Tag.php b/components/Tag/Tag.php index 63b5d64013..56edfb44bc 100644 --- a/components/Tag/Tag.php +++ b/components/Tag/Tag.php @@ -150,21 +150,20 @@ class Tag extends Component * * $term /^(note|tag|people|actor)/ means we want to match only either a note or an actor */ - public function onSearchCreateExpression(ExpressionBuilder $eb, string $term, &$note_expr, &$actor_expr): bool + public function onSearchCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, &$note_expr, &$actor_expr): bool { - $search_term = str_contains($term, ':#') ? explode(':', $term)[1] : $term; - $temp_note_expr = $eb->eq('note_tag.tag', $search_term); - $temp_actor_expr = $eb->eq('actor_tag.tag', $search_term); + $search_term = str_contains($term, ':#') ? explode(':', $term)[1] : $term; + $canon_search_term = self::canonicalTag($search_term, $language); + $temp_note_expr = $eb->eq('note_tag.canonical', $canon_search_term); + $temp_actor_expr = $eb->eq('actor_tag.canonical', $canon_search_term); if (Formatting::startsWith($term, ['note', 'tag'])) { $note_expr = $temp_note_expr; + } elseif (Formatting::startsWith($term, ['people', 'actor'])) { + $actor_expr = $temp_actor_expr; } else { - if (Formatting::startsWith($term, ['people', 'actor'])) { - $actor_expr = $temp_actor_expr; - } else { - $note_expr = $temp_note_expr; - $actor_expr = $temp_actor_expr; - return Event::next; - } + $note_expr = $temp_note_expr; + $actor_expr = $temp_actor_expr; + return Event::next; } return Event::stop; }