Alpha 6: Feed-level Pages (#21)

Reviewed-on: #21
This commit is contained in:
Daniel J. Summers 2024-05-23 23:04:41 +00:00
parent 93377ffa0e
commit 4fa4dcb831
14 changed files with 256 additions and 85 deletions

View File

@ -1,7 +1,7 @@
<?php
/** The current Feed Reader Central version */
const FRC_VERSION = '1.0.0-alpha5';
const FRC_VERSION = '1.0.0-alpha6';
spl_autoload_register(function ($class) {
$file = implode(DIRECTORY_SEPARATOR, [__DIR__, 'lib', "$class.php"]);

View File

@ -79,26 +79,6 @@ class Data {
}
}
/**
* Retrieve a feed by its ID for the current user
*
* @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 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', $_SESSION[Key::USER_ID]);
$result = $query->execute();
return $result ? $result->fetchArray(SQLITE3_ASSOC) : false;
} finally {
if (is_null($dbConn)) $db->close();
}
}
/**
* Return the last SQLite error message as a result array
*

View File

@ -514,4 +514,18 @@ class Feed {
return sizeof($errors) == 0 ? ['ok' => true] : ['error' => implode("\n", $errors)];
}
/**
* Retrieve a feed by its ID for the current user
*
* @param int $feedId The ID of the feed to retrieve
* @param SQLite3 $db A database connection to use to retrieve the feed
* @return array|bool The data for the feed if found, false if not found
*/
public static function retrieveById(int $feedId, SQLite3 $db): array|bool {
$query = $db->prepare('SELECT * FROM feed WHERE id = :id AND user_id = :user');
$query->bindValue(':id', $feedId);
$query->bindValue(':user', $_SESSION[Key::USER_ID]);
return ($result = $query->execute()) ? $result->fetchArray(SQLITE3_ASSOC) : false;
}
}

View File

@ -22,10 +22,14 @@ header {
border-bottom-right-radius: .5rem;
color: white;
display: flex;
flex-flow: row nowrap;
flex-flow: row wrap;
justify-content: space-between;
align-items: baseline;
div {
margin-bottom: .25rem;
}
.title {
font-size: 1.5rem;
}
@ -101,6 +105,10 @@ article {
width: unset;
}
}
.meta {
font-size: .9rem;
}
}
article.docs {
line-height: 1.4rem;

View File

@ -7,10 +7,11 @@ Security::verifyUser($db, redirectIfAnonymous: false);
page_head('Documentation'); ?>
<h1>Documentation Home</h1>
<article>
<p><a href=./the-cli>About the CLI</a> provides orientation on Feed Reader Central&rsquo;s command line interface
<p><a href=./security-modes>Configuring Security Modes</a> describes the three security modes and how to manage each
of them
<p><a href=./refresh-feeds>Refresh Feeds</a> has instructions on how feeds can be refreshed on a schedule
<p><?=hx_get('./the-cli', 'About the CLI')?> provides orientation on Feed Reader Central&rsquo;s command line
interface
<p><?=hx_get('./security-modes', 'Configuring Security Modes')?> describes the three security modes and how to
manage each of them
<p><?=hx_get('./refresh-feeds', 'Refresh Feeds')?> has instructions on how feeds can be refreshed on a schedule
</article><?php
page_foot();
$db->close();

View File

@ -6,7 +6,7 @@ Security::verifyUser($db, redirectIfAnonymous: false);
page_head('Refresh Feeds | Documentation'); ?>
<h1>Refresh Feeds</h1>
<p class=back-link><a href=./>&lang;&lang; Documentation Home</a>
<p class=back-link><?=hx_get('./', '&lang;&lang; Documentation Home')?>
<article class=docs>
<h2>Manual Feed Refresh</h2>
<p>Next to the &ldquo;Your Unread Items&rdquo; heading on the main page, there is a link labeled &ldquo;Refresh All

View File

@ -6,7 +6,7 @@ Security::verifyUser($db, redirectIfAnonymous: false);
page_head('Security Modes | Documentation'); ?>
<h1>Configuring Security Modes</h1>
<p class=back-link><a href=./>&lang;&lang; Documentation Home</a>
<p class=back-link><?=hx_get('./', '&lang;&lang; Documentation Home')?>
<article class=docs>
<h2>Security Modes</h2>
<p><strong>Single-User</strong> mode assumes that every connection to the application is the same person. It is

View File

@ -6,7 +6,7 @@ Security::verifyUser($db, redirectIfAnonymous: false);
page_head('About the CLI | Documentation'); ?>
<h1>About the CLI</h1>
<p class=back-link><a href=./>&lang;&lang; Documentation Home</a>
<p class=back-link><?=hx_get('./', '&lang;&lang; Documentation Home')?>
<article class=docs>
<p>Feed Reader Central&rsquo;s low-friction design includes having many administrative tasks run in a terminal or
shell. &ldquo;CLI&rdquo; is short for &ldquo;Command Line Interface&rdquo;, and refers to commands that are run

View File

@ -1,32 +1,46 @@
<?php
/**
* Add/Edit Feed Page
* Add/Edit/Delete Feed Page
*
* Allows users to add or edit RSS feeds
* Allows users to add, edit, and delete feeds
*/
include '../start.php';
include '../../start.php';
$db = Data::getConnection();
Security::verifyUser($db);
$feedId = $_GET['id'] ?? '';
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
if (!($feed = Feed::retrieveById($feedId, $db))) not_found();
$itemDelete = $db->prepare('DELETE FROM item WHERE feed_id = :feed');
$itemDelete->bindValue(':feed', $feed['id']);
if (!$itemDelete->execute()) add_error(Data::error($db)['error']);
$feedDelete = $db->prepare('DELETE FROM feed WHERE id = :feed');
$feedDelete->bindValue(':feed', $feed['id']);
if ($feedDelete->execute()) {
add_info('Feed &ldquo;' . htmlentities($feed['title']) . '&rdquo; deleted successfully');
} else {
add_error(Data::error($db)['error']);
}
frc_redirect('/feeds');
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$isNew = $_POST['id'] == 'new';
if ($isNew) {
$result = Feed::add($_POST['url'], $db);
} else {
$toEdit = Data::retrieveFeedById($_POST['id'], $db);
$toEdit = Feed::retrieveById($_POST['id'], $db);
$result = $toEdit ? Feed::update($toEdit, $_POST['url'], $db) : ['error' => "Feed {$_POST['id']} not found"];
}
if (array_key_exists('ok', $result)) {
add_info('Feed saved successfully');
$feedId = $isNew ? $result['ok'] : $_POST['id'];
} else {
add_error($result['error']);
$feedId = 'error';
frc_redirect('/feeds');
}
add_error($result['error']);
$feedId = 'error';
}
if ($feedId == 'new') {
@ -36,19 +50,13 @@ if ($feedId == 'new') {
$title = 'Edit RSS Feed';
if ($feedId == 'error') {
$feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? ''];
} else {
$feed = Data::retrieveFeedById((int) $feedId, $db);
if (!$feed) {
http_response_code(404);
die();
}
}
} elseif (!($feed = Feed::retrieveById((int) $feedId, $db))) not_found();
}
page_head($title); ?>
<h1><?=$title?></h1>
<article>
<form method=POST action=/feed hx-post=/feed>
<form method=POST action=/feed/ hx-post=/feed/>
<input type=hidden name=id value=<?=$feed['id']?>>
<label>
Feed URL

