Alpha 6: Feed-level Pages #21

Merged
danieljsummers merged 4 commits from add-via-html into main 2024-05-23 23:04:41 +00:00
14 changed files with 256 additions and 85 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
/** The current Feed Reader Central version */ /** 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) { spl_autoload_register(function ($class) {
$file = implode(DIRECTORY_SEPARATOR, [__DIR__, 'lib', "$class.php"]); $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 * 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)]; 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; border-bottom-right-radius: .5rem;
color: white; color: white;
display: flex; display: flex;
flex-flow: row nowrap; flex-flow: row wrap;
justify-content: space-between; justify-content: space-between;
align-items: baseline; align-items: baseline;
div {
margin-bottom: .25rem;
}
.title { .title {
font-size: 1.5rem; font-size: 1.5rem;
} }
@ -101,6 +105,10 @@ article {
width: unset; width: unset;
} }
} }
.meta {
font-size: .9rem;
}
} }
article.docs { article.docs {
line-height: 1.4rem; line-height: 1.4rem;

View File

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

View File

@ -6,7 +6,7 @@ Security::verifyUser($db, redirectIfAnonymous: false);
page_head('Refresh Feeds | Documentation'); ?> page_head('Refresh Feeds | Documentation'); ?>
<h1>Refresh Feeds</h1> <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> <article class=docs>
<h2>Manual Feed Refresh</h2> <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 <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'); ?> page_head('Security Modes | Documentation'); ?>
<h1>Configuring Security Modes</h1> <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> <article class=docs>
<h2>Security Modes</h2> <h2>Security Modes</h2>
<p><strong>Single-User</strong> mode assumes that every connection to the application is the same person. It is <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'); ?> page_head('About the CLI | Documentation'); ?>
<h1>About the CLI</h1> <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> <article class=docs>
<p>Feed Reader Central&rsquo;s low-friction design includes having many administrative tasks run in a terminal or <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 shell. &ldquo;CLI&rdquo; is short for &ldquo;Command Line Interface&rdquo;, and refers to commands that are run

View File

@ -1,33 +1,47 @@
<?php <?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(); $db = Data::getConnection();
Security::verifyUser($db); Security::verifyUser($db);
$feedId = $_GET['id'] ?? ''; $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') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$isNew = $_POST['id'] == 'new'; $isNew = $_POST['id'] == 'new';
if ($isNew) { if ($isNew) {
$result = Feed::add($_POST['url'], $db); $result = Feed::add($_POST['url'], $db);
} else { } 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"]; $result = $toEdit ? Feed::update($toEdit, $_POST['url'], $db) : ['error' => "Feed {$_POST['id']} not found"];
} }
if (array_key_exists('ok', $result)) { if (array_key_exists('ok', $result)) {
add_info('Feed saved successfully'); add_info('Feed saved successfully');
$feedId = $isNew ? $result['ok'] : $_POST['id']; frc_redirect('/feeds');
} else { }
add_error($result['error']); add_error($result['error']);
$feedId = 'error'; $feedId = 'error';
} }
}
if ($feedId == 'new') { if ($feedId == 'new') {
$title = 'Add RSS Feed'; $title = 'Add RSS Feed';
@ -36,19 +50,13 @@ if ($feedId == 'new') {
$title = 'Edit RSS Feed'; $title = 'Edit RSS Feed';
if ($feedId == 'error') { if ($feedId == 'error') {
$feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? '']; $feed = ['id' => $_POST['id'] ?? '', 'url' => $_POST['url'] ?? ''];
} else { } elseif (!($feed = Feed::retrieveById((int) $feedId, $db))) not_found();
$feed = Data::retrieveFeedById((int) $feedId, $db);
if (!$feed) {
http_response_code(404);
die();
}
}
} }
page_head($title); ?> page_head($title); ?>
<h1><?=$title?></h1> <h1><?=$title?></h1>
<article> <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']?>> <input type=hidden name=id value=<?=$feed['id']?>>
<label> <label>
Feed URL 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' $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 feed.title AS feed_title
FROM item FROM item
INNER JOIN feed ON feed.id = item.feed_id INNER JOIN feed ON feed.id = item.feed_id
@ -41,8 +41,9 @@ page_head('Your Unread Items'); ?>
<article><?php <article><?php
if ($item) { if ($item) {
while ($item) { ?> while ($item) { ?>
<p><a href=/item?id=<?=$item['id']?> hx-get=/item?id=<?=$item['id']?>><?=strip_tags($item['item_title'])?></a> <p><?=hx_get("/item?id={$item['id']}", strip_tags($item['item_title']))?><br>
<br><?=htmlentities($item['feed_title'])?><br><small><em><?=date_time($item['as_of'])?></em></small><?php <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); $item = $result->fetchArray(SQLITE3_ASSOC);
} }
} else { ?> } else { ?>

View File

@ -27,9 +27,11 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$keepUnread->execute(); $keepUnread->execute();
} }
$db->close(); $db->close();
frc_redirect('/'); frc_redirect($_POST['from']);
} }
$from = $_GET['from'] ?? '/';
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') { if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
$deleteQuery = $db->prepare(<<<'SQL' $deleteQuery = $db->prepare(<<<'SQL'
DELETE FROM item DELETE FROM item
@ -47,7 +49,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
add_error(Data::error($db)['error']); add_error(Data::error($db)['error']);
} }
$db->close(); $db->close();
frc_redirect('/'); frc_redirect($from);
} }
$query = $db->prepare(<<<'SQL' $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> <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> <form class=action_buttons action=/item method=POST hx-post=/item>
<input type=hidden name=id value=<?=$_GET['id']?>> <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=submit>Keep as New</button>
<button type=button hx-delete=/item>Delete</button> <button type=button hx-delete=/item>Delete</button>
</form> </form>

View File

@ -42,18 +42,38 @@ function add_info(string $message): void {
$is_htmx = array_key_exists('HTTP_HX_REQUEST', $_SERVER) $is_htmx = array_key_exists('HTTP_HX_REQUEST', $_SERVER)
&& !array_key_exists('HTTP_HX_HISTORY_RESTORE_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 * Render the page title
* @param string $title The title of the page being displayed * @param string $title The title of the page being displayed
*/ */
function page_head(string $title): void { function page_head(string $title): void {
global $is_htmx; 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> <!DOCTYPE html>
<html lang=en> <html lang=en>
@ -61,28 +81,12 @@ function page_head(string $title): void {
<title><?=$title?> | Feed Reader Central</title><?php <title><?=$title?> | Feed Reader Central</title><?php
if (!$is_htmx) { ?> if (!$is_htmx) { ?>
<meta name=viewport content="width=device-width, initial-scale=1"> <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 <link href=/assets/style.css rel=stylesheet><?php
} ?> } ?>
</head> </head>
<body><?php <body><?php
if (!$is_htmx) { ?> if (!$is_htmx) title_bar();
<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 (sizeof($messages = $_SESSION[Key::USER_MSG] ?? []) > 0) { ?> if (sizeof($messages = $_SESSION[Key::USER_MSG] ?? []) > 0) { ?>
<div class=user_messages><?php <div class=user_messages><?php
array_walk($messages, function ($msg) { ?> array_walk($messages, function ($msg) { ?>
@ -139,3 +143,25 @@ function date_time(string $value): string {
return '(invalid date)'; 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');
}