Replace PHP filtering with pure SQL (#323)

* Replace PHP filtering with pure SQL
This commit is contained in:
Mike Colagrosso 2024-01-21 13:08:46 -07:00 committed by GitHub
parent 67fcdedd08
commit d8360b28ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 43 deletions

View file

@ -834,31 +834,6 @@ class Artwork extends PropertiesBase{
', [$this->ArtworkId]); ', [$this->ArtworkId]);
} }
public function Contains(string $query): bool{
$searchString = $this->Name;
$searchString .= ' ' . $this->Artist->Name;
$searchString .= ' ' . implode(' ', $this->Artist->AlternateNames);
foreach($this->Tags as $tag){
$searchString .= ' ' . $tag->Name;
}
// Remove diacritics and non-alphanumeric characters
$searchString = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($searchString)));
$query = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($query)));
if($query == ''){
return false;
}
if(mb_stripos($searchString, $query) !== false){
return true;
}
return false;
}
// *********** // ***********
// ORM METHODS // ORM METHODS
// *********** // ***********

View file

@ -7,6 +7,7 @@ use function Safe\filesize;
use function Safe\glob; use function Safe\glob;
use function Safe\ksort; use function Safe\ksort;
use function Safe\preg_replace; use function Safe\preg_replace;
use function Safe\preg_split;
use function Safe\shell_exec; use function Safe\shell_exec;
use function Safe\sleep; use function Safe\sleep;
use function Safe\usort; use function Safe\usort;
@ -212,28 +213,34 @@ class Library{
$orderBy = 'art.CompletedYear desc'; $orderBy = 'art.CompletedYear desc';
} }
// Remove diacritics and non-alphanumeric characters
$query = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($query ?? '')));
$tokenizedQuery = '\b(' . implode('|', preg_split('/\b|\s+/', $query, -1, PREG_SPLIT_NO_EMPTY)) . ')\b';
$params[] = $tokenizedQuery; // art.Name
$params[] = $tokenizedQuery; // art.EbookWwwFilesystemPath
$params[] = $tokenizedQuery; // a.Name
$params[] = $tokenizedQuery; // aan.Name
$params[] = $tokenizedQuery; // t.Name
$artworks = Db::Query(' $artworks = Db::Query('
SELECT art.* SELECT art.*
from Artworks art from Artworks art
inner join Artists a using (ArtistId) inner join Artists a using (ArtistId)
where ' . $statusCondition . left join ArtistAlternateNames aan using (ArtistId)
' order by ' . $orderBy, $params, 'Artwork'); left join ArtworkTags at using (ArtworkId)
left join Tags t using (TagId)
where ' . $statusCondition . '
and (art.Name regexp ?
or art.EbookWwwFilesystemPath regexp ?
or a.Name regexp ?
or aan.Name regexp ?
or t.Name regexp ?)
group by art.ArtworkId
order by ' . $orderBy, $params, 'Artwork');
$matches = $artworks; return $artworks;
if($query !== null){
$filteredMatches = [];
foreach($matches as $artwork){
if($artwork->Contains($query)){
$filteredMatches[] = $artwork;
}
}
$matches = $filteredMatches;
}
return $matches;
} }
/** /**