diff --git a/lib/Database.php b/lib/Database.php index 2bed918..0262b78 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -23,6 +23,7 @@ use JKingWeb\Arsse\Misc\URL; * - Folders, which belong to users and contain subscriptions * - Tags, which belong to users and can be assigned to multiple subscriptions * - Feeds to which users are subscribed + * - Icons, which are associated with feeds * - Articles, which belong to feeds and for which users can only affect metadata * - Editions, identifying authorial modifications to articles * - Labels, which belong to users and can be assigned to multiple articles @@ -933,6 +934,29 @@ class Database { } return (string) $this->db->prepare($q->getQuery(), $q->getTypes())->run($q->getValues())->getValue(); } + + /** Retrieves detailed information about the icon for a subscription. + * + * The returned information is: + * + * - "id": The umeric identifier of the icon (not the subscription) + * - "url": The URL of the icon + * - "type": The Content-Type of the icon e.g. "image/png" + * - "data": The icon itself, as a binary sring; if $withData is false this will be null + * + * @param string $user The user whose subscription icon is to be retrieved + * @param int $subscription The numeric identifier of the subscription + */ + public function subscriptionIcon(string $user, int $subscription): array { + if (!Arsse::$user->authorize($user, __FUNCTION__)) { + throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]); + } + $out = $this->db->prepare("SELECT i.id, i.url, i.type, i.data from arsse_icons as i join arsse_feeds as f on i.id = f.icon join arsse_subscriptions as s on s.feed = f.id where s.owner = ? and s.id = ?", "str", "int")->run($user, $subscription)->getRow(); + if (!$out) { + throw new Db\ExceptionInput("idMissing", ["action" => __FUNCTION__, "field" => "subscription", 'id' => $subscription]); + } + return $out; + } /** Returns the time at which any of a user's subscriptions (or a specific subscription) was last refreshed, as a DateTimeImmutable object */ public function subscriptionRefreshed(string $user, int $id = null): ?\DateTimeImmutable { @@ -1255,16 +1279,13 @@ class Database { /** Retrieve a feed icon by URL, for use during feed refreshing * - * @param string $url The URL of the icon to Retrieve - * @param bool $withData Whether to return the icon content along with the metadata + * @param string $url The URL of the icon to retrieve */ - protected function iconGetByUrl(string $url, bool $withData = true): ?array { - $data = $withData ? "data" : "null as data"; - return $this->db->prepare("SELECT id, url, type, $data, next_fetch from arsse_icons where url = ?", "str")->run($url)->getRow(); + protected function iconGetByUrl(string $url): ?array { + return $this->db->prepare("SELECT id, url, type, data from arsse_icons where url = ?", "str")->run($url)->getRow(); } - - - /** Returns information about an icon for a feed to which a user is subscribed, with or without the binary content of the icon itself + + /** Retrieves information about an icon * * The returned information is: * @@ -1273,18 +1294,16 @@ class Database { * - "type": The Content-Type of the icon e.g. "image/png" * - "data": The icon itself, as a binary sring; if $withData is false this will be null * - * @param string $user The user whose subscription icon is to be retrieved - * @param int $subscription The numeric identifier of the subscription with which the icon is associated - * @param bool $withData Whether to retrireve the icon content in addition to its metadata + * @param string $user The user whose icon is to be retrieved + * @param int $subscription The numeric identifier of the icon */ - public function iconGet(string $user, int $subscrption, bool $withData = true): array { + public function iconGet(string $user, int $id): array { if (!Arsse::$user->authorize($user, __FUNCTION__)) { throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]); } - $data = $withData ? "data" : "null as data"; - $out = $this->db->prepare("SELECT i.id, i.url, i.type, $data from arsse_icons as i join arsse_feeds as f on i.id = f.icon join arsse_subscriptions as s on s.feed = f.id where s.owner = ? and s.id = ?", "str", "int")->run($user, $subscription)->getRow(); + $out = $this->db->prepare("SELECT i.id, i.url, i.type, i.data from arsse_icons as i join arsse_feeds as f on i.id = f.icon join arsse_subscriptions as s on s.feed = f.id where s.owner = ? and i.id = ?", "str", "int")->run($user, $id)->getRow(); if (!$out) { - throw new Db\ExceptionInput("subjectMissing", ["action" => __FUNCTION__, "field" => "subscription", 'id' => $subscription]); + throw new Db\ExceptionInput("subjectMissing", ["action" => __FUNCTION__, "field" => "subscription", 'id' => $id]); } return $out; } @@ -1292,14 +1311,32 @@ class Database { /** Lists icons for feeds to which a user is subscribed, with or without the binary content of the icon itself * * @param string $user The user whose subscription icons are to be retrieved - * @param bool $withData Whether to retrireve the icon content in addition to its metadata */ - public function iconList(string $user, bool $withData = true): Db\Result { + public function iconList(string $user): Db\Result { if (!Arsse::$user->authorize($user, __FUNCTION__)) { throw new User\ExceptionAuthz("notAuthorized", ["action" => __FUNCTION__, "user" => $user]); } - $data = $withData ? "i.data" : "null as data"; - return $this->db->prepare("SELECT i.id, i.url, i.type, $data from arsse_icons as i join arsse_feeds as f on i.id = f.icon join arsse_subscriptions as s on s.feed = f.id where s.owner = ?", "str")->run($user); + return $this->db->prepare("SELECT i.id, i.url, i.type, i.data from arsse_icons as i join arsse_feeds as f on i.id = f.icon join arsse_subscriptions as s on s.feed = f.id where s.owner = ?", "str")->run($user); + } + + /** Deletes orphaned icons from the database + * + * Icons are orphaned if no subscribed newsfeed uses them. + */ + public function iconCleanup(): int { + $tr = $this->begin(); + // first unmark any icons which are no longer orphaned; an icon is considered orphaned if it is not used or only used by feeds which are themselves orphaned + $this->db->query("UPDATE arsse_icons set orphaned = null where id in (select distinct icon from arsse_feeds where icon is not null and orphaned is null)"); + // next mark any newly orphaned icons with the current date and time + $this->db->query("UPDATE arsse_icons set orphaned = CURRENT_TIMESTAMP where id not in (select distinct icon from arsse_feeds where icon is not null and orphaned is null)"); + // finally delete icons that have been orphaned longer than the feed retention period, if a a purge threshold has been specified + $out = 0; + if (Arsse::$conf->purgeFeeds) { + $limit = Date::sub(Arsse::$conf->purgeFeeds); + $out += $this->db->prepare("DELETE from arsse_icons where orphaned <= ?", "datetime")->run($limit)->changes(); + } + $tr->commit(); + return $out; } /** Returns an associative array of result column names and their SQL computations for article queries diff --git a/tests/cases/Database/AbstractTest.php b/tests/cases/Database/AbstractTest.php index ff5acdd..6e0e2ec 100644 --- a/tests/cases/Database/AbstractTest.php +++ b/tests/cases/Database/AbstractTest.php @@ -18,6 +18,7 @@ abstract class AbstractTest extends \JKingWeb\Arsse\Test\AbstractTest { use SeriesToken; use SeriesFolder; use SeriesFeed; + use SeriesIcon; use SeriesSubscription; use SeriesLabel; use SeriesTag; diff --git a/tests/cases/Database/SeriesIcon.php b/tests/cases/Database/SeriesIcon.php new file mode 100644 index 0000000..7a7e348 --- /dev/null +++ b/tests/cases/Database/SeriesIcon.php @@ -0,0 +1,89 @@ +data = [ + 'arsse_users' => [ + 'columns' => [ + 'id' => 'str', + 'password' => 'str', + 'num' => 'int', + ], + 'rows' => [ + ["jane.doe@example.com", "",1], + ["john.doe@example.com", "",2], + ], + ], + 'arsse_icons' => [ + 'columns' => [ + 'id' => "int", + 'url' => "str", + 'type' => "str", + 'data' => "blob", + ], + 'rows' => [ + [1,'http://localhost:8000/Icon/PNG','image/png',base64_decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMjHxIGmVAAAADUlEQVQYV2NgYGBgAAAABQABijPjAAAAAABJRU5ErkJggg==")], + [2,'http://localhost:8000/Icon/GIF','image/gif',base64_decode("R0lGODlhAQABAIABAAAAAP///yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==")], + [3,'http://localhost:8000/Icon/SVG1','image/svg+xml',''], + [4,'http://localhost:8000/Icon/SVG2','image/svg+xml',''], + ], + ], + 'arsse_feeds' => [ + 'columns' => [ + 'id' => "int", + 'url' => "str", + 'title' => "str", + 'err_count' => "int", + 'err_msg' => "str", + 'modified' => "datetime", + 'next_fetch' => "datetime", + 'size' => "int", + 'icon' => "int", + ], + 'rows' => [ + [1,"http://localhost:8000/Feed/Matching/3","Ook",0,"",$past,$past,0,null], + [2,"http://localhost:8000/Feed/Matching/1","Eek",5,"There was an error last time",$past,$future,0,null], + [3,"http://localhost:8000/Feed/Fetching/Error?code=404","Ack",0,"",$past,$now,0,null], + [4,"http://localhost:8000/Feed/NextFetch/NotModified?t=".time(),"Ooook",0,"",$past,$past,0,null], + [5,"http://localhost:8000/Feed/Parsing/Valid","Ooook",0,"",$past,$future,0,null], + // these feeds all test icon caching + [6,"http://localhost:8000/Feed/WithIcon/PNG",null,0,"",$past,$future,0,1], // no change when updated + [7,"http://localhost:8000/Feed/WithIcon/GIF",null,0,"",$past,$future,0,1], // icon ID 2 will be assigned to feed when updated + [8,"http://localhost:8000/Feed/WithIcon/SVG1",null,0,"",$past,$future,0,3], // icon ID 3 will be modified when updated + [9,"http://localhost:8000/Feed/WithIcon/SVG2",null,0,"",$past,$future,0,null], // icon ID 4 will be created and assigned to feed when updated + ], + ], + 'arsse_subscriptions' => [ + 'columns' => [ + 'id' => "int", + 'owner' => "str", + 'feed' => "int", + ], + 'rows' => [ + [1,'john.doe@example.com',1], + [2,'john.doe@example.com',2], + [3,'john.doe@example.com',3], + [4,'john.doe@example.com',4], + [5,'john.doe@example.com',5], + [6,'jane.doe@example.com',1], + ], + ], + ]; + } + + protected function tearDownSeriesIcon(): void { + unset($this->data); + } +}