Add editing of feed URL (#4)

- Move feed-specific database calls to Feed class
- Detect when feed items have been updated
- Add const keys for $_REQUEST values
This commit is contained in:
2024-04-11 22:01:36 -04:00
parent 7d294b9be8
commit da9a569e4a
7 changed files with 275 additions and 144 deletions

View File

@@ -8,7 +8,7 @@ class Data {
* Obtain a new connection to the database
* @return SQLite3 A new connection to the database
*/
private static function getConnection(): SQLite3 {
public static function getConnection(): SQLite3 {
$db = new SQLite3('../data/' . DATABASE_NAME);
$db->exec('PRAGMA foreign_keys = ON;');
return $db;
@@ -74,15 +74,19 @@ class Data {
*/
public static function findUserByEmail(string $email): ?array {
$db = self::getConnection();
$query = $db->prepare('SELECT * FROM frc_user WHERE email = :email');
$query->bindValue(':email', $email);
$result = $query->execute();
if ($result) {
$user = $result->fetchArray(SQLITE3_ASSOC);
if ($user) return $user;
try {
$query = $db->prepare('SELECT * FROM frc_user WHERE email = :email');
$query->bindValue(':email', $email);
$result = $query->execute();
if ($result) {
$user = $result->fetchArray(SQLITE3_ASSOC);
if ($user) return $user;
return null;
}
return null;
} finally {
$db->close();
}
return null;
}
/**
@@ -93,10 +97,14 @@ class Data {
*/
public static function addUser(string $email, string $password): void {
$db = self::getConnection();
$query = $db->prepare('INSERT INTO frc_user (email, password) VALUES (:email, :password)');
$query->bindValue(':email', $email);
$query->bindValue(':password', password_hash($password, PASSWORD_DEFAULT));
$query->execute();
try {
$query = $db->prepare('INSERT INTO frc_user (email, password) VALUES (:email, :password)');
$query->bindValue(':email', $email);
$query->bindValue(':password', password_hash($password, PASSWORD_DEFAULT));
$query->execute();
} finally {
$db->close();
}
}
/**
@@ -105,7 +113,7 @@ class Data {
* @param ?string $value The date/time to be parsed and formatted
* @return string|null The date/time in `DateTimeInterface::ATOM` format, or `null` if the input cannot be parsed
*/
private static function formatDate(?string $value): ?string {
public static function formatDate(?string $value): ?string {
try {
return $value ? (new DateTimeImmutable($value))->format(DateTimeInterface::ATOM) : null;
} catch (Exception) {
@@ -114,77 +122,22 @@ class Data {
}
/**
* Add an RSS feed
* Retrieve a feed by its ID for the current user
*
* @param string $url The URL for the RSS feed
* @param string $title The title of the RSS feed
* @param ?string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked)
* @return int The ID of the added feed
* @param int $feedId The ID of the feed to retrieve
* @param ?SQLite3 $dbConn A database connection to use (optional; will use standalone if not provided)
* @return array|bool The data for the feed if found, false if not found
*/
public static function addFeed(string $url, string $title, ?string $updatedOn): int {
$db = self::getConnection();
$query = $db->prepare(<<<'SQL'
INSERT INTO feed (
user_id, url, title, updated_on, checked_on
) VALUES (
:user, :url, :title, :updated, :checked
)
SQL);
$query->bindValue(':user', $_REQUEST['FRC_USER_ID']);
$query->bindValue(':url', $url);
$query->bindValue(':title', $title);
$query->bindValue(':updated', self::formatDate($updatedOn));
$query->bindValue(':checked', self::formatDate('now'));
$result = $query->execute();
return $result ? $db->lastInsertRowID() : -1;
}
/**
* Does a feed item already exist?
*
* @param int $feedId The ID of the feed to which the item belongs
* @param string $guid The GUID from the RSS feed, uniquely identifying the item
* @return bool True if the item exists, false if not
*/
public static function itemExists(int $feedId, string $guid): bool {
$db = self::getConnection();
$query = $db->prepare('SELECT COUNT(*) FROM item WHERE feed_id = :feed AND item_guid = :guid');
$query->bindValue(':feed', $feedId);
$query->bindValue(':guid', $guid);
$result = $query->execute();
return $result && $result->fetchArray(SQLITE3_NUM)[0] == 1;
}
/**
* Add a feed item
*
* @param int $feedId The ID of the feed to which the item should be added
* @param string $guid The GUID from the RSS feed (uses link if `<guid>` not specified)
* @param string $link The link to this item
* @param string $title The title of the item
* @param string $publishedOn The date/time the item was published
* @param ?string $updatedOn The date/time the item was last updated
* @param string $content The content of the item
* @param bool $isEncoded Whether the content has HTML (true) or is plaintext (false)
*/
public static function addItem(int $feedId, string $guid, string $link, string $title, string $publishedOn,
?string $updatedOn, string $content, bool $isEncoded): void {
$db = self::getConnection();
$query = $db->prepare(<<<'SQL'
INSERT INTO item (
feed_id, item_guid, item_link, title, published_on, updated_on, content, is_encoded
) VALUES (
:feed, :guid, :link, :title, :published, :updated, :content, :encoded
)
SQL);
$query->bindValue(':feed', $feedId);
$query->bindValue(':guid', $guid);
$query->bindValue(':link', $link);
$query->bindValue(':title', $title);
$query->bindValue(':published', self::formatDate($publishedOn));
$query->bindValue(':updated', self::formatDate($updatedOn));
$query->bindValue(':content', $content);
$query->bindValue(':encoded', $isEncoded);
$query->execute();
public static function retrieveFeedById(int $feedId, ?SQLite3 $dbConn = null): array|bool {
$db = $dbConn ?? self::getConnection();
try {
$query = $db->prepare('SELECT * FROM feed WHERE id = :id AND user_id = :user');
$query->bindValue(':id', $feedId);
$query->bindValue(':user', $_REQUEST[Key::USER_ID]);
$result = $query->execute();
return $result ? $result->fetchArray(SQLITE3_ASSOC) : false;
} finally {
if (is_null($dbConn)) $db->close();
}
}
}