A free, self-hostable news aggregator…
Fix hash of articles with loadCompleteContent (#5576)
Alexandre Alapetited165ed1fb609
diff --git a/app/Models/Entry.php b/app/Models/Entry.phpindex 77f39f256..7da27e409 100644--- a/app/Models/Entry.php+++ b/app/Models/Entry.php@@ -61,7 +61,7 @@ class FreshRSS_Entry extends Minz_Model {}/** @param array{'id'?:string,'id_feed'?:int,'guid'?:string,'title'?:string,'author'?:string,'content'?:string,'link'?:string,'date'?:int|string,'lastSeen'?:int,- * 'is_read'?:bool|int,'is_favorite'?:bool|int,'tags'?:string|array<string>,'attributes'?:string,'thumbnail'?:string,'timestamp'?:string} $dao */+ * 'hash'?:string,'is_read'?:bool|int,'is_favorite'?:bool|int,'tags'?:string|array<string>,'attributes'?:string,'thumbnail'?:string,'timestamp'?:string} $dao */public static function fromArray(array $dao): FreshRSS_Entry {if (empty($dao['content'])) {$dao['content'] = '';@@ -101,6 +101,9 @@ class FreshRSS_Entry extends Minz_Model {if (!empty($dao['attributes'])) {$entry->_attributes('', $dao['attributes']);}+ if (!empty($dao['hash'])) {+ $entry->_hash($dao['hash']);+ }return $entry;}@@ -147,6 +150,13 @@ class FreshRSS_Entry extends Minz_Model {($mime == '' && $length == 0 && preg_match('/[.](avif|gif|jpe?g|png|svg|webp)$/i', $elink));}+ /**+ * Provides the original content without additional content potentially added by loadCompleteContent().+ */+ public function originalContent(): string {+ return preg_replace('#<!-- FULLCONTENT start //-->.*<!-- FULLCONTENT end //-->#s', '', $this->content);+ }+/*** @param bool $withEnclosures Set to true to include the enclosures in the returned HTML, false otherwise.* @param bool $allowDuplicateEnclosures Set to false to remove obvious enclosure duplicates (based on simple string comparison), true otherwise.@@ -412,7 +422,7 @@ HTML;public function hash(): string {if ($this->hash == '') {//Do not include $this->date because it may be automatically generated when lacking- $this->hash = md5($this->link . $this->title . $this->authors(true) . $this->content . $this->tags(true));+ $this->hash = md5($this->link . $this->title . $this->authors(true) . $this->originalContent() . $this->tags(true));}return $this->hash;}@@ -473,7 +483,6 @@ HTML;}/** @param int|string $value */public function _date($value): void {- $this->hash = '';$value = intval($value);$this->date = $value > 1 ? $value : time();}@@ -770,7 +779,7 @@ HTML;);if ('' !== $fullContent) {$fullContent = "<!-- FULLCONTENT start //-->{$fullContent}<!-- FULLCONTENT end //-->";- $originalContent = preg_replace('#<!-- FULLCONTENT start //-->.*<!-- FULLCONTENT end //-->#s', '', $this->content());+ $originalContent = $this->originalContent();switch ($feed->attributes('content_action')) {case 'prepend':$this->content = $fullContent . $originalContent;diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.phpindex e8a531ec0..8306320a0 100644--- a/app/Models/EntryDAO.php+++ b/app/Models/EntryDAO.php@@ -729,8 +729,9 @@ SQL;public function searchByGuid(int $id_feed, string $guid): ?FreshRSS_Entry {$content = static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content';+ $hash = static::sqlHexEncode('hash');$sql = <<<SQL-SELECT id, guid, title, author, link, date, is_read, is_favorite, id_feed, tags, attributes, {$content}+SELECT id, guid, title, author, link, date, is_read, is_favorite, {$hash} AS hash, id_feed, tags, attributes, {$content}FROM `_entry` WHERE id_feed=:id_feed AND guid=:guidSQL;$res = $this->fetchAssoc($sql, [':id_feed' => $id_feed, ':guid' => $guid]);@@ -741,8 +742,9 @@ SQL;public function searchById(string $id): ?FreshRSS_Entry {$content = static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content';+ $hash = static::sqlHexEncode('hash');$sql = <<<SQL-SELECT id, guid, title, author, link, date, is_read, is_favorite, id_feed, tags, attributes, {$content}+SELECT id, guid, title, author, link, date, is_read, is_favorite, {$hash} AS hash, id_feed, tags, attributes, {$content}FROM `_entry` WHERE id=:idSQL;$res = $this->fetchAssoc($sql, [':id' => $id]);@@ -1136,9 +1138,10 @@ SQL;if ($order !== 'DESC' && $order !== 'ASC') {$order = 'DESC';}- $content = static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content';+ $content = static::isCompressed() ? 'UNCOMPRESS(e0.content_bin) AS content' : 'e0.content';+ $hash = static::sqlHexEncode('e0.hash');$sql = <<<SQL-SELECT e0.id, e0.guid, e0.title, e0.author, {$content}, e0.link, e0.date, e0.is_read, e0.is_favorite, e0.id_feed, e0.tags, e0.attributes+SELECT e0.id, e0.guid, e0.title, e0.author, {$content}, e0.link, e0.date, {$hash} AS hash, e0.is_read, e0.is_favorite, e0.id_feed, e0.tags, e0.attributesFROM `_entry` e0INNER JOIN ({$sql}) e2 ON e2.id=e0.idORDER BY e0.id {$order}@@ -1169,7 +1172,7 @@ SQL;if ($stm) {while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {/** @var array{'id':string,'id_feed':int,'guid':string,'title':string,'author':string,'content':string,'link':string,'date':int,- * 'is_read':int,'is_favorite':int,'tags':string,'attributes'?:string} $row */+ * 'hash':string,'is_read':int,'is_favorite':int,'tags':string,'attributes'?:string} $row */yield FreshRSS_Entry::fromArray($row);}}@@ -1198,9 +1201,10 @@ SQL;$order = 'DESC';}$content = static::isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content';+ $hash = static::sqlHexEncode('hash');$repeats = str_repeat('?,', count($ids) - 1) . '?';$sql = <<<SQL-SELECT id, guid, title, author, link, date, is_read, is_favorite, id_feed, tags, attributes, {$content}+SELECT id, guid, title, author, link, date, {$hash} AS hash, is_read, is_favorite, id_feed, tags, attributes, {$content}FROM `_entry`WHERE id IN ({$repeats})ORDER BY id {$order}@@ -1211,7 +1215,7 @@ SQL;}while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {/** @var array{'id':string,'id_feed':int,'guid':string,'title':string,'author':string,'content':string,'link':string,'date':int,- * 'is_read':int,'is_favorite':int,'tags':string,'attributes'?:string} $row */+ * 'hash':string,'is_read':int,'is_favorite':int,'tags':string,'attributes'?:string} $row */yield FreshRSS_Entry::fromArray($row);}}