From acb6b2949f1543e4d3a7193699732cbb7ac43542 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Mon, 13 May 2024 13:44:04 -0500 Subject: [PATCH] Use static class names instead of strings when getting objects from the DB --- lib/Artist.php | 5 +++-- lib/Artwork.php | 6 +++--- lib/ArtworkTag.php | 2 +- lib/HttpInput.php | 2 +- lib/Library.php | 11 +++++------ lib/Museum.php | 2 +- lib/NewsletterSubscription.php | 2 +- lib/Patron.php | 4 ++-- lib/Poll.php | 6 +++--- lib/PollItem.php | 2 +- lib/PollVote.php | 2 +- lib/Session.php | 4 ++-- lib/User.php | 10 +++++----- scripts/update-patrons-circle | 4 ++-- www/artworks/index.php | 4 +++- www/polls/index.php | 4 ++-- 16 files changed, 36 insertions(+), 34 deletions(-) diff --git a/lib/Artist.php b/lib/Artist.php index a276826d..e177fd2f 100644 --- a/lib/Artist.php +++ b/lib/Artist.php @@ -130,7 +130,7 @@ class Artist{ SELECT * from Artists where ArtistId = ? - ', [$artistId], 'Artist'); + ', [$artistId], Artist::class); return $result[0] ?? throw new Exceptions\ArtistNotFoundException();; } @@ -161,7 +161,7 @@ class Artist{ where a.UrlName = ? or aan.UrlName = ? limit 1 - ', [$artist->UrlName, $artist->UrlName], 'Artist'); + ', [$artist->UrlName, $artist->UrlName], Artist::class); if(isset($result[0])){ return $result[0]; @@ -178,6 +178,7 @@ class Artist{ from Artists where ArtistId = ? ', [$this->ArtistId]); + Db::Query(' DELETE from ArtistAlternateNames diff --git a/lib/Artwork.php b/lib/Artwork.php index c5efb7b5..e6982da9 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -158,7 +158,7 @@ class Artwork{ from Tags t inner join ArtworkTags at using (TagId) where ArtworkId = ? - ', [$this->ArtworkId], 'ArtworkTag'); + ', [$this->ArtworkId], ArtworkTag::class); } return $this->_Tags; @@ -849,7 +849,7 @@ class Artwork{ SELECT * from Artworks where ArtworkId = ? - ', [$artworkId], 'Artwork'); + ', [$artworkId], Artwork::class); return $result[0] ?? throw new Exceptions\ArtworkNotFoundException(); } @@ -867,7 +867,7 @@ class Artwork{ from Artworks inner join Artists using (ArtistId) where Artists.UrlName = ? and Artworks.UrlName = ? - ', [$artistUrlName, $artworkUrlName], 'Artwork'); + ', [$artistUrlName, $artworkUrlName], Artwork::class); return $result[0] ?? throw new Exceptions\ArtworkNotFoundException(); } diff --git a/lib/ArtworkTag.php b/lib/ArtworkTag.php index 3605f2b9..d756b671 100644 --- a/lib/ArtworkTag.php +++ b/lib/ArtworkTag.php @@ -67,7 +67,7 @@ class ArtworkTag extends Tag{ SELECT * from Tags where Name = ? - ', [$artworkTag->Name], 'ArtworkTag'); + ', [$artworkTag->Name], ArtworkTag::class); if(isset($result[0])){ return $result[0]; diff --git a/lib/HttpInput.php b/lib/HttpInput.php index cd35e42b..e60340b2 100644 --- a/lib/HttpInput.php +++ b/lib/HttpInput.php @@ -48,7 +48,7 @@ class HttpInput{ } /** - * @return int The maximum size for an HTTP POST request, in bytes + * @return int The maximum size for an HTTP POST request, in bytes. */ public static function GetMaxPostSize(): int{ $post_max_size = ini_get('post_max_size'); diff --git a/lib/Library.php b/lib/Library.php index 5bfa8e3e..1b7ba7bb 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -171,7 +171,7 @@ class Library{ * @param string $query * @param string $status * @param ArtworkSort $sort - * @return Array + * @return array|int> */ public static function FilterArtwork(string $query = null, string $status = null, ArtworkSort $sort = null, int $submitterUserId = null, int $page = 1, int $perPage = ARTWORK_PER_PAGE): array{ // Returns an array of: @@ -253,7 +253,7 @@ class Library{ where ' . $statusCondition . ' order by ' . $orderBy . ' limit ? - offset ?', $params, 'Artwork'); + offset ?', $params, Artwork::class); } else{ // Split the query on word boundaries followed by spaces. This keeps words with apostrophes intact. @@ -308,13 +308,12 @@ class Library{ group by art.ArtworkId order by ' . $orderBy . ' limit ? - offset ?', $params, 'Artwork'); + offset ?', $params, Artwork::class); } return ['artworks' => $artworks, 'artworksCount' => $artworksCount]; } - /** * @return array * @throws Exceptions\ArtistNotFoundException @@ -354,7 +353,7 @@ class Library{ inner join Artists a using (ArtistId) where ' . $statusCondition . ' and a.UrlName = ? - order by art.Created desc', $params, 'Artwork'); + order by art.Created desc', $params, Artwork::class); return $artworks; } @@ -819,6 +818,6 @@ class Library{ return Db::Query(' SELECT * from Artists - order by Name asc', [], 'Artist'); + order by Name asc', [], Artist::class); } } diff --git a/lib/Museum.php b/lib/Museum.php index 248022b7..c3680a0d 100644 --- a/lib/Museum.php +++ b/lib/Museum.php @@ -592,7 +592,7 @@ class Museum{ from Museums where ? like concat("%", Domain, "%") limit 1; - ', [$parsedUrl['host']], 'Museum'); + ', [$parsedUrl['host']], Museum::class); return $result[0] ?? throw new Exceptions\MuseumNotFoundException(); } diff --git a/lib/NewsletterSubscription.php b/lib/NewsletterSubscription.php index 762aa2aa..534d59a3 100644 --- a/lib/NewsletterSubscription.php +++ b/lib/NewsletterSubscription.php @@ -166,7 +166,7 @@ class NewsletterSubscription{ from NewsletterSubscriptions ns inner join Users u using(UserId) where u.Uuid = ? - ', [$uuid], 'NewsletterSubscription'); + ', [$uuid], NewsletterSubscription::class); return $result[0] ?? throw new Exceptions\NewsletterSubscriptionNotFoundException(); } diff --git a/lib/Patron.php b/lib/Patron.php index f860798a..35e6308b 100644 --- a/lib/Patron.php +++ b/lib/Patron.php @@ -93,7 +93,7 @@ class Patron{ SELECT * from Patrons where UserId = ? - ', [$userId], 'Patron'); + ', [$userId], Patron::class); return $result[0] ?? throw new Exceptions\PatronNotFoundException();; } @@ -111,7 +111,7 @@ class Patron{ from Patrons p inner join Users u using(UserId) where u.Email = ? - ', [$email], 'Patron'); + ', [$email], Patron::class); return $result[0] ?? throw new Exceptions\PatronNotFoundException(); } diff --git a/lib/Poll.php b/lib/Poll.php index 4da92f86..5b0688f6 100644 --- a/lib/Poll.php +++ b/lib/Poll.php @@ -61,7 +61,7 @@ class Poll{ from PollItems where PollId = ? order by SortOrder asc - ', [$this->PollId], 'PollItem'); + ', [$this->PollId], PollItem::class); } return $this->_PollItems; @@ -113,7 +113,7 @@ class Poll{ SELECT * from Polls where PollId = ? - ', [$pollId], 'Poll'); + ', [$pollId], Poll::class); return $result[0] ?? throw new Exceptions\PollNotFoundException(); } @@ -130,7 +130,7 @@ class Poll{ SELECT * from Polls where UrlName = ? - ', [$urlName], 'Poll'); + ', [$urlName], Poll::class); return $result[0] ?? throw new Exceptions\PollNotFoundException(); } diff --git a/lib/PollItem.php b/lib/PollItem.php index 206906b5..7dc27dee 100644 --- a/lib/PollItem.php +++ b/lib/PollItem.php @@ -51,7 +51,7 @@ class PollItem{ SELECT * from PollItems where PollItemId = ? - ', [$pollItemId], 'PollItem'); + ', [$pollItemId], PollItem::class); return $result[0] ?? throw new Exceptions\PollItemNotFoundException(); } diff --git a/lib/PollVote.php b/lib/PollVote.php index 300a258d..c300ef43 100644 --- a/lib/PollVote.php +++ b/lib/PollVote.php @@ -131,7 +131,7 @@ class PollVote{ inner join Polls p using (PollId) where p.UrlName = ? ) x using (PollItemId) where pv.UserId = ? - ', [$pollUrlName, $userId], 'PollVote'); + ', [$pollUrlName, $userId], PollVote::class); return $result[0] ?? throw new Exceptions\PollVoteNotFoundException(); } diff --git a/lib/Session.php b/lib/Session.php index 75a52b5a..aa3f70a5 100644 --- a/lib/Session.php +++ b/lib/Session.php @@ -86,7 +86,7 @@ class Session{ from Users u inner join Sessions s using (UserId) where s.SessionId = ? - ', [$sessionId], 'User'); + ', [$sessionId], User::class); if(sizeof($result) > 0){ self::SetSessionCookie($sessionId); @@ -113,7 +113,7 @@ class Session{ SELECT * from Sessions where SessionId = ? - ', [$sessionId], 'Session'); + ', [$sessionId], Session::class); return $result[0] ?? throw new Exceptions\SessionNotFoundException(); } diff --git a/lib/User.php b/lib/User.php index e693849c..39fc23fd 100644 --- a/lib/User.php +++ b/lib/User.php @@ -38,7 +38,7 @@ class User{ from Payments where UserId = ? order by Created desc - ', [$this->UserId], 'Payment'); + ', [$this->UserId], Payment::class); } return $this->_Payments; @@ -50,7 +50,7 @@ class User{ SELECT * from Benefits where UserId = ? - ', [$this->UserId], 'Benefits'); + ', [$this->UserId], Benefits::class); if(sizeof($result) == 0){ $this->_Benefits = new Benefits(); @@ -129,7 +129,7 @@ class User{ SELECT * from Users where UserId = ? - ', [$userId], 'User'); + ', [$userId], User::class); return $result[0] ?? throw new Exceptions\UserNotFoundException(); } @@ -146,7 +146,7 @@ class User{ SELECT * from Users where Email = ? - ', [$email], 'User'); + ', [$email], User::class); return $result[0] ?? throw new Exceptions\UserNotFoundException(); } @@ -169,7 +169,7 @@ class User{ inner join Benefits using (UserId) where u.Email = ? or u.Uuid = ? - ', [$identifier, $identifier], 'User'); + ', [$identifier, $identifier], User::class); if(sizeof($result) == 0){ throw new Exceptions\UserNotFoundException(); diff --git a/scripts/update-patrons-circle b/scripts/update-patrons-circle index 3e7c3397..782e7a39 100755 --- a/scripts/update-patrons-circle +++ b/scripts/update-patrons-circle @@ -26,7 +26,7 @@ $expiredPatrons = Db::Query(' (IsRecurring = false and Amount >= 100 and Created > ? - interval 1 year) ) ) -', [$now, $now], 'Patron'); +', [$now, $now], Patron::class); if(sizeof($expiredPatrons) > 0){ $ebooksThisYear = 0; @@ -70,7 +70,7 @@ if(sizeof($expiredPatrons) > 0){ where UserId = ? order by Created desc limit 1 - ', [$patron->UserId], 'Payment'); + ', [$patron->UserId], Payment::class); if(sizeof($lastPayment) > 0 && $patron->User->Email !== null){ $em = new Email(); diff --git a/www/artworks/index.php b/www/artworks/index.php index fefeaa8f..3556a9c0 100644 --- a/www/artworks/index.php +++ b/www/artworks/index.php @@ -63,12 +63,14 @@ try{ } if($queryEbookUrl !== null){ - $artworks = Db::Query('SELECT * from Artworks where EbookUrl = ? and Status = ? limit 1', [$queryEbookUrl, ArtworkStatus::Approved], 'Artwork'); + $artworks = Db::Query('SELECT * from Artworks where EbookUrl = ? and Status = ? limit 1', [$queryEbookUrl, ArtworkStatus::Approved], Artwork::class); $totalArtworkCount = sizeof($artworks); } else{ $result = Library::FilterArtwork($query, $filterArtworkStatus, $sort, $submitterUserId, $page, $perPage); + /** @var array $artworks */ $artworks = $result['artworks']; + /** @var int $totalArtworkCount */ $totalArtworkCount = $result['artworksCount']; } diff --git a/www/polls/index.php b/www/polls/index.php index 04670833..ded5f73d 100644 --- a/www/polls/index.php +++ b/www/polls/index.php @@ -4,7 +4,7 @@ $pastPolls = Db::Query(' from Polls where utc_timestamp() >= end order by Start desc - ', [], 'Poll'); + ', [], Poll::class); $openPolls = Db::Query(' SELECT * @@ -15,7 +15,7 @@ $openPolls = Db::Query(' (Start is null or Start <= utc_timestamp()) order by Start desc - ', [], 'Poll'); + ', [], Poll::class); ?> 'Polls', 'highlight' => '', 'description' => 'The various polls active at Standard Ebooks.']) ?>