Remove direct PDO handling
This commit is contained in:
@@ -2,19 +2,10 @@
|
||||
|
||||
namespace FeedReaderCentral;
|
||||
|
||||
use BitBadger\PDODocument\Configuration;
|
||||
use BitBadger\PDODocument\Custom;
|
||||
use BitBadger\PDODocument\Document;
|
||||
use BitBadger\PDODocument\DocumentException;
|
||||
use BitBadger\PDODocument\DocumentList;
|
||||
use BitBadger\PDODocument\Exists;
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Find;
|
||||
use BitBadger\PDODocument\Parameters;
|
||||
use BitBadger\PDODocument\Patch;
|
||||
use BitBadger\PDODocument\Query;
|
||||
use BitBadger\PDODocument\{
|
||||
Configuration, Custom, Document, DocumentException, DocumentList, Exists, Field, Find, Parameters, Patch, Query
|
||||
};
|
||||
use DateTimeInterface;
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
* An RSS or Atom feed
|
||||
@@ -75,23 +66,22 @@ class Feed
|
||||
* @param int $feedId The ID of the feed to which these items belong
|
||||
* @param ParsedFeed $parsed The extracted Atom or RSS feed items
|
||||
* @param DateTimeInterface $lastChecked When this feed was last checked (only new items will be added)
|
||||
* @param PDO $pdo The database connection over which items should be updated
|
||||
* @return array ['ok' => true] if successful, ['error' => message] if not
|
||||
*/
|
||||
public static function updateItems(int $feedId, ParsedFeed $parsed, DateTimeInterface $lastChecked, PDO $pdo): array
|
||||
public static function updateItems(int $feedId, ParsedFeed $parsed, DateTimeInterface $lastChecked): array
|
||||
{
|
||||
$results =
|
||||
array_map(function ($item) use ($pdo, $feedId) {
|
||||
array_map(function ($item) use ($feedId) {
|
||||
try {
|
||||
$existing = Find::firstByFields(Table::ITEM,
|
||||
[Field::EQ('feed_id', $feedId), Field::EQ('item_guid', $item->guid)], Item::class);
|
||||
if ($existing) {
|
||||
if ($existing->published_on != $item->publishedOn
|
||||
|| ($existing->updated_on != ($item->updatedOn ?? ''))) {
|
||||
Patch::byId(Table::ITEM, $existing->id, $item->patchFields(), $pdo);
|
||||
Patch::byId(Table::ITEM, $existing->id, $item->patchFields());
|
||||
}
|
||||
} else {
|
||||
Document::insert(Table::ITEM, Item::fromFeedItem($feedId, $item), $pdo);
|
||||
Document::insert(Table::ITEM, Item::fromFeedItem($feedId, $item));
|
||||
}
|
||||
return ['ok' => true];
|
||||
} catch (DocumentException $ex) {
|
||||
@@ -107,10 +97,9 @@ class Feed
|
||||
* Purge items for a feed
|
||||
*
|
||||
* @param int $feedId The ID of the feed to be purged
|
||||
* @param PDO $pdo The database connection on which items should be purged
|
||||
* @return array|string[]|true[] ['ok' => true] if purging was successful, ['error' => message] if not
|
||||
*/
|
||||
private static function purgeItems(int $feedId, PDO $pdo): array
|
||||
private static function purgeItems(int $feedId): array
|
||||
{
|
||||
if (!array_search(PURGE_TYPE, [self::PURGE_READ, self::PURGE_BY_DAYS, self::PURGE_BY_COUNT])) {
|
||||
return ['error' => 'Unrecognized purge type ' . PURGE_TYPE];
|
||||
@@ -139,7 +128,7 @@ class Feed
|
||||
SQL;
|
||||
}
|
||||
try {
|
||||
Custom::nonQuery($sql, Parameters::addFields($fields, []), $pdo);
|
||||
Custom::nonQuery($sql, Parameters::addFields($fields, []));
|
||||
return ['ok' => true];
|
||||
} catch (DocumentException $ex) {
|
||||
return ['error' => "$ex"];
|
||||
@@ -151,10 +140,9 @@ class Feed
|
||||
*
|
||||
* @param int $feedId The ID of the feed to be refreshed
|
||||
* @param string $url The URL of the feed to be refreshed
|
||||
* @param PDO $pdo A database connection to use to refresh the feed
|
||||
* @return array|string[]|true[] ['ok' => true] if successful, ['error' => message] if not
|
||||
*/
|
||||
public static function refreshFeed(int $feedId, string $url, PDO $pdo): array
|
||||
public static function refreshFeed(int $feedId, string $url): array
|
||||
{
|
||||
$feedRetrieval = ParsedFeed::retrieve($url);
|
||||
if (key_exists('error', $feedRetrieval)) return $feedRetrieval;
|
||||
@@ -165,7 +153,7 @@ class Feed
|
||||
if (!$feedDoc) return ['error' => 'Could not derive date last checked for feed'];
|
||||
$lastChecked = date_create_immutable($feedDoc->checked_on ?? WWW_EPOCH);
|
||||
|
||||
$itemUpdate = self::updateItems($feedId, $feed, $lastChecked, $pdo);
|
||||
$itemUpdate = self::updateItems($feedId, $feed, $lastChecked);
|
||||
if (key_exists('error', $itemUpdate)) return $itemUpdate;
|
||||
|
||||
$patch = [
|
||||
@@ -174,12 +162,12 @@ class Feed
|
||||
'checked_on' => Data::formatDate('now')
|
||||
];
|
||||
if ($url == $feed->url) $patch['url'] = $feed->url;
|
||||
Patch::byId(Table::FEED, $feedId, $patch, $pdo);
|
||||
Patch::byId(Table::FEED, $feedId, $patch);
|
||||
} catch (DocumentException $ex) {
|
||||
return ['error' => "$ex"];
|
||||
}
|
||||
|
||||
return PURGE_TYPE == self::PURGE_NONE ? ['ok' => true] : self::purgeItems($feedId, $pdo);
|
||||
return PURGE_TYPE == self::PURGE_NONE ? ['ok' => true] : self::purgeItems($feedId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -196,18 +184,17 @@ class Feed
|
||||
$feed = $feedExtract['ok'];
|
||||
|
||||
try {
|
||||
$pdo = Configuration::dbConn();
|
||||
$fields = [Field::EQ('user_id', $_SESSION[Key::USER_ID]), Field::EQ('url', $feed->url)];
|
||||
if (Exists::byFields(Table::FEED, $fields, $pdo)) {
|
||||
if (Exists::byFields(Table::FEED, $fields)) {
|
||||
return ['error' => "Already subscribed to feed $feed->url"];
|
||||
}
|
||||
|
||||
Document::insert(Table::FEED, self::fromParsed($feed), $pdo);
|
||||
Document::insert(Table::FEED, self::fromParsed($feed));
|
||||
|
||||
$doc = Find::firstByFields(Table::FEED, $fields, static::class);
|
||||
if (!$doc) return ['error' => 'Could not retrieve inserted feed'];
|
||||
|
||||
$result = self::updateItems($doc->id, $feed, date_create_immutable(WWW_EPOCH), $pdo);
|
||||
$result = self::updateItems($doc->id, $feed, date_create_immutable(WWW_EPOCH));
|
||||
if (key_exists('error', $result)) return $result;
|
||||
|
||||
return ['ok' => $doc->id];
|
||||
@@ -226,12 +213,11 @@ class Feed
|
||||
public static function update(Feed $existing, string $url): array
|
||||
{
|
||||
try {
|
||||
$pdo = Configuration::dbConn();
|
||||
Patch::byFields(Table::FEED,
|
||||
[Field::EQ(Configuration::$idField, $existing->id), Field::EQ('user_id', $_SESSION[Key::USER_ID])],
|
||||
['url' => $url], $pdo);
|
||||
['url' => $url]);
|
||||
|
||||
return self::refreshFeed($existing->id, $url, $pdo);
|
||||
return self::refreshFeed($existing->id, $url);
|
||||
} catch (DocumentException $ex) {
|
||||
return ['error' => "$ex"];
|
||||
}
|
||||
@@ -262,10 +248,9 @@ class Feed
|
||||
try {
|
||||
$feeds = self::retrieveAll($_SESSION[Key::USER_ID]);
|
||||
|
||||
$pdo = Configuration::dbConn();
|
||||
$errors = [];
|
||||
foreach ($feeds->items() as $feed) {
|
||||
$result = self::refreshFeed($feed->id, $feed->url, $pdo);
|
||||
$result = self::refreshFeed($feed->id, $feed->url);
|
||||
if (key_exists('error', $result)) $errors[] = $result['error'];
|
||||
}
|
||||
} catch (DocumentException $ex) {
|
||||
@@ -284,7 +269,9 @@ class Feed
|
||||
*/
|
||||
public static function retrieveById(int $feedId): static|false
|
||||
{
|
||||
define('PDO_DOC_DEBUG_SQL', true);
|
||||
$doc = Find::byId(Table::FEED, $feedId, static::class);
|
||||
echo "Feed $feedId: " . ($doc ? 'found it' : 'not found');
|
||||
return $doc && $doc->user_id == $_SESSION[Key::USER_ID] ? $doc : false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user