Skip to content

Commit

Permalink
Get_hash (#21)
Browse files Browse the repository at this point in the history
* New function get_hash
For easier FreshRSS patches

* PHPDoc see
  • Loading branch information
Alkarex committed Sep 14, 2024
1 parent 44aea92 commit f99d21b
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/SimplePie.php
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,7 @@ public function enable_exceptions(bool $enable = true)
* after having cleaned it from noisy elements such as statistics or comments.
* FreshRSS
* @return string $rss A hash of the cleaned content, or empty string in case of error.
* @see SimplePie::get_hash()
*/
private function clean_hash(string $rss): string
{
Expand Down Expand Up @@ -1678,7 +1679,7 @@ private function clean_hash(string $rss): string
* configuration options get processed, feeds are fetched, cached, and
* parsed, and all of that other good stuff.
*
* @return bool|int positive integer with modification time if using cache, boolean true if otherwise successful, false otherwise // FreshRSS
* @return bool True if successful, false otherwise
*/
public function init()
{
Expand Down Expand Up @@ -1766,7 +1767,7 @@ public function init()

// Fetch the data into $this->raw_data
if (($fetched = $this->fetch_data($cache)) === true) {
return $this->data['mtime'] ?? true; // FreshRSS
return true;
} elseif ($fetched === false) {
return false;
}
Expand Down Expand Up @@ -1841,7 +1842,6 @@ public function init()
}
$this->data['build'] = Misc::get_build();
$this->data['hash'] = $this->data['hash'] ?? $this->clean_hash($this->raw_data); // FreshRSS
$this->data['mtime'] = time(); // FreshRSS

// Cache the file if caching is enabled
$this->data['cache_expiration_time'] = $this->cache_duration + time();
Expand Down Expand Up @@ -1889,7 +1889,6 @@ public function init()
*
* @param Base|DataCache|false $cache Cache handler, or false to not load from the cache
* @return array{array<string, string>, string}|array{}|bool Returns true if the data was loaded from the cache, or an array of HTTP headers and sniffed type
* @phpstan-impure
*/
protected function fetch_data(&$cache)
{
Expand Down Expand Up @@ -1941,8 +1940,7 @@ protected function fetch_data(&$cache)
$this->data = [];
}
// Check if the cache has been updated
// elseif (isset($this->data['cache_expiration_time']) && $this->data['cache_expiration_time'] > time()) { // FreshRSS
elseif (empty($this->data['mtime']) || $this->data['mtime'] + $this->cache_duration <= time()) {
elseif (empty($this->data['cache_expiration_time']) || $this->data['cache_expiration_time'] > time()) { // FreshRSS, https://github.com/simplepie/simplepie/pull/846
// Want to know if we tried to send last-modified and/or etag headers
// when requesting this file. (Note that it's up to the file to
// support this, but we don't always send the headers either.)
Expand Down Expand Up @@ -1980,7 +1978,6 @@ protected function fetch_data(&$cache)
$this->raw_data = false;
if (isset($file)) { // FreshRSS
// Update cache metadata
$this->data['mtime'] = time();
$this->data['headers'] = array_map(function (array $values): string {
return implode(',', $values);
}, $file->get_headers());
Expand All @@ -1994,7 +1991,6 @@ protected function fetch_data(&$cache)
$hash = $this->clean_hash($file->get_body_content());
if (($this->data['hash'] ?? null) === $hash) {
// Update cache metadata
$this->data['mtime'] = time();
$this->data['headers'] = array_map(function (array $values): string {
return implode(',', $values);
}, $file->get_headers());
Expand Down Expand Up @@ -2133,7 +2129,6 @@ protected function fetch_data(&$cache)
'build' => Misc::get_build(),
'cache_expiration_time' => $this->cache_duration + time(),
'hash' => empty($hash) ? $this->clean_hash($file->get_body_content()) : $hash, // FreshRSS
'mtime' => time(), // FreshRSS
];

if (!$cache->set_data($cacheKey, $this->data, $this->cache_duration)) {
Expand Down Expand Up @@ -2179,6 +2174,18 @@ public function status_code()
return $this->status_code;
}

/**
* Get the last hash of the feed
*
* @return string Hash of the content of the feed
* @see SimplePie::clean_hash()
* FreshRSS
*/
public function get_hash(): string
{
return $this->data['hash'] ?? '';
}

/**
* Get the raw XML
*
Expand Down

0 comments on commit f99d21b

Please sign in to comment.