Merge pull request 'Fix #135: make the flag filter work' (#143) from fix-flag-filter into config

Reviewed-on: leftypol/leftypol#143
This commit is contained in:
Zankaria 2025-07-23 16:49:04 -05:00
commit 00a4435a61
2 changed files with 6 additions and 4 deletions

View file

@ -220,7 +220,7 @@ class UserPostQueries {
for ($i = 0; $i < \count($flags); $i++) {
// Yes, vichan stores the flag inside the generated HTML. Now you know why it's slow as shit.
// English lacks the words to express my feelings about it in a satisfying manner.
$flag_acc[] = "CONCAT('%<tinyboard>', :flag$i, '</tinyboard>%')";
$flag_acc[] = "CONCAT('%<tinyboard flag alt>', :flag$i, '</tinyboard>%')";
}
$where_acc[] = 'body_nomarkup LIKE (' . \implode(' OR ', $flag_acc) . ')';
}

View file

@ -138,7 +138,7 @@ class SearchService {
private static function matchStrings(array $strings, array $fragments): array {
return \array_filter($strings, function ($str) use ($fragments) {
// Saves the last position. We use this to ensure the fragments are one after the other.
$last_ret = 0;
$last_ret = -1;
foreach ($fragments as $fragment) {
if ($last_ret + 1 > \strlen($fragment)) {
// Cannot possibly match.
@ -147,7 +147,7 @@ class SearchService {
$last_ret = \stripos($str, $fragment, $last_ret + 1);
if ($last_ret === false) {
// Exclude flags that don't much even a single fragment.
// Exclude flags that don't match even a single fragment.
return false;
}
}
@ -383,7 +383,9 @@ class SearchService {
$flags = [];
if (!empty($filters->flag) && !empty($this->flag_map)) {
$flags = $this->matchStrings($this->flag_map, $filters->flag);
// A double array_values is necessary in order to re-index the array, otherwise it's left with random indexes.
$reverse_flags = \array_values(\array_values($this->flag_map));
$flags = $this->matchStrings($reverse_flags, $filters->flag);
if (empty($flags)) {
// The query doesn't match any flags so it will always fail anyway.
return [];