Move list retrieve/render to class (#15)

- array_key_exists -> key_exists
This commit is contained in:
2024-05-25 23:03:39 -04:00
parent c4e85e6734
commit 210377b4da
9 changed files with 215 additions and 126 deletions

View File

@@ -239,19 +239,19 @@ class Feed {
$start = strtolower(strlen($doc['content']) >= 9 ? substr($doc['content'], 0, 9) : $doc['content']);
if ($start == '<!doctype' || str_starts_with($start, '<html')) {
$derivedURL = self::deriveFeedFromHTML($doc['content']);
if (array_key_exists('error', $derivedURL)) return ['error' => $derivedURL['error']];
if (key_exists('error', $derivedURL)) return ['error' => $derivedURL['error']];
$feedURL = $derivedURL['ok'];
if (!str_starts_with($feedURL, 'http')) {
// Relative URL; feed should be retrieved in the context of the original URL
$original = parse_url($url);
$port = array_key_exists('port', $original) ? ":{$original['port']}" : '';
$port = key_exists('port', $original) ? ":{$original['port']}" : '';
$feedURL = $original['scheme'] . '://' . $original['host'] . $port . $feedURL;
}
$doc = self::retrieveDocument($feedURL);
}
$parsed = self::parseFeed($doc['content']);
if (array_key_exists('error', $parsed)) return ['error' => $parsed['error']];
if (key_exists('error', $parsed)) return ['error' => $parsed['error']];
$extract = $parsed['ok']->getElementsByTagNameNS(self::ATOM_NS, 'feed')->length > 0
? self::fromAtom(...) : self::fromRSS(...);
@@ -388,7 +388,7 @@ class Feed {
*/
public static function refreshFeed(int $feedId, string $url, SQLite3 $db): array {
$feedRetrieval = self::retrieveFeed($url);
if (array_key_exists('error', $feedRetrieval)) return $feedRetrieval;
if (key_exists('error', $feedRetrieval)) return $feedRetrieval;
$feed = $feedRetrieval['ok'];
$lastCheckedQuery = $db->prepare('SELECT checked_on FROM feed where id = :id');
@@ -399,7 +399,7 @@ class Feed {
}
$itemUpdate = self::updateItems($feedId, $feed, $lastChecked, $db);
if (array_key_exists('error', $itemUpdate)) return $itemUpdate;
if (key_exists('error', $itemUpdate)) return $itemUpdate;
$urlUpdate = $url == $feed->url ? '' : ', url = :url';
$feedUpdate = $db->prepare(<<<SQL
@@ -428,7 +428,7 @@ class Feed {
*/
public static function add(string $url, SQLite3 $db): array {
$feedExtract = self::retrieveFeed($url);
if (array_key_exists('error', $feedExtract)) return $feedExtract;
if (key_exists('error', $feedExtract)) return $feedExtract;
$feed = $feedExtract['ok'];
@@ -454,7 +454,7 @@ class Feed {
$feedId = $db->lastInsertRowID();
$result = self::updateItems($feedId, $feed, date_create_immutable(WWW_EPOCH), $db);
if (array_key_exists('error', $result)) return $result;
if (key_exists('error', $result)) return $result;
return ['ok' => $feedId];
}
@@ -504,12 +504,12 @@ class Feed {
*/
public static function refreshAll(SQLite3 $db): array {
$feeds = self::retrieveAll($db, $_SESSION[Key::USER_ID]);
if (array_key_exists('error', $feeds)) return $feeds;
if (key_exists('error', $feeds)) return $feeds;
$errors = [];
array_walk($feeds, function ($feed) use ($db, &$errors) {
$result = self::refreshFeed($feed['id'], $feed['url'], $db);
if (array_key_exists('error', $result)) $errors[] = $result['error'];
if (key_exists('error', $result)) $errors[] = $result['error'];
});
return sizeof($errors) == 0 ? ['ok' => true] : ['error' => implode("\n", $errors)];