beta4 changes #26
|
@ -3,6 +3,7 @@
|
||||||
"minimum-stability": "beta",
|
"minimum-stability": "beta",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.2",
|
"php": ">=8.2",
|
||||||
|
"bit-badger/inspired-by-fsharp": "@dev",
|
||||||
"bit-badger/pdo-document": "^1",
|
"bit-badger/pdo-document": "^1",
|
||||||
"ext-curl": "*",
|
"ext-curl": "*",
|
||||||
"ext-dom": "*",
|
"ext-dom": "*",
|
||||||
|
|
51
src/composer.lock
generated
51
src/composer.lock
generated
|
@ -4,8 +4,53 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "d5b7f01ef79cdce09364eed3dca3281d",
|
"content-hash": "2caacda2b3694b265db846cd539e1ca3",
|
||||||
"packages": [
|
"packages": [
|
||||||
|
{
|
||||||
|
"name": "bit-badger/inspired-by-fsharp",
|
||||||
|
"version": "dev-main",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp",
|
||||||
|
"reference": "193147cfb32f423dceb1619f091445efa1a2f13e"
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": "^8"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^11"
|
||||||
|
},
|
||||||
|
"default-branch": true,
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"BitBadger\\InspiredByFSharp\\": "./src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Daniel J. Summers",
|
||||||
|
"email": "daniel@bitbadger.solutions",
|
||||||
|
"homepage": "https://bitbadger.solutions",
|
||||||
|
"role": "Developer"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "PHP utility classes whose functionality is inspired by their F# counterparts",
|
||||||
|
"keywords": [
|
||||||
|
"option",
|
||||||
|
"result"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"email": "daniel@bitbadger.solutions",
|
||||||
|
"rss": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp.rss",
|
||||||
|
"source": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp"
|
||||||
|
},
|
||||||
|
"time": "2024-07-27T02:55:02+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "bit-badger/pdo-document",
|
"name": "bit-badger/pdo-document",
|
||||||
"version": "v1.0.0-beta7",
|
"version": "v1.0.0-beta7",
|
||||||
|
@ -252,7 +297,9 @@
|
||||||
"packages-dev": [],
|
"packages-dev": [],
|
||||||
"aliases": [],
|
"aliases": [],
|
||||||
"minimum-stability": "beta",
|
"minimum-stability": "beta",
|
||||||
"stability-flags": [],
|
"stability-flags": {
|
||||||
|
"bit-badger/inspired-by-fsharp": 20
|
||||||
|
},
|
||||||
"prefer-stable": false,
|
"prefer-stable": false,
|
||||||
"prefer-lowest": false,
|
"prefer-lowest": false,
|
||||||
"platform": {
|
"platform": {
|
||||||
|
|
|
@ -8,12 +8,11 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace FeedReaderCentral;
|
namespace FeedReaderCentral;
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\{Option, Result};
|
||||||
use BitBadger\PDODocument\{
|
use BitBadger\PDODocument\{
|
||||||
Configuration, Custom, Document, DocumentException, DocumentList, Exists, Field, Find, Parameters, Patch, Query
|
Configuration, Custom, Document, DocumentException, DocumentList, Exists, Field, Find, Parameters, Patch, Query
|
||||||
};
|
};
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
use GrahamCampbell\ResultType\{Error, Result, Success};
|
|
||||||
use PhpOption\Option;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An RSS or Atom feed
|
* An RSS or Atom feed
|
||||||
|
@ -114,15 +113,14 @@ class Feed
|
||||||
} else {
|
} else {
|
||||||
Item::add($feedId, $item);
|
Item::add($feedId, $item);
|
||||||
}
|
}
|
||||||
return Success::create(true);
|
return Result::OK(true);
|
||||||
} catch (DocumentException $ex) {
|
} catch (DocumentException $ex) {
|
||||||
return Error::create("$ex");
|
return Result::Error("$ex");
|
||||||
}
|
}
|
||||||
}, array_filter($parsed->items,
|
}, array_filter($parsed->items,
|
||||||
fn(ParsedItem $it) => date_create_immutable($it->updatedOn ?? $it->publishedOn) >= $lastChecked));
|
fn(ParsedItem $it) => date_create_immutable($it->updatedOn ?? $it->publishedOn) >= $lastChecked));
|
||||||
$errors = array_map(fn(Result $it) => $it->error()->get(),
|
$errors = array_map(fn(Result $it) => $it->getError(), array_filter($results, Result::isError(...)));
|
||||||
array_filter($results, fn(Result $it) => $it->error()->isDefined()));
|
return sizeof($errors) > 0 ? Result::Error(implode("\n", $errors)) : Result::OK(true);
|
||||||
return sizeof($errors) > 0 ? Error::create(implode("\n", $errors)) : Success::create(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,13 +159,13 @@ class Feed
|
||||||
SQL;
|
SQL;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return Error::create('Unrecognized purge type ' . PURGE_TYPE);
|
return Result::Error('Unrecognized purge type ' . PURGE_TYPE);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Custom::nonQuery($sql, Parameters::addFields($fields, []));
|
Custom::nonQuery($sql, Parameters::addFields($fields, []));
|
||||||
return Success::create(true);
|
return Result::OK(true);
|
||||||
} catch (DocumentException $ex) {
|
} catch (DocumentException $ex) {
|
||||||
return Error::create("$ex");
|
return Result::Error("$ex");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,16 +180,16 @@ class Feed
|
||||||
public static function refreshFeed(int $feedId, string $url): Result
|
public static function refreshFeed(int $feedId, string $url): Result
|
||||||
{
|
{
|
||||||
$tryRetrieve = ParsedFeed::retrieve($url);
|
$tryRetrieve = ParsedFeed::retrieve($url);
|
||||||
if ($tryRetrieve->error()->isDefined()) return $tryRetrieve->error()->get();
|
if (Result::isError($tryRetrieve)) return $tryRetrieve;
|
||||||
$feed = $tryRetrieve->success()->get();
|
$feed = $tryRetrieve->getOK();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$feedDoc = Find::byId(Table::Feed, $feedId, self::class);
|
$feedDoc = Find::byId(Table::Feed, $feedId, self::class);
|
||||||
if ($feedDoc->isEmpty()) return Error::create('Could not derive date last checked for feed');
|
if ($feedDoc->isEmpty()) return Result::Error('Could not derive date last checked for feed');
|
||||||
$lastChecked = date_create_immutable($feedDoc->get()->checked_on ?? WWW_EPOCH);
|
$lastChecked = date_create_immutable($feedDoc->get()->checked_on ?? WWW_EPOCH);
|
||||||
|
|
||||||
$itemUpdate = self::updateItems($feedId, $feed, $lastChecked);
|
$itemUpdate = self::updateItems($feedId, $feed, $lastChecked);
|
||||||
if ($itemUpdate->error()->isDefined()) return $itemUpdate->error()->get();
|
if (Result::isError($itemUpdate)) return $itemUpdate;
|
||||||
|
|
||||||
$patch = [
|
$patch = [
|
||||||
'title' => $feed->title,
|
'title' => $feed->title,
|
||||||
|
@ -201,10 +199,10 @@ class Feed
|
||||||
if ($url !== $feed->url) $patch['url'] = $feed->url;
|
if ($url !== $feed->url) $patch['url'] = $feed->url;
|
||||||
Patch::byId(Table::Feed, $feedId, $patch);
|
Patch::byId(Table::Feed, $feedId, $patch);
|
||||||
} catch (DocumentException $ex) {
|
} catch (DocumentException $ex) {
|
||||||
return Error::create("$ex");
|
return Result::Error("$ex");
|
||||||
}
|
}
|
||||||
|
|
||||||
return PURGE_TYPE === self::PurgeNone ? Success::create(true) : self::purgeItems($feedId);
|
return PURGE_TYPE === self::PurgeNone ? Result::OK(true) : self::purgeItems($feedId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -216,25 +214,25 @@ class Feed
|
||||||
public static function add(string $url): Result
|
public static function add(string $url): Result
|
||||||
{
|
{
|
||||||
$tryRetrieve = ParsedFeed::retrieve($url);
|
$tryRetrieve = ParsedFeed::retrieve($url);
|
||||||
if ($tryRetrieve->error()->isDefined()) return $tryRetrieve->error()->get();
|
if (Result::isError($tryRetrieve)) return $tryRetrieve;
|
||||||
$feed = $tryRetrieve->success()->get();
|
$feed = $tryRetrieve->getOK();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$fields = [Field::EQ('user_id', $_SESSION[Key::UserId]), Field::EQ('url', $feed->url)];
|
$fields = [Field::EQ('user_id', $_SESSION[Key::UserId]), Field::EQ('url', $feed->url)];
|
||||||
if (Exists::byFields(Table::Feed, $fields)) {
|
if (Exists::byFields(Table::Feed, $fields)) {
|
||||||
return Error::create("Already subscribed to feed $feed->url");
|
return Result::Error("Already subscribed to feed $feed->url");
|
||||||
}
|
}
|
||||||
|
|
||||||
Document::insert(Table::Feed, self::fromParsed($feed));
|
Document::insert(Table::Feed, self::fromParsed($feed));
|
||||||
|
|
||||||
$tryDoc = Find::firstByFields(Table::Feed, $fields, self::class);
|
$tryDoc = Find::firstByFields(Table::Feed, $fields, self::class);
|
||||||
if ($tryDoc->isEmpty()) return Error::create('Could not retrieve inserted feed');
|
if ($tryDoc->isEmpty()) return Result::Error('Could not retrieve inserted feed');
|
||||||
$doc = $tryDoc->get();
|
$doc = $tryDoc->get();
|
||||||
|
|
||||||
$result = self::updateItems($doc->id, $feed, date_create_immutable(WWW_EPOCH));
|
$result = self::updateItems($doc->id, $feed, date_create_immutable(WWW_EPOCH));
|
||||||
return $result->error()->isDefined() ? $result->error()->get() : Success::create($doc->id);
|
return Result::isError($result) ? $result : Result::OK($doc->id);
|
||||||
} catch (DocumentException $ex) {
|
} catch (DocumentException $ex) {
|
||||||
return Error::create("$ex");
|
return Result::Error("$ex");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,7 +251,7 @@ class Feed
|
||||||
['url' => $url]);
|
['url' => $url]);
|
||||||
return self::refreshFeed($existing->id, $url);
|
return self::refreshFeed($existing->id, $url);
|
||||||
} catch (DocumentException $ex) {
|
} catch (DocumentException $ex) {
|
||||||
return Error::create("$ex");
|
return Result::Error("$ex");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,31 +276,31 @@ class Feed
|
||||||
*/
|
*/
|
||||||
public static function refreshAll(): Result
|
public static function refreshAll(): Result
|
||||||
{
|
{
|
||||||
try {
|
$errors = [];
|
||||||
$feeds = self::retrieveAll($_SESSION[Key::UserId]);
|
|
||||||
|
|
||||||
$errors = [];
|
try {
|
||||||
foreach ($feeds->items() as $feed) {
|
self::retrieveAll($_SESSION[Key::UserId])->iter(function (Feed $feed) use (&$errors) {
|
||||||
$result = self::refreshFeed($feed->id, $feed->url);
|
Result::mapError(function (string $err) use (&$errors) { $errors[] = $err; },
|
||||||
if ($result->error()->isDefined()) $errors[] = $result->error()->get();
|
self::refreshFeed($feed->id, $feed->url));
|
||||||
}
|
});
|
||||||
} catch (DocumentException $ex) {
|
} catch (DocumentException $ex) {
|
||||||
return Error::create("$ex");
|
return Result::Error("$ex");
|
||||||
}
|
}
|
||||||
|
|
||||||
return empty($errors) ? Success::create(true) : Error::create(implode("\n", $errors));
|
return empty($errors) ? Result::OK(true) : Result::Error(implode("\n", $errors));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a feed by its ID for the current user
|
* Retrieve a feed by its ID for the current user
|
||||||
*
|
*
|
||||||
* @param int $feedId The ID of the feed to retrieve
|
* @param int $feedId The ID of the feed to retrieve
|
||||||
* @return Option<static> A `Some` value with the data for the feed if found, `None` otherwise
|
* @return Option<Feed> A `Some` value with the data for the feed if found, `None` otherwise
|
||||||
* @throws DocumentException If any is encountered
|
* @throws DocumentException If any is encountered
|
||||||
*/
|
*/
|
||||||
public static function retrieveById(int $feedId): Option
|
public static function retrieveById(int $feedId): Option
|
||||||
{
|
{
|
||||||
return Find::byId(Table::Feed, $feedId, static::class)
|
return to_option(
|
||||||
->filter(fn($it) => $it->user_id === $_SESSION[Key::UserId]);
|
Find::byId(Table::Feed, $feedId, static::class)
|
||||||
|
->filter(fn($it) => $it->user_id === $_SESSION[Key::UserId]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace FeedReaderCentral;
|
namespace FeedReaderCentral;
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Option;
|
||||||
use BitBadger\PDODocument\{Configuration, Custom, DocumentException, Field, Parameters, Query};
|
use BitBadger\PDODocument\{Configuration, Custom, DocumentException, Field, Parameters, Query};
|
||||||
use BitBadger\PDODocument\Mapper\{DocumentMapper, ExistsMapper};
|
use BitBadger\PDODocument\Mapper\{DocumentMapper, ExistsMapper};
|
||||||
use PhpOption\Option;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A combined item and feed (used for lists)
|
* A combined item and feed (used for lists)
|
||||||
|
@ -68,7 +68,8 @@ class ItemWithFeed extends Item
|
||||||
public static function retrieveById(int $id): Option
|
public static function retrieveById(int $id): Option
|
||||||
{
|
{
|
||||||
$fields = self::idAndUserFields($id);
|
$fields = self::idAndUserFields($id);
|
||||||
return Custom::single(self::SELECT_WITH_FEED . ' WHERE ' . Query::whereByFields($fields),
|
return to_option(
|
||||||
Parameters::addFields($fields, []), new DocumentMapper(self::class));
|
Custom::single(self::SELECT_WITH_FEED . ' WHERE ' . Query::whereByFields($fields),
|
||||||
|
Parameters::addFields($fields, []), new DocumentMapper(self::class)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,13 +8,11 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace FeedReaderCentral;
|
namespace FeedReaderCentral;
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Result;
|
||||||
use DOMDocument;
|
use DOMDocument;
|
||||||
use DOMElement;
|
use DOMElement;
|
||||||
use DOMException;
|
use DOMException;
|
||||||
use DOMNode;
|
use DOMNode;
|
||||||
use GrahamCampbell\ResultType\Error;
|
|
||||||
use GrahamCampbell\ResultType\Result;
|
|
||||||
use GrahamCampbell\ResultType\Success;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A feed, as parsed from the Atom or RSS XML
|
* A feed, as parsed from the Atom or RSS XML
|
||||||
|
@ -73,9 +71,9 @@ readonly class ParsedFeed
|
||||||
try {
|
try {
|
||||||
$feed = new DOMDocument();
|
$feed = new DOMDocument();
|
||||||
$feed->loadXML($content);
|
$feed->loadXML($content);
|
||||||
return Success::create($feed);
|
return Result::OK($feed);
|
||||||
} catch (DOMException $ex) {
|
} catch (DOMException $ex) {
|
||||||
return Error::create($ex->getMessage());
|
return Result::Error($ex->getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
restore_error_handler();
|
restore_error_handler();
|
||||||
}
|
}
|
||||||
|
@ -106,7 +104,7 @@ readonly class ParsedFeed
|
||||||
$channel = $xml->getElementsByTagName('channel')->item(0);
|
$channel = $xml->getElementsByTagName('channel')->item(0);
|
||||||
if (!($channel instanceof DOMElement)) {
|
if (!($channel instanceof DOMElement)) {
|
||||||
$type = $channel?->nodeType ?? -1;
|
$type = $channel?->nodeType ?? -1;
|
||||||
return Error::create("Channel element not found ($type)");
|
return Result::Error("Channel element not found ($type)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Atom namespace provides a lastBuildDate, which contains the last time an item in the feed was updated; if
|
// The Atom namespace provides a lastBuildDate, which contains the last time an item in the feed was updated; if
|
||||||
|
@ -117,7 +115,7 @@ readonly class ParsedFeed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Success::create(new self(
|
return Result::OK(new self(
|
||||||
url: $url,
|
url: $url,
|
||||||
title: self::rssValue($channel, 'title'),
|
title: self::rssValue($channel, 'title'),
|
||||||
updatedOn: Data::formatDate($updatedOn),
|
updatedOn: Data::formatDate($updatedOn),
|
||||||
|
@ -172,7 +170,7 @@ readonly class ParsedFeed
|
||||||
$root = $xml->getElementsByTagNameNS(self::ATOM_NS, 'feed')->item(0);
|
$root = $xml->getElementsByTagNameNS(self::ATOM_NS, 'feed')->item(0);
|
||||||
if (($updatedOn = self::atomValue($root, 'updated')) == 'pubDate not found') $updatedOn = null;
|
if (($updatedOn = self::atomValue($root, 'updated')) == 'pubDate not found') $updatedOn = null;
|
||||||
|
|
||||||
return Success::create(new self(
|
return Result::OK(new self(
|
||||||
url: $url,
|
url: $url,
|
||||||
title: self::atomValue($root, 'title'),
|
title: self::atomValue($root, 'title'),
|
||||||
updatedOn: Data::formatDate($updatedOn),
|
updatedOn: Data::formatDate($updatedOn),
|
||||||
|
@ -197,9 +195,9 @@ readonly class ParsedFeed
|
||||||
curl_setopt($docReq, CURLOPT_USERAGENT, self::USER_AGENT);
|
curl_setopt($docReq, CURLOPT_USERAGENT, self::USER_AGENT);
|
||||||
|
|
||||||
$error = curl_error($docReq);
|
$error = curl_error($docReq);
|
||||||
if ($error !== '') return Error::create($error);
|
if ($error !== '') return Result::Error($error);
|
||||||
|
|
||||||
return Success::create([
|
return Result::OK([
|
||||||
'content' => curl_exec($docReq),
|
'content' => curl_exec($docReq),
|
||||||
'code' => curl_getinfo($docReq, CURLINFO_RESPONSE_CODE),
|
'code' => curl_getinfo($docReq, CURLINFO_RESPONSE_CODE),
|
||||||
'url' => curl_getinfo($docReq, CURLINFO_EFFECTIVE_URL)
|
'url' => curl_getinfo($docReq, CURLINFO_EFFECTIVE_URL)
|
||||||
|
@ -220,17 +218,17 @@ readonly class ParsedFeed
|
||||||
$html = new DOMDocument();
|
$html = new DOMDocument();
|
||||||
$html->loadHTML(substr($content, 0, strpos($content, '</head>') + 7));
|
$html->loadHTML(substr($content, 0, strpos($content, '</head>') + 7));
|
||||||
$headTags = $html->getElementsByTagName('head');
|
$headTags = $html->getElementsByTagName('head');
|
||||||
if ($headTags->length < 1) return Error::create('Cannot find feed at this URL');
|
if ($headTags->length < 1) return Result::Error('Cannot find feed at this URL');
|
||||||
$head = $headTags->item(0);
|
$head = $headTags->item(0);
|
||||||
foreach ($head->getElementsByTagName('link') as $link) {
|
foreach ($head->getElementsByTagName('link') as $link) {
|
||||||
if (self::attrValue($link, 'rel') === 'alternate') {
|
if (self::attrValue($link, 'rel') === 'alternate') {
|
||||||
$type = self::attrValue($link, 'type');
|
$type = self::attrValue($link, 'type');
|
||||||
if ($type === 'application/rss+xml' || $type === 'application/atom+xml') {
|
if ($type === 'application/rss+xml' || $type === 'application/atom+xml') {
|
||||||
return Success::create(self::attrValue($link, 'href'));
|
return Result::OK(self::attrValue($link, 'href'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Error::create('Cannot find feed at this URL');
|
return Result::Error('Cannot find feed at this URL');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -242,18 +240,18 @@ readonly class ParsedFeed
|
||||||
public static function retrieve(string $url): Result
|
public static function retrieve(string $url): Result
|
||||||
{
|
{
|
||||||
$tryDoc = self::retrieveDocument($url);
|
$tryDoc = self::retrieveDocument($url);
|
||||||
|
if (Result::isError($tryDoc)) return $tryDoc;
|
||||||
|
|
||||||
if ($tryDoc->error()->isDefined()) return $tryDoc->error()->get();
|
$doc = $tryDoc->getOK();
|
||||||
$doc = $tryDoc->success()->get();
|
|
||||||
if ($doc['code'] !== 200) {
|
if ($doc['code'] !== 200) {
|
||||||
return Error::create("Prospective feed URL $url returned HTTP Code {$doc['code']}: {$doc['content']}");
|
return Result::Error("Prospective feed URL $url returned HTTP Code {$doc['code']}: {$doc['content']}");
|
||||||
}
|
}
|
||||||
|
|
||||||
$start = strtolower(strlen($doc['content']) >= 9 ? substr($doc['content'], 0, 9) : $doc['content']);
|
$start = strtolower(strlen($doc['content']) >= 9 ? substr($doc['content'], 0, 9) : $doc['content']);
|
||||||
if ($start === '<!doctype' || str_starts_with($start, '<html')) {
|
if ($start === '<!doctype' || str_starts_with($start, '<html')) {
|
||||||
$derivedURL = self::deriveFeedFromHTML($doc['content']);
|
$derivedURL = self::deriveFeedFromHTML($doc['content']);
|
||||||
if ($derivedURL->error()->isDefined()) return $derivedURL->error()->get();
|
if (Result::isError($derivedURL)) return $derivedURL;
|
||||||
$feedURL = $derivedURL->success()->get();
|
$feedURL = $derivedURL->getOK();
|
||||||
if (!str_starts_with($feedURL, 'http')) {
|
if (!str_starts_with($feedURL, 'http')) {
|
||||||
// Relative URL; feed should be retrieved in the context of the original URL
|
// Relative URL; feed should be retrieved in the context of the original URL
|
||||||
$original = parse_url($url);
|
$original = parse_url($url);
|
||||||
|
@ -261,17 +259,17 @@ readonly class ParsedFeed
|
||||||
$feedURL = $original['scheme'] . '://' . $original['host'] . $port . $feedURL;
|
$feedURL = $original['scheme'] . '://' . $original['host'] . $port . $feedURL;
|
||||||
}
|
}
|
||||||
$tryDoc = self::retrieveDocument($feedURL);
|
$tryDoc = self::retrieveDocument($feedURL);
|
||||||
if ($tryDoc->error()->isDefined()) return $tryDoc->error()->get();
|
if (Result::isError($tryDoc)) return $tryDoc;
|
||||||
$doc = $tryDoc->success()->get();
|
$doc = $tryDoc->getOK();
|
||||||
if ($doc['code'] !== 200) {
|
if ($doc['code'] !== 200) {
|
||||||
return Error::create("Derived feed URL $url returned HTTP Code {$doc['code']}: {$doc['content']}");
|
return Result::Error("Derived feed URL $url returned HTTP Code {$doc['code']}: {$doc['content']}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$tryParse = self::parseFeed($doc['content']);
|
$tryParse = self::parseFeed($doc['content']);
|
||||||
if ($tryParse->error()->isDefined()) return $tryParse->error()->get();
|
if (Result::isError($tryParse)) return $tryParse;
|
||||||
|
|
||||||
$parsed = $tryParse->success()->get();
|
$parsed = $tryParse->getOK();
|
||||||
$extract = $parsed->getElementsByTagNameNS(self::ATOM_NS, 'feed')->length > 0
|
$extract = $parsed->getElementsByTagNameNS(self::ATOM_NS, 'feed')->length > 0
|
||||||
? self::fromAtom(...) : self::fromRSS(...);
|
? self::fromAtom(...) : self::fromRSS(...);
|
||||||
return $extract($parsed, $doc['url']);
|
return $extract($parsed, $doc['url']);
|
||||||
|
|
|
@ -8,6 +8,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace FeedReaderCentral;
|
namespace FeedReaderCentral;
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Option;
|
||||||
use BitBadger\PDODocument\{DocumentException, Field, Patch};
|
use BitBadger\PDODocument\{DocumentException, Field, Patch};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -93,8 +94,7 @@ readonly class Security
|
||||||
}
|
}
|
||||||
$dbEmail = $email;
|
$dbEmail = $email;
|
||||||
}
|
}
|
||||||
$user = User::findByEmail($dbEmail);
|
Option::iter(fn(User $it) => self::verifyPassword($it, $password, $returnTo), User::findByEmail($dbEmail));
|
||||||
if ($user->isDefined()) self::verifyPassword($user->get(), $password, $returnTo);
|
|
||||||
add_error('Invalid credentials; log on unsuccessful');
|
add_error('Invalid credentials; log on unsuccessful');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +119,7 @@ readonly class Security
|
||||||
private static function logOnSingleUser(): void
|
private static function logOnSingleUser(): void
|
||||||
{
|
{
|
||||||
$user = User::findByEmail(self::SingleUserEmail);
|
$user = User::findByEmail(self::SingleUserEmail);
|
||||||
if ($user->isEmpty()) {
|
if (Option::isNone($user)) {
|
||||||
User::add(self::SingleUserEmail, self::SingleUserPassword);
|
User::add(self::SingleUserEmail, self::SingleUserPassword);
|
||||||
$user = User::findByEmail(self::SingleUserEmail);
|
$user = User::findByEmail(self::SingleUserEmail);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace FeedReaderCentral;
|
namespace FeedReaderCentral;
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Option;
|
||||||
use BitBadger\PDODocument\{Custom, Document, DocumentException, Field, Find, Parameters, Query};
|
use BitBadger\PDODocument\{Custom, Document, DocumentException, Field, Find, Parameters, Query};
|
||||||
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
||||||
use PhpOption\Option;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A user of Feed Reader Central
|
* A user of Feed Reader Central
|
||||||
|
@ -35,7 +35,7 @@ class User
|
||||||
*/
|
*/
|
||||||
public static function findByEmail(string $email): Option
|
public static function findByEmail(string $email): Option
|
||||||
{
|
{
|
||||||
return Find::firstByFields(Table::User, [Field::EQ('email', $email)], User::class);
|
return to_option(Find::firstByFields(Table::User, [Field::EQ('email', $email)], User::class));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Option;
|
||||||
use BitBadger\PDODocument\{DocumentException, Patch};
|
use BitBadger\PDODocument\{DocumentException, Patch};
|
||||||
use FeedReaderCentral\{ItemWithFeed, Table};
|
use FeedReaderCentral\{ItemWithFeed, Table};
|
||||||
|
|
||||||
|
@ -18,22 +19,21 @@ include '../start.php';
|
||||||
FeedReaderCentral\Security::verifyUser();
|
FeedReaderCentral\Security::verifyUser();
|
||||||
|
|
||||||
$id = key_exists('id', $_GET) ? (int)$_GET['id'] : -1;
|
$id = key_exists('id', $_GET) ? (int)$_GET['id'] : -1;
|
||||||
$item = ItemWithFeed::retrieveById($id)->getOrCall(not_found(...));
|
$item = Option::getOrCall(not_found(...), ItemWithFeed::retrieveById($id));
|
||||||
|
|
||||||
if (key_exists('action', $_GET)) {
|
if (key_exists('action', $_GET)) {
|
||||||
$flag = match ($_GET['action']) {
|
Option::iter(function (int $flag) use ($id, &$item) {
|
||||||
'add' => 1,
|
|
||||||
'remove' => 0,
|
|
||||||
default => null
|
|
||||||
};
|
|
||||||
if (isset($flag)) {
|
|
||||||
try {
|
try {
|
||||||
Patch::byId(Table::Item, $id, ['is_bookmarked' => $flag]);
|
Patch::byId(Table::Item, $id, ['is_bookmarked' => $flag]);
|
||||||
$item->is_bookmarked = $flag;
|
$item->is_bookmarked = $flag;
|
||||||
} catch (DocumentException $ex) {
|
} catch (DocumentException $ex) {
|
||||||
add_error("$ex");
|
add_error("$ex");
|
||||||
}
|
}
|
||||||
}
|
}, match ($_GET['action']) {
|
||||||
|
'add' => Option::Some(1),
|
||||||
|
'remove' => Option::Some(0),
|
||||||
|
default => Option::None(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
$action = $item->isBookmarked() ? 'remove' : 'add';
|
$action = $item->isBookmarked() ? 'remove' : 'add';
|
||||||
|
|
|
@ -10,9 +10,9 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\{Option, Result};
|
||||||
use BitBadger\PDODocument\{Delete, DocumentException, Field};
|
use BitBadger\PDODocument\{Delete, DocumentException, Field};
|
||||||
use FeedReaderCentral\{Feed, Security, Table};
|
use FeedReaderCentral\{Feed, Security, Table};
|
||||||
use GrahamCampbell\ResultType\Error;
|
|
||||||
|
|
||||||
include '../../start.php';
|
include '../../start.php';
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ $feedId = key_exists('id', $_GET) ? (int)$_GET['id'] : -1;
|
||||||
switch ($_SERVER['REQUEST_METHOD']) {
|
switch ($_SERVER['REQUEST_METHOD']) {
|
||||||
case 'DELETE':
|
case 'DELETE':
|
||||||
try {
|
try {
|
||||||
$feed = Feed::retrieveById($feedId)->getOrCall(not_found(...));
|
$feed = Option::getOrCall(not_found(...), Feed::retrieveById($feedId));
|
||||||
Delete::byFields(Table::Item, [Field::EQ('feed_id', $feed->id)]);
|
Delete::byFields(Table::Item, [Field::EQ('feed_id', $feed->id)]);
|
||||||
Delete::byId(Table::Feed, $feed->id);
|
Delete::byId(Table::Feed, $feed->id);
|
||||||
add_info('Feed “' . htmlentities($feed->title) . '” deleted successfully');
|
add_info('Feed “' . htmlentities($feed->title) . '” deleted successfully');
|
||||||
|
@ -40,15 +40,15 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
||||||
} else {
|
} else {
|
||||||
$feedId = (int)$_POST['id'];
|
$feedId = (int)$_POST['id'];
|
||||||
$toEdit = Feed::retrieveById($feedId);
|
$toEdit = Feed::retrieveById($feedId);
|
||||||
$result = $toEdit->isDefined()
|
$result = Option::isSome($toEdit)
|
||||||
? Feed::update($toEdit->get(), $_POST['url'])
|
? Feed::update($toEdit->get(), $_POST['url'])
|
||||||
: Error::create("Feed $feedId not found");
|
: Result::Error("Feed $feedId not found");
|
||||||
}
|
}
|
||||||
if ($result->success()->isDefined()) {
|
if (Result::isOK($result)) {
|
||||||
add_info('Feed saved successfully');
|
add_info('Feed saved successfully');
|
||||||
frc_redirect('/feeds');
|
frc_redirect('/feeds');
|
||||||
}
|
}
|
||||||
add_error($result->error()->get());
|
add_error($result->getError());
|
||||||
$feedId = 'error';
|
$feedId = 'error';
|
||||||
} catch (DocumentException $ex) {
|
} catch (DocumentException $ex) {
|
||||||
add_error("$ex");
|
add_error("$ex");
|
||||||
|
@ -63,7 +63,7 @@ if ($feedId == -1) {
|
||||||
$title = 'Edit RSS Feed';
|
$title = 'Edit RSS Feed';
|
||||||
$feed = $feedId == 'error'
|
$feed = $feedId == 'error'
|
||||||
? new Feed(id: (int)$_POST['id'], url: $_POST['url'] ?? '')
|
? new Feed(id: (int)$_POST['id'], url: $_POST['url'] ?? '')
|
||||||
: Feed::retrieveById((int)$feedId)->getOrCall(not_found(...));
|
: Option::getOrCall(not_found(...), Feed::retrieveById((int)$feedId));
|
||||||
}
|
}
|
||||||
|
|
||||||
page_head($title); ?>
|
page_head($title); ?>
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Option;
|
||||||
use FeedReaderCentral\{Feed, ItemList};
|
use FeedReaderCentral\{Feed, ItemList};
|
||||||
|
|
||||||
include '../../start.php';
|
include '../../start.php';
|
||||||
|
@ -17,7 +18,7 @@ include '../../start.php';
|
||||||
FeedReaderCentral\Security::verifyUser();
|
FeedReaderCentral\Security::verifyUser();
|
||||||
|
|
||||||
$id = key_exists('id', $_GET) ? (int)$_GET['id'] : -1;
|
$id = key_exists('id', $_GET) ? (int)$_GET['id'] : -1;
|
||||||
$feed = Feed::retrieveById($id)->getOrCall(not_found(...));
|
$feed = Option::getOrCall(not_found(...), Feed::retrieveById($id));
|
||||||
|
|
||||||
$list = match (true) {
|
$list = match (true) {
|
||||||
key_exists('unread', $_GET) => ItemList::unreadForFeed($feed->id),
|
key_exists('unread', $_GET) => ItemList::unreadForFeed($feed->id),
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Result;
|
||||||
use FeedReaderCentral\{Feed, ItemList};
|
use FeedReaderCentral\{Feed, ItemList};
|
||||||
|
|
||||||
include '../start.php';
|
include '../start.php';
|
||||||
|
@ -18,16 +19,16 @@ FeedReaderCentral\Security::verifyUser();
|
||||||
|
|
||||||
if (key_exists('refresh', $_GET)) {
|
if (key_exists('refresh', $_GET)) {
|
||||||
$refreshResult = Feed::refreshAll();
|
$refreshResult = Feed::refreshAll();
|
||||||
if ($refreshResult->success()->isDefined()) {
|
if (Result::isOK($refreshResult)) {
|
||||||
add_info('All feeds refreshed successfully');
|
add_info('All feeds refreshed successfully');
|
||||||
} else {
|
} else {
|
||||||
add_error(nl2br($refreshResult->error()->get()));
|
add_error(nl2br($refreshResult->getError()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$list = match (true) {
|
$list = match (true) {
|
||||||
key_exists('bookmarked', $_GET) => ItemList::allBookmarked(),
|
key_exists('bookmarked', $_GET) => ItemList::allBookmarked(),
|
||||||
default => ItemList::allUnread()
|
default => ItemList::allUnread(),
|
||||||
};
|
};
|
||||||
$title = "Your $list->itemType Items";
|
$title = "Your $list->itemType Items";
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Option;
|
||||||
use BitBadger\PDODocument\{Delete, DocumentException, Patch};
|
use BitBadger\PDODocument\{Delete, DocumentException, Patch};
|
||||||
use FeedReaderCentral\{ItemWithFeed, Table};
|
use FeedReaderCentral\{ItemWithFeed, Table};
|
||||||
|
|
||||||
|
@ -49,7 +50,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
||||||
frc_redirect($from);
|
frc_redirect($from);
|
||||||
}
|
}
|
||||||
|
|
||||||
!$item = ItemWithFeed::retrieveById($id)->getOrCall(not_found(...));
|
!$item = Option::getOrCall(not_found(...), ItemWithFeed::retrieveById($id));
|
||||||
try {
|
try {
|
||||||
Patch::byId(Table::Item, $id, ['is_read' => 1]);
|
Patch::byId(Table::Item, $id, ['is_read' => 1]);
|
||||||
} catch (DocumentException $ex) {
|
} catch (DocumentException $ex) {
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Option;
|
||||||
use BitBadger\PDODocument\Configuration;
|
use BitBadger\PDODocument\Configuration;
|
||||||
use FeedReaderCentral\{Key, Security, User};
|
use FeedReaderCentral\{Key, Security, User};
|
||||||
|
|
||||||
|
@ -186,3 +187,14 @@ function not_found(): never
|
||||||
http_response_code(404);
|
http_response_code(404);
|
||||||
die('Not Found');
|
die('Not Found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform a `PhpOption` into the option we use
|
||||||
|
*
|
||||||
|
* @param \PhpOption\Option $option The option to be transformed
|
||||||
|
* @return Option An option in the form Feed Reader Central uses
|
||||||
|
*/
|
||||||
|
function to_option(PhpOption\Option $option): Option
|
||||||
|
{
|
||||||
|
return $option->isDefined() ? Option::Some($option->get()) : Option::None();
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Result;
|
||||||
use BitBadger\PDODocument\{DocumentException, Find};
|
use BitBadger\PDODocument\{DocumentException, Find};
|
||||||
use FeedReaderCentral\{Feed, Table, User};
|
use FeedReaderCentral\{Feed, Table, User};
|
||||||
|
|
||||||
|
@ -51,9 +52,9 @@ function refresh_all(): void
|
||||||
$users[$userKey] = Find::byId(Table::User, $feed->user_id, User::class)
|
$users[$userKey] = Find::byId(Table::User, $feed->user_id, User::class)
|
||||||
->getOrElse(new User(email: 'user-not-found'));
|
->getOrElse(new User(email: 'user-not-found'));
|
||||||
}
|
}
|
||||||
if ($result->error()->isDefined()) {
|
if (Result::isError($result)) {
|
||||||
printfn('ERR (%s) %s', $users[$userKey]->email, $feed->url);
|
printfn('ERR (%s) %s', $users[$userKey]->email, $feed->url);
|
||||||
printfn(' %s', $result->error()->get());
|
printfn(' %s', $result->getError());
|
||||||
} else {
|
} else {
|
||||||
printfn('OK (%s) %s', $users[$userKey]->email, $feed->url);
|
printfn('OK (%s) %s', $users[$userKey]->email, $feed->url);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use BitBadger\InspiredByFSharp\Option;
|
||||||
use BitBadger\PDODocument\{Count, Custom, Delete, DocumentException, Field, Parameters, Patch, Query};
|
use BitBadger\PDODocument\{Count, Custom, Delete, DocumentException, Field, Parameters, Patch, Query};
|
||||||
use FeedReaderCentral\{Security, Table, User};
|
use FeedReaderCentral\{Security, Table, User};
|
||||||
|
|
||||||
|
@ -103,7 +104,7 @@ function add_user(): void
|
||||||
try {
|
try {
|
||||||
// Ensure there is not already a user with this e-mail address
|
// Ensure there is not already a user with this e-mail address
|
||||||
$user = User::findByEmail($argv[2]);
|
$user = User::findByEmail($argv[2]);
|
||||||
if ($user->isDefined()) {
|
if (Option::isSome($user)) {
|
||||||
printfn('A user with e-mail address "%s" already exists', $argv[2]);
|
printfn('A user with e-mail address "%s" already exists', $argv[2]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -124,7 +125,7 @@ function add_user(): void
|
||||||
*/
|
*/
|
||||||
function display_user(string $email): string
|
function display_user(string $email): string
|
||||||
{
|
{
|
||||||
return $email == Security::SingleUserEmail ? 'single-user mode user' : "user \"$email\"";
|
return $email === Security::SingleUserEmail ? 'single-user mode user' : "user \"$email\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -137,7 +138,7 @@ function set_password(string $email, string $password): void
|
||||||
|
|
||||||
// Ensure this user exists
|
// Ensure this user exists
|
||||||
$user = User::findByEmail($email);
|
$user = User::findByEmail($email);
|
||||||
if ($user->isEmpty()) {
|
if (Option::isNone($user)) {
|
||||||
printfn('No %s exists', $displayUser);
|
printfn('No %s exists', $displayUser);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -164,7 +165,7 @@ function delete_user(string $email): void
|
||||||
|
|
||||||
// Get the user for the provided e-mail address
|
// Get the user for the provided e-mail address
|
||||||
$tryUser = User::findByEmail($email);
|
$tryUser = User::findByEmail($email);
|
||||||
if ($tryUser->isEmpty()) {
|
if (Option::isNone($tryUser)) {
|
||||||
printfn('No %s exists', $displayUser);
|
printfn('No %s exists', $displayUser);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -209,7 +210,7 @@ function migrate_single_user(): void
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$single = User::findByEmail(Security::SingleUserEmail);
|
$single = User::findByEmail(Security::SingleUserEmail);
|
||||||
if ($single->isEmpty()) {
|
if (Option::isNone($single)) {
|
||||||
printfn('There is no single-user mode user to be migrated');
|
printfn('There is no single-user mode user to be migrated');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user