Remove direct PDO handling
This commit is contained in:
@@ -2,16 +2,11 @@
|
||||
|
||||
namespace FeedReaderCentral;
|
||||
|
||||
use BitBadger\PDODocument\Configuration;
|
||||
use BitBadger\PDODocument\Custom;
|
||||
use BitBadger\PDODocument\Definition;
|
||||
use BitBadger\PDODocument\DocumentException;
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\{Configuration, Custom, Definition, DocumentException, Field};
|
||||
use BitBadger\PDODocument\Mapper\StringMapper;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Exception;
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
* A centralized place for data access for the application
|
||||
@@ -21,18 +16,17 @@ class Data
|
||||
/**
|
||||
* Create the search index and synchronization triggers for the item table
|
||||
*
|
||||
* @param PDO $pdo The database connection on which these will be created
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function createSearchIndex(PDO $pdo): void
|
||||
public static function createSearchIndex(): void
|
||||
{
|
||||
Custom::nonQuery("CREATE VIRTUAL TABLE item_search USING fts5(content, content='item', content_rowid='id')",
|
||||
[], $pdo);
|
||||
[]);
|
||||
Custom::nonQuery(<<<'SQL'
|
||||
CREATE TRIGGER item_ai AFTER INSERT ON item BEGIN
|
||||
INSERT INTO item_search (rowid, content) VALUES (new.data->>'id', new.data->>'content');
|
||||
END;
|
||||
SQL, [], $pdo);
|
||||
SQL, []);
|
||||
Custom::nonQuery(<<<'SQL'
|
||||
CREATE TRIGGER item_au AFTER UPDATE ON item BEGIN
|
||||
INSERT INTO item_search (
|
||||
@@ -42,7 +36,7 @@ class Data
|
||||
);
|
||||
INSERT INTO item_search (rowid, content) VALUES (new.data->>'id', new.data->>'content');
|
||||
END;
|
||||
SQL, [], $pdo);
|
||||
SQL, []);
|
||||
Custom::nonQuery(<<<'SQL'
|
||||
CREATE TRIGGER item_ad AFTER DELETE ON item BEGIN
|
||||
INSERT INTO item_search (
|
||||
@@ -51,7 +45,7 @@ class Data
|
||||
'delete', old.data->>'id', old.data->>'content'
|
||||
);
|
||||
END;
|
||||
SQL, [], $pdo);
|
||||
SQL, []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,19 +56,18 @@ class Data
|
||||
public static function ensureDb(): void
|
||||
{
|
||||
$tables = Custom::array("SELECT name FROM sqlite_master WHERE type = 'table'", [], new StringMapper('name'));
|
||||
$pdo = Configuration::dbConn();
|
||||
if (!in_array(Table::USER, $tables)) {
|
||||
Definition::ensureTable(Table::USER, $pdo);
|
||||
Definition::ensureFieldIndex(Table::USER, 'email', ['email'], $pdo);
|
||||
Definition::ensureTable(Table::USER);
|
||||
Definition::ensureFieldIndex(Table::USER, 'email', ['email']);
|
||||
}
|
||||
if (!in_array(Table::FEED, $tables)) {
|
||||
Definition::ensureTable(Table::FEED, $pdo);
|
||||
Definition::ensureFieldIndex(Table::FEED, 'user', ['user_id'], $pdo);
|
||||
Definition::ensureTable(Table::FEED);
|
||||
Definition::ensureFieldIndex(Table::FEED, 'user', ['user_id']);
|
||||
}
|
||||
if (!in_array(Table::ITEM, $tables)) {
|
||||
Definition::ensureTable(Table::ITEM, $pdo);
|
||||
Definition::ensureFieldIndex(Table::ITEM, 'feed', ['feed_id', 'item_link'], $pdo);
|
||||
self::createSearchIndex($pdo);
|
||||
Definition::ensureTable(Table::ITEM);
|
||||
Definition::ensureFieldIndex(Table::ITEM, 'feed', ['feed_id', 'item_link']);
|
||||
self::createSearchIndex();
|
||||
}
|
||||
$journalMode = Custom::scalar("PRAGMA journal_mode", [], new StringMapper('journal_mode'));
|
||||
if ($journalMode <> 'wal') Custom::nonQuery("PRAGMA journal_mode = 'wal'", []);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,7 @@
|
||||
|
||||
namespace FeedReaderCentral;
|
||||
|
||||
use BitBadger\PDODocument\Configuration;
|
||||
use BitBadger\PDODocument\Custom;
|
||||
use BitBadger\PDODocument\DocumentException;
|
||||
use BitBadger\PDODocument\DocumentList;
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Parameters;
|
||||
use BitBadger\PDODocument\Query;
|
||||
use BitBadger\PDODocument\{Configuration, Custom, DocumentException, DocumentList, Field, Parameters, Query};
|
||||
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
||||
|
||||
/**
|
||||
@@ -156,27 +150,26 @@ class ItemList
|
||||
echo "<p>Error retrieving list:<br>$this->error";
|
||||
return;
|
||||
}
|
||||
$return = $this->returnURL == '' ? '' : '&from=' . urlencode($this->returnURL);
|
||||
$hasItems = false;
|
||||
$return = $this->returnURL == '' ? '' : '&from=' . urlencode($this->returnURL);
|
||||
echo '<article>';
|
||||
foreach ($this->dbList->items() as $it) {
|
||||
$hasItems = true;
|
||||
echo '<p>' . hx_get("/item?id=$it->id$return", strip_tags($it->title)) . '<br><small>';
|
||||
if ($this->showIndicators) {
|
||||
if (!$it->isRead()) echo '<strong>Unread</strong> ';
|
||||
if ($it->isBookmarked()) echo '<strong>Bookmarked</strong> ';
|
||||
if ($this->dbList->hasItems()) {
|
||||
foreach ($this->dbList->items() as $it) {
|
||||
echo '<p>' . hx_get("/item?id=$it->id$return", strip_tags($it->title)) . '<br><small>';
|
||||
if ($this->showIndicators) {
|
||||
if (!$it->isRead()) echo '<strong>Unread</strong> ';
|
||||
if ($it->isBookmarked()) echo '<strong>Bookmarked</strong> ';
|
||||
}
|
||||
echo '<em>' . date_time($it->updated_on ?? $it->published_on) . '</em>';
|
||||
if ($this->linkFeed) {
|
||||
echo ' • ' .
|
||||
hx_get("/feed/items?id={$it->feed->id}&" . strtolower($this->itemType),
|
||||
htmlentities($it->feed->title));
|
||||
} elseif ($this->displayFeed) {
|
||||
echo ' • ' . htmlentities($it->feed->title);
|
||||
}
|
||||
echo '</small>';
|
||||
}
|
||||
echo '<em>' . date_time($it->updated_on ?? $it->published_on) . '</em>';
|
||||
if ($this->linkFeed) {
|
||||
echo ' • ' .
|
||||
hx_get("/feed/items?id={$it->feed->id}&" . strtolower($this->itemType),
|
||||
htmlentities($it->feed->title));
|
||||
} elseif ($this->displayFeed) {
|
||||
echo ' • ' . htmlentities($it->feed->title);
|
||||
}
|
||||
echo '</small>';
|
||||
}
|
||||
if (!$hasItems) {
|
||||
} else {
|
||||
echo '<p><em>There are no ' . strtolower($this->itemType) . ' items</em>';
|
||||
}
|
||||
echo '</article>';
|
||||
|
||||
@@ -2,14 +2,8 @@
|
||||
|
||||
namespace FeedReaderCentral;
|
||||
|
||||
use BitBadger\PDODocument\Configuration;
|
||||
use BitBadger\PDODocument\Custom;
|
||||
use BitBadger\PDODocument\DocumentException;
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Parameters;
|
||||
use BitBadger\PDODocument\Query;
|
||||
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
||||
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
||||
use BitBadger\PDODocument\{Configuration, Custom, DocumentException, Field, Parameters, Query};
|
||||
use BitBadger\PDODocument\Mapper\{DocumentMapper, ExistsMapper};
|
||||
|
||||
/**
|
||||
* A combined item and feed (used for lists)
|
||||
|
||||
@@ -2,14 +2,8 @@
|
||||
|
||||
namespace FeedReaderCentral;
|
||||
|
||||
use BitBadger\PDODocument\Custom;
|
||||
use BitBadger\PDODocument\Document;
|
||||
use BitBadger\PDODocument\DocumentException;
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Find;
|
||||
use BitBadger\PDODocument\{Custom, Document, DocumentException, Field, Find, Parameters, Query};
|
||||
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
||||
use BitBadger\PDODocument\Parameters;
|
||||
use BitBadger\PDODocument\Query;
|
||||
|
||||
/**
|
||||
* A user of Feed Reader Central
|
||||
|
||||
Reference in New Issue
Block a user