2 use andreskrey\Readability\Readability
;
3 use andreskrey\Readability\Configuration
;
5 class Af_Readability
extends Plugin
{
7 /* @var PluginHost $host */
12 "Try to inline article content using Readability",
17 return array("needs_curl" => true);
21 $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
23 $this->host
->set($this, "enable_share_anything", $enable_share_anything);
25 echo __("Data saved.");
32 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
33 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
34 $host->add_hook($host::HOOK_PREFS_EDIT_FEED
, $this);
35 $host->add_hook($host::HOOK_PREFS_SAVE_FEED
, $this);
37 $host->add_filter_action($this, "action_inline", __("Inline content"));
40 function hook_prefs_tab($args) {
41 if ($args != "prefFeeds") return;
43 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Readability settings (af_readability)')."\">";
45 print_notice("Enable the plugin for specific feeds in the feed editor.");
47 print "<form dojoType=\"dijit.form.Form\">";
49 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
51 if (this.validate()) {
52 console.log(dojo.objectToQuery(this.getValues()));
53 new Ajax.Request('backend.php', {
54 parameters: dojo.objectToQuery(this.getValues()),
55 onComplete: function(transport) {
56 notify_info(transport.responseText);
63 print_hidden("op", "pluginhandler");
64 print_hidden("method", "save");
65 print_hidden("plugin", "af_readability");
67 $enable_share_anything = $this->host
->get($this, "enable_share_anything");
69 print_checkbox("enable_share_anything", $enable_share_anything);
70 print " <label for=\"enable_share_anything\">" . __("Use Readability for pages shared via bookmarklet.") . "</label>";
72 print "<p>"; print_button("submit", __("Save"));
75 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
76 if (!is_array($enabled_feeds)) $enabled_feeds = array();
78 $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
79 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
81 if (count($enabled_feeds) > 0) {
82 print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
84 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
85 foreach ($enabled_feeds as $f) {
87 "<img src='images/pub_set.png'
88 style='vertical-align : middle'> <a href='#'
89 onclick='editFeed($f)'>".
90 Feeds
::getFeedTitle($f) . "</a></li>";
98 function hook_prefs_edit_feed($feed_id) {
99 print "<div class=\"dlgSec\">".__("Readability")."</div>";
100 print "<div class=\"dlgSecCont\">";
102 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
103 if (!is_array($enabled_feeds)) $enabled_feeds = array();
105 $key = array_search($feed_id, $enabled_feeds);
106 $checked = $key !== FALSE ?
"checked" : "";
108 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"af_readability_enabled\"
109 name=\"af_readability_enabled\"
110 $checked> <label for=\"af_readability_enabled\">".__('Inline article content')."</label>";
115 function hook_prefs_save_feed($feed_id) {
116 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
117 if (!is_array($enabled_feeds)) $enabled_feeds = array();
119 $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]);
120 $key = array_search($feed_id, $enabled_feeds);
123 if ($key === FALSE) {
124 array_push($enabled_feeds, $feed_id);
127 if ($key !== FALSE) {
128 unset($enabled_feeds[$key]);
132 $this->host
->set($this, "enabled_feeds", $enabled_feeds);
136 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
138 function hook_article_filter_action($article, $action) {
139 return $this->process_article($article);
142 public function extract_content($url) {
143 global $fetch_effective_url;
145 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__
)). "/lib/readability/Readability.php");
147 $tmp = fetch_file_contents([
149 "http_accept" => "text/*",
150 "type" => "text/html"]);
152 if ($tmp && mb_strlen($tmp) < 1024 * 500) {
153 $tmpdoc = new DOMDocument("1.0", "UTF-8");
155 if (!$tmpdoc->loadHTML('<?xml encoding="utf-8" ?>\n' . $tmp))
158 if (strtolower($tmpdoc->encoding
) != 'utf-8') {
159 $tmpxpath = new DOMXPath($tmpdoc);
161 foreach ($tmpxpath->query("//meta") as $elem) {
162 $elem->parentNode
->removeChild($elem);
165 $tmp = $tmpdoc->saveHTML();
168 $r = new Readability(new Configuration());
171 if ($r->parse($tmp)) {
173 $tmpxpath = new DOMXPath($r->getDOMDOcument());
174 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
176 foreach ($entries as $entry) {
177 if ($entry->hasAttribute("href")) {
178 $entry->setAttribute("href",
179 rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href")));
183 if ($entry->hasAttribute("src")) {
184 $entry->setAttribute("src",
185 rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src")));
190 return $r->getContent();
193 } catch (ParseException
$e) {
202 function process_article($article) {
204 $extracted_content = $this->extract_content($article["link"]);
206 if ($extracted_content) {
207 $article["content"] = $extracted_content;
213 function hook_article_filter($article) {
215 $enabled_feeds = $this->host
->get($this, "enabled_feeds");
216 if (!is_array($enabled_feeds)) return $article;
218 $key = array_search($article["feed"]["id"], $enabled_feeds);
219 if ($key === FALSE) return $article;
221 return $this->process_article($article);
225 function api_version() {
229 private function filter_unknown_feeds($enabled_feeds) {
232 foreach ($enabled_feeds as $feed) {
234 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
235 $sth->execute([$feed, $_SESSION['uid']]);
237 if ($row = $sth->fetch()) {
238 array_push($tmp, $feed);