From e55fecaaa234b62f516e2c8569036d9fcb5d9459 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sat, 13 Apr 2024 14:05:14 -0500 Subject: [PATCH] Switch from DateTime to DateTimeImmutable across codebase --- lib/Artist.php | 8 ++++---- lib/Artwork.php | 12 ++++++------ lib/AtomFeed.php | 6 +++--- lib/Constants.php | 8 ++++---- lib/DbConnection.php | 4 ++-- lib/Ebook.php | 10 +++++----- lib/Exceptions/ValidationException.php | 2 +- lib/Library.php | 8 ++++---- lib/Log.php | 4 ++-- lib/NewsletterSubscription.php | 6 +++--- lib/OpdsAcquisitionFeed.php | 2 -- lib/OpdsFeed.php | 6 +++--- lib/OpdsNavigationEntry.php | 6 ++++-- lib/OpdsNavigationFeed.php | 4 ++-- lib/Patron.php | 8 ++++---- lib/Payment.php | 4 +++- lib/Poll.php | 10 +++++----- lib/PollVote.php | 6 +++--- lib/RssFeed.php | 4 ++-- lib/Session.php | 6 +++--- lib/User.php | 6 +++--- scripts/generate-feeds | 8 ++++---- scripts/ingest-fa-payments | 4 ++-- scripts/process-pending-payments | 8 ++++---- scripts/update-patrons-circle | 8 +++++--- templates/ArtworkForm.php | 4 ++-- templates/DonationCounter.php | 7 ++++--- templates/DonationProgress.php | 8 ++++---- templates/RssFeed.php | 2 -- www/ebooks/ebook.php | 2 +- www/feeds/atom/search.php | 4 ++-- www/feeds/opds/search.php | 4 ++-- www/feeds/rss/search.php | 4 ++-- www/polls/get.php | 6 +++--- www/webhooks/zoho.php | 2 +- 35 files changed, 102 insertions(+), 99 deletions(-) diff --git a/lib/Artist.php b/lib/Artist.php index dfef6164..735f4988 100644 --- a/lib/Artist.php +++ b/lib/Artist.php @@ -1,5 +1,5 @@ format('Y')); $error = new Exceptions\ValidationException(); diff --git a/lib/Artwork.php b/lib/Artwork.php index e3cdc4e8..dd9f6853 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -1,7 +1,7 @@ format('Y')); $error = new Exceptions\ValidationException(); @@ -680,7 +680,7 @@ class Artwork extends Accessor{ $this->Validate($imagePath, true); - $this->Created = new DateTime(); + $this->Created = new DateTimeImmutable(); $tags = []; foreach($this->Tags as $artworkTag){ @@ -745,7 +745,7 @@ class Artwork extends Accessor{ // Manually set the updated timestamp, because if we only update the image and nothing else, the row's // updated timestamp won't change automatically. - $this->Updated = new DateTime('now', new DateTimeZone('UTC')); + $this->Updated = new DateTimeImmutable(); $this->_ImageUrl = null; $this->_ThumbUrl = null; $this->_Thumb2xUrl = null; diff --git a/lib/AtomFeed.php b/lib/AtomFeed.php index 28d2845f..d6b73fcb 100644 --- a/lib/AtomFeed.php +++ b/lib/AtomFeed.php @@ -1,10 +1,10 @@ HasChanged($this->Path)){ // Files don't match, save the file - $this->Updated = new DateTime(); + $this->Updated = new DateTimeImmutable(); $this->Save(); return true; } diff --git a/lib/Constants.php b/lib/Constants.php index a1d22428..c2575377 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -1,10 +1,10 @@ format('Y')) - 96); define('PD_STRING', 'January 1, ' . (PD_YEAR + 1)); -define('DONATION_HOLIDAY_ALERT_ON', $now > new DateTime('November 15, ' . $now->format('Y'), new DateTimeZone('UTC')) || $now < new DateTime('January 7, ' . $now->add(new DateInterval('P1Y'))->format('Y'), new DateTimeZone('UTC'))); +define('DONATION_HOLIDAY_ALERT_ON', $now > new DateTimeImmutable('November 15, ' . $now->format('Y')) || $now < new DateTimeImmutable('January 7, ' . $now->add(new DateInterval('P1Y'))->format('Y'))); define('DONATION_ALERT_ON', DONATION_HOLIDAY_ALERT_ON || rand(1, 4) == 2); // Controls the progress bar donation dialog diff --git a/lib/DbConnection.php b/lib/DbConnection.php index 1c4fd68e..9e12b7c7 100644 --- a/lib/DbConnection.php +++ b/lib/DbConnection.php @@ -1,5 +1,5 @@ {$metadata[$i]['name']} = new DateTime($row[$i], new DateTimeZone('UTC')); + $object->{$metadata[$i]['name']} = new DateTimeImmutable($row[$i], new DateTimeZone('UTC')); break; case 'LONG': diff --git a/lib/Ebook.php b/lib/Ebook.php index bc8cf929..6b185d11 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -1,5 +1,5 @@ xpath('/package/metadata/dc:date') ?: []; if($date !== false && sizeof($date) > 0){ - $this->Created = new DateTime((string)$date[0]); + $this->Created = new DateTimeImmutable((string)$date[0]); } $modifiedDate = $xml->xpath('/package/metadata/meta[@property="dcterms:modified"]') ?: []; if($modifiedDate !== false && sizeof($modifiedDate) > 0){ - $this->Updated = new DateTime((string)$modifiedDate[0]); + $this->Updated = new DateTimeImmutable((string)$modifiedDate[0]); } // Get SE tags diff --git a/lib/Exceptions/ValidationException.php b/lib/Exceptions/ValidationException.php index 8762c51d..16ffdba1 100644 --- a/lib/Exceptions/ValidationException.php +++ b/lib/Exceptions/ValidationException.php @@ -22,7 +22,7 @@ class ValidationException extends AppException{ public function Add(\Exception $exception, bool $isFatal = false): void{ if(is_a($exception, static::class)){ // Type hint for linter - /** @var ValidationException $exception */ + /** @var ValidationException $childException */ foreach($exception->Exceptions as $childException){ $this->Add($childException); } diff --git a/lib/Library.php b/lib/Library.php index cc9117a4..2b9f9cd7 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -1,5 +1,5 @@ EbookCount = exec('attr -g se-ebook-count ' . escapeshellarg($dir)) ?: null; @@ -479,7 +479,7 @@ class Library{ $obj->ZipFiles[] = $zipFile; } - $obj->Updated = new DateTime('@' . filemtime($files[0])); + $obj->Updated = new DateTimeImmutable('@' . filemtime($files[0])); $obj->UpdatedString = $obj->Updated->format('M j'); // Add a period to the abbreviated month, but not if it's May (the only 3-letter month) $obj->UpdatedString = preg_replace('/^(.+?)(?UpdatedString); @@ -546,7 +546,7 @@ class Library{ foreach($dirs as $dir){ $obj = self::FillBulkDownloadObject($dir, 'months', '/months'); - $date = new DateTime($obj->Label . '-01'); + $date = new DateTimeImmutable($obj->Label . '-01'); $year = $date->format('Y'); $month = $date->format('F'); diff --git a/lib/Log.php b/lib/Log.php index 6aa11194..ad55a2c9 100644 --- a/lib/Log.php +++ b/lib/Log.php @@ -1,5 +1,5 @@ format('Y-m-d H:i:s') . "\t" . $this->RequestId . "\t" . $text . "\n"); fclose($fp); diff --git a/lib/NewsletterSubscription.php b/lib/NewsletterSubscription.php index 277bd3fe..4679273e 100644 --- a/lib/NewsletterSubscription.php +++ b/lib/NewsletterSubscription.php @@ -1,5 +1,5 @@ UserId = $this->User->UserId; - $this->Created = new DateTime(); + $this->Created = new DateTimeImmutable(); try{ Db::Query(' diff --git a/lib/OpdsAcquisitionFeed.php b/lib/OpdsAcquisitionFeed.php index 9089c3ed..ac4c79fa 100644 --- a/lib/OpdsAcquisitionFeed.php +++ b/lib/OpdsAcquisitionFeed.php @@ -1,6 +1,4 @@ Entries as $entry){ if($entry instanceof OpdsNavigationEntry){ @@ -50,7 +50,7 @@ class OpdsFeed extends AtomFeed{ if($this->HasChanged($this->Path)){ // Files don't match, save the file and update the parent navigation feed with the last updated timestamp - $this->Updated = new DateTime(); + $this->Updated = new DateTimeImmutable(); if($this->Parent !== null){ $this->Parent->SaveUpdated($this->Id, $this->Updated); diff --git a/lib/OpdsNavigationEntry.php b/lib/OpdsNavigationEntry.php index 4f671c4c..44ccea13 100644 --- a/lib/OpdsNavigationEntry.php +++ b/lib/OpdsNavigationEntry.php @@ -1,15 +1,17 @@ Id = SITE_URL . $url; $this->Url = $url; $this->Rel = $rel; diff --git a/lib/OpdsNavigationFeed.php b/lib/OpdsNavigationFeed.php index 128c11fe..894e8417 100644 --- a/lib/OpdsNavigationFeed.php +++ b/lib/OpdsNavigationFeed.php @@ -1,5 +1,5 @@ xpath('//entry') ?: [] as $existingEntry){ foreach($this->Entries as $entry){ if($entry->Id == $existingEntry->id){ - $entry->Updated = new DateTime($existingEntry->updated); + $entry->Updated = new DateTimeImmutable($existingEntry->updated); } } } diff --git a/lib/Patron.php b/lib/Patron.php index 6a788b70..1344a949 100644 --- a/lib/Patron.php +++ b/lib/Patron.php @@ -1,5 +1,5 @@ Created = new DateTime(); + $this->Created = new DateTimeImmutable(); Db::Query(' INSERT into Patrons (Created, UserId, IsAnonymous, AlternateName, IsSubscribedToEmails) values(?, diff --git a/lib/Payment.php b/lib/Payment.php index de1eb7d5..bc70ab42 100644 --- a/lib/Payment.php +++ b/lib/Payment.php @@ -1,11 +1,13 @@ Start !== null && $this->Start > $now) || ($this->End !== null && $this->End < $now)){ return false; } diff --git a/lib/PollVote.php b/lib/PollVote.php index 4d905a74..ed2fda4b 100644 --- a/lib/PollVote.php +++ b/lib/PollVote.php @@ -1,5 +1,5 @@ Validate(); - $this->Created = new DateTime(); + $this->Created = new DateTimeImmutable(); Db::Query(' INSERT into PollVotes (UserId, PollItemId, Created) values (?, diff --git a/lib/RssFeed.php b/lib/RssFeed.php index b59507eb..8960944c 100644 --- a/lib/RssFeed.php +++ b/lib/RssFeed.php @@ -1,5 +1,5 @@ XmlString === null){ - $feed = Template::RssFeed(['url' => $this->Url, 'description' => $this->Description, 'title' => $this->Title, 'entries' => $this->Entries, 'updated' => (new DateTime())->format('r')]); + $feed = Template::RssFeed(['url' => $this->Url, 'description' => $this->Description, 'title' => $this->Title, 'entries' => $this->Entries, 'updated' => (new DateTimeImmutable())->format('r')]); $this->XmlString = $this->CleanXmlString($feed); } diff --git a/lib/Session.php b/lib/Session.php index 3efca7eb..9072ab54 100644 --- a/lib/Session.php +++ b/lib/Session.php @@ -2,7 +2,7 @@ use Exceptions\InvalidLoginException; use Ramsey\Uuid\Uuid; -use Safe\DateTime; +use Safe\DateTimeImmutable; use function Safe\strtotime; /** @@ -12,7 +12,7 @@ use function Safe\strtotime; class Session extends Accessor{ public int $UserId; protected ?User $_User = null; - public DateTime $Created; + public DateTimeImmutable $Created; public string $SessionId; public ?string $_Url = null; @@ -53,7 +53,7 @@ class Session extends Accessor{ else{ $uuid = Uuid::uuid4(); $this->SessionId = $uuid->toString(); - $this->Created = new DateTime(); + $this->Created = new DateTimeImmutable(); Db::Query(' INSERT into Sessions (UserId, SessionId, Created) values (?, diff --git a/lib/User.php b/lib/User.php index b196b341..7c993a50 100644 --- a/lib/User.php +++ b/lib/User.php @@ -1,6 +1,6 @@ $Payments @@ -12,7 +12,7 @@ class User extends Accessor{ public int $UserId; public ?string $Name = null; public ?string $Email = null; - public DateTime $Created; + public DateTimeImmutable $Created; public string $Uuid; public ?string $PasswordHash = null; protected ?bool $_IsRegistered = null; @@ -78,7 +78,7 @@ class User extends Accessor{ public function Create(?string $password = null): void{ $uuid = Uuid::uuid4(); $this->Uuid = $uuid->toString(); - $this->Created = new DateTime(); + $this->Created = new DateTimeImmutable(); $this->PasswordHash = null; if($password !== null){ diff --git a/scripts/generate-feeds b/scripts/generate-feeds index 7c676ec5..6272727c 100755 --- a/scripts/generate-feeds +++ b/scripts/generate-feeds @@ -2,7 +2,7 @@ Updated <=> $a->Updated; } -function SaveFeed(Feed $feed, bool $force, ?string $label = null, ?string $labelSort = null, DateTime $now = null): void{ +function SaveFeed(Feed $feed, bool $force, ?string $label = null, ?string $labelSort = null, DateTimeImmutable $now = null): void{ $updateAttrs = false; if($force){ @@ -31,7 +31,7 @@ function SaveFeed(Feed $feed, bool $force, ?string $label = null, ?string $label } } -function CreateOpdsCollectionFeed(string $name, string $url, string $description, array $collections, array $ebooks, DateTime $now, string $webRoot, OpdsNavigationFeed $opdsRoot, bool $force): void{ +function CreateOpdsCollectionFeed(string $name, string $url, string $description, array $collections, array $ebooks, DateTimeImmutable $now, string $webRoot, OpdsNavigationFeed $opdsRoot, bool $force): void{ $collator = collator_create('en_US'); // Used for sorting letters with diacritics like in author names usort($collections, function($a, $b) use($collator){ return $collator->compare($a['sortedname'], $b['sortedname']); }); @@ -108,7 +108,7 @@ usort($allEbooks, 'SortByUpdatedDesc'); usort($newestEbooks, function($a, $b){ return $b->Created <=> $a->Created; }); $newestEbooks = array_slice($newestEbooks, 0, $ebooksPerNewestEbooksFeed); -$now = new DateTime(); +$now = new DateTimeImmutable(); // Create OPDS feeds $opdsRootEntries = [ diff --git a/scripts/ingest-fa-payments b/scripts/ingest-fa-payments index 28a17bdd..de51b3ef 100755 --- a/scripts/ingest-fa-payments +++ b/scripts/ingest-fa-payments @@ -16,7 +16,7 @@ use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Firefox\FirefoxDriver; use Facebook\WebDriver\Firefox\FirefoxOptions; -use Safe\DateTime; +use Safe\DateTimeImmutable; use function Safe\file_get_contents; use function Safe\preg_replace; use function Safe\putenv; @@ -44,7 +44,7 @@ $lastSeenTransactionId = null; $firstTransactionId = null; $transactionFilePath = '/tmp/last-fa-donation'; $transactionIds = []; -$now = new DateTime('now', new DateTimeZone('UTC')); +$now = new DateTimeImmutable(); $today = $now->format('n/j/Y'); $faItemsPerPage = 20; // How many items are on a full page of FA results? diff --git a/scripts/process-pending-payments b/scripts/process-pending-payments index 21fbf32b..56516af5 100755 --- a/scripts/process-pending-payments +++ b/scripts/process-pending-payments @@ -9,7 +9,7 @@ use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Firefox\FirefoxDriver; use Facebook\WebDriver\Firefox\FirefoxOptions; -use Safe\DateTime; +use Safe\DateTimeImmutable; use function Safe\preg_match; use function Safe\preg_replace; use function Safe\putenv; @@ -31,8 +31,8 @@ $capabilities->setCapability(FirefoxOptions::CAPABILITY, $firefoxOptions); $driver = null; $log = new Log(DONATIONS_LOG_FILE_PATH); -$lastMonth = (new DateTime())->sub(new DateInterval('P45D')); // 45 days, a 15 day grace period before Patrons Circle members are dropped off -$lastYear = (new DateTime())->sub(new DateInterval('P1Y')); +$lastMonth = (new DateTimeImmutable())->sub(new DateInterval('P45D')); // 45 days, a 15 day grace period before Patrons Circle members are dropped off +$lastYear = (new DateTimeImmutable())->sub(new DateInterval('P1Y')); $faUsername = get_cfg_var('se.secrets.fractured_atlas.username'); $faPassword = get_cfg_var('se.secrets.fractured_atlas.password'); @@ -152,7 +152,7 @@ try{ $payment->User = null; } - $payment->Created = DateTime::createFromFormat('n/j/Y', trim($detailsRow->findElement(WebDriverBy::xpath('//td[preceding-sibling::th[normalize-space(.) = "Created"]]'))->getText())); + $payment->Created = DateTimeImmutable::createFromFormat('n/j/Y', trim($detailsRow->findElement(WebDriverBy::xpath('//td[preceding-sibling::th[normalize-space(.) = "Created"]]'))->getText())); $payment->IsRecurring = sizeof($headerRow->findElements(WebDriverBy::xpath('//td[contains(., "Recurring")]'))) > 0; $payment->Amount = floatval(str_replace('$', '', trim($detailsRow->findElement(WebDriverBy::xpath('//td[preceding-sibling::th[normalize-space(.) = "Total Amount"]]'))->getText()))); $payment->Fee = floatval(str_replace('$', '', trim($detailsRow->findElement(WebDriverBy::xpath('//td[preceding-sibling::th[normalize-space(.) = "Fee"]]'))->getText()))); diff --git a/scripts/update-patrons-circle b/scripts/update-patrons-circle index 32f0a5ec..3e7c3397 100755 --- a/scripts/update-patrons-circle +++ b/scripts/update-patrons-circle @@ -2,12 +2,14 @@ 0){ preg_match_all('/(.+?)<\/dc:date>/iu', $metadata, $matches); if(sizeof($matches) > 0){ - $created = new DateTime($matches[1][0]); + $created = new DateTimeImmutable($matches[1][0]); if($created >= $lastYear){ $ebooksThisYear++; } diff --git a/templates/ArtworkForm.php b/templates/ArtworkForm.php index ccd929b6..3ab8f6f9 100644 --- a/templates/ArtworkForm.php +++ b/templates/ArtworkForm.php @@ -1,5 +1,5 @@
Artist details diff --git a/templates/DonationCounter.php b/templates/DonationCounter.php index 9017e59b..53ab00d6 100644 --- a/templates/DonationCounter.php +++ b/templates/DonationCounter.php @@ -1,8 +1,9 @@ $end){ diff --git a/templates/DonationProgress.php b/templates/DonationProgress.php index 380e2208..3f901835 100644 --- a/templates/DonationProgress.php +++ b/templates/DonationProgress.php @@ -1,14 +1,14 @@ $end){ return; diff --git a/templates/RssFeed.php b/templates/RssFeed.php index d4021aba..4eec714f 100644 --- a/templates/RssFeed.php +++ b/templates/RssFeed.php @@ -1,6 +1,4 @@ GitCommits as $commit){ ?>
  • - +

    Message) ?>

  • diff --git a/www/feeds/atom/search.php b/www/feeds/atom/search.php index 80dcdea4..4e08520e 100644 --- a/www/feeds/atom/search.php +++ b/www/feeds/atom/search.php @@ -1,5 +1,5 @@ \nSearch Results Results for “”. /images/logo.png - Format('Y-m-d\TH:i:s\Z') ?> + Format('Y-m-d\TH:i:s\Z') ?> Standard Ebooks diff --git a/www/feeds/opds/search.php b/www/feeds/opds/search.php index 7198f394..2d21b9bd 100644 --- a/www/feeds/opds/search.php +++ b/www/feeds/opds/search.php @@ -1,5 +1,5 @@ \nSearch Results Results for “”. /images/logo.png - Format('Y-m-d\TH:i:s\Z') ?> + Format('Y-m-d\TH:i:s\Z') ?> Standard Ebooks diff --git a/www/feeds/rss/search.php b/www/feeds/rss/search.php index 7e06ebe5..702b2821 100644 --- a/www/feeds/rss/search.php +++ b/www/feeds/rss/search.php @@ -1,5 +1,5 @@ \nResults for “”. en-US https://creativecommons.org/publicdomain/zero/1.0/ - format('r') ?> + format('r') ?> http://blogs.law.harvard.edu/tech/rss