add HOOK_ARTICLE_IMAGE for Article::get_article_image()

This commit is contained in:
Andrew Dolgov 2019-08-15 09:04:42 +03:00
parent ffb842f752
commit 9d852e052c
2 changed files with 49 additions and 42 deletions

View File

@ -828,59 +828,65 @@ class Article extends Handler_Protected {
$article_image = "";
$article_stream = "";
$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";
$article_stream = "https://youtu.be/" . $matches[1];
break;
}
} else if ($e->nodeName == "video") {
$article_image = $e->getAttribute("poster");
$src = $tmpxpath->query("//source[@src]", $e)->item(0);
if ($src) {
$article_stream = $src->getAttribute("src");
}
break;
} else if ($e->nodeName == 'img') {
if (mb_strpos($e->getAttribute("src"), "data:") !== 0) {
$article_image = $e->getAttribute("src");
}
break;
}
}
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_IMAGE) as $p) {
list ($article_image, $article_stream) = $p->hook_article_image($enclosures, $content, $site_url);
}
if ($article_image)
$article_image = rewrite_relative_url($site_url, $article_image);
if (!$article_image && !$article_stream) {
$tmpdoc = new DOMDocument();
if ($article_stream)
$article_stream = rewrite_relative_url($site_url, $article_stream);
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/")])');
if (!$article_image)
foreach ($enclosures as $enc) {
if (strpos($enc["content_type"], "image/") !== FALSE) {
$article_image = rewrite_relative_url($site_url, $enc["content_url"]);
break;
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";
$article_stream = "https://youtu.be/" . $matches[1];
break;
}
} else if ($e->nodeName == "video") {
$article_image = $e->getAttribute("poster");
$src = $tmpxpath->query("//source[@src]", $e)->item(0);
if ($src) {
$article_stream = $src->getAttribute("src");
}
break;
} else if ($e->nodeName == 'img') {
if (mb_strpos($e->getAttribute("src"), "data:") !== 0) {
$article_image = $e->getAttribute("src");
}
break;
}
}
}
if (!$article_image)
foreach ($enclosures as $enc) {
if (strpos($enc["content_type"], "image/") !== FALSE) {
$article_image = $enc["content_url"];
break;
}
}
if ($article_image)
$article_image = rewrite_relative_url($site_url, $article_image);
if ($article_stream)
$article_stream = rewrite_relative_url($site_url, $article_stream);
}
$cache = new DiskCache("images");
if ($cache->exists(sha1($article_image)))
if ($article_image && $cache->exists(sha1($article_image)))
$article_image = $cache->getUrl(sha1($article_image));
if ($cache->exists(sha1($article_stream)))
if ($article_stream && $cache->exists(sha1($article_stream)))
$article_stream = $cache->getUrl(sha1($article_stream));
return [$article_image, $article_stream];

View File

@ -59,6 +59,7 @@ class PluginHost {
const HOOK_SEND_MAIL = 39;
const HOOK_FILTER_TRIGGERED = 40;
const HOOK_GET_FULL_TEXT = 41;
const HOOK_ARTICLE_IMAGE = 42;
const KIND_ALL = 1;
const KIND_SYSTEM = 2;