mirror of
https://github.com/standardebooks/web.git
synced 2025-07-19 04:44:48 -04:00
Refactor functions out of Library
This commit is contained in:
parent
1449148989
commit
b7b63a4be5
24 changed files with 419 additions and 444 deletions
391
lib/Library.php
391
lib/Library.php
|
@ -6,389 +6,8 @@ use function Safe\filemtime;
|
|||
use function Safe\filesize;
|
||||
use function Safe\glob;
|
||||
use function Safe\preg_replace;
|
||||
use function Safe\preg_split;
|
||||
|
||||
class Library{
|
||||
/**
|
||||
* @param array<string> $tags
|
||||
*
|
||||
* @return array{ebooks: array<Ebook>, ebooksCount: int}
|
||||
*/
|
||||
public static function FilterEbooks(string $query = null, array $tags = [], Enums\EbookSortType $sort = null, int $page = 1, int $perPage = EBOOKS_PER_PAGE): array{
|
||||
$limit = $perPage;
|
||||
$offset = (($page - 1) * $perPage);
|
||||
$joinContributors = '';
|
||||
$joinTags = '';
|
||||
$params = [];
|
||||
$whereCondition = 'where true';
|
||||
|
||||
$orderBy = 'e.EbookCreated desc';
|
||||
if($sort == Enums\EbookSortType::AuthorAlpha){
|
||||
$joinContributors = 'inner join Contributors con using (EbookId)';
|
||||
$whereCondition .= ' AND con.MarcRole = "aut"';
|
||||
$orderBy = 'con.SortName, e.EbookCreated desc';
|
||||
}
|
||||
elseif($sort == Enums\EbookSortType::ReadingEase){
|
||||
$orderBy = 'e.ReadingEase desc';
|
||||
}
|
||||
elseif($sort == Enums\EbookSortType::Length){
|
||||
$orderBy = 'e.WordCount';
|
||||
}
|
||||
|
||||
if(sizeof($tags) > 0 && !in_array('all', $tags)){ // 0 tags means "all ebooks"
|
||||
$joinTags = 'inner join EbookTags et using (EbookId)
|
||||
inner join Tags t using (TagId)';
|
||||
$whereCondition .= ' AND t.UrlName in ' . Db::CreateSetSql($tags) . ' ';
|
||||
$params = $tags;
|
||||
}
|
||||
|
||||
if($query !== null && $query != ''){
|
||||
$query = trim(preg_replace('|[^a-zA-Z0-9 ]|ius', ' ', Formatter::RemoveDiacritics($query)));
|
||||
$query = sprintf('"%s"', $query); // Require an exact match via double quotes.
|
||||
$whereCondition .= ' AND match(e.IndexableText) against(? IN BOOLEAN MODE) ';
|
||||
$params[] = $query;
|
||||
}
|
||||
|
||||
$ebooksCount = Db::QueryInt('
|
||||
SELECT count(distinct e.EbookId)
|
||||
from Ebooks e
|
||||
' . $joinContributors . '
|
||||
' . $joinTags . '
|
||||
' . $whereCondition . '
|
||||
', $params);
|
||||
|
||||
$params[] = $limit;
|
||||
$params[] = $offset;
|
||||
|
||||
$ebooks = Db::Query('
|
||||
SELECT distinct e.*
|
||||
from Ebooks e
|
||||
' . $joinContributors . '
|
||||
' . $joinTags . '
|
||||
' . $whereCondition . '
|
||||
order by ' . $orderBy . '
|
||||
limit ?
|
||||
offset ?', $params, Ebook::class);
|
||||
|
||||
return ['ebooks' => $ebooks, 'ebooksCount' => $ebooksCount];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Ebook>
|
||||
*/
|
||||
public static function GetEbooks(): array{
|
||||
// Get all ebooks, unsorted.
|
||||
return Db::Query('
|
||||
SELECT *
|
||||
from Ebooks
|
||||
', [], Ebook::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Ebook>
|
||||
*/
|
||||
public static function GetEbooksByAuthor(string $urlPath): array{
|
||||
if(mb_strpos($urlPath, '_') === false){
|
||||
// Single author
|
||||
return Db::Query('
|
||||
SELECT e.*
|
||||
from Ebooks e
|
||||
inner join Contributors con using (EbookId)
|
||||
where con.MarcRole = "aut"
|
||||
and con.UrlName = ?
|
||||
order by e.EbookCreated desc
|
||||
', [$urlPath], Ebook::class);
|
||||
}
|
||||
else{
|
||||
// Multiple authors, e.g., `karl-marx_friedrich-engels`.
|
||||
$authors = explode('_', $urlPath);
|
||||
|
||||
$params = $authors;
|
||||
$params[] = sizeof($authors); // The number of authors in the URL must match the number of `Contributor` records.
|
||||
|
||||
return Db::Query('
|
||||
SELECT e.*
|
||||
from Ebooks e
|
||||
inner join Contributors con using (EbookId)
|
||||
where con.MarcRole = "aut"
|
||||
and con.UrlName in ' . Db::CreateSetSql($authors) . '
|
||||
group by e.EbookId
|
||||
having count(distinct con.UrlName) = ?
|
||||
order by e.EbookCreated desc
|
||||
', $params, Ebook::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Collection>
|
||||
*
|
||||
* @throws Exceptions\AppException
|
||||
*/
|
||||
public static function GetEbookCollections(): array{
|
||||
$collections = Db::Query('
|
||||
SELECT *
|
||||
from Collections
|
||||
', [], Collection::class);
|
||||
|
||||
$collator = Collator::create('en_US');
|
||||
if($collator === null){
|
||||
throw new Exceptions\AppException('Couldn\'t create collator object when getting collections.');
|
||||
}
|
||||
usort($collections, function(Collection $a, Collection $b) use($collator){
|
||||
$result = $collator->compare($a->GetSortedName(), $b->GetSortedName());
|
||||
return $result === false ? 0 : $result;
|
||||
});
|
||||
return $collections;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Ebook>
|
||||
*/
|
||||
public static function GetEbooksByCollection(string $collection): array{
|
||||
$ebooks = Db::Query('
|
||||
SELECT e.*
|
||||
from Ebooks e
|
||||
inner join CollectionEbooks ce using (EbookId)
|
||||
inner join Collections c using (CollectionId)
|
||||
where c.UrlName = ?
|
||||
order by ce.SequenceNumber, e.EbookCreated desc
|
||||
', [$collection], Ebook::class);
|
||||
|
||||
return $ebooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Ebook>
|
||||
*/
|
||||
public static function GetRelatedEbooks(Ebook $ebook, int $count, ?EbookTag $relatedTag): array{
|
||||
if($relatedTag !== null){
|
||||
$relatedEbooks = Db::Query('
|
||||
SELECT e.*
|
||||
from Ebooks e
|
||||
inner join EbookTags et using (EbookId)
|
||||
where et.TagId = ?
|
||||
and et.EbookId != ?
|
||||
order by RAND()
|
||||
limit ?
|
||||
', [$relatedTag->TagId, $ebook->EbookId, $count], Ebook::class);
|
||||
}
|
||||
else{
|
||||
$relatedEbooks = Db::Query('
|
||||
SELECT *
|
||||
from Ebooks
|
||||
where EbookId != ?
|
||||
order by RAND()
|
||||
limit ?
|
||||
', [$ebook->EbookId, $count], Ebook::class);
|
||||
}
|
||||
|
||||
return $relatedEbooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<EbookTag>
|
||||
*/
|
||||
public static function GetTags(): array{
|
||||
$tags = Db::Query('
|
||||
SELECT *
|
||||
from Tags t
|
||||
where Type = "ebook"
|
||||
order by Name
|
||||
', [], EbookTag::class);
|
||||
|
||||
return $tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{artworks: array<Artwork>, artworksCount: int}
|
||||
*/
|
||||
public static function FilterArtwork(?string $query = null, ?string $status = null, ?Enums\ArtworkSortType $sort = null, ?int $submitterUserId = null, int $page = 1, int $perPage = ARTWORK_PER_PAGE): array{
|
||||
// $status is either the string value of an ArtworkStatus enum, or one of these special statuses:
|
||||
// null: same as "all"
|
||||
// "all": Show all approved and in use artwork
|
||||
// "all-admin": Show all artwork regardless of status
|
||||
// "all-submitter": Show all approved and in use artwork, plus unverified artwork from the submitter
|
||||
// "unverified-submitter": Show unverified artwork from the submitter
|
||||
// "in-use": Show only in-use artwork
|
||||
|
||||
$statusCondition = '';
|
||||
$params = [];
|
||||
|
||||
if($status === null || $status == 'all'){
|
||||
$statusCondition = 'Status = ?';
|
||||
$params[] = Enums\ArtworkStatusType::Approved->value;
|
||||
}
|
||||
elseif($status == 'all-admin'){
|
||||
$statusCondition = 'true';
|
||||
}
|
||||
elseif($status == 'all-submitter' && $submitterUserId !== null){
|
||||
$statusCondition = '(Status = ? or (Status = ? and SubmitterUserId = ?))';
|
||||
$params[] = Enums\ArtworkStatusType::Approved->value;
|
||||
$params[] = Enums\ArtworkStatusType::Unverified->value;
|
||||
$params[] = $submitterUserId;
|
||||
}
|
||||
elseif($status == 'unverified-submitter' && $submitterUserId !== null){
|
||||
$statusCondition = 'Status = ? and SubmitterUserId = ?';
|
||||
$params[] = Enums\ArtworkStatusType::Unverified->value;
|
||||
$params[] = $submitterUserId;
|
||||
}
|
||||
elseif($status == 'in-use'){
|
||||
$statusCondition = 'Status = ? and EbookUrl is not null';
|
||||
$params[] = Enums\ArtworkStatusType::Approved->value;
|
||||
}
|
||||
elseif($status == Enums\ArtworkStatusType::Approved->value){
|
||||
$statusCondition = 'Status = ? and EbookUrl is null';
|
||||
$params[] = Enums\ArtworkStatusType::Approved->value;
|
||||
}
|
||||
else{
|
||||
$statusCondition = 'Status = ?';
|
||||
$params[] = $status;
|
||||
}
|
||||
|
||||
$orderBy = 'art.Created desc';
|
||||
if($sort == Enums\ArtworkSortType::ArtistAlpha){
|
||||
$orderBy = 'a.Name';
|
||||
}
|
||||
elseif($sort == Enums\ArtworkSortType::CompletedNewest){
|
||||
$orderBy = 'art.CompletedYear desc';
|
||||
}
|
||||
|
||||
// Remove diacritics and non-alphanumeric characters, but preserve apostrophes
|
||||
if($query !== null && $query != ''){
|
||||
$query = trim(preg_replace('|[^a-zA-Z0-9\'’ ]|ius', ' ', Formatter::RemoveDiacritics($query)));
|
||||
}
|
||||
else{
|
||||
$query = '';
|
||||
}
|
||||
|
||||
// We use replace() below because if there's multiple contributors separated by an underscore,
|
||||
// the underscore won't count as word boundary and we won't get a match.
|
||||
// See https://github.com/standardebooks/web/pull/325
|
||||
$limit = $perPage;
|
||||
$offset = (($page - 1) * $perPage);
|
||||
|
||||
if($query == ''){
|
||||
$artworksCount = Db::QueryInt('
|
||||
SELECT count(*)
|
||||
from Artworks art
|
||||
where ' . $statusCondition, $params);
|
||||
|
||||
$params[] = $limit;
|
||||
$params[] = $offset;
|
||||
|
||||
$artworks = Db::Query('
|
||||
SELECT art.*
|
||||
from Artworks art
|
||||
inner join Artists a USING (ArtistId)
|
||||
where ' . $statusCondition . '
|
||||
order by ' . $orderBy . '
|
||||
limit ?
|
||||
offset ?', $params, Artwork::class);
|
||||
}
|
||||
else{
|
||||
// Split the query on word boundaries followed by spaces. This keeps words with apostrophes intact.
|
||||
$tokenArray = preg_split('/\b\s+/', $query, -1, PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
// Join the tokens with '|' to search on any token, but add word boundaries to force the full token to match
|
||||
$tokenizedQuery = '\b(' . implode('|', $tokenArray) . ')\b';
|
||||
|
||||
$params[] = $tokenizedQuery; // art.Name
|
||||
$params[] = $tokenizedQuery; // art.EbookUrl
|
||||
$params[] = $tokenizedQuery; // a.Name
|
||||
$params[] = $tokenizedQuery; // aan.Name
|
||||
$params[] = $tokenizedQuery; // t.Name
|
||||
|
||||
$artworksCount = Db::QueryInt('
|
||||
SELECT
|
||||
count(*)
|
||||
from
|
||||
(SELECT distinct
|
||||
ArtworkId
|
||||
from
|
||||
Artworks art
|
||||
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 replace(art.EbookUrl, "_", " ") regexp ?
|
||||
or a.Name regexp ?
|
||||
or aan.Name regexp ?
|
||||
or t.Name regexp ?)
|
||||
group by art.ArtworkId) x', $params);
|
||||
|
||||
$params[] = $limit;
|
||||
$params[] = $offset;
|
||||
|
||||
$artworks = Db::Query('
|
||||
SELECT art.*
|
||||
from Artworks art
|
||||
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 replace(art.EbookUrl, "_", " ") regexp ?
|
||||
or a.Name regexp ?
|
||||
or aan.Name regexp ?
|
||||
or t.Name regexp ?)
|
||||
group by art.ArtworkId
|
||||
order by ' . $orderBy . '
|
||||
limit ?
|
||||
offset ?', $params, Artwork::class);
|
||||
}
|
||||
|
||||
return ['artworks' => $artworks, 'artworksCount' => $artworksCount];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Artwork>
|
||||
*
|
||||
* @throws Exceptions\ArtistNotFoundException
|
||||
*/
|
||||
public static function GetArtworksByArtist(?string $artistUrlName, ?string $status, ?int $submitterUserId): array{
|
||||
if($artistUrlName === null){
|
||||
throw new Exceptions\ArtistNotFoundException();
|
||||
}
|
||||
|
||||
// $status is only one of three special statuses, which are a subset of FilterArtwork() above:
|
||||
// null: same as "all"
|
||||
// "all": Show all approved and in use artwork
|
||||
// "all-admin": Show all artwork regardless of status
|
||||
// "all-submitter": Show all approved and in use artwork, plus unverified artwork from the submitter
|
||||
$statusCondition = '';
|
||||
$params = [];
|
||||
|
||||
if($status == 'all-admin'){
|
||||
$statusCondition = 'true';
|
||||
}
|
||||
elseif($status == 'all-submitter' && $submitterUserId !== null){
|
||||
$statusCondition = '(Status = ? or (Status = ? and SubmitterUserId = ?))';
|
||||
$params[] = Enums\ArtworkStatusType::Approved->value;
|
||||
$params[] = Enums\ArtworkStatusType::Unverified->value;
|
||||
$params[] = $submitterUserId;
|
||||
}
|
||||
else{
|
||||
$statusCondition = 'Status = ?';
|
||||
$params[] = Enums\ArtworkStatusType::Approved->value;
|
||||
}
|
||||
|
||||
$params[] = $artistUrlName; // a.UrlName
|
||||
|
||||
$artworks = Db::Query('
|
||||
SELECT art.*
|
||||
from Artworks art
|
||||
inner join Artists a using (ArtistId)
|
||||
where ' . $statusCondition . '
|
||||
and a.UrlName = ?
|
||||
order by art.Created desc', $params, Artwork::class);
|
||||
|
||||
return $artworks;
|
||||
}
|
||||
|
||||
private static function FillBulkDownloadObject(string $dir, string $downloadType, string $urlRoot): stdClass{
|
||||
$obj = new stdClass();
|
||||
|
||||
|
@ -614,14 +233,4 @@ class Library{
|
|||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<Artist>
|
||||
*/
|
||||
public static function GetArtists(): array{
|
||||
return Db::Query('
|
||||
SELECT *
|
||||
from Artists
|
||||
order by Name asc', [], Artist::class);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue