cache_starred_images: minor fixes

This commit is contained in:
Andrew Dolgov 2021-02-17 16:17:05 +03:00
parent ea37d05a83
commit 6ecee2abbd
1 changed files with 28 additions and 25 deletions

View File

@ -5,7 +5,7 @@ class Cache_Starred_Images extends Plugin {
private $host;
/* @var DiskCache $cache */
private $cache;
private $max_cache_attempts = 5; // per-article
private $max_cache_attempts = 5; // per-article
function about() {
return array(1.0,
@ -38,13 +38,13 @@ class Cache_Starred_Images extends Plugin {
Debug::log("caching media of starred articles for user " . $this->host->get_owner_uid() . "...");
$sth = $this->pdo->prepare("SELECT content, ttrss_entries.title,
ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
(ttrss_user_entries.feed_id = ttrss_feeds.id)
WHERE ref_id = ttrss_entries.id AND
marked = true AND
site_url != '' AND
ttrss_user_entries.owner_uid = ? AND
ttrss_user_entries.owner_uid = ? AND
plugin_data NOT LIKE '%starred_cache_images%'
ORDER BY ".Db::sql_random_function()." LIMIT 100");
@ -59,7 +59,7 @@ class Cache_Starred_Images extends Plugin {
$success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
if ($success) {
$plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
$plugin_data = "starred_cache_images," . $line["owner_uid"] . ":" . $line["plugin_data"];
$usth->execute([$plugin_data, $line['id']]);
}
@ -71,7 +71,10 @@ class Cache_Starred_Images extends Plugin {
Debug::log("expiring " . $this->cache->get_dir() . "...");
$files = glob($this->cache->get_dir() . "/*.{png,mp4,status}", GLOB_BRACE);
$files = array_merge(
glob($this->cache->get_dir() . "/*.png"),
glob($this->cache->get_dir() . "/*.mp4"),
glob($this->cache->get_dir() . "/*.status"));
$last_article_id = 0;
$article_exists = 1;
@ -105,7 +108,7 @@ class Cache_Starred_Images extends Plugin {
}
function hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id) {
$xpath = new DOMXpath($doc);
$xpath = new DOMXPath($doc);
if ($article_id) {
$entries = $xpath->query('(//img[@src])|(//video/source[@src])');
@ -158,30 +161,30 @@ class Cache_Starred_Images extends Plugin {
Debug::log("status: $status_filename", Debug::$LOG_VERBOSE);
if ($this->cache->exists($status_filename))
$status = json_decode($this->cache->get($status_filename), true);
else
$status = [];
if ($this->cache->exists($status_filename))
$status = json_decode($this->cache->get($status_filename), true);
else
$status = [];
$status["attempt"] += 1;
$status["attempt"] += 1;
// only allow several download attempts for article
if ($status["attempt"] > $this->max_cache_attempts) {
Debug::log("too many attempts for $site_url", Debug::$LOG_VERBOSE);
return false;
}
// only allow several download attempts for article
if ($status["attempt"] > $this->max_cache_attempts) {
Debug::log("too many attempts for $site_url", Debug::$LOG_VERBOSE);
return false;
}
if (!$this->cache->put($status_filename, json_encode($status))) {
user_error("unable to write status file: $status_filename", E_USER_WARNING);
return false;
}
if (!$this->cache->put($status_filename, json_encode($status))) {
user_error("unable to write status file: $status_filename", E_USER_WARNING);
return false;
}
$doc = new DOMDocument();
$has_images = false;
$success = false;
if (@$doc->loadHTML('<?xml encoding="UTF-8">' . $content)) {
if (@$doc->loadHTML('<?xml encoding="UTF-8">' . $content)) {
$xpath = new DOMXPath($doc);
$entries = $xpath->query('(//img[@src])|(//video/source[@src])');
@ -203,11 +206,11 @@ class Cache_Starred_Images extends Plugin {
$esth = $this->pdo->prepare("SELECT content_url FROM ttrss_enclosures WHERE post_id = ? AND
(content_type LIKE '%image%' OR content_type LIKE '%video%')");
if ($esth->execute([$article_id])) {
while ($enc = $esth->fetch()) {
if ($esth->execute([$article_id])) {
while ($enc = $esth->fetch()) {
$has_images = true;
$url = rewrite_relative_url($site_url, $enc["content_url"]);
$has_images = true;
$url = rewrite_relative_url($site_url, $enc["content_url"]);
if ($this->cache_url($article_id, $url)) {
$success = true;