79
src/public/feed/items.php Normal file
View File

@ -0,0 +1,79 @@
<?php
/**
* Feed Item List Page
*
* Lists items in a given feed (all, unread, or bookmarked)
*/
include '../../start.php';
$db = Data::getConnection();
Security::verifyUser($db);
if (!($feed = Feed::retrieveById($_GET['id'], $db))) not_found();
/** Display a list of unread items for this feed */
const TYPE_UNREAD = 0;
/** Display a list of bookmarked items for this feed */
const TYPE_BOOKMARKED = 1;
/** Display all items for this feed */
const TYPE_ALL = 2;
$type = match (true) {
array_key_exists('unread', $_GET) => TYPE_UNREAD,
array_key_exists('bookmarked', $_GET) => TYPE_BOOKMARKED,
default => TYPE_ALL
};
$extraSQL = match ($type) {
TYPE_UNREAD => ' AND is_read = 0',
TYPE_BOOKMARKED => ' AND is_bookmarked = 1',
default => ''
};
$itemQuery = $db->prepare(<<<SQL
SELECT id, title, coalesce(updated_on, published_on) AS as_of, is_read, is_bookmarked
FROM item
WHERE feed_id = :feed$extraSQL
ORDER BY date(coalesce(updated_on, published_on)) DESC
SQL);
$itemQuery->bindValue(':feed', $feed['id']);
if (!($itemResult = $itemQuery->execute())) add_error(Data::error($db)['error']);
$item = $itemResult ? $itemResult->fetchArray(SQLITE3_ASSOC) : false;
$queryParam = match ($type) {
TYPE_UNREAD => '&unread',
TYPE_BOOKMARKED => '&bookmarked',
default => ''
};
$thisURL = urlencode("/feed/items?id={$feed['id']}$queryParam");
$listType = match ($type) {
TYPE_UNREAD => 'Unread',
TYPE_BOOKMARKED => 'Bookmarked',
default => ''
};
page_head(($type != TYPE_ALL ? "$listType Items | " : '') . strip_tags($feed['title']));
if ($type == TYPE_ALL) { ?>
<h1><?=htmlentities($feed['title'])?></h1><?php
} else { ?>
<h1 class=item_heading><?=htmlentities($feed['title'])?></h1>
<div class=item_published><?=$listType?> Items</div><?php
} ?>
<article><?php
if ($item) {
while ($item) { ?>
<p><?=hx_get("/item?id={$item['id']}&from=$thisURL", strip_tags($item['title']))?><br>
<small><?=$item['is_read'] == 0 ? '<strong>New</strong> &nbsp; ' : ''?>
<em><?=date_time($item['as_of'])?></em></small><?php
$item = $itemResult->fetchArray(SQLITE3_ASSOC);
}
} else { ?>
<p><em>There are no <?=strtolower($listType)?> items</em><?php
} ?>
</article>
<?php
page_foot();
$db->close();

