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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\date;
/**
@@ -12,8 +12,8 @@ use function Safe\date;
class Artist extends Accessor{
public ?int $ArtistId = null;
public ?string $Name = null;
- public ?datetime $Created = null;
- public ?datetime $Updated = null;
+ public ?DateTimeImmutable $Created = null;
+ public ?DateTimeImmutable $Updated = null;
protected ?int $_DeathYear = null;
protected ?string $_UrlName = null;
protected ?string $_Url = null;
@@ -82,7 +82,7 @@ class Artist extends Accessor{
// *******
public function Validate(): void{
- $now = new DateTime('now', new DateTimeZone('UTC'));
+ $now = new DateTimeImmutable();
$thisYear = intval($now->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 @@
use Exceptions\InvalidUrlException;
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\apcu_cache_info;
use function Safe\copy;
@@ -40,8 +40,8 @@ class Artwork extends Accessor{
public ?int $ArtistId = null;
public ?int $CompletedYear = null;
public bool $CompletedYearIsCirca = false;
- public ?DateTime $Created = null;
- public ?DateTime $Updated = null;
+ public ?DateTimeImmutable $Created = null;
+ public ?DateTimeImmutable $Updated = null;
public ?string $EbookUrl = null;
public ?int $SubmitterUserId = null;
public ?int $ReviewerUserId = null;
@@ -356,7 +356,7 @@ class Artwork extends Accessor{
}
}
- $now = new DateTime('now', new DateTimeZone('UTC'));
+ $now = new DateTimeImmutable();
$thisYear = intval($now->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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\file_get_contents;
class AtomFeed extends Feed{
public string $Id;
- public ?DateTime $Updated = null;
+ public ?DateTimeImmutable $Updated = null;
public ?string $Subtitle = null;
/**
@@ -39,7 +39,7 @@ class AtomFeed extends Feed{
// Did we actually update the feed? If so, write to file and update the index
if($this->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 @@
// Auto-included by Composer in composer.json to satisfy PHPStan
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\define;
-$now = new DateTime('now', new DateTimeZone('UTC'));
-$nowPd = new DateTime('now', new DateTimeZone('America/Juneau')); // Latest continental US time zone
+$now = new DateTimeImmutable();
+$nowPd = new DateTimeImmutable('now', new DateTimeZone('America/Juneau')); // Latest continental US time zone
const SITE_STATUS_LIVE = 'live';
const SITE_STATUS_DEV = 'dev';
@@ -94,7 +94,7 @@ const ARTWORK_UPLOADS_LOG_FILE_PATH = '/var/log/local/artwork-uploads.log'; // M
define('PD_YEAR', intval($nowPd->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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\preg_match;
use function Safe\posix_getpwuid;
@@ -199,7 +199,7 @@ class DbConnection{
switch($metadata[$i]['native_type'] ?? null){
case 'DATETIME':
case 'TIMESTAMP':
- $object->{$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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\file_get_contents;
use function Safe\filesize;
use function Safe\json_encode;
@@ -67,8 +67,8 @@ class Ebook{
public $Contributors = [];
public ?string $ContributorsHtml = null;
public string $TitleWithCreditsHtml = '';
- public DateTime $Created;
- public DateTime $Updated;
+ public DateTimeImmutable $Created;
+ public DateTimeImmutable $Updated;
public string $TextUrl;
public string $TextSinglePageUrl;
public ?string $TextSinglePageSizeNumber = null;
@@ -222,12 +222,12 @@ class Ebook{
$date = $xml->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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\apcu_fetch;
use function Safe\exec;
use function Safe\filemtime;
@@ -432,7 +432,7 @@ class Library{
private static function FillBulkDownloadObject(string $dir, string $downloadType, string $urlRoot): stdClass{
$obj = new stdClass();
- $now = new DateTime('now', new DateTimeZone('UTC'));
+ $now = new DateTimeImmutable();
// The count of ebooks in each file is stored as a filesystem attribute
$obj->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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\fopen;
use function Safe\fwrite;
use function Safe\fclose;
@@ -33,7 +33,7 @@ class Log{
return;
}
- $now = new DateTime('now', new DateTimeZone('UTC'));
+ $now = new DateTimeImmutable();
fwrite($fp, $now->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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
/**
* @property User $User
@@ -10,7 +10,7 @@ class NewsletterSubscription extends Accessor{
public bool $IsSubscribedToSummary = false;
public bool $IsSubscribedToNewsletter = false;
public ?int $UserId = null;
- public DateTime $Created;
+ public DateTimeImmutable $Created;
protected $_User;
protected $_Url = null;
@@ -44,7 +44,7 @@ class NewsletterSubscription extends Accessor{
}
$this->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 @@
-use Safe\DateTime;
-
class OpdsAcquisitionFeed extends OpdsFeed{
public bool $IsCrawlable;
diff --git a/lib/OpdsFeed.php b/lib/OpdsFeed.php
index ffa9012d..f4d5ba8c 100644
--- a/lib/OpdsFeed.php
+++ b/lib/OpdsFeed.php
@@ -1,5 +1,5 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\file_put_contents;
class OpdsFeed extends AtomFeed{
@@ -24,7 +24,7 @@ class OpdsFeed extends AtomFeed{
// METHODS
// *******
- protected function SaveUpdated(string $entryId, DateTime $updated): void{
+ protected function SaveUpdated(string $entryId, DateTimeImmutable $updated): void{
// Only save the updated timestamp for the given entry ID in this file
foreach($this->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 @@
+use Safe\DateTimeImmutable;
+
class OpdsNavigationEntry{
public string $Id;
public string $Url;
public string $Rel;
public string $Type;
- public ?DateTime $Updated = null;
+ public ?DateTimeImmutable $Updated = null;
public string $Description;
public string $Title;
public string $SortTitle;
- public function __construct(string $title, string $description, string $url, ?DateTime $updated, string $rel, string $type){
+ public function __construct(string $title, string $description, string $url, ?DateTimeImmutable $updated, string $rel, string $type){
$this->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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\file_get_contents;
class OpdsNavigationFeed extends OpdsFeed{
@@ -25,7 +25,7 @@ class OpdsNavigationFeed extends OpdsFeed{
foreach($xml->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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
/**
* @property User $User
@@ -10,8 +10,8 @@ class Patron extends Accessor{
public bool $IsAnonymous;
public ?string $AlternateName = null;
public bool $IsSubscribedToEmails;
- public ?DateTime $Created = null;
- public ?DateTime $Ended = null;
+ public ?DateTimeImmutable $Created = null;
+ public ?DateTimeImmutable $Ended = null;
// *******
@@ -19,7 +19,7 @@ class Patron extends Accessor{
// *******
public function Create(): void{
- $this->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 @@
+use Safe\DateTimeImmutable;
+
/**
* @property User $User
*/
class Payment extends Accessor{
public int $PaymentId;
public ?int $UserId = null;
- public DateTime $Created;
+ public DateTimeImmutable $Created;
public int $ChannelId;
public string $TransactionId;
public float $Amount;
diff --git a/lib/Poll.php b/lib/Poll.php
index 46a766df..06ccf5b6 100644
--- a/lib/Poll.php
+++ b/lib/Poll.php
@@ -1,5 +1,5 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\usort;
/**
@@ -15,9 +15,9 @@ class Poll extends Accessor{
public string $Name;
public string $UrlName;
public string $Description;
- public DateTime $Created;
- public DateTime $Start;
- public DateTime $End;
+ public DateTimeImmutable $Created;
+ public DateTimeImmutable $Start;
+ public DateTimeImmutable $End;
protected ?string $_Url = null;
protected $_PollItems = null;
protected $_PollItemsByWinner = null;
@@ -85,7 +85,7 @@ class Poll extends Accessor{
// *******
public function IsActive(): bool{
- $now = new DateTime();
+ $now = new DateTimeImmutable();
if( ($this->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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
/**
* @property User $User
@@ -8,7 +8,7 @@ use Safe\DateTime;
*/
class PollVote extends Accessor{
public int $UserId;
- public DateTime $Created;
+ public DateTimeImmutable $Created;
public ?int $PollItemId = null;
protected ?User $_User = null;
protected ?PollItem $_PollItem = null;
@@ -98,7 +98,7 @@ class PollVote extends Accessor{
}
$this->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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\file_get_contents;
use function Safe\filesize;
use function Safe\preg_replace;
@@ -27,7 +27,7 @@ class RssFeed extends Feed{
protected function GetXmlString(): string{
if($this->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 @@
use Ramsey\Uuid\Uuid;
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
/**
* @property Array $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 @@
require_once('/standardebooks.org/web/lib/Core.php');
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\getopt;
use function Safe\mkdir;
use function Safe\preg_replace;
@@ -11,7 +11,7 @@ function SortByUpdatedDesc($a, $b){
return $b->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 @@
require_once('/standardebooks.org/web/lib/Core.php');
+use Safe\DateTimeImmutable;
+
// Get a list of payments that are within 1 year / 45 days of today, and deactivate Patrons Circle members
// who aren't in that list.
// We give a 15 day grace period to Patrons Circle members because sometimes FA can be delayed in charging.
-$now = new DateTime();
-$lastYear = new DateTime('-1 year');
+$now = new DateTimeImmutable();
+$lastYear = new DateTimeImmutable('-1 year');
$expiredPatrons = Db::Query('
SELECT * from Patrons
@@ -38,7 +40,7 @@ if(sizeof($expiredPatrons) > 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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
$artwork = $artwork ?? null;
@@ -10,7 +10,7 @@ if($artwork === null){
$isEditForm = $isEditForm ?? false;
-$now = new DateTime('now', new DateTimeZone('America/Juneau')); // Latest continental US time zone
+$now = new DateTimeImmutable('now', new DateTimeZone('America/Juneau')); // Latest continental US time zone
?>
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 @@
+use Safe\DateTimeImmutable;
-$start = new DateTime(DONATION_DRIVE_COUNTER_START);
-$end = new DateTime(DONATION_DRIVE_COUNTER_END);
-$now = new DateTime();
+$start = new DateTimeImmutable(DONATION_DRIVE_COUNTER_START);
+$end = new DateTimeImmutable(DONATION_DRIVE_COUNTER_END);
+$now = new DateTimeImmutable();
// Hide the alert if the user has closed it
if(!DONATION_DRIVE_COUNTER_ON || ($autoHide ?? $_COOKIE['hide-donation-alert'] ?? false) || $now > $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 @@
+use Safe\DateTimeImmutable;
-$start = new DateTime(DONATION_DRIVE_START);
-$end = new DateTime(DONATION_DRIVE_END);
+$start = new DateTimeImmutable(DONATION_DRIVE_START);
+$end = new DateTimeImmutable(DONATION_DRIVE_END);
+$now = new DateTimeImmutable();
$totalCurrent = 0;
$baseTarget = 50;
$stretchCurrent = 0;
$stretchTarget = 20;
-$now = new DateTime();
-
// Hide the alert if the user has closed it
if(!DONATION_DRIVE_ON || ($autoHide ?? $_COOKIE['hide-donation-alert'] ?? false) || $GLOBALS['User'] !== null || $now > $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 @@
-use Safe\DateTime;
-
// Note that the XSL stylesheet gets stripped during `se clean` when we generate the feed.
// `se clean` will also start adding empty namespaces everywhere if we include the stylesheet declaration first.
// We have to add it programmatically when saving the feed file.
diff --git a/www/ebooks/ebook.php b/www/ebooks/ebook.php
index d43d2b9c..618050bd 100644
--- a/www/ebooks/ebook.php
+++ b/www/ebooks/ebook.php
@@ -297,7 +297,7 @@ catch(Exceptions\EbookNotFoundException){
foreach($ebook->GitCommits as $commit){ ?>
- = $commit->Created->format('M j, Y') ?>
+ = $commit->Created->format('M j, Y') ?>
= Formatter::EscapeHtml($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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
$ebooks = [];
@@ -25,7 +25,7 @@ print("\nSearch Results
Results for “= Formatter::EscapeXml($query) ?>”.
= SITE_URL ?>/images/logo.png
- = (new Datetime())->Format('Y-m-d\TH:i:s\Z') ?>
+ = (new DateTimeImmutable())->Format('Y-m-d\TH:i:s\Z') ?>
Standard Ebooks
= SITE_URL ?>
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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
$ebooks = [];
@@ -26,7 +26,7 @@ print("\nSearch Results
Results for “= Formatter::EscapeXml($query) ?>”.
= SITE_URL ?>/images/logo.png
- = (new Datetime())->Format('Y-m-d\TH:i:s\Z') ?>
+ = (new DateTimeImmutable())->Format('Y-m-d\TH:i:s\Z') ?>
Standard Ebooks
= SITE_URL ?>
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 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
$ebooks = [];
@@ -24,7 +24,7 @@ print("\nResults for “= Formatter::EscapeXml($query) ?>”.
en-US
https://creativecommons.org/publicdomain/zero/1.0/
- = (new DateTime())->format('r') ?>
+ = (new DateTimeImmutable())->format('r') ?>
http://blogs.law.harvard.edu/tech/rss
diff --git a/www/polls/get.php b/www/polls/get.php
index c4a2c40e..dafdf815 100644
--- a/www/polls/get.php
+++ b/www/polls/get.php
@@ -1,5 +1,5 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
$poll = new Poll();
$canVote = true; // Allow non-logged-in users to see the 'vote' button
@@ -7,7 +7,7 @@ $canVote = true; // Allow non-logged-in users to see the 'vote' button
try{
$poll = Poll::GetByUrlName(HttpInput::Str(GET, 'pollurlname'));
- if(!$poll->IsActive() && $poll->End !== null && $poll->End < new DateTime()){
+ if(!$poll->IsActive() && $poll->End !== null && $poll->End < new DateTimeImmutable()){
// If the poll ended, redirect to the results
header('Location: ' . $poll->Url . '/votes');
exit();
@@ -47,7 +47,7 @@ catch(Exceptions\AppException){
View results
}else{ ?>
- if($poll->Start !== null && $poll->Start > new DateTime()){ ?>
+ if($poll->Start !== null && $poll->Start > new DateTimeImmutable()){ ?>
This poll opens on = $poll->Start->format('F j, Y g:i a') ?>.
}else{ ?>
This poll closed on = $poll->End->format('F j, Y g:i a') ?>.
diff --git a/www/webhooks/zoho.php b/www/webhooks/zoho.php
index 8279d765..b73eba1d 100644
--- a/www/webhooks/zoho.php
+++ b/www/webhooks/zoho.php
@@ -1,5 +1,5 @@
-use Safe\DateTime;
+use Safe\DateTimeImmutable;
use function Safe\file_get_contents;
use function Safe\preg_match;
use function Safe\preg_replace;