From f08f7dcff988de90e899ce310246f0b552a4fe3d Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Thu, 16 Oct 2025 21:15:44 +0200 Subject: Sort by article length (#8119) * Sort by article length fix https://github.com/FreshRSS/Extensions/issues/378 Very basic using simply SQL `LENGTH()` function. image * Improve content length retrieval --- app/Models/EntryDAO.php | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'app/Models/EntryDAO.php') diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php index c9fae7923..0d34df3ac 100644 --- a/app/Models/EntryDAO.php +++ b/app/Models/EntryDAO.php @@ -812,27 +812,31 @@ SQL; public function searchByGuid(int $id_feed, string $guid): ?FreshRSS_Entry { $content = static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content'; + $contentLength = 'LENGTH(' . (static::isCompressed() ? 'content_bin' : 'content') . ') AS content_length'; $hash = static::sqlHexEncode('hash'); $sql = <<fetchAssoc($sql, [':id_feed' => $id_feed, ':guid' => $guid]); /** @var list $res */ + * is_read:int,is_favorite:int,tags:string,attributes:?string,content_length:int}> $res */ return isset($res[0]) ? FreshRSS_Entry::fromArray($res[0]) : null; } public function searchById(string $id): ?FreshRSS_Entry { $content = static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content'; + $contentLength = 'LENGTH(' . (static::isCompressed() ? 'content_bin' : 'content') . ') AS content_length'; $hash = static::sqlHexEncode('hash'); $sql = <<fetchAssoc($sql, [':id' => $id]); - /** @var list $res */ + /** @var list $res */ return isset($res[0]) ? FreshRSS_Entry::fromArray($res[0]) : null; } @@ -1256,7 +1260,7 @@ SQL; /** * @param numeric-string $id_min * @param numeric-string $id_max - * @param 'id'|'c.name'|'date'|'f.name'|'link'|'title'|'rand'|'lastUserModified' $sort + * @param 'id'|'c.name'|'date'|'f.name'|'link'|'title'|'rand'|'lastUserModified'|'length' $sort * @param 'ASC'|'DESC' $order * @param numeric-string $continuation_id * @param list $continuation_values @@ -1324,12 +1328,13 @@ SQL; $values[] = $id_min; } - if ($continuation_id !== '0' && in_array($sort, ['c.name', 'date', 'f.name', 'link', 'title', 'lastUserModified'], true)) { + if ($continuation_id !== '0' && in_array($sort, ['c.name', 'date', 'f.name', 'link', 'title', 'lastUserModified', 'length'], true)) { $sign = $order === 'ASC' ? '>' : '<'; $orderBy = match ($sort) { 'c.name' => 'c.name', 'f.name' => 'f.name', 'lastUserModified' => $alias . '`lastUserModified`', + 'length' => 'LENGTH(' . $alias . (static::isCompressed() ? 'content_bin' : 'content') . ')', default => $alias . $sort, }; // Keyset pagination (Compatibility syntax due to poor performance of tuple syntax in MySQL https://bugs.mysql.com/bug.php?id=104128) @@ -1367,7 +1372,7 @@ SQL; * @param int $id category/feed/tag ID * @param numeric-string $id_min * @param numeric-string $id_max - * @param 'id'|'c.name'|'date'|'f.name'|'link'|'title'|'rand'|'lastUserModified' $sort + * @param 'id'|'c.name'|'date'|'f.name'|'link'|'title'|'rand'|'lastUserModified'|'length' $sort * @param 'ASC'|'DESC' $order * @param numeric-string $continuation_id * @param list $continuation_values @@ -1426,11 +1431,12 @@ SQL; } $order = in_array($order, ['ASC', 'DESC'], true) ? $order : 'DESC'; - $sort = in_array($sort, ['id', 'c.name', 'date', 'f.name', 'link', 'title', 'rand', 'lastUserModified'], true) ? $sort : 'id'; + $sort = in_array($sort, ['id', 'c.name', 'date', 'f.name', 'link', 'title', 'rand', 'lastUserModified', 'length'], true) ? $sort : 'id'; $orderBy = match ($sort) { 'c.name' => 'c.name', 'f.name' => 'f.name', 'lastUserModified' => 'e.`lastUserModified`', + 'length' => 'LENGTH(e.' . (static::isCompressed() ? 'content_bin' : 'content') . ')', 'rand' => static::sqlRandom(), default => 'e.' . $sort, }; @@ -1460,7 +1466,7 @@ SQL; * @param int $id category/feed/tag ID * @param numeric-string $id_min * @param numeric-string $id_max - * @param 'id'|'c.name'|'date'|'f.name'|'link'|'title'|'rand'|'lastUserModified' $sort + * @param 'id'|'c.name'|'date'|'f.name'|'link'|'title'|'rand'|'lastUserModified'|'length' $sort * @param 'ASC'|'DESC' $order * @param numeric-string $continuation_id * @param list $continuation_values @@ -1470,7 +1476,7 @@ SQL; string $id_min = '0', string $id_max = '0', string $sort = 'id', string $order = 'DESC', string $continuation_id = '0', array $continuation_values = [], int $limit = 1, int $offset = 0): PDOStatement|false { $order = in_array($order, ['ASC', 'DESC'], true) ? $order : 'DESC'; - $sort = in_array($sort, ['id', 'c.name', 'date', 'f.name', 'link', 'title', 'rand', 'lastUserModified'], true) ? $sort : 'id'; + $sort = in_array($sort, ['id', 'c.name', 'date', 'f.name', 'link', 'title', 'rand', 'lastUserModified', 'length'], true) ? $sort : 'id'; [$values, $sql] = $this->sqlListWhere($type, $id, $state, $filters, id_min: $id_min, id_max: $id_max, sort: $sort, order: $order, continuation_id: $continuation_id, continuation_values: $continuation_values, limit: $limit, offset: $offset); @@ -1479,6 +1485,7 @@ SQL; 'c.name' => 'c0.name', 'f.name' => 'f0.name', 'lastUserModified' => 'e0.`lastUserModified`', + 'length' => 'LENGTH(e0.' . (static::isCompressed() ? 'content_bin' : 'content') . ')', 'rand' => static::sqlRandom(), default => 'e0.' . $sort, }; @@ -1523,7 +1530,7 @@ SQL; * @param int $id category/feed/tag ID * @param numeric-string $id_min * @param numeric-string $id_max - * @param 'id'|'c.name'|'date'|'f.name'|'link'|'title'|'rand'|'lastUserModified' $sort + * @param 'id'|'c.name'|'date'|'f.name'|'link'|'title'|'rand'|'lastUserModified'|'length' $sort * @param 'ASC'|'DESC' $order * @param numeric-string $continuation_id * @param list $continuation_values -- cgit v1.2.3