mirror of
https://github.com/standardebooks/web.git
synced 2025-07-15 02:46:46 -04:00
Replace PHP filtering with pure SQL (#323)
* Replace PHP filtering with pure SQL
This commit is contained in:
parent
67fcdedd08
commit
d8360b28ca
2 changed files with 25 additions and 43 deletions
|
@ -834,31 +834,6 @@ class Artwork extends PropertiesBase{
|
|||
', [$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
|
||||
// ***********
|
||||
|
|
|
@ -7,6 +7,7 @@ use function Safe\filesize;
|
|||
use function Safe\glob;
|
||||
use function Safe\ksort;
|
||||
use function Safe\preg_replace;
|
||||
use function Safe\preg_split;
|
||||
use function Safe\shell_exec;
|
||||
use function Safe\sleep;
|
||||
use function Safe\usort;
|
||||
|
@ -212,28 +213,34 @@ class Library{
|
|||
$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('
|
||||
SELECT art.*
|
||||
from Artworks art
|
||||
inner join Artists a using (ArtistId)
|
||||
where ' . $statusCondition .
|
||||
' order by ' . $orderBy, $params, 'Artwork');
|
||||
inner join Artists a using (ArtistId)
|
||||
left join ArtistAlternateNames aan using (ArtistId)
|
||||
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;
|
||||
|
||||
if($query !== null){
|
||||
$filteredMatches = [];
|
||||
|
||||
foreach($matches as $artwork){
|
||||
if($artwork->Contains($query)){
|
||||
$filteredMatches[] = $artwork;
|
||||
}
|
||||
}
|
||||
|
||||
$matches = $filteredMatches;
|
||||
}
|
||||
|
||||
return $matches;
|
||||
return $artworks;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue