* move get_article_image to Article; implement better og:image detection (similar to android app)

* pass article image to API clients in headlines row object
This commit is contained in:
Andrew Dolgov 2019-08-14 16:55:38 +03:00
parent 26dbe02968
commit 68e2b05f65
3 changed files with 47 additions and 33 deletions

View File

@ -759,9 +759,10 @@ class API extends Handler {
"tags" => $tags, "tags" => $tags,
); );
$enclosures = Article::get_article_enclosures($line['id']);
if ($include_attachments) if ($include_attachments)
$headline_row['attachments'] = Article::get_article_enclosures( $headline_row['attachments'] = $enclosures;
$line['id']);
if ($show_excerpt) if ($show_excerpt)
$headline_row["excerpt"] = $line["content_preview"]; $headline_row["excerpt"] = $line["content_preview"];
@ -801,7 +802,8 @@ class API extends Handler {
$headline_row = $p->hook_render_article_api(array("headline" => $headline_row)); $headline_row = $p->hook_render_article_api(array("headline" => $headline_row));
} }
$headline_row['content'] = DiskCache::rewriteUrls($headline_row['content']); $headline_row["content"] = DiskCache::rewriteUrls($headline_row['content']);
$headline_row["flavor_image"] = Article::get_article_image($enclosures, $line["content"], $line["site_url"]);
array_push($headlines, $headline_row); array_push($headlines, $headline_row);
} }

View File

@ -823,4 +823,44 @@ class Article extends Handler_Protected {
return true; return true;
} }
static function get_article_image($enclosures, $content, $site_url) {
$article_image = false;
$tmpdoc = new DOMDocument();
if (@$tmpdoc->loadHTML('<?xml encoding="UTF-8">' . mb_substr($content, 0, 131070))) {
$tmpxpath = new DOMXPath($tmpdoc);
$elems = $tmpxpath->query('(//img[@src]|//video[@poster]|//iframe[contains(@src , "youtube.com/embed/")])');
foreach ($elems as $e) {
if ($e->nodeName == "iframe") {
$matches = [];
if ($rrr = preg_match("/\/embed\/([\w-]+)/", $e->getAttribute("src"), $matches)) {
$article_image = "https://img.youtube.com/vi/" . $matches[1] . "/hqdefault.jpg";
break;
}
} else if ($e->nodeName == "video") {
$article_image = $e->getAttribute("poster");
break;
} else if ($e->nodeName == 'img') {
if (mb_strpos($e->getAttribute("src"), "data:") !== 0) {
$article_image = $e->getAttribute("src");
}
break;
}
}
}
if ($article_image)
return rewrite_relative_url($site_url, $article_image);
foreach ($enclosures as $enc) {
if (strpos($enc["content_type"], "image/") !== FALSE) {
return rewrite_relative_url($site_url, $enc["content_url"]);
}
}
return false;
}
} }

View File

@ -157,7 +157,7 @@ class Handler_Public extends Handler {
} }
$tpl->setVariable('ARTICLE_OG_IMAGE', $tpl->setVariable('ARTICLE_OG_IMAGE',
$this->get_article_image($enclosures, $line['content'], $feed_site_url), true); Article::get_article_image($enclosures, $line['content'], $feed_site_url), true);
$tpl->addBlock('entry'); $tpl->addBlock('entry');
} }
@ -319,34 +319,6 @@ class Handler_Public extends Handler {
print "Article not found."; print "Article not found.";
} }
private function get_article_image($enclosures, $content, $site_url) {
$og_image = false;
foreach ($enclosures as $enc) {
if (strpos($enc["content_type"], "image/") !== FALSE) {
return rewrite_relative_url($site_url, $enc["content_url"]);
}
}
if (!$og_image) {
$tmpdoc = new DOMDocument();
if (@$tmpdoc->loadHTML('<?xml encoding="UTF-8">' . mb_substr($content, 0, 131070))) {
$tmpxpath = new DOMXPath($tmpdoc);
$imgs = $tmpxpath->query("//img");
foreach ($imgs as $img) {
$src = $img->getAttribute("src");
if (mb_strpos($src, "data:") !== 0)
return rewrite_relative_url($site_url, $src);
}
}
}
return false;
}
private function format_article($id, $owner_uid) { private function format_article($id, $owner_uid) {
$pdo = Db::pdo(); $pdo = Db::pdo();
@ -409,7 +381,7 @@ class Handler_Public extends Handler {
$rv .= "</head>"; $rv .= "</head>";
$og_image = $this->get_article_image($enclosures, $line['content'], $line["site_url"]); $og_image = Article::get_article_image($enclosures, $line['content'], $line["site_url"]);
if ($og_image) { if ($og_image) {
$rv .= "<meta property='og:image' content=\"" . htmlspecialchars($og_image) . "\"/>"; $rv .= "<meta property='og:image' content=\"" . htmlspecialchars($og_image) . "\"/>";