51
src/public/feeds.php Normal file
View File

@ -0,0 +1,51 @@
<?php
/**
* Feed Maintenance Page
*
* List feeds and provide links for maintenance actions
*/
include '../start.php';
$db = Data::getConnection();
Security::verifyUser($db);
$feedQuery = $db->prepare('SELECT * FROM feed WHERE user_id = :user ORDER BY lower(title)');
$feedQuery->bindValue(':user', $_SESSION[Key::USER_ID]);
if (!($feedResult = $feedQuery->execute())) {
add_error(Data::error($db)['error']);
}
page_head('Your Feeds'); ?>
<h1>Your Feeds</h1>
<article>
<p class=action_buttons><?=hx_get('/feed/?id=new', 'Add Feed')?></p><?php
if ($feedResult) {
while ($feed = $feedResult->fetchArray(SQLITE3_ASSOC)) {
$feedId = $feed['id'];
$countQuery = $db->prepare(<<<'SQL'
SELECT (SELECT COUNT(*) FROM item WHERE feed_id = :feed) AS total,
(SELECT COUNT(*) FROM item WHERE feed_id = :feed AND is_read = 0) AS unread,
(SELECT COUNT(*) FROM item WHERE feed_id = :feed AND is_bookmarked = 1) AS marked
SQL);
$countQuery->bindValue(':feed', $feed['id']);
$countResult = $countQuery->execute();
$counts = $countResult
? $countResult->fetchArray(SQLITE3_ASSOC) : ['total' => 0, 'unread' => 0, 'marked' => 0]; ?>
<p><strong><?=htmlentities($feed['title'])?></strong><br>
<span class=meta><em>Last Updated <?=date_time($feed['updated_on'])?> &bull;
As of <?=date_time($feed['checked_on'])?></em><br>
<?=hx_get("/feed/?id=$feedId", 'Edit')?> &bull; Read
<?=$counts['unread'] > 0 ? hx_get("/feed/items?id=$feedId&unread", 'Unread') : 'Unread'?>
(<?=$counts['unread']?>) |
<?=$counts['total'] > 0 ? hx_get("/feed/items?id=$feedId", 'All') : 'All'?> (<?=$counts['total']?>) |
<?=$counts['marked'] > 0 ? hx_get("/feed/items?id=$feedId&bookmarked", 'Bookmarked') : 'Bookmarked'?>
(<?=$counts['marked']?>) &bull;
<a href=/feed/?id=<?=$feedId?> hx-delete=/feed/?id=<?=$feedId?>
hx-confirm="Are you sure you want to delete &ldquo;<?=htmlentities($feed['title'], ENT_QUOTES)?>&rdquo;? This will remove the feed and all its items, including unread and bookmarked.">Delete</a>
</span><?php
}
} ?>
</article><?php
page_foot();
$db->close();

