Alpha 5 #20

Merged
danieljsummers merged 5 commits from add-via-html into main 2024-04-30 23:46:37 +00:00
2 changed files with 86 additions and 23 deletions
Showing only changes of commit 10638101d3 - Show all commits

View File

@ -228,7 +228,7 @@ class Feed {
// 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);
$port = array_key_exists('port', $original) ? ":{$original['port']}" : ''; $port = array_key_exists('port', $original) ? ":{$original['port']}" : '';
$feedURL = "{$original['scheme']}://{$original['host']}$port$feedURL"; $feedURL = $original['scheme'] . '://' . $original['host'] . $port . $feedURL;
} }
$doc = self::retrieveDocument($feedURL); $doc = self::retrieveDocument($feedURL);
} }
@ -329,17 +329,12 @@ class Feed {
/** /**
* Refresh a feed * Refresh a feed
* *
* @param int $feedId The ID of the feed to be refreshed
* @param string $url The URL of the feed to be refreshed * @param string $url The URL of the feed to be refreshed
* @param SQLite3 $db A database connection to use to refresh the feed * @param SQLite3 $db A database connection to use to refresh the feed
* @return array|string[]|true[] ['ok' => true] if successful, ['error' => message] if not * @return array|string[]|true[] ['ok' => true] if successful, ['error' => message] if not
*/ */
private static function refreshFeed(string $url, SQLite3 $db): array { public static function refreshFeed(int $feedId, string $url, SQLite3 $db): array {
$feedQuery = $db->prepare('SELECT id FROM feed WHERE url = :url AND user_id = :user');
$feedQuery->bindValue(':url', $url);
$feedQuery->bindValue(':user', $_SESSION[Key::USER_ID]);
$feedResult = $feedQuery->execute();
$feedId = $feedResult ? $feedResult->fetchArray(SQLITE3_NUM)[0] : -1;
if ($feedId < 0) return ['error' => "No feed for URL $url found"];
$feedExtract = self::retrieveFeed($url); $feedExtract = self::retrieveFeed($url);
if (array_key_exists('error', $feedExtract)) return $feedExtract; if (array_key_exists('error', $feedExtract)) return $feedExtract;
@ -423,7 +418,26 @@ class Feed {
$query->bindValue(':user', $_SESSION[Key::USER_ID]); $query->bindValue(':user', $_SESSION[Key::USER_ID]);
$query->execute(); $query->execute();
return self::refreshFeed($url, $db); return self::refreshFeed($existing['id'], $url, $db);
}
/**
* Retrieve all feeds, optionally for a specific user
*
* @param SQLite3 $db The database connection to use to retrieve the feeds
* @param int $user The ID of the user whose feeds should be retrieved (optional, defaults to all feeds)
* @return array An array of arrays with ['id', 'url', 'email'] keys
*/
public static function retrieveAll(SQLite3 $db, int $user = 0): array {
$extraSQL = $user > 0 ? ' WHERE u.id = :user' : '';
$query = $db->prepare(
"SELECT f.id, f.url, u.email FROM feed f INNER JOIN frc_user u ON u.id = f.user_id$extraSQL");
if ($user > 0) $query->bindValue(':user', $user);
$result = $query->execute();
if (!$result) return ['error', $db->lastErrorMsg()];
$feeds = [];
while ($feed = $result->fetchArray(SQLITE3_ASSOC)) $feeds[] = $feed;
return $feeds;
} }
/** /**
@ -433,19 +447,15 @@ class Feed {
* @return array|true[] ['ok => true] if successful, ['error' => message] if not (may have multiple error lines) * @return array|true[] ['ok => true] if successful, ['error' => message] if not (may have multiple error lines)
*/ */
public static function refreshAll(SQLite3 $db): array { public static function refreshAll(SQLite3 $db): array {
$query = $db->prepare('SELECT url FROM feed WHERE user_id = :user'); $feeds = self::retrieveAll($db, $_SESSION[Key::USER_ID]);
$query->bindValue(':user', $_SESSION[Key::USER_ID]); if (array_key_exists('error', $feeds)) return $feeds;
$result = $query->execute();
$url = $result ? $result->fetchArray(SQLITE3_NUM) : false; $errors = [];
if ($url) { array_walk($feeds, function ($feed) use ($db, &$errors) {
$errors = array(); $result = self::refreshFeed($feed['id'], $feed['url'], $db);
while ($url) { if (array_key_exists('error', $result)) $errors[] = $result['error'];
$updateResult = self::refreshFeed($url[0], $db); });
if (array_key_exists('error', $updateResult)) $errors[] = $updateResult['error'];
$url = $result->fetchArray(SQLITE3_NUM); return sizeof($errors) == 0 ? ['ok' => true] : ['error' => implode("\n", $errors)];
}
return sizeof($errors) == 0 ? ['ok' => true] : ['error' => implode("\n", $errors)];
}
return ['error' => $db->lastErrorMsg()];
} }
} }

53
src/util/refresh.php Normal file
View File

@ -0,0 +1,53 @@
<?php
use JetBrains\PhpStorm\NoReturn;
require __DIR__ . '/../cli-start.php';
cli_title('FEED REFRESH');
if ($argc < 2) display_help();
switch ($argv[1]) {
case 'all':
refresh_all();
break;
default:
printfn('Unrecognized option "%s"', $argv[1]);
display_help();
}
/**
* Display the options for this utility and exit
*/
#[NoReturn]
function display_help(): void {
printfn('Options:');
printfn(' - all');
printfn(' Refreshes all feeds');
exit(0);
}
function refresh_all(): void {
$db = Data::getConnection();
try {
$feeds = Feed::retrieveAll($db);
if (array_key_exists('error', $feeds)) {
printfn('SQLite error: %s', $feeds['error']);
return;
}
array_walk($feeds, function ($feed) use ($db) {
$result = Feed::refreshFeed($feed['id'], $feed['url'], $db);
if (array_key_exists('error', $result)) {
printfn('ERR (%s) %s', $feed['email'], $feed['url']);
printfn(' %s', $result['error']);
} else {
printfn('OK (%s) %s', $feed['email'], $feed['url']);
}
});
printfn(PHP_EOL . 'All feeds refreshed');
} finally {
$db->close();
}
}