View File

@ -20,7 +20,7 @@ if (array_key_exists('refresh', $_GET)) {
}
$query = $db->prepare(<<<'SQL'
SELECT item.id, item.title AS item_title, coalesce(item.updated_on, item.published_on) AS as_of,
SELECT item.id, item.feed_id, item.title AS item_title, coalesce(item.updated_on, item.published_on) AS as_of,
feed.title AS feed_title
FROM item
INNER JOIN feed ON feed.id = item.feed_id
@ -41,8 +41,9 @@ page_head('Your Unread Items'); ?>
<article><?php
if ($item) {
while ($item) { ?>
<p><a href=/item?id=<?=$item['id']?> hx-get=/item?id=<?=$item['id']?>><?=strip_tags($item['item_title'])?></a>
<br><?=htmlentities($item['feed_title'])?><br><small><em><?=date_time($item['as_of'])?></em></small><?php
<p><?=hx_get("/item?id={$item['id']}", strip_tags($item['item_title']))?><br>
<small><?=date_time($item['as_of'])?> &bull;
<?=hx_get("/feed/items?id={$item['feed_id']}&unread", htmlentities($item['feed_title']))?></small><?php
$item = $result->fetchArray(SQLITE3_ASSOC);
}
} else { ?>

View File

@ -27,9 +27,11 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$keepUnread->execute();
}
$db->close();
frc_redirect('/');
frc_redirect($_POST['from']);
}
$from = $_GET['from'] ?? '/';
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
$deleteQuery = $db->prepare(<<<'SQL'
DELETE FROM item
@ -47,7 +49,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
add_error(Data::error($db)['error']);
}
$db->close();
frc_redirect('/');
frc_redirect($from);
}
$query = $db->prepare(<<<'SQL'
@ -83,7 +85,8 @@ page_head(htmlentities("{$item['item_title']} | {$item['feed_title']}")); ?>
<div class=item_content><?=str_replace('<a ', '<a target=_blank rel=noopener ', $item['content'])?></div>
<form class=action_buttons action=/item method=POST hx-post=/item>
<input type=hidden name=id value=<?=$_GET['id']?>>
<a href=/ hx-get="/">Done</a>
<input type=hidden name=from value="<?=$from?>">
<?=hx_get($from, 'Done')?>
<button type=submit>Keep as New</button>
<button type=button hx-delete=/item>Delete</button>
</form>

View File

@ -42,18 +42,38 @@ function add_info(string $message): void {
$is_htmx = array_key_exists('HTTP_HX_REQUEST', $_SERVER)
&& !array_key_exists('HTTP_HX_HISTORY_RESTORE_REQUEST', $_SERVER);
/**
* Render the title bar for the page
*/
function title_bar(): void {
$version = match (true) {
str_ends_with(FRC_VERSION, '.0.0') => substr(FRC_VERSION, 0, strlen(FRC_VERSION) - 4),
str_ends_with(FRC_VERSION, '.0') => substr(FRC_VERSION, 0, strlen(FRC_VERSION) - 2),
default => FRC_VERSION
}; ?>
<header hx-target=#main hx-push-url=true>
<div><?=hx_get('/', 'Feed Reader Central', 'class=title')?><span class=version>v<?=$version?></span></div>
<div><?php
if (array_key_exists(Key::USER_ID, $_SESSION)) { ?>
<?=hx_get('/feeds', 'Feeds')?> | <?=hx_get('/docs/', 'Docs')?> |
<a href=/user/log-off>Log Off</a><?php
if ($_SESSION[Key::USER_EMAIL] != Security::SINGLE_USER_EMAIL) { ?>
| <?=$_SESSION[Key::USER_EMAIL]?><?php
}
} else { ?>
<?=hx_get('/user/log-on', 'Log On')?> | <?=hx_get('/docs/', 'Docs')?><?php
} ?>
</div>
</header>
<main id=main hx-target=this hx-push-url=true hx-swap="innerHTML show:window:top"><?php
}
/**
* Render the page title
* @param string $title The title of the page being displayed
*/
function page_head(string $title): void {
global $is_htmx;
$version = match (true) {
str_ends_with(FRC_VERSION, '.0.0') => substr(FRC_VERSION, 0, strlen(FRC_VERSION) - 4),
str_ends_with(FRC_VERSION, '.0') => substr(FRC_VERSION, 0, strlen(FRC_VERSION) - 2),
default => FRC_VERSION
};
//if ($is_htmx) header('HX-Push-Url: true');
?>
<!DOCTYPE html>
<html lang=en>
@ -61,39 +81,23 @@ function page_head(string $title): void {
<title><?=$title?> | Feed Reader Central</title><?php
if (!$is_htmx) { ?>
<meta name=viewport content="width=device-width, initial-scale=1">
<meta name=htmx-config content='{"historyCacheSize":0}'>
<link href=/assets/style.css rel=stylesheet><?php
} ?>
</head>
<body><?php
if (!$is_htmx) { ?>
<header hx-target=#main hx-push-url=true>
<div><a class=title href=/ hx-get="/">Feed Reader Central</a><span class=version>v<?=$version?></span></div>
<div><?php
if (array_key_exists(Key::USER_ID, $_SESSION)) { ?>
<a href=/feed?id=new hx-get=/feed?id=new>Add Feed</a> |
<a href=/docs/ hx-get=/docs/>Docs</a> |
<a href=/user/log-off hx-get=/user/log-off>Log Off</a><?php
if ($_SESSION[Key::USER_EMAIL] != Security::SINGLE_USER_EMAIL) { ?>
| <?=$_SESSION[Key::USER_EMAIL]?><?php
}
} else { ?>
<a href=/user/log-on hx-get=/user/log-on>Log On</a> | <a href=/docs/ hx-get=/docs/>Docs</a><?php
} ?>
</div>
</header>
<main id=main hx-target=this hx-push-url=true hx-swap="innerHTML show:window:top"><?php
if (!$is_htmx) title_bar();
if (sizeof($messages = $_SESSION[Key::USER_MSG] ?? []) > 0) { ?>
<div class=user_messages><?php
array_walk($messages, function ($msg) { ?>
<div class=user_message>
<?=$msg['level'] == 'INFO' ? '' : "<strong>{$msg['level']}</strong><br>"?>
<?=$msg['message']?>
</div><?php
}); ?>
</div><?php
$_SESSION[Key::USER_MSG] = [];
}
if (sizeof($messages = $_SESSION[Key::USER_MSG] ?? []) > 0) { ?>
<div class=user_messages><?php
array_walk($messages, function ($msg) { ?>
<div class=user_message>
<?=$msg['level'] == 'INFO' ? '' : "<strong>{$msg['level']}</strong><br>"?>
<?=$msg['message']?>
</div><?php
}); ?>
</div><?php
$_SESSION[Key::USER_MSG] = [];
}
}
/**
@ -139,3 +143,25 @@ function date_time(string $value): string {
return '(invalid date)';
}
}
/**
* Create an anchor tag with both `href` and `hx-get` attributes
*
* @param string $url The URL to which navigation should occur
* @param string $text The text for the link
* @param string $extraAttrs Extra attributes for the anchor tag (must be attribute-encoded)
* @return string The anchor tag with both `href` and `hx-get` attributes
*/
function hx_get(string $url, string $text, string $extraAttrs = ''): string {
$attrs = $extraAttrs != '' ? " $extraAttrs" : '';
return "<a href=\"$url\" hx-get=\"$url\"$attrs>$text</a>";
}
/**
* Return a 404 Not Found
*/
#[NoReturn]
function not_found(): void {
http_response_code(404);
die('Not Found');
}