diff --git a/inc/3rdparty/config.php b/inc/3rdparty/config.php
index e618117..ec680d8 100755
--- a/inc/3rdparty/config.php
+++ b/inc/3rdparty/config.php
@@ -19,7 +19,7 @@ if (!isset($options)) $options = new stdClass();
// Enable service
// ----------------------
// Set this to false if you want to disable the service.
-// If set to false, no feed is produced and users will
+// If set to false, no feed is produced and users will
// be told that the service is disabled.
$options->enabled = true;
@@ -43,10 +43,64 @@ $options->default_entries = 5;
// ----------------------
// The maximum number of feed items to process when no access key is supplied.
// This limits the user-supplied &max=x value. For example, if the user
-// asks for 20 items to be processed (&max=20), if max_entries is set to
+// asks for 20 items to be processed (&max=20), if max_entries is set to
// 10, only 10 will be processed.
$options->max_entries = 10;
+// Full content
+// ----------------------
+// By default Full-Text RSS includes the extracted content in the output.
+// You can exclude this from the output by passing '&content=0' in the querystring.
+//
+// Possible values...
+// Always include: true
+// Never include: false
+// Include unless user overrides (&content=0): 'user' (default)
+//
+// Note: currently this does not disable full content extraction. It simply omits it
+// from the output.
+$options->content = 'user';
+
+// Excerpts
+// ----------------------
+// By default Full-Text RSS does not include excerpts in the output.
+// You can enable this by passing '&summary=1' in the querystring.
+// This will include a plain text excerpt from the extracted content.
+//
+// Possible values...
+// Always include: true (recommended for new users)
+// Never include: false
+// Don't include unless user overrides (&summary=1): 'user' (default)
+//
+// Important: if both content and excerpts are requested, the excerpt will be
+// placed in the description element and the full content inside content:encoded.
+// If excerpts are not requested, the full content will go inside the description element.
+//
+// Why are we not returning both excerpts and content by default?
+// Mainly for backward compatibility.
+// Excerpts should appear in the feed item's description element. Previous versions
+// of Full-Text RSS did not return excerpts, so the description element was always
+// used for the full content (as recommended by the RSS advisory). When returning both,
+// we need somewhere else to place the content (content:encoded).
+// Having both enabled should not create any problems for news readers, but it may create
+// problems for developers upgrading from one of our earlier versions who may now find
+// their applications are returning excerpts instead of the full content they were
+// expecting. To avoid such surprises for users who are upgrading Full-Text RSS,
+// excerpts must be explicitly requested in the querystring by default.
+//
+// Why not use a different element name for excerpts?
+// According to the RSS advisory:
+// "Publishers who employ summaries should store the summary in description and
+// the full content in content:encoded, ordering description first within the item.
+// On items with no summary, the full content should be stored in description."
+// See: http://www.rssboard.org/rss-profile#namespace-elements-content-encoded
+//
+// For more consistent element naming, we recommend new users set this option to true.
+// The full content can still be excluded via the querystring, but the element names
+// will not change: when $options->summary = true, the description element will always
+// be reserved for the excerpt and content:encoded always for full content.
+$options->summary = 'user';
+
// Rewrite relative URLs
// ----------------------
// With this enabled relative URLs found in the extracted content
@@ -67,7 +121,7 @@ $options->exclude_items_on_fail = 'user';
// Enable multi-page support
// -------------------------
// If enabled, we will try to follow next page links on multi-page articles.
-// Currently this only happens for sites where next_page_link has been defined
+// Currently this only happens for sites where next_page_link has been defined
// in a site config file.
$options->multipage = true;
@@ -125,10 +179,10 @@ $options->detect_language = 1;
// Registration key
// ---------------
-// The registration key is optional. It is not required to use Full-Text RSS,
-// and does not affect the normal operation of Full-Text RSS. It is currently
-// only used on admin pages which help you update site patterns with the
-// latest version offered by FiveFilters.org. For these admin-related
+// The registration key is optional. It is not required to use Full-Text RSS,
+// and does not affect the normal operation of Full-Text RSS. It is currently
+// only used on admin pages which help you update site patterns with the
+// latest version offered by FiveFilters.org. For these admin-related
// tasks to complete, we will require a valid registration key.
// If you would like one, you can purchase the latest version of Full-Text RSS
// at http://fivefilters.org/content-only/
@@ -144,12 +198,12 @@ $options->registration_key = '';
// ----------------------
// Certain pages/actions, e.g. updating site patterns with our online tool, will require admin credentials.
// To use these pages, enter a password here and you'll be prompted for it when you try to access those pages.
-// If no password or username is set, pages requiring admin privelages will be inaccessible.
+// If no password or username is set, pages requiring admin privelages will be inaccessible.
// The default username is 'admin'.
// If overriding with an environment variable, separate username and password with a colon, e.g.:
// ftr_admin_credentials: admin:my-secret-password
// Example: $options->admin_credentials = array('username'=>'admin', 'password'=>'my-secret-password');
-$options->admin_credentials = array('username'=>'admin', 'password'=>'admin');
+$options->admin_credentials = array('username'=>'admin', 'password'=>'');
// URLs to allow
// ----------------------
@@ -178,12 +232,12 @@ $options->key_required = false;
// ----------------------
// By default, when processing feeds, we assume item titles in the feed
// have not been truncated. So after processing web pages, the extracted titles
-// are not used in the generated feed. If you prefer to have extracted titles in
-// the feed you can either set this to false, in which case we will always favour
-// extracted titles. Alternatively, if set to 'user' (default) we'll use the
+// are not used in the generated feed. If you prefer to have extracted titles in
+// the feed you can either set this to false, in which case we will always favour
+// extracted titles. Alternatively, if set to 'user' (default) we'll use the
// extracted title if you pass '&use_extracted_title' in the querystring.
// Possible values:
-// * Favour feed titles: true
+// * Favour feed titles: true
// * Favour extracted titles: false
// * Favour feed titles with user override: 'user' (default)
// Note: this has no effect when the input URL is to a web page - in these cases
@@ -192,17 +246,17 @@ $options->favour_feed_titles = 'user';
// Access keys (password protected access)
// ------------------------------------
-// NOTE: You do not need an API key from fivefilters.org to run your own
+// NOTE: You do not need an API key from fivefilters.org to run your own
// copy of the code. This is here if you'd like to restrict access to
// _your_ copy.
// Keys let you group users - those with a key and those without - and
// restrict access to the service to those without a key.
// If you want everyone to access the service in the same way, you can
// leave the array below empty and ignore the access key options further down.
-// The options further down let you control how the service should behave
+// The options further down let you control how the service should behave
// in each mode.
-// Note: Explicitly including the index number (1 and 2 in the examples below)
-// is highly recommended (when generating feeds, we encode the key and
+// Note: Explicitly including the index number (1 and 2 in the examples below)
+// is highly recommended (when generating feeds, we encode the key and
// refer to it by index number and hash).
$options->api_keys = array();
// Example:
@@ -232,13 +286,13 @@ $options->max_entries_with_key = 10;
// filter the resulting HTML for XSS attacks, making it redundant for
// Full-Text RSS do the same. Similarly with frameworks/CMS which display
// feed content - the content should be treated like any other user-submitted content.
-//
+//
// If you are writing an application yourself which is processing feeds generated by
// Full-Text RSS, you can either filter the HTML yourself to remove potential XSS attacks
// or enable this option. This might be useful if you are processing our generated
// feeds with JavaScript on the client side - although there's client side xss
// filtering available too, e.g. https://code.google.com/p/google-caja/wiki/JsHtmlSanitizer
-//
+//
// If enabled, we'll pass retrieved HTML content through htmLawed with
// safe flag on and style attributes denied, see
// http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/htmLawed_README.htm#s3.6
@@ -253,8 +307,8 @@ $options->xss_filter = 'user';
// Allowed parsers
// ----------------------
// Full-Text RSS attempts to use PHP's libxml extension to process HTML.
-// While fast, on some sites it may not always produce good results.
-// For these sites, you can specify an alternative HTML parser:
+// While fast, on some sites it may not always produce good results.
+// For these sites, you can specify an alternative HTML parser:
// parser: html5lib
// The html5lib parser is bundled with Full-Text RSS.
// see http://code.google.com/p/html5lib/
@@ -273,7 +327,7 @@ $options->cors = false;
// Use APC user cache?
// ----------------------
-// If enabled we will store site config files (when requested
+// If enabled we will store site config files (when requested
// for the first time) in APC's user cache. Keys prefixed with 'sc.'
// This improves performance by reducing disk access.
// Note: this has no effect if APC is unavailable on your server.
@@ -346,7 +400,7 @@ $options->rewrite_url = array(
// Valid actions:
// * 'exclude' - exclude this item from the result
// * 'link' - create HTML link to the item
-$options->content_type_exc = array(
+$options->content_type_exc = array(
'application/pdf' => array('action'=>'link', 'name'=>'PDF'),
'image' => array('action'=>'link', 'name'=>'Image'),
'audio' => array('action'=>'link', 'name'=>'Audio'),
@@ -375,13 +429,13 @@ $options->cache_cleanup = 100;
/// DO NOT CHANGE ANYTHING BELOW THIS ///////////
/////////////////////////////////////////////////
-if (!defined('_FF_FTR_VERSION')) define('_FF_FTR_VERSION', '3.1');
+if (!defined('_FF_FTR_VERSION')) define('_FF_FTR_VERSION', '3.2');
if (basename(__FILE__) == 'config.php') {
if (file_exists(dirname(__FILE__).'/custom_config.php')) {
require_once dirname(__FILE__).'/custom_config.php';
}
-
+
// check for environment variables - often used on cloud platforms
// environment variables should be prefixed with 'ftr_', e.g.
// ftr_max_entries: 1
diff --git a/inc/3rdparty/libraries/content-extractor/ContentExtractor.php b/inc/3rdparty/libraries/content-extractor/ContentExtractor.php
index ddd33bb..21e693e 100644
--- a/inc/3rdparty/libraries/content-extractor/ContentExtractor.php
+++ b/inc/3rdparty/libraries/content-extractor/ContentExtractor.php
@@ -1,728 +1,727 @@
- true,
- 'output-xhtml' => true,
- 'logical-emphasis' => true,
- 'show-body-only' => false,
- 'new-blocklevel-tags' => 'article, aside, footer, header, hgroup, menu, nav, section, details, datagrid',
- 'new-inline-tags' => 'mark, time, meter, progress, data',
- 'wrap' => 0,
- 'drop-empty-paras' => true,
- 'drop-proprietary-attributes' => false,
- 'enclose-text' => true,
- 'enclose-block-text' => true,
- 'merge-divs' => true,
- 'merge-spans' => true,
- 'char-encoding' => 'utf8',
- 'hide-comments' => true
- );
- protected $html;
- protected $config;
- protected $title;
- protected $author = array();
- protected $language;
- protected $date;
- protected $body;
- protected $success = false;
- protected $nextPageUrl;
- public $allowedParsers = array('libxml', 'html5lib');
- public $fingerprints = array();
- public $readability;
- public $debug = false;
- public $debugVerbose = false;
-
- function __construct($path, $fallback=null) {
- SiteConfig::set_config_path($path, $fallback);
- }
-
- protected function debug($msg) {
- if ($this->debug) {
- $mem = round(memory_get_usage()/1024, 2);
- $memPeak = round(memory_get_peak_usage()/1024, 2);
- echo '* ',$msg;
- if ($this->debugVerbose) echo ' - mem used: ',$mem," (peak: $memPeak)";
- echo "\n";
- ob_flush();
- flush();
- }
- }
-
- public function reset() {
- $this->html = null;
- $this->readability = null;
- $this->config = null;
- $this->title = null;
- $this->body = null;
- $this->author = array();
- $this->language = null;
- $this->date = null;
- $this->nextPageUrl = null;
- $this->success = false;
- }
-
- public function findHostUsingFingerprints($html) {
- $this->debug('Checking fingerprints...');
- $head = substr($html, 0, 8000);
- foreach ($this->fingerprints as $_fp => $_fphost) {
- $lookin = 'html';
- if (is_array($_fphost)) {
- if (isset($_fphost['head']) && $_fphost['head']) {
- $lookin = 'head';
- }
- $_fphost = $_fphost['hostname'];
- }
- if (strpos($$lookin, $_fp) !== false) {
- $this->debug("Found match: $_fphost");
- return $_fphost;
- }
- }
- $this->debug('No fingerprint matches');
- return false;
- }
-
- // returns SiteConfig instance (joined in order: exact match, wildcard, fingerprint, global, default)
- public function buildSiteConfig($url, $html='', $add_to_cache=true) {
- // extract host name
- $host = @parse_url($url, PHP_URL_HOST);
- $host = strtolower($host);
- if (substr($host, 0, 4) == 'www.') $host = substr($host, 4);
- // is merged version already cached?
- if (SiteConfig::is_cached("$host.merged")) {
- $this->debug("Returning cached and merged site config for $host");
- return SiteConfig::build("$host.merged");
- }
- // let's build from site_config/custom/ and standard/
- $config = SiteConfig::build($host);
- if ($add_to_cache && $config && !SiteConfig::is_cached("$host")) {
- SiteConfig::add_to_cache($host, $config);
- }
- // if no match, use defaults
- if (!$config) $config = new SiteConfig();
- // load fingerprint config?
- if ($config->autodetect_on_failure()) {
- // check HTML for fingerprints
- if (!empty($this->fingerprints) && ($_fphost = $this->findHostUsingFingerprints($html))) {
- if ($config_fingerprint = SiteConfig::build($_fphost)) {
- $this->debug("Appending site config settings from $_fphost (fingerprint match)");
- $config->append($config_fingerprint);
- if ($add_to_cache && !SiteConfig::is_cached($_fphost)) {
- //$config_fingerprint->cache_in_apc = true;
- SiteConfig::add_to_cache($_fphost, $config_fingerprint);
- }
- }
- }
- }
- // load global config?
- if ($config->autodetect_on_failure()) {
- if ($config_global = SiteConfig::build('global', true)) {
- $this->debug('Appending site config settings from global.txt');
- $config->append($config_global);
- if ($add_to_cache && !SiteConfig::is_cached('global')) {
- //$config_global->cache_in_apc = true;
- SiteConfig::add_to_cache('global', $config_global);
- }
- }
- }
- // store copy of merged config
- if ($add_to_cache) {
- // do not store in APC if wildcard match
- $use_apc = ($host == $config->cache_key);
- $config->cache_key = null;
- SiteConfig::add_to_cache("$host.merged", $config, $use_apc);
- }
- return $config;
- }
-
- // returns true on success, false on failure
- // $smart_tidy indicates that if tidy is used and no results are produced, we will
- // try again without it. Tidy helps us deal with PHP's patchy HTML parsing most of the time
- // but it has problems of its own which we try to avoid with this option.
- public function process($html, $url, $smart_tidy=true) {
- $this->reset();
- $this->config = $this->buildSiteConfig($url, $html);
-
- // do string replacements
- if (!empty($this->config->find_string)) {
- if (count($this->config->find_string) == count($this->config->replace_string)) {
- $html = str_replace($this->config->find_string, $this->config->replace_string, $html, $_count);
- $this->debug("Strings replaced: $_count (find_string and/or replace_string)");
- } else {
- $this->debug('Skipped string replacement - incorrect number of find-replace strings in site config');
- }
- unset($_count);
- }
-
- // use tidy (if it exists)?
- // This fixes problems with some sites which would otherwise
- // trouble DOMDocument's HTML parsing. (Although sometimes it
- // makes matters worse, which is why you can override it in site config files.)
- $tidied = false;
- if ($this->config->tidy() && function_exists('tidy_parse_string') && $smart_tidy) {
- $this->debug('Using Tidy');
- $tidy = tidy_parse_string($html, self::$tidy_config, 'UTF8');
- if (tidy_clean_repair($tidy)) {
- $original_html = $html;
- $tidied = true;
- $html = $tidy->value;
- }
- unset($tidy);
- }
-
- // load and parse html
- $_parser = $this->config->parser();
- if (!in_array($_parser, $this->allowedParsers)) {
- $this->debug("HTML parser $_parser not listed, using libxml instead");
- $_parser = 'libxml';
- }
- $this->debug("Attempting to parse HTML with $_parser");
- $this->readability = new Readability($html, $url, $_parser);
-
- // we use xpath to find elements in the given HTML document
- // see http://en.wikipedia.org/wiki/XPath_1.0
- $xpath = new DOMXPath($this->readability->dom);
-
- // try to get next page link
- foreach ($this->config->next_page_link as $pattern) {
- $elems = @$xpath->evaluate($pattern, $this->readability->dom);
- if (is_string($elems)) {
- $this->nextPageUrl = trim($elems);
- break;
- } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
- foreach ($elems as $item) {
- if ($item instanceof DOMElement && $item->hasAttribute('href')) {
- $this->nextPageUrl = $item->getAttribute('href');
- break 2;
- } elseif ($item instanceof DOMAttr && $item->value) {
- $this->nextPageUrl = $item->value;
- break 2;
- }
- }
- }
- }
-
- // try to get title
- foreach ($this->config->title as $pattern) {
- // $this->debug("Trying $pattern");
- $elems = @$xpath->evaluate($pattern, $this->readability->dom);
- if (is_string($elems)) {
- $this->title = trim($elems);
- $this->debug('Title expression evaluated as string: '.$this->title);
- $this->debug("...XPath match: $pattern");
- break;
- } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
- $this->title = $elems->item(0)->textContent;
- $this->debug('Title matched: '.$this->title);
- $this->debug("...XPath match: $pattern");
- // remove title from document
- try {
- $elems->item(0)->parentNode->removeChild($elems->item(0));
- } catch (DOMException $e) {
- // do nothing
- }
- break;
- }
- }
-
- // try to get author (if it hasn't already been set)
- if (empty($this->author)) {
- foreach ($this->config->author as $pattern) {
- $elems = @$xpath->evaluate($pattern, $this->readability->dom);
- if (is_string($elems)) {
- if (trim($elems) != '') {
- $this->author[] = trim($elems);
- $this->debug('Author expression evaluated as string: '.trim($elems));
- $this->debug("...XPath match: $pattern");
- break;
- }
- } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
- foreach ($elems as $elem) {
- if (!isset($elem->parentNode)) continue;
- $this->author[] = trim($elem->textContent);
- $this->debug('Author matched: '.trim($elem->textContent));
- }
- if (!empty($this->author)) {
- $this->debug("...XPath match: $pattern");
- break;
- }
- }
- }
- }
-
- // try to get language
- $_lang_xpath = array('//html[@lang]/@lang', '//meta[@name="DC.language"]/@content');
- foreach ($_lang_xpath as $pattern) {
- $elems = @$xpath->evaluate($pattern, $this->readability->dom);
- if (is_string($elems)) {
- if (trim($elems) != '') {
- $this->language = trim($elems);
- $this->debug('Language matched: '.$this->language);
- break;
- }
- } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
- foreach ($elems as $elem) {
- if (!isset($elem->parentNode)) continue;
- $this->language = trim($elem->textContent);
- $this->debug('Language matched: '.$this->language);
- }
- if ($this->language) break;
- }
- }
-
- // try to get date
- foreach ($this->config->date as $pattern) {
- $elems = @$xpath->evaluate($pattern, $this->readability->dom);
- if (is_string($elems)) {
- $this->date = strtotime(trim($elems, "; \t\n\r\0\x0B"));
- } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
- $this->date = $elems->item(0)->textContent;
- $this->date = strtotime(trim($this->date, "; \t\n\r\0\x0B"));
- // remove date from document
- // $elems->item(0)->parentNode->removeChild($elems->item(0));
- }
- if (!$this->date) {
- $this->date = null;
- } else {
- $this->debug('Date matched: '.date('Y-m-d H:i:s', $this->date));
- $this->debug("...XPath match: $pattern");
- break;
- }
- }
-
- // strip elements (using xpath expressions)
- foreach ($this->config->strip as $pattern) {
- $elems = @$xpath->query($pattern, $this->readability->dom);
- // check for matches
- if ($elems && $elems->length > 0) {
- $this->debug('Stripping '.$elems->length.' elements (strip)');
- for ($i=$elems->length-1; $i >= 0; $i--) {
- $elems->item($i)->parentNode->removeChild($elems->item($i));
- }
- }
- }
-
- // strip elements (using id and class attribute values)
- foreach ($this->config->strip_id_or_class as $string) {
- $string = strtr($string, array("'"=>'', '"'=>''));
- $elems = @$xpath->query("//*[contains(@class, '$string') or contains(@id, '$string')]", $this->readability->dom);
- // check for matches
- if ($elems && $elems->length > 0) {
- $this->debug('Stripping '.$elems->length.' elements (strip_id_or_class)');
- for ($i=$elems->length-1; $i >= 0; $i--) {
- $elems->item($i)->parentNode->removeChild($elems->item($i));
- }
- }
- }
-
- // strip images (using src attribute values)
- foreach ($this->config->strip_image_src as $string) {
- $string = strtr($string, array("'"=>'', '"'=>''));
- $elems = @$xpath->query("//img[contains(@src, '$string')]", $this->readability->dom);
- // check for matches
- if ($elems && $elems->length > 0) {
- $this->debug('Stripping '.$elems->length.' image elements');
- for ($i=$elems->length-1; $i >= 0; $i--) {
- $elems->item($i)->parentNode->removeChild($elems->item($i));
- }
- }
- }
- // strip elements using Readability.com and Instapaper.com ignore class names
- // .entry-unrelated and .instapaper_ignore
- // See https://www.readability.com/publishers/guidelines/#view-plainGuidelines
- // and http://blog.instapaper.com/post/730281947
- $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' entry-unrelated ') or contains(concat(' ',normalize-space(@class),' '),' instapaper_ignore ')]", $this->readability->dom);
- // check for matches
- if ($elems && $elems->length > 0) {
- $this->debug('Stripping '.$elems->length.' .entry-unrelated,.instapaper_ignore elements');
- for ($i=$elems->length-1; $i >= 0; $i--) {
- $elems->item($i)->parentNode->removeChild($elems->item($i));
- }
- }
-
- // strip elements that contain style="display: none;"
- $elems = @$xpath->query("//*[contains(@style,'display:none')]", $this->readability->dom);
- // check for matches
- if ($elems && $elems->length > 0) {
- $this->debug('Stripping '.$elems->length.' elements with inline display:none style');
- for ($i=$elems->length-1; $i >= 0; $i--) {
- $elems->item($i)->parentNode->removeChild($elems->item($i));
- }
- }
-
- // try to get body
- foreach ($this->config->body as $pattern) {
- $elems = @$xpath->query($pattern, $this->readability->dom);
- // check for matches
- if ($elems && $elems->length > 0) {
- $this->debug('Body matched');
- $this->debug("...XPath match: $pattern");
- if ($elems->length == 1) {
- $this->body = $elems->item(0);
- // prune (clean up elements that may not be content)
- if ($this->config->prune()) {
- $this->debug('...pruning content');
- $this->readability->prepArticle($this->body);
- }
- break;
- } else {
- $this->body = $this->readability->dom->createElement('div');
- $this->debug($elems->length.' body elems found');
- foreach ($elems as $elem) {
- if (!isset($elem->parentNode)) continue;
- $isDescendant = false;
- foreach ($this->body->childNodes as $parent) {
- if ($this->isDescendant($parent, $elem)) {
- $isDescendant = true;
- break;
- }
- }
- if ($isDescendant) {
- $this->debug('...element is child of another body element, skipping.');
- } else {
- // prune (clean up elements that may not be content)
- if ($this->config->prune()) {
- $this->debug('Pruning content');
- $this->readability->prepArticle($elem);
- }
- $this->debug('...element added to body');
- $this->body->appendChild($elem);
- }
- }
- if ($this->body->hasChildNodes()) break;
- }
- }
- }
-
- // auto detect?
- $detect_title = $detect_body = $detect_author = $detect_date = false;
- // detect title?
- if (!isset($this->title)) {
- if (empty($this->config->title) || $this->config->autodetect_on_failure()) {
- $detect_title = true;
- }
- }
- // detect body?
- if (!isset($this->body)) {
- if (empty($this->config->body) || $this->config->autodetect_on_failure()) {
- $detect_body = true;
- }
- }
- // detect author?
- if (empty($this->author)) {
- if (empty($this->config->author) || $this->config->autodetect_on_failure()) {
- $detect_author = true;
- }
- }
- // detect date?
- if (!isset($this->date)) {
- if (empty($this->config->date) || $this->config->autodetect_on_failure()) {
- $detect_date = true;
- }
- }
-
- // check for hNews
- if ($detect_title || $detect_body) {
- // check for hentry
- $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' hentry ')]", $this->readability->dom);
- if ($elems && $elems->length > 0) {
- $this->debug('hNews: found hentry');
- $hentry = $elems->item(0);
-
- if ($detect_title) {
- // check for entry-title
- $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' entry-title ')]", $hentry);
- if ($elems && $elems->length > 0) {
- $this->title = $elems->item(0)->textContent;
- $this->debug('hNews: found entry-title: '.$this->title);
- // remove title from document
- $elems->item(0)->parentNode->removeChild($elems->item(0));
- $detect_title = false;
- }
- }
-
- if ($detect_date) {
- // check for time element with pubdate attribute
- $elems = @$xpath->query(".//time[@pubdate] | .//abbr[contains(concat(' ',normalize-space(@class),' '),' published ')]", $hentry);
- if ($elems && $elems->length > 0) {
- $this->date = strtotime(trim($elems->item(0)->textContent));
- // remove date from document
- //$elems->item(0)->parentNode->removeChild($elems->item(0));
- if ($this->date) {
- $this->debug('hNews: found publication date: '.date('Y-m-d H:i:s', $this->date));
- $detect_date = false;
- } else {
- $this->date = null;
- }
- }
- }
-
- if ($detect_author) {
- // check for time element with pubdate attribute
- $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' vcard ') and (contains(concat(' ',normalize-space(@class),' '),' author ') or contains(concat(' ',normalize-space(@class),' '),' byline '))]", $hentry);
- if ($elems && $elems->length > 0) {
- $author = $elems->item(0);
- $fn = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' fn ')]", $author);
- if ($fn && $fn->length > 0) {
- foreach ($fn as $_fn) {
- if (trim($_fn->textContent) != '') {
- $this->author[] = trim($_fn->textContent);
- $this->debug('hNews: found author: '.trim($_fn->textContent));
- }
- }
- } else {
- if (trim($author->textContent) != '') {
- $this->author[] = trim($author->textContent);
- $this->debug('hNews: found author: '.trim($author->textContent));
- }
- }
- $detect_author = empty($this->author);
- }
- }
-
- // check for entry-content.
- // according to hAtom spec, if there are multiple elements marked entry-content,
- // we include all of these in the order they appear - see http://microformats.org/wiki/hatom#Entry_Content
- if ($detect_body) {
- $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' entry-content ')]", $hentry);
- if ($elems && $elems->length > 0) {
- $this->debug('hNews: found entry-content');
- if ($elems->length == 1) {
- // what if it's empty? (some sites misuse hNews - place their content outside an empty entry-content element)
- $e = $elems->item(0);
- if (($e->tagName == 'img') || (trim($e->textContent) != '')) {
- $this->body = $elems->item(0);
- // prune (clean up elements that may not be content)
- if ($this->config->prune()) {
- $this->debug('Pruning content');
- $this->readability->prepArticle($this->body);
- }
- $detect_body = false;
- } else {
- $this->debug('hNews: skipping entry-content - appears not to contain content');
- }
- unset($e);
- } else {
- $this->body = $this->readability->dom->createElement('div');
- $this->debug($elems->length.' entry-content elems found');
- foreach ($elems as $elem) {
- if (!isset($elem->parentNode)) continue;
- $isDescendant = false;
- foreach ($this->body->childNodes as $parent) {
- if ($this->isDescendant($parent, $elem)) {
- $isDescendant = true;
- break;
- }
- }
- if ($isDescendant) {
- $this->debug('Element is child of another body element, skipping.');
- } else {
- // prune (clean up elements that may not be content)
- if ($this->config->prune()) {
- $this->debug('Pruning content');
- $this->readability->prepArticle($elem);
- }
- $this->debug('Element added to body');
- $this->body->appendChild($elem);
- }
- }
- $detect_body = false;
- }
- }
- }
- }
- }
-
- // check for elements marked with instapaper_title
- if ($detect_title) {
- // check for instapaper_title
- $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' instapaper_title ')]", $this->readability->dom);
- if ($elems && $elems->length > 0) {
- $this->title = $elems->item(0)->textContent;
- $this->debug('Title found (.instapaper_title): '.$this->title);
- // remove title from document
- $elems->item(0)->parentNode->removeChild($elems->item(0));
- $detect_title = false;
- }
- }
- // check for elements marked with instapaper_body
- if ($detect_body) {
- $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' instapaper_body ')]", $this->readability->dom);
- if ($elems && $elems->length > 0) {
- $this->debug('body found (.instapaper_body)');
- $this->body = $elems->item(0);
- // prune (clean up elements that may not be content)
- if ($this->config->prune()) {
- $this->debug('Pruning content');
- $this->readability->prepArticle($this->body);
- }
- $detect_body = false;
- }
- }
-
- // Find author in rel="author" marked element
- // We only use this if there's exactly one.
- // If there's more than one, it could indicate more than
- // one author, but it could also indicate that we're processing
- // a page listing different articles with different authors.
- if ($detect_author) {
- $elems = @$xpath->query("//a[contains(concat(' ',normalize-space(@rel),' '),' author ')]", $this->readability->dom);
- if ($elems && $elems->length == 1) {
- $author = trim($elems->item(0)->textContent);
- if ($author != '') {
- $this->debug("Author found (rel=\"author\"): $author");
- $this->author[] = $author;
- $detect_author = false;
- }
- }
- }
-
- // Find date in pubdate marked time element
- // For the same reason given above, we only use this
- // if there's exactly one element.
- if ($detect_date) {
- $elems = @$xpath->query("//time[@pubdate]", $this->readability->dom);
- if ($elems && $elems->length == 1) {
- $this->date = strtotime(trim($elems->item(0)->textContent));
- // remove date from document
- //$elems->item(0)->parentNode->removeChild($elems->item(0));
- if ($this->date) {
- $this->debug('Date found (pubdate marked time element): '.date('Y-m-d H:i:s', $this->date));
- $detect_date = false;
- } else {
- $this->date = null;
- }
- }
- }
-
- // still missing title or body, so we detect using Readability
- if ($detect_title || $detect_body) {
- $this->debug('Using Readability');
- // clone body if we're only using Readability for title (otherwise it may interfere with body element)
- if (isset($this->body)) $this->body = $this->body->cloneNode(true);
- $success = $this->readability->init();
- }
- if ($detect_title) {
- $this->debug('Detecting title');
- $this->title = $this->readability->getTitle()->textContent;
- }
- if ($detect_body && $success) {
- $this->debug('Detecting body');
- $this->body = $this->readability->getContent();
- if ($this->body->childNodes->length == 1 && $this->body->firstChild->nodeType === XML_ELEMENT_NODE) {
- $this->body = $this->body->firstChild;
- }
- // prune (clean up elements that may not be content)
- if ($this->config->prune()) {
- $this->debug('Pruning content');
- $this->readability->prepArticle($this->body);
- }
- }
- if (isset($this->body)) {
- // remove scripts
- $this->readability->removeScripts($this->body);
- // remove any h1-h6 elements that appear as first thing in the body
- // and which match our title
- if (isset($this->title) && ($this->title != '')) {
- $firstChild = $this->body->firstChild;
- while ($firstChild->nodeType && ($firstChild->nodeType !== XML_ELEMENT_NODE)) {
- $firstChild = $firstChild->nextSibling;
- }
- if (($firstChild->nodeType === XML_ELEMENT_NODE)
- && in_array(strtolower($firstChild->tagName), array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))
- && (strtolower(trim($firstChild->textContent)) == strtolower(trim($this->title)))) {
- $this->body->removeChild($firstChild);
- }
- }
- // prevent self-closing iframes
- $elems = $this->body->getElementsByTagName('iframe');
- for ($i = $elems->length-1; $i >= 0; $i--) {
- $e = $elems->item($i);
- if (!$e->hasChildNodes()) {
- $e->appendChild($this->body->ownerDocument->createTextNode('[embedded content]'));
- }
- }
- // remove image lazy loading - WordPress plugin http://wordpress.org/extend/plugins/lazy-load/
- // the plugin replaces the src attribute to point to a 1x1 gif and puts the original src
- // inside the data-lazy-src attribute. It also places the original image inside a noscript element
- // next to the amended one.
- $elems = @$xpath->query("//img[@data-lazy-src]", $this->body);
- for ($i = $elems->length-1; $i >= 0; $i--) {
- $e = $elems->item($i);
- // let's see if we can grab image from noscript
- if ($e->nextSibling !== null && $e->nextSibling->nodeName === 'noscript') {
- $_new_elem = $e->ownerDocument->createDocumentFragment();
- @$_new_elem->appendXML($e->nextSibling->innerHTML);
- $e->nextSibling->parentNode->replaceChild($_new_elem, $e->nextSibling);
- $e->parentNode->removeChild($e);
- } else {
- // Use data-lazy-src as src value
- $e->setAttribute('src', $e->getAttribute('data-lazy-src'));
- $e->removeAttribute('data-lazy-src');
- }
- }
-
- $this->success = true;
- }
-
- // if we've had no success and we've used tidy, there's a chance
- // that tidy has messed up. So let's try again without tidy...
- if (!$this->success && $tidied && $smart_tidy) {
- $this->debug('Trying again without tidy');
- $this->process($original_html, $url, false);
- }
-
- return $this->success;
- }
-
- private function isDescendant(DOMElement $parent, DOMElement $child) {
- $node = $child->parentNode;
- while ($node != null) {
- if ($node->isSameNode($parent)) return true;
- $node = $node->parentNode;
- }
- return false;
- }
-
- public function getContent() {
- return $this->body;
- }
-
- public function getTitle() {
- return $this->title;
- }
-
- public function getAuthors() {
- return $this->author;
- }
-
- public function getLanguage() {
- return $this->language;
- }
-
- public function getDate() {
- return $this->date;
- }
-
- public function getSiteConfig() {
- return $this->config;
- }
-
- public function getNextPageUrl() {
- return $this->nextPageUrl;
- }
-}
-?>
\ No newline at end of file
+ true,
+ 'output-xhtml' => true,
+ 'logical-emphasis' => true,
+ 'show-body-only' => false,
+ 'new-blocklevel-tags' => 'article, aside, footer, header, hgroup, menu, nav, section, details, datagrid',
+ 'new-inline-tags' => 'mark, time, meter, progress, data',
+ 'wrap' => 0,
+ 'drop-empty-paras' => true,
+ 'drop-proprietary-attributes' => false,
+ 'enclose-text' => true,
+ 'enclose-block-text' => true,
+ 'merge-divs' => true,
+ 'merge-spans' => true,
+ 'char-encoding' => 'utf8',
+ 'hide-comments' => true
+ );
+ protected $html;
+ protected $config;
+ protected $title;
+ protected $author = array();
+ protected $language;
+ protected $date;
+ protected $body;
+ protected $success = false;
+ protected $nextPageUrl;
+ public $allowedParsers = array('libxml', 'html5lib');
+ public $fingerprints = array();
+ public $readability;
+ public $debug = false;
+ public $debugVerbose = false;
+
+ function __construct($path, $fallback=null) {
+ SiteConfig::set_config_path($path, $fallback);
+ }
+
+ protected function debug($msg) {
+ if ($this->debug) {
+ $mem = round(memory_get_usage()/1024, 2);
+ $memPeak = round(memory_get_peak_usage()/1024, 2);
+ echo '* ',$msg;
+ if ($this->debugVerbose) echo ' - mem used: ',$mem," (peak: $memPeak)";
+ echo "\n";
+ ob_flush();
+ flush();
+ }
+ }
+
+ public function reset() {
+ $this->html = null;
+ $this->readability = null;
+ $this->config = null;
+ $this->title = null;
+ $this->body = null;
+ $this->author = array();
+ $this->language = null;
+ $this->date = null;
+ $this->nextPageUrl = null;
+ $this->success = false;
+ }
+
+ public function findHostUsingFingerprints($html) {
+ $this->debug('Checking fingerprints...');
+ $head = substr($html, 0, 8000);
+ foreach ($this->fingerprints as $_fp => $_fphost) {
+ $lookin = 'html';
+ if (is_array($_fphost)) {
+ if (isset($_fphost['head']) && $_fphost['head']) {
+ $lookin = 'head';
+ }
+ $_fphost = $_fphost['hostname'];
+ }
+ if (strpos($$lookin, $_fp) !== false) {
+ $this->debug("Found match: $_fphost");
+ return $_fphost;
+ }
+ }
+ $this->debug('No fingerprint matches');
+ return false;
+ }
+
+ // returns SiteConfig instance (joined in order: exact match, wildcard, fingerprint, global, default)
+ public function buildSiteConfig($url, $html='', $add_to_cache=true) {
+ // extract host name
+ $host = @parse_url($url, PHP_URL_HOST);
+ $host = strtolower($host);
+ if (substr($host, 0, 4) == 'www.') $host = substr($host, 4);
+ // is merged version already cached?
+ if (SiteConfig::is_cached("$host.merged")) {
+ $this->debug("Returning cached and merged site config for $host");
+ return SiteConfig::build("$host.merged");
+ }
+ // let's build from site_config/custom/ and standard/
+ $config = SiteConfig::build($host);
+ if ($add_to_cache && $config && !SiteConfig::is_cached("$host")) {
+ SiteConfig::add_to_cache($host, $config);
+ }
+ // if no match, use defaults
+ if (!$config) $config = new SiteConfig();
+ // load fingerprint config?
+ if ($config->autodetect_on_failure()) {
+ // check HTML for fingerprints
+ if (!empty($this->fingerprints) && ($_fphost = $this->findHostUsingFingerprints($html))) {
+ if ($config_fingerprint = SiteConfig::build($_fphost)) {
+ $this->debug("Appending site config settings from $_fphost (fingerprint match)");
+ $config->append($config_fingerprint);
+ if ($add_to_cache && !SiteConfig::is_cached($_fphost)) {
+ //$config_fingerprint->cache_in_apc = true;
+ SiteConfig::add_to_cache($_fphost, $config_fingerprint);
+ }
+ }
+ }
+ }
+ // load global config?
+ if ($config->autodetect_on_failure()) {
+ if ($config_global = SiteConfig::build('global', true)) {
+ $this->debug('Appending site config settings from global.txt');
+ $config->append($config_global);
+ if ($add_to_cache && !SiteConfig::is_cached('global')) {
+ //$config_global->cache_in_apc = true;
+ SiteConfig::add_to_cache('global', $config_global);
+ }
+ }
+ }
+ // store copy of merged config
+ if ($add_to_cache) {
+ // do not store in APC if wildcard match
+ $use_apc = ($host == $config->cache_key);
+ $config->cache_key = null;
+ SiteConfig::add_to_cache("$host.merged", $config, $use_apc);
+ }
+ return $config;
+ }
+
+ // returns true on success, false on failure
+ // $smart_tidy indicates that if tidy is used and no results are produced, we will
+ // try again without it. Tidy helps us deal with PHP's patchy HTML parsing most of the time
+ // but it has problems of its own which we try to avoid with this option.
+ public function process($html, $url, $smart_tidy=true) {
+ $this->reset();
+ $this->config = $this->buildSiteConfig($url, $html);
+
+ // do string replacements
+ if (!empty($this->config->find_string)) {
+ if (count($this->config->find_string) == count($this->config->replace_string)) {
+ $html = str_replace($this->config->find_string, $this->config->replace_string, $html, $_count);
+ $this->debug("Strings replaced: $_count (find_string and/or replace_string)");
+ } else {
+ $this->debug('Skipped string replacement - incorrect number of find-replace strings in site config');
+ }
+ unset($_count);
+ }
+
+ // use tidy (if it exists)?
+ // This fixes problems with some sites which would otherwise
+ // trouble DOMDocument's HTML parsing. (Although sometimes it
+ // makes matters worse, which is why you can override it in site config files.)
+ $tidied = false;
+ if ($this->config->tidy() && function_exists('tidy_parse_string') && $smart_tidy) {
+ $this->debug('Using Tidy');
+ $tidy = tidy_parse_string($html, self::$tidy_config, 'UTF8');
+ if (tidy_clean_repair($tidy)) {
+ $original_html = $html;
+ $tidied = true;
+ $html = $tidy->value;
+ }
+ unset($tidy);
+ }
+
+ // load and parse html
+ $_parser = $this->config->parser();
+ if (!in_array($_parser, $this->allowedParsers)) {
+ $this->debug("HTML parser $_parser not listed, using libxml instead");
+ $_parser = 'libxml';
+ }
+ $this->debug("Attempting to parse HTML with $_parser");
+ $this->readability = new Readability($html, $url, $_parser);
+
+ // we use xpath to find elements in the given HTML document
+ // see http://en.wikipedia.org/wiki/XPath_1.0
+ $xpath = new DOMXPath($this->readability->dom);
+
+ // try to get next page link
+ foreach ($this->config->next_page_link as $pattern) {
+ $elems = @$xpath->evaluate($pattern, $this->readability->dom);
+ if (is_string($elems)) {
+ $this->nextPageUrl = trim($elems);
+ break;
+ } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
+ foreach ($elems as $item) {
+ if ($item instanceof DOMElement && $item->hasAttribute('href')) {
+ $this->nextPageUrl = $item->getAttribute('href');
+ break 2;
+ } elseif ($item instanceof DOMAttr && $item->value) {
+ $this->nextPageUrl = $item->value;
+ break 2;
+ }
+ }
+ }
+ }
+
+ // try to get title
+ foreach ($this->config->title as $pattern) {
+ // $this->debug("Trying $pattern");
+ $elems = @$xpath->evaluate($pattern, $this->readability->dom);
+ if (is_string($elems)) {
+ $this->title = trim($elems);
+ $this->debug('Title expression evaluated as string: '.$this->title);
+ $this->debug("...XPath match: $pattern");
+ break;
+ } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
+ $this->title = $elems->item(0)->textContent;
+ $this->debug('Title matched: '.$this->title);
+ $this->debug("...XPath match: $pattern");
+ // remove title from document
+ try {
+ @$elems->item(0)->parentNode->removeChild($elems->item(0));
+ } catch (DOMException $e) {
+ // do nothing
+ }
+ break;
+ }
+ }
+
+ // try to get author (if it hasn't already been set)
+ if (empty($this->author)) {
+ foreach ($this->config->author as $pattern) {
+ $elems = @$xpath->evaluate($pattern, $this->readability->dom);
+ if (is_string($elems)) {
+ if (trim($elems) != '') {
+ $this->author[] = trim($elems);
+ $this->debug('Author expression evaluated as string: '.trim($elems));
+ $this->debug("...XPath match: $pattern");
+ break;
+ }
+ } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
+ foreach ($elems as $elem) {
+ if (!isset($elem->parentNode)) continue;
+ $this->author[] = trim($elem->textContent);
+ $this->debug('Author matched: '.trim($elem->textContent));
+ }
+ if (!empty($this->author)) {
+ $this->debug("...XPath match: $pattern");
+ break;
+ }
+ }
+ }
+ }
+
+ // try to get language
+ $_lang_xpath = array('//html[@lang]/@lang', '//meta[@name="DC.language"]/@content');
+ foreach ($_lang_xpath as $pattern) {
+ $elems = @$xpath->evaluate($pattern, $this->readability->dom);
+ if (is_string($elems)) {
+ if (trim($elems) != '') {
+ $this->language = trim($elems);
+ $this->debug('Language matched: '.$this->language);
+ break;
+ }
+ } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
+ foreach ($elems as $elem) {
+ if (!isset($elem->parentNode)) continue;
+ $this->language = trim($elem->textContent);
+ $this->debug('Language matched: '.$this->language);
+ }
+ if ($this->language) break;
+ }
+ }
+
+ // try to get date
+ foreach ($this->config->date as $pattern) {
+ $elems = @$xpath->evaluate($pattern, $this->readability->dom);
+ if (is_string($elems)) {
+ $this->date = strtotime(trim($elems, "; \t\n\r\0\x0B"));
+ } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
+ $this->date = $elems->item(0)->textContent;
+ $this->date = strtotime(trim($this->date, "; \t\n\r\0\x0B"));
+ // remove date from document
+ // $elems->item(0)->parentNode->removeChild($elems->item(0));
+ }
+ if (!$this->date) {
+ $this->date = null;
+ } else {
+ $this->debug('Date matched: '.date('Y-m-d H:i:s', $this->date));
+ $this->debug("...XPath match: $pattern");
+ break;
+ }
+ }
+
+ // strip elements (using xpath expressions)
+ foreach ($this->config->strip as $pattern) {
+ $elems = @$xpath->query($pattern, $this->readability->dom);
+ // check for matches
+ if ($elems && $elems->length > 0) {
+ $this->debug('Stripping '.$elems->length.' elements (strip)');
+ for ($i=$elems->length-1; $i >= 0; $i--) {
+ $elems->item($i)->parentNode->removeChild($elems->item($i));
+ }
+ }
+ }
+
+ // strip elements (using id and class attribute values)
+ foreach ($this->config->strip_id_or_class as $string) {
+ $string = strtr($string, array("'"=>'', '"'=>''));
+ $elems = @$xpath->query("//*[contains(@class, '$string') or contains(@id, '$string')]", $this->readability->dom);
+ // check for matches
+ if ($elems && $elems->length > 0) {
+ $this->debug('Stripping '.$elems->length.' elements (strip_id_or_class)');
+ for ($i=$elems->length-1; $i >= 0; $i--) {
+ $elems->item($i)->parentNode->removeChild($elems->item($i));
+ }
+ }
+ }
+
+ // strip images (using src attribute values)
+ foreach ($this->config->strip_image_src as $string) {
+ $string = strtr($string, array("'"=>'', '"'=>''));
+ $elems = @$xpath->query("//img[contains(@src, '$string')]", $this->readability->dom);
+ // check for matches
+ if ($elems && $elems->length > 0) {
+ $this->debug('Stripping '.$elems->length.' image elements');
+ for ($i=$elems->length-1; $i >= 0; $i--) {
+ $elems->item($i)->parentNode->removeChild($elems->item($i));
+ }
+ }
+ }
+ // strip elements using Readability.com and Instapaper.com ignore class names
+ // .entry-unrelated and .instapaper_ignore
+ // See https://www.readability.com/publishers/guidelines/#view-plainGuidelines
+ // and http://blog.instapaper.com/post/730281947
+ $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' entry-unrelated ') or contains(concat(' ',normalize-space(@class),' '),' instapaper_ignore ')]", $this->readability->dom);
+ // check for matches
+ if ($elems && $elems->length > 0) {
+ $this->debug('Stripping '.$elems->length.' .entry-unrelated,.instapaper_ignore elements');
+ for ($i=$elems->length-1; $i >= 0; $i--) {
+ $elems->item($i)->parentNode->removeChild($elems->item($i));
+ }
+ }
+
+ // strip elements that contain style="display: none;"
+ $elems = @$xpath->query("//*[contains(@style,'display:none')]", $this->readability->dom);
+ // check for matches
+ if ($elems && $elems->length > 0) {
+ $this->debug('Stripping '.$elems->length.' elements with inline display:none style');
+ for ($i=$elems->length-1; $i >= 0; $i--) {
+ $elems->item($i)->parentNode->removeChild($elems->item($i));
+ }
+ }
+
+ // try to get body
+ foreach ($this->config->body as $pattern) {
+ $elems = @$xpath->query($pattern, $this->readability->dom);
+ // check for matches
+ if ($elems && $elems->length > 0) {
+ $this->debug('Body matched');
+ $this->debug("...XPath match: $pattern");
+ if ($elems->length == 1) {
+ $this->body = $elems->item(0);
+ // prune (clean up elements that may not be content)
+ if ($this->config->prune()) {
+ $this->debug('...pruning content');
+ $this->readability->prepArticle($this->body);
+ }
+ break;
+ } else {
+ $this->body = $this->readability->dom->createElement('div');
+ $this->debug($elems->length.' body elems found');
+ foreach ($elems as $elem) {
+ if (!isset($elem->parentNode)) continue;
+ $isDescendant = false;
+ foreach ($this->body->childNodes as $parent) {
+ if ($this->isDescendant($parent, $elem)) {
+ $isDescendant = true;
+ break;
+ }
+ }
+ if ($isDescendant) {
+ $this->debug('...element is child of another body element, skipping.');
+ } else {
+ // prune (clean up elements that may not be content)
+ if ($this->config->prune()) {
+ $this->debug('Pruning content');
+ $this->readability->prepArticle($elem);
+ }
+ $this->debug('...element added to body');
+ $this->body->appendChild($elem);
+ }
+ }
+ if ($this->body->hasChildNodes()) break;
+ }
+ }
+ }
+
+ // auto detect?
+ $detect_title = $detect_body = $detect_author = $detect_date = false;
+ // detect title?
+ if (!isset($this->title)) {
+ if (empty($this->config->title) || $this->config->autodetect_on_failure()) {
+ $detect_title = true;
+ }
+ }
+ // detect body?
+ if (!isset($this->body)) {
+ if (empty($this->config->body) || $this->config->autodetect_on_failure()) {
+ $detect_body = true;
+ }
+ }
+ // detect author?
+ if (empty($this->author)) {
+ if (empty($this->config->author) || $this->config->autodetect_on_failure()) {
+ $detect_author = true;
+ }
+ }
+ // detect date?
+ if (!isset($this->date)) {
+ if (empty($this->config->date) || $this->config->autodetect_on_failure()) {
+ $detect_date = true;
+ }
+ }
+
+ // check for hNews
+ if ($detect_title || $detect_body) {
+ // check for hentry
+ $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' hentry ')]", $this->readability->dom);
+ if ($elems && $elems->length > 0) {
+ $this->debug('hNews: found hentry');
+ $hentry = $elems->item(0);
+
+ if ($detect_title) {
+ // check for entry-title
+ $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' entry-title ')]", $hentry);
+ if ($elems && $elems->length > 0) {
+ $this->title = $elems->item(0)->textContent;
+ $this->debug('hNews: found entry-title: '.$this->title);
+ // remove title from document
+ $elems->item(0)->parentNode->removeChild($elems->item(0));
+ $detect_title = false;
+ }
+ }
+
+ if ($detect_date) {
+ // check for time element with pubdate attribute
+ $elems = @$xpath->query(".//time[@pubdate] | .//abbr[contains(concat(' ',normalize-space(@class),' '),' published ')]", $hentry);
+ if ($elems && $elems->length > 0) {
+ $this->date = strtotime(trim($elems->item(0)->textContent));
+ // remove date from document
+ //$elems->item(0)->parentNode->removeChild($elems->item(0));
+ if ($this->date) {
+ $this->debug('hNews: found publication date: '.date('Y-m-d H:i:s', $this->date));
+ $detect_date = false;
+ } else {
+ $this->date = null;
+ }
+ }
+ }
+
+ if ($detect_author) {
+ // check for time element with pubdate attribute
+ $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' vcard ') and (contains(concat(' ',normalize-space(@class),' '),' author ') or contains(concat(' ',normalize-space(@class),' '),' byline '))]", $hentry);
+ if ($elems && $elems->length > 0) {
+ $author = $elems->item(0);
+ $fn = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' fn ')]", $author);
+ if ($fn && $fn->length > 0) {
+ foreach ($fn as $_fn) {
+ if (trim($_fn->textContent) != '') {
+ $this->author[] = trim($_fn->textContent);
+ $this->debug('hNews: found author: '.trim($_fn->textContent));
+ }
+ }
+ } else {
+ if (trim($author->textContent) != '') {
+ $this->author[] = trim($author->textContent);
+ $this->debug('hNews: found author: '.trim($author->textContent));
+ }
+ }
+ $detect_author = empty($this->author);
+ }
+ }
+
+ // check for entry-content.
+ // according to hAtom spec, if there are multiple elements marked entry-content,
+ // we include all of these in the order they appear - see http://microformats.org/wiki/hatom#Entry_Content
+ if ($detect_body) {
+ $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' entry-content ')]", $hentry);
+ if ($elems && $elems->length > 0) {
+ $this->debug('hNews: found entry-content');
+ if ($elems->length == 1) {
+ // what if it's empty? (some sites misuse hNews - place their content outside an empty entry-content element)
+ $e = $elems->item(0);
+ if (($e->tagName == 'img') || (trim($e->textContent) != '')) {
+ $this->body = $elems->item(0);
+ // prune (clean up elements that may not be content)
+ if ($this->config->prune()) {
+ $this->debug('Pruning content');
+ $this->readability->prepArticle($this->body);
+ }
+ $detect_body = false;
+ } else {
+ $this->debug('hNews: skipping entry-content - appears not to contain content');
+ }
+ unset($e);
+ } else {
+ $this->body = $this->readability->dom->createElement('div');
+ $this->debug($elems->length.' entry-content elems found');
+ foreach ($elems as $elem) {
+ if (!isset($elem->parentNode)) continue;
+ $isDescendant = false;
+ foreach ($this->body->childNodes as $parent) {
+ if ($this->isDescendant($parent, $elem)) {
+ $isDescendant = true;
+ break;
+ }
+ }
+ if ($isDescendant) {
+ $this->debug('Element is child of another body element, skipping.');
+ } else {
+ // prune (clean up elements that may not be content)
+ if ($this->config->prune()) {
+ $this->debug('Pruning content');
+ $this->readability->prepArticle($elem);
+ }
+ $this->debug('Element added to body');
+ $this->body->appendChild($elem);
+ }
+ }
+ $detect_body = false;
+ }
+ }
+ }
+ }
+ }
+
+ // check for elements marked with instapaper_title
+ if ($detect_title) {
+ // check for instapaper_title
+ $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' instapaper_title ')]", $this->readability->dom);
+ if ($elems && $elems->length > 0) {
+ $this->title = $elems->item(0)->textContent;
+ $this->debug('Title found (.instapaper_title): '.$this->title);
+ // remove title from document
+ $elems->item(0)->parentNode->removeChild($elems->item(0));
+ $detect_title = false;
+ }
+ }
+ // check for elements marked with instapaper_body
+ if ($detect_body) {
+ $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' instapaper_body ')]", $this->readability->dom);
+ if ($elems && $elems->length > 0) {
+ $this->debug('body found (.instapaper_body)');
+ $this->body = $elems->item(0);
+ // prune (clean up elements that may not be content)
+ if ($this->config->prune()) {
+ $this->debug('Pruning content');
+ $this->readability->prepArticle($this->body);
+ }
+ $detect_body = false;
+ }
+ }
+
+ // Find author in rel="author" marked element
+ // We only use this if there's exactly one.
+ // If there's more than one, it could indicate more than
+ // one author, but it could also indicate that we're processing
+ // a page listing different articles with different authors.
+ if ($detect_author) {
+ $elems = @$xpath->query("//a[contains(concat(' ',normalize-space(@rel),' '),' author ')]", $this->readability->dom);
+ if ($elems && $elems->length == 1) {
+ $author = trim($elems->item(0)->textContent);
+ if ($author != '') {
+ $this->debug("Author found (rel=\"author\"): $author");
+ $this->author[] = $author;
+ $detect_author = false;
+ }
+ }
+ }
+
+ // Find date in pubdate marked time element
+ // For the same reason given above, we only use this
+ // if there's exactly one element.
+ if ($detect_date) {
+ $elems = @$xpath->query("//time[@pubdate]", $this->readability->dom);
+ if ($elems && $elems->length == 1) {
+ $this->date = strtotime(trim($elems->item(0)->textContent));
+ // remove date from document
+ //$elems->item(0)->parentNode->removeChild($elems->item(0));
+ if ($this->date) {
+ $this->debug('Date found (pubdate marked time element): '.date('Y-m-d H:i:s', $this->date));
+ $detect_date = false;
+ } else {
+ $this->date = null;
+ }
+ }
+ }
+
+ // still missing title or body, so we detect using Readability
+ if ($detect_title || $detect_body) {
+ $this->debug('Using Readability');
+ // clone body if we're only using Readability for title (otherwise it may interfere with body element)
+ if (isset($this->body)) $this->body = $this->body->cloneNode(true);
+ $success = $this->readability->init();
+ }
+ if ($detect_title) {
+ $this->debug('Detecting title');
+ $this->title = $this->readability->getTitle()->textContent;
+ }
+ if ($detect_body && $success) {
+ $this->debug('Detecting body');
+ $this->body = $this->readability->getContent();
+ if ($this->body->childNodes->length == 1 && $this->body->firstChild->nodeType === XML_ELEMENT_NODE) {
+ $this->body = $this->body->firstChild;
+ }
+ // prune (clean up elements that may not be content)
+ if ($this->config->prune()) {
+ $this->debug('Pruning content');
+ $this->readability->prepArticle($this->body);
+ }
+ }
+ if (isset($this->body)) {
+ // remove scripts
+ $this->readability->removeScripts($this->body);
+ // remove any h1-h6 elements that appear as first thing in the body
+ // and which match our title
+ if (isset($this->title) && ($this->title != '')) {
+ $firstChild = $this->body->firstChild;
+ while ($firstChild->nodeType && ($firstChild->nodeType !== XML_ELEMENT_NODE)) {
+ $firstChild = $firstChild->nextSibling;
+ }
+ if (($firstChild->nodeType === XML_ELEMENT_NODE)
+ && in_array(strtolower($firstChild->tagName), array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))
+ && (strtolower(trim($firstChild->textContent)) == strtolower(trim($this->title)))) {
+ $this->body->removeChild($firstChild);
+ }
+ }
+ // prevent self-closing iframes
+ $elems = $this->body->getElementsByTagName('iframe');
+ for ($i = $elems->length-1; $i >= 0; $i--) {
+ $e = $elems->item($i);
+ if (!$e->hasChildNodes()) {
+ $e->appendChild($this->body->ownerDocument->createTextNode('[embedded content]'));
+ }
+ }
+ // remove image lazy loading - WordPress plugin http://wordpress.org/extend/plugins/lazy-load/
+ // the plugin replaces the src attribute to point to a 1x1 gif and puts the original src
+ // inside the data-lazy-src attribute. It also places the original image inside a noscript element
+ // next to the amended one.
+ $elems = @$xpath->query("//img[@data-lazy-src]", $this->body);
+ for ($i = $elems->length-1; $i >= 0; $i--) {
+ $e = $elems->item($i);
+ // let's see if we can grab image from noscript
+ if ($e->nextSibling !== null && $e->nextSibling->nodeName === 'noscript') {
+ $_new_elem = $e->ownerDocument->createDocumentFragment();
+ @$_new_elem->appendXML($e->nextSibling->innerHTML);
+ $e->nextSibling->parentNode->replaceChild($_new_elem, $e->nextSibling);
+ $e->parentNode->removeChild($e);
+ } else {
+ // Use data-lazy-src as src value
+ $e->setAttribute('src', $e->getAttribute('data-lazy-src'));
+ $e->removeAttribute('data-lazy-src');
+ }
+ }
+
+ $this->success = true;
+ }
+
+ // if we've had no success and we've used tidy, there's a chance
+ // that tidy has messed up. So let's try again without tidy...
+ if (!$this->success && $tidied && $smart_tidy) {
+ $this->debug('Trying again without tidy');
+ $this->process($original_html, $url, false);
+ }
+
+ return $this->success;
+ }
+
+ private function isDescendant(DOMElement $parent, DOMElement $child) {
+ $node = $child->parentNode;
+ while ($node != null) {
+ if ($node->isSameNode($parent)) return true;
+ $node = $node->parentNode;
+ }
+ return false;
+ }
+
+ public function getContent() {
+ return $this->body;
+ }
+
+ public function getTitle() {
+ return $this->title;
+ }
+
+ public function getAuthors() {
+ return $this->author;
+ }
+
+ public function getLanguage() {
+ return $this->language;
+ }
+
+ public function getDate() {
+ return $this->date;
+ }
+
+ public function getSiteConfig() {
+ return $this->config;
+ }
+
+ public function getNextPageUrl() {
+ return $this->nextPageUrl;
+ }
+}
\ No newline at end of file
diff --git a/inc/3rdparty/libraries/content-extractor/SiteConfig.php b/inc/3rdparty/libraries/content-extractor/SiteConfig.php
index c5e300d..1f6a760 100644
--- a/inc/3rdparty/libraries/content-extractor/SiteConfig.php
+++ b/inc/3rdparty/libraries/content-extractor/SiteConfig.php
@@ -1,338 +1,343 @@
-tidy)) ? $this->tidy : $this->default_tidy;
- return $this->tidy;
- }
-
- // return bool or null
- public function prune($use_default=true) {
- if ($use_default) return (isset($this->prune)) ? $this->prune : $this->default_prune;
- return $this->prune;
- }
-
- // return string or null
- public function parser($use_default=true) {
- if ($use_default) return (isset($this->parser)) ? $this->parser : $this->default_parser;
- return $this->parser;
- }
-
- // return bool or null
- public function autodetect_on_failure($use_default=true) {
- if ($use_default) return (isset($this->autodetect_on_failure)) ? $this->autodetect_on_failure : $this->default_autodetect_on_failure;
- return $this->autodetect_on_failure;
- }
-
- public static function set_config_path($path, $fallback=null) {
- self::$config_path = $path;
- self::$config_path_fallback = $fallback;
- }
-
- public static function add_to_cache($key, SiteConfig $config, $use_apc=true) {
- $key = strtolower($key);
- if (substr($key, 0, 4) == 'www.') $key = substr($key, 4);
- if ($config->cache_key) $key = $config->cache_key;
- self::$config_cache[$key] = $config;
- if (self::$apc && $use_apc) {
- self::debug("Adding site config to APC cache with key sc.$key");
- apc_add("sc.$key", $config);
- }
- self::debug("Cached site config with key $key");
- }
-
- public static function is_cached($key) {
- $key = strtolower($key);
- if (substr($key, 0, 4) == 'www.') $key = substr($key, 4);
- if (array_key_exists($key, self::$config_cache)) {
- return true;
- } elseif (self::$apc && (bool)apc_fetch("sc.$key")) {
- return true;
- }
- return false;
- }
-
- public function append(SiteConfig $newconfig) {
- // check for commands where we accept multiple statements (no test_url)
- foreach (array('title', 'body', 'author', 'date', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'single_page_link_in_feed', 'next_page_link', 'http_header', 'find_string', 'replace_string') as $var) {
- // append array elements for this config variable from $newconfig to this config
- //$this->$var = $this->$var + $newconfig->$var;
- $this->$var = array_unique(array_merge($this->$var, $newconfig->$var));
- }
- // check for single statement commands
- // we do not overwrite existing non null values
- foreach (array('tidy', 'prune', 'parser', 'autodetect_on_failure') as $var) {
- if ($this->$var === null) $this->$var = $newconfig->$var;
- }
- }
-
- // returns SiteConfig instance if an appropriate one is found, false otherwise
- // if $exact_host_match is true, we will not look for wildcard config matches
- // by default if host is 'test.example.org' we will look for and load '.example.org.txt' if it exists
- public static function build($host, $exact_host_match=false) {
- $host = strtolower($host);
- if (substr($host, 0, 4) == 'www.') $host = substr($host, 4);
- if (!$host || (strlen($host) > 200) || !preg_match(self::HOSTNAME_REGEX, ltrim($host, '.'))) return false;
- // check for site configuration
- $try = array($host);
- // should we look for wildcard matches
- if (!$exact_host_match) {
- $split = explode('.', $host);
- if (count($split) > 1) {
- array_shift($split);
- $try[] = '.'.implode('.', $split);
- }
- }
-
- // look for site config file in primary folder
- self::debug(". looking for site config for $host in primary folder");
- foreach ($try as $h) {
- if (array_key_exists($h, self::$config_cache)) {
- self::debug("... site config for $h already loaded in this request");
- return self::$config_cache[$h];
- } elseif (self::$apc && ($sconfig = apc_fetch("sc.$h"))) {
- self::debug("... site config for $h in APC cache");
- return $sconfig;
- } elseif (file_exists(self::$config_path."/$h.txt")) {
- self::debug("... found site config ($h.txt)");
- $file_primary = self::$config_path."/$h.txt";
- $matched_name = $h;
- break;
- }
- }
-
- // if we found site config, process it
- if (isset($file_primary)) {
- $config_lines = file($file_primary, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
- if (!$config_lines || !is_array($config_lines)) return false;
- $config = self::build_from_array($config_lines);
- // if APC caching is available and enabled, mark this for cache
- //$config->cache_in_apc = true;
- $config->cache_key = $matched_name;
-
- // if autodetec on failure is off (on by default) we do not need to look
- // in secondary folder
- if (!$config->autodetect_on_failure()) {
- self::debug('... autodetect on failure is disabled (no other site config files will be loaded)');
- return $config;
- }
- }
-
- // look for site config file in secondary folder
- if (isset(self::$config_path_fallback)) {
- self::debug(". looking for site config for $host in secondary folder");
- foreach ($try as $h) {
- if (file_exists(self::$config_path_fallback."/$h.txt")) {
- self::debug("... found site config in secondary folder ($h.txt)");
- $file_secondary = self::$config_path_fallback."/$h.txt";
- $matched_name = $h;
- break;
- }
- }
- if (!isset($file_secondary)) {
- self::debug("... no site config match in secondary folder");
- }
- }
-
- // return false if no config file found
- if (!isset($file_primary) && !isset($file_secondary)) {
- self::debug("... no site config match for $host");
- return false;
- }
-
- // return primary config if secondary not found
- if (!isset($file_secondary) && isset($config)) {
- return $config;
- }
-
- // process secondary config file
- $config_lines = file($file_secondary, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
- if (!$config_lines || !is_array($config_lines)) {
- // failed to process secondary
- if (isset($config)) {
- // return primary config
- return $config;
- } else {
- return false;
- }
- }
-
- // merge with primary and return
- if (isset($config)) {
- self::debug('. merging config files');
- $config->append(self::build_from_array($config_lines));
- return $config;
- } else {
- // return just secondary
- $config = self::build_from_array($config_lines);
- // if APC caching is available and enabled, mark this for cache
- //$config->cache_in_apc = true;
- $config->cache_key = $matched_name;
- return $config;
- }
- }
-
- public static function build_from_array(array $lines) {
- $config = new SiteConfig();
- foreach ($lines as $line) {
- $line = trim($line);
-
- // skip comments, empty lines
- if ($line == '' || $line[0] == '#') continue;
-
- // get command
- $command = explode(':', $line, 2);
- // if there's no colon ':', skip this line
- if (count($command) != 2) continue;
- $val = trim($command[1]);
- $command = trim($command[0]);
- if ($command == '' || $val == '') continue;
-
- // check for commands where we accept multiple statements
- if (in_array($command, array('title', 'body', 'author', 'date', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'single_page_link_in_feed', 'next_page_link', 'http_header', 'test_url', 'find_string', 'replace_string'))) {
- array_push($config->$command, $val);
- // check for single statement commands that evaluate to true or false
- } elseif (in_array($command, array('tidy', 'prune', 'autodetect_on_failure'))) {
- $config->$command = ($val == 'yes');
- // check for single statement commands stored as strings
- } elseif (in_array($command, array('parser'))) {
- $config->$command = $val;
- // check for replace_string(find): replace
- } elseif ((substr($command, -1) == ')') && preg_match('!^([a-z0-9_]+)\((.*?)\)$!i', $command, $match)) {
- if (in_array($match[1], array('replace_string'))) {
- $command = $match[1];
- array_push($config->find_string, $match[2]);
- array_push($config->$command, $val);
- }
- }
- }
- return $config;
- }
-}
-?>
\ No newline at end of file
+tidy)) ? $this->tidy : $this->default_tidy;
+ return $this->tidy;
+ }
+
+ // return bool or null
+ public function prune($use_default=true) {
+ if ($use_default) return (isset($this->prune)) ? $this->prune : $this->default_prune;
+ return $this->prune;
+ }
+
+ // return string or null
+ public function parser($use_default=true) {
+ if ($use_default) return (isset($this->parser)) ? $this->parser : $this->default_parser;
+ return $this->parser;
+ }
+
+ // return bool or null
+ public function autodetect_on_failure($use_default=true) {
+ if ($use_default) return (isset($this->autodetect_on_failure)) ? $this->autodetect_on_failure : $this->default_autodetect_on_failure;
+ return $this->autodetect_on_failure;
+ }
+
+ public static function set_config_path($path, $fallback=null) {
+ self::$config_path = $path;
+ self::$config_path_fallback = $fallback;
+ }
+
+ public static function add_to_cache($key, SiteConfig $config, $use_apc=true) {
+ $key = strtolower($key);
+ if (substr($key, 0, 4) == 'www.') $key = substr($key, 4);
+ if ($config->cache_key) $key = $config->cache_key;
+ self::$config_cache[$key] = $config;
+ if (self::$apc && $use_apc) {
+ self::debug("Adding site config to APC cache with key sc.$key");
+ apc_add("sc.$key", $config);
+ }
+ self::debug("Cached site config with key $key");
+ }
+
+ public static function is_cached($key) {
+ $key = strtolower($key);
+ if (substr($key, 0, 4) == 'www.') $key = substr($key, 4);
+ if (array_key_exists($key, self::$config_cache)) {
+ return true;
+ } elseif (self::$apc && (bool)apc_fetch("sc.$key")) {
+ return true;
+ }
+ return false;
+ }
+
+ public function append(SiteConfig $newconfig) {
+ // check for commands where we accept multiple statements (no test_url)
+ foreach (array('title', 'body', 'author', 'date', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'single_page_link_in_feed', 'next_page_link', 'http_header') as $var) {
+ // append array elements for this config variable from $newconfig to this config
+ //$this->$var = $this->$var + $newconfig->$var;
+ $this->$var = array_unique(array_merge($this->$var, $newconfig->$var));
+ }
+ // check for single statement commands
+ // we do not overwrite existing non null values
+ foreach (array('tidy', 'prune', 'parser', 'autodetect_on_failure') as $var) {
+ if ($this->$var === null) $this->$var = $newconfig->$var;
+ }
+ // treat find_string and replace_string separately (don't apply array_unique) (thanks fabrizio!)
+ foreach (array('find_string', 'replace_string') as $var) {
+ // append array elements for this config variable from $newconfig to this config
+ //$this->$var = $this->$var + $newconfig->$var;
+ $this->$var = array_merge($this->$var, $newconfig->$var);
+ }
+ }
+
+ // returns SiteConfig instance if an appropriate one is found, false otherwise
+ // if $exact_host_match is true, we will not look for wildcard config matches
+ // by default if host is 'test.example.org' we will look for and load '.example.org.txt' if it exists
+ public static function build($host, $exact_host_match=false) {
+ $host = strtolower($host);
+ if (substr($host, 0, 4) == 'www.') $host = substr($host, 4);
+ if (!$host || (strlen($host) > 200) || !preg_match(self::HOSTNAME_REGEX, ltrim($host, '.'))) return false;
+ // check for site configuration
+ $try = array($host);
+ // should we look for wildcard matches
+ if (!$exact_host_match) {
+ $split = explode('.', $host);
+ if (count($split) > 1) {
+ array_shift($split);
+ $try[] = '.'.implode('.', $split);
+ }
+ }
+
+ // look for site config file in primary folder
+ self::debug(". looking for site config for $host in primary folder");
+ foreach ($try as $h) {
+ if (array_key_exists($h, self::$config_cache)) {
+ self::debug("... site config for $h already loaded in this request");
+ return self::$config_cache[$h];
+ } elseif (self::$apc && ($sconfig = apc_fetch("sc.$h"))) {
+ self::debug("... site config for $h in APC cache");
+ return $sconfig;
+ } elseif (file_exists(self::$config_path."/$h.txt")) {
+ self::debug("... found site config ($h.txt)");
+ $file_primary = self::$config_path."/$h.txt";
+ $matched_name = $h;
+ break;
+ }
+ }
+
+ // if we found site config, process it
+ if (isset($file_primary)) {
+ $config_lines = file($file_primary, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ if (!$config_lines || !is_array($config_lines)) return false;
+ $config = self::build_from_array($config_lines);
+ // if APC caching is available and enabled, mark this for cache
+ //$config->cache_in_apc = true;
+ $config->cache_key = $matched_name;
+
+ // if autodetec on failure is off (on by default) we do not need to look
+ // in secondary folder
+ if (!$config->autodetect_on_failure()) {
+ self::debug('... autodetect on failure is disabled (no other site config files will be loaded)');
+ return $config;
+ }
+ }
+
+ // look for site config file in secondary folder
+ if (isset(self::$config_path_fallback)) {
+ self::debug(". looking for site config for $host in secondary folder");
+ foreach ($try as $h) {
+ if (file_exists(self::$config_path_fallback."/$h.txt")) {
+ self::debug("... found site config in secondary folder ($h.txt)");
+ $file_secondary = self::$config_path_fallback."/$h.txt";
+ $matched_name = $h;
+ break;
+ }
+ }
+ if (!isset($file_secondary)) {
+ self::debug("... no site config match in secondary folder");
+ }
+ }
+
+ // return false if no config file found
+ if (!isset($file_primary) && !isset($file_secondary)) {
+ self::debug("... no site config match for $host");
+ return false;
+ }
+
+ // return primary config if secondary not found
+ if (!isset($file_secondary) && isset($config)) {
+ return $config;
+ }
+
+ // process secondary config file
+ $config_lines = file($file_secondary, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ if (!$config_lines || !is_array($config_lines)) {
+ // failed to process secondary
+ if (isset($config)) {
+ // return primary config
+ return $config;
+ } else {
+ return false;
+ }
+ }
+
+ // merge with primary and return
+ if (isset($config)) {
+ self::debug('. merging config files');
+ $config->append(self::build_from_array($config_lines));
+ return $config;
+ } else {
+ // return just secondary
+ $config = self::build_from_array($config_lines);
+ // if APC caching is available and enabled, mark this for cache
+ //$config->cache_in_apc = true;
+ $config->cache_key = $matched_name;
+ return $config;
+ }
+ }
+
+ public static function build_from_array(array $lines) {
+ $config = new SiteConfig();
+ foreach ($lines as $line) {
+ $line = trim($line);
+
+ // skip comments, empty lines
+ if ($line == '' || $line[0] == '#') continue;
+
+ // get command
+ $command = explode(':', $line, 2);
+ // if there's no colon ':', skip this line
+ if (count($command) != 2) continue;
+ $val = trim($command[1]);
+ $command = trim($command[0]);
+ if ($command == '' || $val == '') continue;
+
+ // check for commands where we accept multiple statements
+ if (in_array($command, array('title', 'body', 'author', 'date', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'single_page_link_in_feed', 'next_page_link', 'http_header', 'test_url', 'find_string', 'replace_string'))) {
+ array_push($config->$command, $val);
+ // check for single statement commands that evaluate to true or false
+ } elseif (in_array($command, array('tidy', 'prune', 'autodetect_on_failure'))) {
+ $config->$command = ($val == 'yes');
+ // check for single statement commands stored as strings
+ } elseif (in_array($command, array('parser'))) {
+ $config->$command = $val;
+ // check for replace_string(find): replace
+ } elseif ((substr($command, -1) == ')') && preg_match('!^([a-z0-9_]+)\((.*?)\)$!i', $command, $match)) {
+ if (in_array($match[1], array('replace_string'))) {
+ $command = $match[1];
+ array_push($config->find_string, $match[2]);
+ array_push($config->$command, $val);
+ }
+ }
+ }
+ return $config;
+ }
+}
\ No newline at end of file
diff --git a/inc/3rdparty/libraries/feedwriter/FeedItem.php b/inc/3rdparty/libraries/feedwriter/FeedItem.php
old mode 100644
new mode 100755
index 54a56f2..4078659
--- a/inc/3rdparty/libraries/feedwriter/FeedItem.php
+++ b/inc/3rdparty/libraries/feedwriter/FeedItem.php
@@ -1,7 +1,7 @@
version = $version;
}
/**
* Set element (overwrites existing elements with $elementName)
- *
+ *
* @access public
* @param srting The tag name of an element
* @param srting The content of tag
@@ -38,11 +38,11 @@
unset($this->elements[$elementName]);
}
$this->addElement($elementName, $content, $attributes);
- }
-
+ }
+
/**
* Add an element to elements array
- *
+ *
* @access public
* @param srting The tag name of an element
* @param srting The content of tag
@@ -61,11 +61,11 @@
$this->elements[$elementName][$i]['content'] = $content;
$this->elements[$elementName][$i]['attributes'] = $attributes;
}
-
+
/**
- * Set multiple feed elements from an array.
+ * Set multiple feed elements from an array.
* Elements which have attributes cannot be added by this method
- *
+ *
* @access public
* @param array array of elements in 'tagName' => 'tagContent' format.
* @return void
@@ -73,15 +73,15 @@
public function addElementArray($elementArray)
{
if(! is_array($elementArray)) return;
- foreach ($elementArray as $elementName => $content)
+ foreach ($elementArray as $elementName => $content)
{
$this->addElement($elementName, $content);
}
}
-
+
/**
* Return the collection of elements in this feed item
- *
+ *
* @access public
* @return array
*/
@@ -89,68 +89,74 @@
{
return $this->elements;
}
-
+
// Wrapper functions ------------------------------------------------------
-
+
/**
* Set the 'dscription' element of feed item
- *
+ *
* @access public
* @param string The content of 'description' element
* @return void
*/
- public function setDescription($description)
+ public function setDescription($description)
{
- $this->setElement('description', $description);
+ $tag = ($this->version == ATOM)? 'summary' : 'description';
+ $this->setElement($tag, $description);
}
-
+
/**
* @desc Set the 'title' element of feed item
* @access public
* @param string The content of 'title' element
* @return void
*/
- public function setTitle($title)
+ public function setTitle($title)
{
- $this->setElement('title', $title);
+ $this->setElement('title', $title);
}
-
+
/**
* Set the 'date' element of feed item
- *
+ *
* @access public
* @param string The content of 'date' element
* @return void
*/
- public function setDate($date)
+ public function setDate($date)
{
if(! is_numeric($date))
{
$date = strtotime($date);
}
-
- if($this->version == RSS2)
+
+ if($this->version == ATOM)
{
- $tag = 'pubDate';
- $value = date(DATE_RSS, $date);
+ $tag = 'updated';
+ $value = date(DATE_ATOM, $date);
}
- else
+ elseif($this->version == RSS2)
{
- $tag = 'dc:date';
- $value = date("Y-m-d", $date);
+ $tag = 'pubDate';
+ $value = date(DATE_RSS, $date);
}
-
- $this->setElement($tag, $value);
+ else
+ {
+ $tag = 'dc:date';
+ $value = date("Y-m-d", $date);
+ }
+
+ $this->setElement($tag, $value);
}
-
+
/**
* Set the 'link' element of feed item
- *
+ *
* @access public
* @param string The content of 'link' element
* @return void
*/
- public function setLink($link)
+ public function setLink($link)
{
if($this->version == RSS2 || $this->version == RSS1)
{
@@ -161,27 +167,27 @@
{
$this->setElement('link','',array('href'=>$link));
$this->setElement('id', FeedWriter::uuid($link,'urn:uuid:'));
- }
-
+ }
+
}
/**
* Set the 'source' element of feed item
- *
+ *
* @access public
* @param string The content of 'source' element
* @return void
*/
- public function setSource($link)
+ public function setSource($link)
{
$attributes = array('url'=>$link);
$this->setElement('source', "wallabag",$attributes);
}
-
+
/**
* Set the 'encloser' element of feed item
* For RSS 2.0 only
- *
+ *
* @access public
* @param string The url attribute of encloser tag
* @param string The length attribute of encloser tag
@@ -193,6 +199,6 @@
$attributes = array('url'=>$url, 'length'=>$length, 'type'=>$type);
$this->setElement('enclosure','',$attributes);
}
-
+
} // end of class FeedItem
?>
\ No newline at end of file
diff --git a/inc/3rdparty/libraries/feedwriter/FeedWriter.php b/inc/3rdparty/libraries/feedwriter/FeedWriter.php
index d708e99..7775569 100755
--- a/inc/3rdparty/libraries/feedwriter/FeedWriter.php
+++ b/inc/3rdparty/libraries/feedwriter/FeedWriter.php
@@ -97,15 +97,12 @@ define('JSONP', 3, true);
header('X-content-type-options: nosniff');
} elseif ($this->version == JSON) {
header('Content-type: application/json; charset=UTF-8');
+ $this->json = new stdClass();
} elseif ($this->version == JSONP) {
header('Content-type: application/javascript; charset=UTF-8');
+ $this->json = new stdClass();
}
}
-
- if ($this->version == JSON || $this->version == JSONP) {
- $this->json = new stdClass();
- }
-
$this->printHead();
$this->printChannels();
@@ -116,6 +113,11 @@ define('JSONP', 3, true);
}
}
+ public function &getItems()
+ {
+ return $this->items;
+ }
+
/**
* Create a new FeedItem.
*
@@ -199,7 +201,8 @@ define('JSONP', 3, true);
*/
public function setDescription($description)
{
- $this->setChannelElement('description', $description);
+ $tag = ($this->version == ATOM)? 'subtitle' : 'description';
+ $this->setChannelElement($tag, $desciption);
}
/**
@@ -244,7 +247,7 @@ define('JSONP', 3, true);
{
$out = ''."\n";
if ($this->xsl) $out .= 'xsl).'"?>' . PHP_EOL;
- $out .= '
- * Cookies are stored like this:
- * [domain][path][name] = array
- * where array is:
- * 0 => value, 1 => secure, 2 => expires
- *
- * @var array
- * @access private
- */
- public $cookies = array();
- public $debug = false;
-
- /**
- * Constructor
- */
- function __construct() {
- }
-
- protected function debug($msg, $file=null, $line=null) {
- if ($this->debug) {
- $mem = round(memory_get_usage()/1024, 2);
- $memPeak = round(memory_get_peak_usage()/1024, 2);
- echo '* ',$msg;
- if (isset($file, $line)) echo " ($file line $line)";
- echo ' - mem used: ',$mem," (peak: $memPeak)\n";
- ob_flush();
- flush();
- }
- }
-
- /**
- * Get matching cookies
- *
- * Only use this method if you cannot use add_cookie_header(), for example, if you want to use
- * this cookie jar class without using the request class.
- *
- * @param array $param associative array containing 'domain', 'path', 'secure' keys
- * @return string
- * @see add_cookie_header()
- */
- public function getMatchingCookies($url)
- {
- if (($parts = @parse_url($url)) && isset($parts['scheme'], $parts['host'], $parts['path'])) {
- $param['domain'] = $parts['host'];
- $param['path'] = $parts['path'];
- $param['secure'] = (strtolower($parts['scheme']) == 'https');
- unset($parts);
- } else {
- return false;
- }
- // RFC 2965 notes:
- // If multiple cookies satisfy the criteria above, they are ordered in
- // the Cookie header such that those with more specific Path attributes
- // precede those with less specific. Ordering with respect to other
- // attributes (e.g., Domain) is unspecified.
- $domain = $param['domain'];
- if (strpos($domain, '.') === false) $domain .= '.local';
- $request_path = $param['path'];
- if ($request_path == '') $request_path = '/';
- $request_secure = $param['secure'];
- $now = time();
- $matched_cookies = array();
- // domain - find matching domains
- $this->debug('Finding matching domains for '.$domain, __FILE__, __LINE__);
- while (strpos($domain, '.') !== false) {
- if (isset($this->cookies[$domain])) {
- $this->debug(' domain match found: '.$domain);
- $cookies =& $this->cookies[$domain];
- } else {
- $domain = $this->_reduce_domain($domain);
- continue;
- }
- // paths - find matching paths starting from most specific
- $this->debug(' - Finding matching paths for '.$request_path);
- $paths = array_keys($cookies);
- usort($paths, array($this, '_cmp_length'));
- foreach ($paths as $path) {
- // continue to next cookie if request path does not path-match cookie path
- if (!$this->_path_match($request_path, $path)) continue;
- // loop through cookie names
- $this->debug(' path match found: '.$path);
- foreach ($cookies[$path] as $name => $values) {
- // if this cookie is secure but request isn't, continue to next cookie
- if ($values[1] && !$request_secure) continue;
- // if cookie is not a session cookie and has expired, continue to next cookie
- if (is_int($values[2]) && ($values[2] < $now)) continue;
- // cookie matches request
- $this->debug(' cookie match: '.$name.'='.$values[0]);
- $matched_cookies[] = $name.'='.$values[0];
- }
- }
- $domain = $this->_reduce_domain($domain);
- }
- // return cookies
- return implode('; ', $matched_cookies);
- }
-
- /**
- * Parse Set-Cookie values.
- *
- * Only use this method if you cannot use extract_cookies(), for example, if you want to use
- * this cookie jar class without using the response class.
- *
- * @param array $set_cookies array holding 1 or more "Set-Cookie" header values
- * @param array $param associative array containing 'host', 'path' keys
- * @return void
- * @see extract_cookies()
- */
- public function storeCookies($url, $set_cookies)
- {
- if (count($set_cookies) == 0) return;
- $param = @parse_url($url);
- if (!is_array($param) || !isset($param['host'])) return;
- $request_host = $param['host'];
- if (strpos($request_host, '.') === false) $request_host .= '.local';
- $request_path = @$param['path'];
- if ($request_path == '') $request_path = '/';
- //
- // loop through set-cookie headers
- //
- foreach ($set_cookies as $set_cookie) {
- $this->debug('Parsing: '.$set_cookie);
- // temporary cookie store (before adding to jar)
- $tmp_cookie = array();
- $param = explode(';', $set_cookie);
- // loop through params
- for ($x=0; $x
+ * Cookies are stored like this:
+ * [domain][path][name] = array
+ * where array is:
+ * 0 => value, 1 => secure, 2 => expires
+ *
+ * @var array
+ * @access private
+ */
+ public $cookies = array();
+ public $debug = false;
+
+ /**
+ * Constructor
+ */
+ function __construct() {
+ }
+
+ protected function debug($msg, $file=null, $line=null) {
+ if ($this->debug) {
+ $mem = round(memory_get_usage()/1024, 2);
+ $memPeak = round(memory_get_peak_usage()/1024, 2);
+ echo '* ',$msg;
+ if (isset($file, $line)) echo " ($file line $line)";
+ echo ' - mem used: ',$mem," (peak: $memPeak)\n";
+ ob_flush();
+ flush();
+ }
+ }
+
+ /**
+ * Get matching cookies
+ *
+ * Only use this method if you cannot use add_cookie_header(), for example, if you want to use
+ * this cookie jar class without using the request class.
+ *
+ * @param array $param associative array containing 'domain', 'path', 'secure' keys
+ * @return string
+ * @see add_cookie_header()
+ */
+ public function getMatchingCookies($url)
+ {
+ if (($parts = @parse_url($url)) && isset($parts['scheme'], $parts['host'], $parts['path'])) {
+ $param['domain'] = $parts['host'];
+ $param['path'] = $parts['path'];
+ $param['secure'] = (strtolower($parts['scheme']) == 'https');
+ unset($parts);
+ } else {
+ return false;
+ }
+ // RFC 2965 notes:
+ // If multiple cookies satisfy the criteria above, they are ordered in
+ // the Cookie header such that those with more specific Path attributes
+ // precede those with less specific. Ordering with respect to other
+ // attributes (e.g., Domain) is unspecified.
+ $domain = $param['domain'];
+ if (strpos($domain, '.') === false) $domain .= '.local';
+ $request_path = $param['path'];
+ if ($request_path == '') $request_path = '/';
+ $request_secure = $param['secure'];
+ $now = time();
+ $matched_cookies = array();
+ // domain - find matching domains
+ $this->debug('Finding matching domains for '.$domain, __FILE__, __LINE__);
+ while (strpos($domain, '.') !== false) {
+ if (isset($this->cookies[$domain])) {
+ $this->debug(' domain match found: '.$domain);
+ $cookies =& $this->cookies[$domain];
+ } else {
+ $domain = $this->_reduce_domain($domain);
+ continue;
+ }
+ // paths - find matching paths starting from most specific
+ $this->debug(' - Finding matching paths for '.$request_path);
+ $paths = array_keys($cookies);
+ usort($paths, array($this, '_cmp_length'));
+ foreach ($paths as $path) {
+ // continue to next cookie if request path does not path-match cookie path
+ if (!$this->_path_match($request_path, $path)) continue;
+ // loop through cookie names
+ $this->debug(' path match found: '.$path);
+ foreach ($cookies[$path] as $name => $values) {
+ // if this cookie is secure but request isn't, continue to next cookie
+ if ($values[1] && !$request_secure) continue;
+ // if cookie is not a session cookie and has expired, continue to next cookie
+ if (is_int($values[2]) && ($values[2] < $now)) continue;
+ // cookie matches request
+ $this->debug(' cookie match: '.$name.'='.$values[0]);
+ $matched_cookies[] = $name.'='.$values[0];
+ }
+ }
+ $domain = $this->_reduce_domain($domain);
+ }
+ // return cookies
+ return implode('; ', $matched_cookies);
+ }
+
+ /**
+ * Parse Set-Cookie values.
+ *
+ * Only use this method if you cannot use extract_cookies(), for example, if you want to use
+ * this cookie jar class without using the response class.
+ *
+ * @param array $set_cookies array holding 1 or more "Set-Cookie" header values
+ * @param array $param associative array containing 'host', 'path' keys
+ * @return void
+ * @see extract_cookies()
+ */
+ public function storeCookies($url, $set_cookies)
+ {
+ if (count($set_cookies) == 0) return;
+ $param = @parse_url($url);
+ if (!is_array($param) || !isset($param['host'])) return;
+ $request_host = $param['host'];
+ if (strpos($request_host, '.') === false) $request_host .= '.local';
+ $request_path = @$param['path'];
+ if ($request_path == '') $request_path = '/';
+ //
+ // loop through set-cookie headers
+ //
+ foreach ($set_cookies as $set_cookie) {
+ $this->debug('Parsing: '.$set_cookie);
+ // temporary cookie store (before adding to jar)
+ $tmp_cookie = array();
+ $param = explode(';', $set_cookie);
+ // loop through params
+ for ($x=0; $x
]*>[ \n\r\t]*){2,}/i',
- 'replaceFonts' => '/<(\/?)font[^>]*>/i',
- // 'trimRe' => '/^\s+|\s+$/g', // PHP has trim()
- 'normalize' => '/\s{2,}/',
- 'killBreaks' => '/(
(\s| ?)*){1,}/',
- 'video' => '!//(player\.|www\.)?(youtube|vimeo|viddler)\.com!i',
- 'skipFootnoteLink' => '/^\s*(\[?[a-z0-9]{1,2}\]?|^|edit|citation needed)\s*$/i'
- );
-
- /* constants */
- const FLAG_STRIP_UNLIKELYS = 1;
- const FLAG_WEIGHT_CLASSES = 2;
- const FLAG_CLEAN_CONDITIONALLY = 4;
-
- /**
- * Create instance of Readability
- * @param string UTF-8 encoded string
- * @param string (optional) URL associated with HTML (used for footnotes)
- * @param string which parser to use for turning raw HTML into a DOMDocument (either 'libxml' or 'html5lib')
- */
- function __construct($html, $url=null, $parser='libxml')
- {
- $this->url = $url;
- /* Turn all double br's into p's */
- $html = preg_replace($this->regexps['replaceBrs'], '
', $html); - $html = preg_replace($this->regexps['replaceFonts'], '<$1span>', $html); - $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"); - if (trim($html) == '') $html = ''; - if ($parser=='html5lib' && ($this->dom = HTML5_Parser::parse($html))) { - // all good - } else { - $this->dom = new DOMDocument(); - $this->dom->preserveWhiteSpace = false; - @$this->dom->loadHTML($html); - } - $this->dom->registerNodeClass('DOMElement', 'JSLikeHTMLElement'); - } - - /** - * Get article title element - * @return DOMElement - */ - public function getTitle() { - return $this->articleTitle; - } - - /** - * Get article content element - * @return DOMElement - */ - public function getContent() { - return $this->articleContent; - } - - /** - * Runs readability. - * - * Workflow: - * 1. Prep the document by removing script tags, css, etc. - * 2. Build readability's DOM tree. - * 3. Grab the article content from the current dom tree. - * 4. Replace the current DOM tree with the new one. - * 5. Read peacefully. - * - * @return boolean true if we found content, false otherwise - **/ - public function init() - { - if (!isset($this->dom->documentElement)) return false; - $this->removeScripts($this->dom); - //die($this->getInnerHTML($this->dom->documentElement)); - - // Assume successful outcome - $this->success = true; - - $bodyElems = $this->dom->getElementsByTagName('body'); - if ($bodyElems->length > 0) { - if ($this->bodyCache == null) { - $this->bodyCache = $bodyElems->item(0)->innerHTML; - } - if ($this->body == null) { - $this->body = $bodyElems->item(0); - } - } - - $this->prepDocument(); - - //die($this->dom->documentElement->parentNode->nodeType); - //$this->setInnerHTML($this->dom->documentElement, $this->getInnerHTML($this->dom->documentElement)); - //die($this->getInnerHTML($this->dom->documentElement)); - - /* Build readability's DOM tree */ - $overlay = $this->dom->createElement('div'); - $innerDiv = $this->dom->createElement('div'); - $articleTitle = $this->getArticleTitle(); - $articleContent = $this->grabArticle(); - - if (!$articleContent) { - $this->success = false; - $articleContent = $this->dom->createElement('div'); - $articleContent->setAttribute('id', 'readability-content'); - $articleContent->innerHTML = '
Sorry, Readability was unable to parse this page for content.
'; - } - - $overlay->setAttribute('id', 'readOverlay'); - $innerDiv->setAttribute('id', 'readInner'); - - /* Glue the structure of our document together. */ - $innerDiv->appendChild($articleTitle); - $innerDiv->appendChild($articleContent); - $overlay->appendChild($innerDiv); - - /* Clear the old HTML, insert the new content. */ - $this->body->innerHTML = ''; - $this->body->appendChild($overlay); - //document.body.insertBefore(overlay, document.body.firstChild); - $this->body->removeAttribute('style'); - - $this->postProcessContent($articleContent); - - // Set title and content instance variables - $this->articleTitle = $articleTitle; - $this->articleContent = $articleContent; - - return $this->success; - } - - /** - * Debug - */ - protected function dbg($msg) { - if ($this->debug) echo '* ',$msg, "\n"; - } - - /** - * Run any post-process modifications to article content as necessary. - * - * @param DOMElement - * @return void - */ - public function postProcessContent($articleContent) { - if ($this->convertLinksToFootnotes && !preg_match('/wikipedia\.org/', @$this->url)) { - $this->addFootnotes($articleContent); - } - } - - /** - * Get the article title as an H1. - * - * @return DOMElement - */ - protected function getArticleTitle() { - $curTitle = ''; - $origTitle = ''; - - try { - $curTitle = $origTitle = $this->getInnerText($this->dom->getElementsByTagName('title')->item(0)); - } catch(Exception $e) {} - - if (preg_match('/ [\|\-] /', $curTitle)) - { - $curTitle = preg_replace('/(.*)[\|\-] .*/i', '$1', $origTitle); - - if (count(explode(' ', $curTitle)) < 3) { - $curTitle = preg_replace('/[^\|\-]*[\|\-](.*)/i', '$1', $origTitle); - } - } - else if (strpos($curTitle, ': ') !== false) - { - $curTitle = preg_replace('/.*:(.*)/i', '$1', $origTitle); - - if (count(explode(' ', $curTitle)) < 3) { - $curTitle = preg_replace('/[^:]*[:](.*)/i','$1', $origTitle); - } - } - else if(strlen($curTitle) > 150 || strlen($curTitle) < 15) - { - $hOnes = $this->dom->getElementsByTagName('h1'); - if($hOnes->length == 1) - { - $curTitle = $this->getInnerText($hOnes->item(0)); - } - } - - $curTitle = trim($curTitle); - - if (count(explode(' ', $curTitle)) <= 4) { - $curTitle = $origTitle; - } - - $articleTitle = $this->dom->createElement('h1'); - $articleTitle->innerHTML = $curTitle; - - return $articleTitle; - } - - /** - * Prepare the HTML document for readability to scrape it. - * This includes things like stripping javascript, CSS, and handling terrible markup. - * - * @return void - **/ - protected function prepDocument() { - /** - * In some cases a body element can't be found (if the HTML is totally hosed for example) - * so we create a new body node and append it to the document. - */ - if ($this->body == null) - { - $this->body = $this->dom->createElement('body'); - $this->dom->documentElement->appendChild($this->body); - } - $this->body->setAttribute('id', 'readabilityBody'); - - /* Remove all style tags in head */ - $styleTags = $this->dom->getElementsByTagName('style'); - for ($i = $styleTags->length-1; $i >= 0; $i--) - { - $styleTags->item($i)->parentNode->removeChild($styleTags->item($i)); - } - - /* Turn all double br's into p's */ - /* Note, this is pretty costly as far as processing goes. Maybe optimize later. */ - //document.body.innerHTML = document.body.innerHTML.replace(readability.regexps.replaceBrs, '').replace(readability.regexps.replaceFonts, '<$1span>'); - // We do this in the constructor for PHP as that's when we have raw HTML - before parsing it into a DOM tree. - // Manipulating innerHTML as it's done in JS is not possible in PHP. - } - - /** - * For easier reading, convert this document to have footnotes at the bottom rather than inline links. - * @see http://www.roughtype.com/archives/2010/05/experiments_in.php - * - * @return void - **/ - public function addFootnotes($articleContent) { - $footnotesWrapper = $this->dom->createElement('div'); - $footnotesWrapper->setAttribute('id', 'readability-footnotes'); - $footnotesWrapper->innerHTML = '
tags, etc.
- *
- * @param DOMElement
- * @return void
- */
- function prepArticle($articleContent) {
- $this->cleanStyles($articleContent);
- $this->killBreaks($articleContent);
- if ($this->revertForcedParagraphElements) {
- $this->revertReadabilityStyledElements($articleContent);
- }
-
- /* Clean out junk from the article content */
- $this->cleanConditionally($articleContent, 'form');
- $this->clean($articleContent, 'object');
- $this->clean($articleContent, 'h1');
-
- /**
- * If there is only one h2, they are probably using it
- * as a header and not a subheader, so remove it since we already have a header.
- ***/
- if (!$this->lightClean && ($articleContent->getElementsByTagName('h2')->length == 1)) {
- $this->clean($articleContent, 'h2');
- }
- $this->clean($articleContent, 'iframe');
-
- $this->cleanHeaders($articleContent);
-
- /* Do these last as the previous stuff may have removed junk that will affect these */
- $this->cleanConditionally($articleContent, 'table');
- $this->cleanConditionally($articleContent, 'ul');
- $this->cleanConditionally($articleContent, 'div');
-
- /* Remove extra paragraphs */
- $articleParagraphs = $articleContent->getElementsByTagName('p');
- for ($i = $articleParagraphs->length-1; $i >= 0; $i--)
- {
- $imgCount = $articleParagraphs->item($i)->getElementsByTagName('img')->length;
- $embedCount = $articleParagraphs->item($i)->getElementsByTagName('embed')->length;
- $objectCount = $articleParagraphs->item($i)->getElementsByTagName('object')->length;
- $iframeCount = $articleParagraphs->item($i)->getElementsByTagName('iframe')->length;
-
- if ($imgCount === 0 && $embedCount === 0 && $objectCount === 0 && $iframeCount === 0 && $this->getInnerText($articleParagraphs->item($i), false) == '')
- {
- $articleParagraphs->item($i)->parentNode->removeChild($articleParagraphs->item($i));
- }
- }
-
- try {
- $articleContent->innerHTML = preg_replace('/
]*>\s*
innerHTML);
- //articleContent.innerHTML = articleContent.innerHTML.replace(/
]*>\s*
dbg("Cleaning innerHTML of breaks failed. This is an IE strict-block-elements bug. Ignoring.: " . $e);
- }
- }
-
- /**
- * Initialize a node with the readability object. Also checks the
- * className/id for special names to add to its score.
- *
- * @param Element
- * @return void
- **/
- protected function initializeNode($node) {
- $readability = $this->dom->createAttribute('readability');
- $readability->value = 0; // this is our contentScore
- $node->setAttributeNode($readability);
-
- switch (strtoupper($node->tagName)) { // unsure if strtoupper is needed, but using it just in case
- case 'DIV':
- $readability->value += 5;
- break;
-
- case 'PRE':
- case 'TD':
- case 'BLOCKQUOTE':
- $readability->value += 3;
- break;
-
- case 'ADDRESS':
- case 'OL':
- case 'UL':
- case 'DL':
- case 'DD':
- case 'DT':
- case 'LI':
- case 'FORM':
- $readability->value -= 3;
- break;
-
- case 'H1':
- case 'H2':
- case 'H3':
- case 'H4':
- case 'H5':
- case 'H6':
- case 'TH':
- $readability->value -= 5;
- break;
- }
- $readability->value += $this->getClassWeight($node);
- }
-
- /***
- * grabArticle - Using a variety of metrics (content score, classname, element types), find the content that is
- * most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
- *
- * @return DOMElement
- **/
- protected function grabArticle($page=null) {
- $stripUnlikelyCandidates = $this->flagIsActive(self::FLAG_STRIP_UNLIKELYS);
- if (!$page) $page = $this->dom;
- $allElements = $page->getElementsByTagName('*');
- /**
- * First, node prepping. Trash nodes that look cruddy (like ones with the class name "comment", etc), and turn divs
- * into P tags where they have been used inappropriately (as in, where they contain no other block level elements.)
- *
- * Note: Assignment from index for performance. See http://www.peachpit.com/articles/article.aspx?p=31567&seqNum=5
- * TODO: Shouldn't this be a reverse traversal?
- **/
- $node = null;
- $nodesToScore = array();
- for ($nodeIndex = 0; ($node = $allElements->item($nodeIndex)); $nodeIndex++) {
- //for ($nodeIndex=$targetList->length-1; $nodeIndex >= 0; $nodeIndex--) {
- //$node = $targetList->item($nodeIndex);
- $tagName = strtoupper($node->tagName);
- /* Remove unlikely candidates */
- if ($stripUnlikelyCandidates) {
- $unlikelyMatchString = $node->getAttribute('class') . $node->getAttribute('id');
- if (
- preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString) &&
- !preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString) &&
- $tagName != 'BODY'
- )
- {
- $this->dbg('Removing unlikely candidate - ' . $unlikelyMatchString);
- //$nodesToRemove[] = $node;
- $node->parentNode->removeChild($node);
- $nodeIndex--;
- continue;
- }
- }
-
- if ($tagName == 'P' || $tagName == 'TD' || $tagName == 'PRE') {
- $nodesToScore[] = $node;
- }
-
- /* Turn all divs that don't have children block level elements into p's */
- if ($tagName == 'DIV') {
- if (!preg_match($this->regexps['divToPElements'], $node->innerHTML)) {
- //$this->dbg('Altering div to p');
- $newNode = $this->dom->createElement('p');
- try {
- $newNode->innerHTML = $node->innerHTML;
- //$nodesToReplace[] = array('new'=>$newNode, 'old'=>$node);
- $node->parentNode->replaceChild($newNode, $node);
- $nodeIndex--;
- $nodesToScore[] = $node; // or $newNode?
- }
- catch(Exception $e) {
- $this->dbg('Could not alter div to p, reverting back to div.: ' . $e);
- }
- }
- else
- {
- /* EXPERIMENTAL */
- // TODO: change these p elements back to text nodes after processing
- for ($i = 0, $il = $node->childNodes->length; $i < $il; $i++) {
- $childNode = $node->childNodes->item($i);
- if ($childNode->nodeType == 3) { // XML_TEXT_NODE
- //$this->dbg('replacing text node with a p tag with the same content.');
- $p = $this->dom->createElement('p');
- $p->innerHTML = $childNode->nodeValue;
- $p->setAttribute('style', 'display: inline;');
- $p->setAttribute('class', 'readability-styled');
- $childNode->parentNode->replaceChild($p, $childNode);
- }
- }
- }
- }
- }
-
- /**
- * Loop through all paragraphs, and assign a score to them based on how content-y they look.
- * Then add their score to their parent node.
- *
- * A score is determined by things like number of commas, class names, etc. Maybe eventually link density.
- **/
- $candidates = array();
- for ($pt=0; $pt < count($nodesToScore); $pt++) {
- $parentNode = $nodesToScore[$pt]->parentNode;
- // $grandParentNode = $parentNode ? $parentNode->parentNode : null;
- $grandParentNode = !$parentNode ? null : (($parentNode->parentNode instanceof DOMElement) ? $parentNode->parentNode : null);
- $innerText = $this->getInnerText($nodesToScore[$pt]);
-
- if (!$parentNode || !isset($parentNode->tagName)) {
- continue;
- }
-
- /* If this paragraph is less than 25 characters, don't even count it. */
- if(strlen($innerText) < 25) {
- continue;
- }
-
- /* Initialize readability data for the parent. */
- if (!$parentNode->hasAttribute('readability'))
- {
- $this->initializeNode($parentNode);
- $candidates[] = $parentNode;
- }
-
- /* Initialize readability data for the grandparent. */
- if ($grandParentNode && !$grandParentNode->hasAttribute('readability') && isset($grandParentNode->tagName))
- {
- $this->initializeNode($grandParentNode);
- $candidates[] = $grandParentNode;
- }
-
- $contentScore = 0;
-
- /* Add a point for the paragraph itself as a base. */
- $contentScore++;
-
- /* Add points for any commas within this paragraph */
- $contentScore += count(explode(',', $innerText));
-
- /* For every 100 characters in this paragraph, add another point. Up to 3 points. */
- $contentScore += min(floor(strlen($innerText) / 100), 3);
-
- /* Add the score to the parent. The grandparent gets half. */
- $parentNode->getAttributeNode('readability')->value += $contentScore;
-
- if ($grandParentNode) {
- $grandParentNode->getAttributeNode('readability')->value += $contentScore/2;
- }
- }
-
- /**
- * After we've calculated scores, loop through all of the possible candidate nodes we found
- * and find the one with the highest score.
- **/
- $topCandidate = null;
- for ($c=0, $cl=count($candidates); $c < $cl; $c++)
- {
- /**
- * Scale the final candidates score based on link density. Good content should have a
- * relatively small link density (5% or less) and be mostly unaffected by this operation.
- **/
- $readability = $candidates[$c]->getAttributeNode('readability');
- $readability->value = $readability->value * (1-$this->getLinkDensity($candidates[$c]));
-
- $this->dbg('Candidate: ' . $candidates[$c]->tagName . ' (' . $candidates[$c]->getAttribute('class') . ':' . $candidates[$c]->getAttribute('id') . ') with score ' . $readability->value);
-
- if (!$topCandidate || $readability->value > (int)$topCandidate->getAttribute('readability')) {
- $topCandidate = $candidates[$c];
- }
- }
-
- /**
- * If we still have no top candidate, just use the body as a last resort.
- * We also have to copy the body node so it is something we can modify.
- **/
- if ($topCandidate === null || strtoupper($topCandidate->tagName) == 'BODY')
- {
- $topCandidate = $this->dom->createElement('div');
- if ($page instanceof DOMDocument) {
- if (!isset($page->documentElement)) {
- // we don't have a body either? what a mess! :)
- } else {
- $topCandidate->innerHTML = $page->documentElement->innerHTML;
- $page->documentElement->innerHTML = '';
- $page->documentElement->appendChild($topCandidate);
- }
- } else {
- $topCandidate->innerHTML = $page->innerHTML;
- $page->innerHTML = '';
- $page->appendChild($topCandidate);
- }
- $this->initializeNode($topCandidate);
- }
-
- /**
- * Now that we have the top candidate, look through its siblings for content that might also be related.
- * Things like preambles, content split by ads that we removed, etc.
- **/
- $articleContent = $this->dom->createElement('div');
- $articleContent->setAttribute('id', 'readability-content');
- $siblingScoreThreshold = max(10, ((int)$topCandidate->getAttribute('readability')) * 0.2);
- $siblingNodes = $topCandidate->parentNode->childNodes;
- if (!isset($siblingNodes)) {
- $siblingNodes = new stdClass;
- $siblingNodes->length = 0;
- }
-
- for ($s=0, $sl=$siblingNodes->length; $s < $sl; $s++)
- {
- $siblingNode = $siblingNodes->item($s);
- $append = false;
-
- $this->dbg('Looking at sibling node: ' . $siblingNode->nodeName . (($siblingNode->nodeType === XML_ELEMENT_NODE && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
-
- //dbg('Sibling has score ' . ($siblingNode->readability ? siblingNode.readability.contentScore : 'Unknown'));
-
- if ($siblingNode === $topCandidate)
- // or if ($siblingNode->isSameNode($topCandidate))
- {
- $append = true;
- }
-
- $contentBonus = 0;
- /* Give a bonus if sibling nodes and top candidates have the example same classname */
- if ($siblingNode->nodeType === XML_ELEMENT_NODE && $siblingNode->getAttribute('class') == $topCandidate->getAttribute('class') && $topCandidate->getAttribute('class') != '') {
- $contentBonus += ((int)$topCandidate->getAttribute('readability')) * 0.2;
- }
-
- if ($siblingNode->nodeType === XML_ELEMENT_NODE && $siblingNode->hasAttribute('readability') && (((int)$siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold)
- {
- $append = true;
- }
-
- if (strtoupper($siblingNode->nodeName) == 'P') {
- $linkDensity = $this->getLinkDensity($siblingNode);
- $nodeContent = $this->getInnerText($siblingNode);
- $nodeLength = strlen($nodeContent);
-
- if ($nodeLength > 80 && $linkDensity < 0.25)
- {
- $append = true;
- }
- else if ($nodeLength < 80 && $linkDensity === 0 && preg_match('/\.( |$)/', $nodeContent))
- {
- $append = true;
- }
- }
-
- if ($append)
- {
- $this->dbg('Appending node: ' . $siblingNode->nodeName);
-
- $nodeToAppend = null;
- $sibNodeName = strtoupper($siblingNode->nodeName);
- if ($sibNodeName != 'DIV' && $sibNodeName != 'P') {
- /* We have a node that isn't a common block level element, like a form or td tag. Turn it into a div so it doesn't get filtered out later by accident. */
-
- $this->dbg('Altering siblingNode of ' . $sibNodeName . ' to div.');
- $nodeToAppend = $this->dom->createElement('div');
- try {
- $nodeToAppend->setAttribute('id', $siblingNode->getAttribute('id'));
- $nodeToAppend->innerHTML = $siblingNode->innerHTML;
- }
- catch(Exception $e)
- {
- $this->dbg('Could not alter siblingNode to div, reverting back to original.');
- $nodeToAppend = $siblingNode;
- $s--;
- $sl--;
- }
- } else {
- $nodeToAppend = $siblingNode;
- $s--;
- $sl--;
- }
-
- /* To ensure a node does not interfere with readability styles, remove its classnames */
- $nodeToAppend->removeAttribute('class');
-
- /* Append sibling and subtract from our list because it removes the node when you append to another node */
- $articleContent->appendChild($nodeToAppend);
- }
- }
-
- /**
- * So we have all of the content that we need. Now we clean it up for presentation.
- **/
- $this->prepArticle($articleContent);
-
- /**
- * Now that we've gone through the full algorithm, check to see if we got any meaningful content.
- * If we didn't, we may need to re-run grabArticle with different flags set. This gives us a higher
- * likelihood of finding the content, and the sieve approach gives us a higher likelihood of
- * finding the -right- content.
- **/
- if (strlen($this->getInnerText($articleContent, false)) < 250)
- {
- // TODO: find out why element disappears sometimes, e.g. for this URL http://www.businessinsider.com/6-hedge-fund-etfs-for-average-investors-2011-7
- // in the meantime, we check and create an empty element if it's not there.
- if (!isset($this->body->childNodes)) $this->body = $this->dom->createElement('body');
- $this->body->innerHTML = $this->bodyCache;
-
- if ($this->flagIsActive(self::FLAG_STRIP_UNLIKELYS)) {
- $this->removeFlag(self::FLAG_STRIP_UNLIKELYS);
- return $this->grabArticle($this->body);
- }
- else if ($this->flagIsActive(self::FLAG_WEIGHT_CLASSES)) {
- $this->removeFlag(self::FLAG_WEIGHT_CLASSES);
- return $this->grabArticle($this->body);
- }
- else if ($this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
- $this->removeFlag(self::FLAG_CLEAN_CONDITIONALLY);
- return $this->grabArticle($this->body);
- }
- else {
- return false;
- }
- }
- return $articleContent;
- }
-
- /**
- * Remove script tags from document
- *
- * @param DOMElement
- * @return void
- */
- public function removeScripts($doc) {
- $scripts = $doc->getElementsByTagName('script');
- for($i = $scripts->length-1; $i >= 0; $i--)
- {
- $scripts->item($i)->parentNode->removeChild($scripts->item($i));
- }
- }
-
- /**
- * Get the inner text of a node.
- * This also strips out any excess whitespace to be found.
- *
- * @param DOMElement $
- * @param boolean $normalizeSpaces (default: true)
- * @return string
- **/
- public function getInnerText($e, $normalizeSpaces=true) {
- $textContent = '';
-
- if (!isset($e->textContent) || $e->textContent == '') {
- return '';
- }
-
- $textContent = trim($e->textContent);
-
- if ($normalizeSpaces) {
- return preg_replace($this->regexps['normalize'], ' ', $textContent);
- } else {
- return $textContent;
- }
- }
-
- /**
- * Get the number of times a string $s appears in the node $e.
- *
- * @param DOMElement $e
- * @param string - what to count. Default is ","
- * @return number (integer)
- **/
- public function getCharCount($e, $s=',') {
- return substr_count($this->getInnerText($e), $s);
- }
-
- /**
- * Remove the style attribute on every $e and under.
- *
- * @param DOMElement $e
- * @return void
- */
- public function cleanStyles($e) {
- if (!is_object($e)) return;
- $elems = $e->getElementsByTagName('*');
- foreach ($elems as $elem) {
- $elem->removeAttribute('style');
- }
- }
-
- /**
- * Get the density of links as a percentage of the content
- * This is the amount of text that is inside a link divided by the total text in the node.
- *
- * @param DOMElement $e
- * @return number (float)
- */
- public function getLinkDensity($e) {
- $links = $e->getElementsByTagName('a');
- $textLength = strlen($this->getInnerText($e));
- $linkLength = 0;
- for ($i=0, $il=$links->length; $i < $il; $i++)
- {
- $linkLength += strlen($this->getInnerText($links->item($i)));
- }
- if ($textLength > 0) {
- return $linkLength / $textLength;
- } else {
- return 0;
- }
- }
-
- /**
- * Get an elements class/id weight. Uses regular expressions to tell if this
- * element looks good or bad.
- *
- * @param DOMElement $e
- * @return number (Integer)
- */
- public function getClassWeight($e) {
- if(!$this->flagIsActive(self::FLAG_WEIGHT_CLASSES)) {
- return 0;
- }
-
- $weight = 0;
-
- /* Look for a special classname */
- if ($e->hasAttribute('class') && $e->getAttribute('class') != '')
- {
- if (preg_match($this->regexps['negative'], $e->getAttribute('class'))) {
- $weight -= 25;
- }
- if (preg_match($this->regexps['positive'], $e->getAttribute('class'))) {
- $weight += 25;
- }
- }
-
- /* Look for a special ID */
- if ($e->hasAttribute('id') && $e->getAttribute('id') != '')
- {
- if (preg_match($this->regexps['negative'], $e->getAttribute('id'))) {
- $weight -= 25;
- }
- if (preg_match($this->regexps['positive'], $e->getAttribute('id'))) {
- $weight += 25;
- }
- }
- return $weight;
- }
-
- /**
- * Remove extraneous break tags from a node.
- *
- * @param DOMElement $node
- * @return void
- */
- public function killBreaks($node) {
- $html = $node->innerHTML;
- $html = preg_replace($this->regexps['killBreaks'], '
', $html);
- $node->innerHTML = $html;
- }
-
- /**
- * Clean a node of all elements of type "tag".
- * (Unless it's a youtube/vimeo video. People love movies.)
- *
- * Updated 2012-09-18 to preserve youtube/vimeo iframes
- *
- * @param DOMElement $e
- * @param string $tag
- * @return void
- */
- public function clean($e, $tag) {
- $targetList = $e->getElementsByTagName($tag);
- $isEmbed = ($tag == 'iframe' || $tag == 'object' || $tag == 'embed');
-
- for ($y=$targetList->length-1; $y >= 0; $y--) {
- /* Allow youtube and vimeo videos through as people usually want to see those. */
- if ($isEmbed) {
- $attributeValues = '';
- for ($i=0, $il=$targetList->item($y)->attributes->length; $i < $il; $i++) {
- $attributeValues .= $targetList->item($y)->attributes->item($i)->value . '|'; // DOMAttr? (TODO: test)
- }
-
- /* First, check the elements attributes to see if any of them contain youtube or vimeo */
- if (preg_match($this->regexps['video'], $attributeValues)) {
- continue;
- }
-
- /* Then check the elements inside this element for the same. */
- if (preg_match($this->regexps['video'], $targetList->item($y)->innerHTML)) {
- continue;
- }
- }
- $targetList->item($y)->parentNode->removeChild($targetList->item($y));
- }
- }
-
- /**
- * Clean an element of all tags of type "tag" if they look fishy.
- * "Fishy" is an algorithm based on content length, classnames,
- * link density, number of images & embeds, etc.
- *
- * @param DOMElement $e
- * @param string $tag
- * @return void
- */
- public function cleanConditionally($e, $tag) {
- if (!$this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
- return;
- }
-
- $tagsList = $e->getElementsByTagName($tag);
- $curTagsLength = $tagsList->length;
-
- /**
- * Gather counts for other typical elements embedded within.
- * Traverse backwards so we can remove nodes at the same time without effecting the traversal.
- *
- * TODO: Consider taking into account original contentScore here.
- */
- for ($i=$curTagsLength-1; $i >= 0; $i--) {
- $weight = $this->getClassWeight($tagsList->item($i));
- $contentScore = ($tagsList->item($i)->hasAttribute('readability')) ? (int)$tagsList->item($i)->getAttribute('readability') : 0;
-
- $this->dbg('Cleaning Conditionally ' . $tagsList->item($i)->tagName . ' (' . $tagsList->item($i)->getAttribute('class') . ':' . $tagsList->item($i)->getAttribute('id') . ')' . (($tagsList->item($i)->hasAttribute('readability')) ? (' with score ' . $tagsList->item($i)->getAttribute('readability')) : ''));
-
- if ($weight + $contentScore < 0) {
- $tagsList->item($i)->parentNode->removeChild($tagsList->item($i));
- }
- else if ( $this->getCharCount($tagsList->item($i), ',') < 10) {
- /**
- * If there are not very many commas, and the number of
- * non-paragraph elements is more than paragraphs or other ominous signs, remove the element.
- **/
- $p = $tagsList->item($i)->getElementsByTagName('p')->length;
- $img = $tagsList->item($i)->getElementsByTagName('img')->length;
- $li = $tagsList->item($i)->getElementsByTagName('li')->length-100;
- $input = $tagsList->item($i)->getElementsByTagName('input')->length;
- $a = $tagsList->item($i)->getElementsByTagName('a')->length;
-
- $embedCount = 0;
- $embeds = $tagsList->item($i)->getElementsByTagName('embed');
- for ($ei=0, $il=$embeds->length; $ei < $il; $ei++) {
- if (preg_match($this->regexps['video'], $embeds->item($ei)->getAttribute('src'))) {
- $embedCount++;
- }
- }
- $embeds = $tagsList->item($i)->getElementsByTagName('iframe');
- for ($ei=0, $il=$embeds->length; $ei < $il; $ei++) {
- if (preg_match($this->regexps['video'], $embeds->item($ei)->getAttribute('src'))) {
- $embedCount++;
- }
- }
-
- $linkDensity = $this->getLinkDensity($tagsList->item($i));
- $contentLength = strlen($this->getInnerText($tagsList->item($i)));
- $toRemove = false;
-
- if ($this->lightClean) {
- $this->dbg('Light clean...');
- if ( ($img > $p) && ($img > 4) ) {
- $this->dbg(' more than 4 images and more image elements than paragraph elements');
- $toRemove = true;
- } else if ($li > $p && $tag != 'ul' && $tag != 'ol') {
- $this->dbg(' too many
', $html); + $html = preg_replace($this->regexps['replaceFonts'], '<$1span>', $html); + $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"); + if (trim($html) == '') $html = ''; + if ($parser=='html5lib' && ($this->dom = HTML5_Parser::parse($html))) { + // all good + } else { + $this->dom = new DOMDocument(); + $this->dom->preserveWhiteSpace = false; + @$this->dom->loadHTML($html); + } + $this->dom->registerNodeClass('DOMElement', 'JSLikeHTMLElement'); + } + + /** + * Get article title element + * @return DOMElement + */ + public function getTitle() { + return $this->articleTitle; + } + + /** + * Get article content element + * @return DOMElement + */ + public function getContent() { + return $this->articleContent; + } + + /** + * Runs readability. + * + * Workflow: + * 1. Prep the document by removing script tags, css, etc. + * 2. Build readability's DOM tree. + * 3. Grab the article content from the current dom tree. + * 4. Replace the current DOM tree with the new one. + * 5. Read peacefully. + * + * @return boolean true if we found content, false otherwise + **/ + public function init() + { + if (!isset($this->dom->documentElement)) return false; + $this->removeScripts($this->dom); + //die($this->getInnerHTML($this->dom->documentElement)); + + // Assume successful outcome + $this->success = true; + + $bodyElems = $this->dom->getElementsByTagName('body'); + if ($bodyElems->length > 0) { + if ($this->bodyCache == null) { + $this->bodyCache = $bodyElems->item(0)->innerHTML; + } + if ($this->body == null) { + $this->body = $bodyElems->item(0); + } + } + + $this->prepDocument(); + + //die($this->dom->documentElement->parentNode->nodeType); + //$this->setInnerHTML($this->dom->documentElement, $this->getInnerHTML($this->dom->documentElement)); + //die($this->getInnerHTML($this->dom->documentElement)); + + /* Build readability's DOM tree */ + $overlay = $this->dom->createElement('div'); + $innerDiv = $this->dom->createElement('div'); + $articleTitle = $this->getArticleTitle(); + $articleContent = $this->grabArticle(); + + if (!$articleContent) { + $this->success = false; + $articleContent = $this->dom->createElement('div'); + $articleContent->setAttribute('id', 'readability-content'); + $articleContent->innerHTML = '
Sorry, Readability was unable to parse this page for content.
'; + } + + $overlay->setAttribute('id', 'readOverlay'); + $innerDiv->setAttribute('id', 'readInner'); + + /* Glue the structure of our document together. */ + $innerDiv->appendChild($articleTitle); + $innerDiv->appendChild($articleContent); + $overlay->appendChild($innerDiv); + + /* Clear the old HTML, insert the new content. */ + $this->body->innerHTML = ''; + $this->body->appendChild($overlay); + //document.body.insertBefore(overlay, document.body.firstChild); + $this->body->removeAttribute('style'); + + $this->postProcessContent($articleContent); + + // Set title and content instance variables + $this->articleTitle = $articleTitle; + $this->articleContent = $articleContent; + + return $this->success; + } + + /** + * Debug + */ + protected function dbg($msg) { + if ($this->debug) echo '* ',$msg, "\n"; + } + + /** + * Run any post-process modifications to article content as necessary. + * + * @param DOMElement + * @return void + */ + public function postProcessContent($articleContent) { + if ($this->convertLinksToFootnotes && !preg_match('/wikipedia\.org/', @$this->url)) { + $this->addFootnotes($articleContent); + } + } + + /** + * Get the article title as an H1. + * + * @return DOMElement + */ + protected function getArticleTitle() { + $curTitle = ''; + $origTitle = ''; + + try { + $curTitle = $origTitle = $this->getInnerText($this->dom->getElementsByTagName('title')->item(0)); + } catch(Exception $e) {} + + if (preg_match('/ [\|\-] /', $curTitle)) + { + $curTitle = preg_replace('/(.*)[\|\-] .*/i', '$1', $origTitle); + + if (count(explode(' ', $curTitle)) < 3) { + $curTitle = preg_replace('/[^\|\-]*[\|\-](.*)/i', '$1', $origTitle); + } + } + else if (strpos($curTitle, ': ') !== false) + { + $curTitle = preg_replace('/.*:(.*)/i', '$1', $origTitle); + + if (count(explode(' ', $curTitle)) < 3) { + $curTitle = preg_replace('/[^:]*[:](.*)/i','$1', $origTitle); + } + } + else if(strlen($curTitle) > 150 || strlen($curTitle) < 15) + { + $hOnes = $this->dom->getElementsByTagName('h1'); + if($hOnes->length == 1) + { + $curTitle = $this->getInnerText($hOnes->item(0)); + } + } + + $curTitle = trim($curTitle); + + if (count(explode(' ', $curTitle)) <= 4) { + $curTitle = $origTitle; + } + + $articleTitle = $this->dom->createElement('h1'); + $articleTitle->innerHTML = $curTitle; + + return $articleTitle; + } + + /** + * Prepare the HTML document for readability to scrape it. + * This includes things like stripping javascript, CSS, and handling terrible markup. + * + * @return void + **/ + protected function prepDocument() { + /** + * In some cases a body element can't be found (if the HTML is totally hosed for example) + * so we create a new body node and append it to the document. + */ + if ($this->body == null) + { + $this->body = $this->dom->createElement('body'); + $this->dom->documentElement->appendChild($this->body); + } + $this->body->setAttribute('id', 'readabilityBody'); + + /* Remove all style tags in head */ + $styleTags = $this->dom->getElementsByTagName('style'); + for ($i = $styleTags->length-1; $i >= 0; $i--) + { + $styleTags->item($i)->parentNode->removeChild($styleTags->item($i)); + } + + /* Turn all double br's into p's */ + /* Note, this is pretty costly as far as processing goes. Maybe optimize later. */ + //document.body.innerHTML = document.body.innerHTML.replace(readability.regexps.replaceBrs, '').replace(readability.regexps.replaceFonts, '<$1span>'); + // We do this in the constructor for PHP as that's when we have raw HTML - before parsing it into a DOM tree. + // Manipulating innerHTML as it's done in JS is not possible in PHP. + } + + /** + * For easier reading, convert this document to have footnotes at the bottom rather than inline links. + * @see http://www.roughtype.com/archives/2010/05/experiments_in.php + * + * @return void + **/ + public function addFootnotes($articleContent) { + $footnotesWrapper = $this->dom->createElement('div'); + $footnotesWrapper->setAttribute('id', 'readability-footnotes'); + $footnotesWrapper->innerHTML = '
tags, etc.
+ *
+ * @param DOMElement
+ * @return void
+ */
+ function prepArticle($articleContent) {
+ $this->cleanStyles($articleContent);
+ $this->killBreaks($articleContent);
+ if ($this->revertForcedParagraphElements) {
+ $this->revertReadabilityStyledElements($articleContent);
+ }
+
+ /* Clean out junk from the article content */
+ $this->cleanConditionally($articleContent, 'form');
+ $this->clean($articleContent, 'object');
+ $this->clean($articleContent, 'h1');
+
+ /**
+ * If there is only one h2, they are probably using it
+ * as a header and not a subheader, so remove it since we already have a header.
+ ***/
+ if (!$this->lightClean && ($articleContent->getElementsByTagName('h2')->length == 1)) {
+ $this->clean($articleContent, 'h2');
+ }
+ $this->clean($articleContent, 'iframe');
+
+ $this->cleanHeaders($articleContent);
+
+ /* Do these last as the previous stuff may have removed junk that will affect these */
+ $this->cleanConditionally($articleContent, 'table');
+ $this->cleanConditionally($articleContent, 'ul');
+ $this->cleanConditionally($articleContent, 'div');
+
+ /* Remove extra paragraphs */
+ $articleParagraphs = $articleContent->getElementsByTagName('p');
+ for ($i = $articleParagraphs->length-1; $i >= 0; $i--)
+ {
+ $imgCount = $articleParagraphs->item($i)->getElementsByTagName('img')->length;
+ $embedCount = $articleParagraphs->item($i)->getElementsByTagName('embed')->length;
+ $objectCount = $articleParagraphs->item($i)->getElementsByTagName('object')->length;
+ $iframeCount = $articleParagraphs->item($i)->getElementsByTagName('iframe')->length;
+
+ if ($imgCount === 0 && $embedCount === 0 && $objectCount === 0 && $iframeCount === 0 && $this->getInnerText($articleParagraphs->item($i), false) == '')
+ {
+ $articleParagraphs->item($i)->parentNode->removeChild($articleParagraphs->item($i));
+ }
+ }
+
+ try {
+ $articleContent->innerHTML = preg_replace('/
]*>\s*
innerHTML);
+ //articleContent.innerHTML = articleContent.innerHTML.replace(/
]*>\s*
dbg("Cleaning innerHTML of breaks failed. This is an IE strict-block-elements bug. Ignoring.: " . $e);
+ }
+ }
+
+ /**
+ * Initialize a node with the readability object. Also checks the
+ * className/id for special names to add to its score.
+ *
+ * @param Element
+ * @return void
+ **/
+ protected function initializeNode($node) {
+ $readability = $this->dom->createAttribute('readability');
+ $readability->value = 0; // this is our contentScore
+ $node->setAttributeNode($readability);
+
+ switch (strtoupper($node->tagName)) { // unsure if strtoupper is needed, but using it just in case
+ case 'DIV':
+ $readability->value += 5;
+ break;
+
+ case 'PRE':
+ case 'TD':
+ case 'BLOCKQUOTE':
+ $readability->value += 3;
+ break;
+
+ case 'ADDRESS':
+ case 'OL':
+ case 'UL':
+ case 'DL':
+ case 'DD':
+ case 'DT':
+ case 'LI':
+ case 'FORM':
+ $readability->value -= 3;
+ break;
+
+ case 'H1':
+ case 'H2':
+ case 'H3':
+ case 'H4':
+ case 'H5':
+ case 'H6':
+ case 'TH':
+ $readability->value -= 5;
+ break;
+ }
+ $readability->value += $this->getClassWeight($node);
+ }
+
+ /***
+ * grabArticle - Using a variety of metrics (content score, classname, element types), find the content that is
+ * most likely to be the stuff a user wants to read. Then return it wrapped up in a div.
+ *
+ * @return DOMElement
+ **/
+ protected function grabArticle($page=null) {
+ $stripUnlikelyCandidates = $this->flagIsActive(self::FLAG_STRIP_UNLIKELYS);
+ if (!$page) $page = $this->dom;
+ $allElements = $page->getElementsByTagName('*');
+ /**
+ * First, node prepping. Trash nodes that look cruddy (like ones with the class name "comment", etc), and turn divs
+ * into P tags where they have been used inappropriately (as in, where they contain no other block level elements.)
+ *
+ * Note: Assignment from index for performance. See http://www.peachpit.com/articles/article.aspx?p=31567&seqNum=5
+ * TODO: Shouldn't this be a reverse traversal?
+ **/
+ $node = null;
+ $nodesToScore = array();
+ for ($nodeIndex = 0; ($node = $allElements->item($nodeIndex)); $nodeIndex++) {
+ //for ($nodeIndex=$targetList->length-1; $nodeIndex >= 0; $nodeIndex--) {
+ //$node = $targetList->item($nodeIndex);
+ $tagName = strtoupper($node->tagName);
+ /* Remove unlikely candidates */
+ if ($stripUnlikelyCandidates) {
+ $unlikelyMatchString = $node->getAttribute('class') . $node->getAttribute('id');
+ if (
+ preg_match($this->regexps['unlikelyCandidates'], $unlikelyMatchString) &&
+ !preg_match($this->regexps['okMaybeItsACandidate'], $unlikelyMatchString) &&
+ $tagName != 'BODY'
+ )
+ {
+ $this->dbg('Removing unlikely candidate - ' . $unlikelyMatchString);
+ //$nodesToRemove[] = $node;
+ $node->parentNode->removeChild($node);
+ $nodeIndex--;
+ continue;
+ }
+ }
+
+ if ($tagName == 'P' || $tagName == 'TD' || $tagName == 'PRE') {
+ $nodesToScore[] = $node;
+ }
+
+ /* Turn all divs that don't have children block level elements into p's */
+ if ($tagName == 'DIV') {
+ if (!preg_match($this->regexps['divToPElements'], $node->innerHTML)) {
+ //$this->dbg('Altering div to p');
+ $newNode = $this->dom->createElement('p');
+ try {
+ $newNode->innerHTML = $node->innerHTML;
+ //$nodesToReplace[] = array('new'=>$newNode, 'old'=>$node);
+ $node->parentNode->replaceChild($newNode, $node);
+ $nodeIndex--;
+ $nodesToScore[] = $node; // or $newNode?
+ }
+ catch(Exception $e) {
+ $this->dbg('Could not alter div to p, reverting back to div.: ' . $e);
+ }
+ }
+ else
+ {
+ /* EXPERIMENTAL */
+ // TODO: change these p elements back to text nodes after processing
+ for ($i = 0, $il = $node->childNodes->length; $i < $il; $i++) {
+ $childNode = $node->childNodes->item($i);
+ if ($childNode->nodeType == 3) { // XML_TEXT_NODE
+ //$this->dbg('replacing text node with a p tag with the same content.');
+ $p = $this->dom->createElement('p');
+ $p->innerHTML = $childNode->nodeValue;
+ $p->setAttribute('style', 'display: inline;');
+ $p->setAttribute('class', 'readability-styled');
+ $childNode->parentNode->replaceChild($p, $childNode);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Loop through all paragraphs, and assign a score to them based on how content-y they look.
+ * Then add their score to their parent node.
+ *
+ * A score is determined by things like number of commas, class names, etc. Maybe eventually link density.
+ **/
+ $candidates = array();
+ for ($pt=0; $pt < count($nodesToScore); $pt++) {
+ $parentNode = $nodesToScore[$pt]->parentNode;
+ // $grandParentNode = $parentNode ? $parentNode->parentNode : null;
+ $grandParentNode = !$parentNode ? null : (($parentNode->parentNode instanceof DOMElement) ? $parentNode->parentNode : null);
+ $innerText = $this->getInnerText($nodesToScore[$pt]);
+
+ if (!$parentNode || !isset($parentNode->tagName)) {
+ continue;
+ }
+
+ /* If this paragraph is less than 25 characters, don't even count it. */
+ if(strlen($innerText) < 25) {
+ continue;
+ }
+
+ /* Initialize readability data for the parent. */
+ if (!$parentNode->hasAttribute('readability'))
+ {
+ $this->initializeNode($parentNode);
+ $candidates[] = $parentNode;
+ }
+
+ /* Initialize readability data for the grandparent. */
+ if ($grandParentNode && !$grandParentNode->hasAttribute('readability') && isset($grandParentNode->tagName))
+ {
+ $this->initializeNode($grandParentNode);
+ $candidates[] = $grandParentNode;
+ }
+
+ $contentScore = 0;
+
+ /* Add a point for the paragraph itself as a base. */
+ $contentScore++;
+
+ /* Add points for any commas within this paragraph */
+ $contentScore += count(explode(',', $innerText));
+
+ /* For every 100 characters in this paragraph, add another point. Up to 3 points. */
+ $contentScore += min(floor(strlen($innerText) / 100), 3);
+
+ /* Add the score to the parent. The grandparent gets half. */
+ $parentNode->getAttributeNode('readability')->value += $contentScore;
+
+ if ($grandParentNode) {
+ $grandParentNode->getAttributeNode('readability')->value += $contentScore/2;
+ }
+ }
+
+ /**
+ * After we've calculated scores, loop through all of the possible candidate nodes we found
+ * and find the one with the highest score.
+ **/
+ $topCandidate = null;
+ for ($c=0, $cl=count($candidates); $c < $cl; $c++)
+ {
+ /**
+ * Scale the final candidates score based on link density. Good content should have a
+ * relatively small link density (5% or less) and be mostly unaffected by this operation.
+ **/
+ $readability = $candidates[$c]->getAttributeNode('readability');
+ $readability->value = $readability->value * (1-$this->getLinkDensity($candidates[$c]));
+
+ $this->dbg('Candidate: ' . $candidates[$c]->tagName . ' (' . $candidates[$c]->getAttribute('class') . ':' . $candidates[$c]->getAttribute('id') . ') with score ' . $readability->value);
+
+ if (!$topCandidate || $readability->value > (int)$topCandidate->getAttribute('readability')) {
+ $topCandidate = $candidates[$c];
+ }
+ }
+
+ /**
+ * If we still have no top candidate, just use the body as a last resort.
+ * We also have to copy the body node so it is something we can modify.
+ **/
+ if ($topCandidate === null || strtoupper($topCandidate->tagName) == 'BODY')
+ {
+ $topCandidate = $this->dom->createElement('div');
+ if ($page instanceof DOMDocument) {
+ if (!isset($page->documentElement)) {
+ // we don't have a body either? what a mess! :)
+ } else {
+ $topCandidate->innerHTML = $page->documentElement->innerHTML;
+ $page->documentElement->innerHTML = '';
+ $page->documentElement->appendChild($topCandidate);
+ }
+ } else {
+ $topCandidate->innerHTML = $page->innerHTML;
+ $page->innerHTML = '';
+ $page->appendChild($topCandidate);
+ }
+ $this->initializeNode($topCandidate);
+ }
+
+ /**
+ * Now that we have the top candidate, look through its siblings for content that might also be related.
+ * Things like preambles, content split by ads that we removed, etc.
+ **/
+ $articleContent = $this->dom->createElement('div');
+ $articleContent->setAttribute('id', 'readability-content');
+ $siblingScoreThreshold = max(10, ((int)$topCandidate->getAttribute('readability')) * 0.2);
+ $siblingNodes = $topCandidate->parentNode->childNodes;
+ if (!isset($siblingNodes)) {
+ $siblingNodes = new stdClass;
+ $siblingNodes->length = 0;
+ }
+
+ for ($s=0, $sl=$siblingNodes->length; $s < $sl; $s++)
+ {
+ $siblingNode = $siblingNodes->item($s);
+ $append = false;
+
+ $this->dbg('Looking at sibling node: ' . $siblingNode->nodeName . (($siblingNode->nodeType === XML_ELEMENT_NODE && $siblingNode->hasAttribute('readability')) ? (' with score ' . $siblingNode->getAttribute('readability')) : ''));
+
+ //dbg('Sibling has score ' . ($siblingNode->readability ? siblingNode.readability.contentScore : 'Unknown'));
+
+ if ($siblingNode === $topCandidate)
+ // or if ($siblingNode->isSameNode($topCandidate))
+ {
+ $append = true;
+ }
+
+ $contentBonus = 0;
+ /* Give a bonus if sibling nodes and top candidates have the example same classname */
+ if ($siblingNode->nodeType === XML_ELEMENT_NODE && $siblingNode->getAttribute('class') == $topCandidate->getAttribute('class') && $topCandidate->getAttribute('class') != '') {
+ $contentBonus += ((int)$topCandidate->getAttribute('readability')) * 0.2;
+ }
+
+ if ($siblingNode->nodeType === XML_ELEMENT_NODE && $siblingNode->hasAttribute('readability') && (((int)$siblingNode->getAttribute('readability')) + $contentBonus) >= $siblingScoreThreshold)
+ {
+ $append = true;
+ }
+
+ if (strtoupper($siblingNode->nodeName) == 'P') {
+ $linkDensity = $this->getLinkDensity($siblingNode);
+ $nodeContent = $this->getInnerText($siblingNode);
+ $nodeLength = strlen($nodeContent);
+
+ if ($nodeLength > 80 && $linkDensity < 0.25)
+ {
+ $append = true;
+ }
+ else if ($nodeLength < 80 && $linkDensity === 0 && preg_match('/\.( |$)/', $nodeContent))
+ {
+ $append = true;
+ }
+ }
+
+ if ($append)
+ {
+ $this->dbg('Appending node: ' . $siblingNode->nodeName);
+
+ $nodeToAppend = null;
+ $sibNodeName = strtoupper($siblingNode->nodeName);
+ if ($sibNodeName != 'DIV' && $sibNodeName != 'P') {
+ /* We have a node that isn't a common block level element, like a form or td tag. Turn it into a div so it doesn't get filtered out later by accident. */
+
+ $this->dbg('Altering siblingNode of ' . $sibNodeName . ' to div.');
+ $nodeToAppend = $this->dom->createElement('div');
+ try {
+ $nodeToAppend->setAttribute('id', $siblingNode->getAttribute('id'));
+ $nodeToAppend->innerHTML = $siblingNode->innerHTML;
+ }
+ catch(Exception $e)
+ {
+ $this->dbg('Could not alter siblingNode to div, reverting back to original.');
+ $nodeToAppend = $siblingNode;
+ $s--;
+ $sl--;
+ }
+ } else {
+ $nodeToAppend = $siblingNode;
+ $s--;
+ $sl--;
+ }
+
+ /* To ensure a node does not interfere with readability styles, remove its classnames */
+ $nodeToAppend->removeAttribute('class');
+
+ /* Append sibling and subtract from our list because it removes the node when you append to another node */
+ $articleContent->appendChild($nodeToAppend);
+ }
+ }
+
+ /**
+ * So we have all of the content that we need. Now we clean it up for presentation.
+ **/
+ $this->prepArticle($articleContent);
+
+ /**
+ * Now that we've gone through the full algorithm, check to see if we got any meaningful content.
+ * If we didn't, we may need to re-run grabArticle with different flags set. This gives us a higher
+ * likelihood of finding the content, and the sieve approach gives us a higher likelihood of
+ * finding the -right- content.
+ **/
+ if (strlen($this->getInnerText($articleContent, false)) < 250)
+ {
+ // TODO: find out why element disappears sometimes, e.g. for this URL http://www.businessinsider.com/6-hedge-fund-etfs-for-average-investors-2011-7
+ // in the meantime, we check and create an empty element if it's not there.
+ if (!isset($this->body->childNodes)) $this->body = $this->dom->createElement('body');
+ $this->body->innerHTML = $this->bodyCache;
+
+ if ($this->flagIsActive(self::FLAG_STRIP_UNLIKELYS)) {
+ $this->removeFlag(self::FLAG_STRIP_UNLIKELYS);
+ return $this->grabArticle($this->body);
+ }
+ else if ($this->flagIsActive(self::FLAG_WEIGHT_CLASSES)) {
+ $this->removeFlag(self::FLAG_WEIGHT_CLASSES);
+ return $this->grabArticle($this->body);
+ }
+ else if ($this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
+ $this->removeFlag(self::FLAG_CLEAN_CONDITIONALLY);
+ return $this->grabArticle($this->body);
+ }
+ else {
+ return false;
+ }
+ }
+ return $articleContent;
+ }
+
+ /**
+ * Remove script tags from document
+ *
+ * @param DOMElement
+ * @return void
+ */
+ public function removeScripts($doc) {
+ $scripts = $doc->getElementsByTagName('script');
+ for($i = $scripts->length-1; $i >= 0; $i--)
+ {
+ $scripts->item($i)->parentNode->removeChild($scripts->item($i));
+ }
+ }
+
+ /**
+ * Get the inner text of a node.
+ * This also strips out any excess whitespace to be found.
+ *
+ * @param DOMElement $
+ * @param boolean $normalizeSpaces (default: true)
+ * @return string
+ **/
+ public function getInnerText($e, $normalizeSpaces=true) {
+ $textContent = '';
+
+ if (!isset($e->textContent) || $e->textContent == '') {
+ return '';
+ }
+
+ $textContent = trim($e->textContent);
+
+ if ($normalizeSpaces) {
+ return preg_replace($this->regexps['normalize'], ' ', $textContent);
+ } else {
+ return $textContent;
+ }
+ }
+
+ /**
+ * Get the number of times a string $s appears in the node $e.
+ *
+ * @param DOMElement $e
+ * @param string - what to count. Default is ","
+ * @return number (integer)
+ **/
+ public function getCharCount($e, $s=',') {
+ return substr_count($this->getInnerText($e), $s);
+ }
+
+ /**
+ * Remove the style attribute on every $e and under.
+ *
+ * @param DOMElement $e
+ * @return void
+ */
+ public function cleanStyles($e) {
+ if (!is_object($e)) return;
+ $elems = $e->getElementsByTagName('*');
+ foreach ($elems as $elem) {
+ $elem->removeAttribute('style');
+ }
+ }
+
+ /**
+ * Get the density of links as a percentage of the content
+ * This is the amount of text that is inside a link divided by the total text in the node.
+ *
+ * @param DOMElement $e
+ * @return number (float)
+ */
+ public function getLinkDensity($e) {
+ $links = $e->getElementsByTagName('a');
+ $textLength = strlen($this->getInnerText($e));
+ $linkLength = 0;
+ for ($i=0, $il=$links->length; $i < $il; $i++)
+ {
+ $linkLength += strlen($this->getInnerText($links->item($i)));
+ }
+ if ($textLength > 0) {
+ return $linkLength / $textLength;
+ } else {
+ return 0;
+ }
+ }
+
+ /**
+ * Get an elements class/id weight. Uses regular expressions to tell if this
+ * element looks good or bad.
+ *
+ * @param DOMElement $e
+ * @return number (Integer)
+ */
+ public function getClassWeight($e) {
+ if(!$this->flagIsActive(self::FLAG_WEIGHT_CLASSES)) {
+ return 0;
+ }
+
+ $weight = 0;
+
+ /* Look for a special classname */
+ if ($e->hasAttribute('class') && $e->getAttribute('class') != '')
+ {
+ if (preg_match($this->regexps['negative'], $e->getAttribute('class'))) {
+ $weight -= 25;
+ }
+ if (preg_match($this->regexps['positive'], $e->getAttribute('class'))) {
+ $weight += 25;
+ }
+ }
+
+ /* Look for a special ID */
+ if ($e->hasAttribute('id') && $e->getAttribute('id') != '')
+ {
+ if (preg_match($this->regexps['negative'], $e->getAttribute('id'))) {
+ $weight -= 25;
+ }
+ if (preg_match($this->regexps['positive'], $e->getAttribute('id'))) {
+ $weight += 25;
+ }
+ }
+ return $weight;
+ }
+
+ /**
+ * Remove extraneous break tags from a node.
+ *
+ * @param DOMElement $node
+ * @return void
+ */
+ public function killBreaks($node) {
+ $html = $node->innerHTML;
+ $html = preg_replace($this->regexps['killBreaks'], '
', $html);
+ $node->innerHTML = $html;
+ }
+
+ /**
+ * Clean a node of all elements of type "tag".
+ * (Unless it's a youtube/vimeo video. People love movies.)
+ *
+ * Updated 2012-09-18 to preserve youtube/vimeo iframes
+ *
+ * @param DOMElement $e
+ * @param string $tag
+ * @return void
+ */
+ public function clean($e, $tag) {
+ $targetList = $e->getElementsByTagName($tag);
+ $isEmbed = ($tag == 'iframe' || $tag == 'object' || $tag == 'embed');
+
+ for ($y=$targetList->length-1; $y >= 0; $y--) {
+ /* Allow youtube and vimeo videos through as people usually want to see those. */
+ if ($isEmbed) {
+ $attributeValues = '';
+ for ($i=0, $il=$targetList->item($y)->attributes->length; $i < $il; $i++) {
+ $attributeValues .= $targetList->item($y)->attributes->item($i)->value . '|'; // DOMAttr? (TODO: test)
+ }
+
+ /* First, check the elements attributes to see if any of them contain youtube or vimeo */
+ if (preg_match($this->regexps['video'], $attributeValues)) {
+ continue;
+ }
+
+ /* Then check the elements inside this element for the same. */
+ if (preg_match($this->regexps['video'], $targetList->item($y)->innerHTML)) {
+ continue;
+ }
+ }
+ $targetList->item($y)->parentNode->removeChild($targetList->item($y));
+ }
+ }
+
+ /**
+ * Clean an element of all tags of type "tag" if they look fishy.
+ * "Fishy" is an algorithm based on content length, classnames,
+ * link density, number of images & embeds, etc.
+ *
+ * @param DOMElement $e
+ * @param string $tag
+ * @return void
+ */
+ public function cleanConditionally($e, $tag) {
+ if (!$this->flagIsActive(self::FLAG_CLEAN_CONDITIONALLY)) {
+ return;
+ }
+
+ $tagsList = $e->getElementsByTagName($tag);
+ $curTagsLength = $tagsList->length;
+
+ /**
+ * Gather counts for other typical elements embedded within.
+ * Traverse backwards so we can remove nodes at the same time without effecting the traversal.
+ *
+ * TODO: Consider taking into account original contentScore here.
+ */
+ for ($i=$curTagsLength-1; $i >= 0; $i--) {
+ $weight = $this->getClassWeight($tagsList->item($i));
+ $contentScore = ($tagsList->item($i)->hasAttribute('readability')) ? (int)$tagsList->item($i)->getAttribute('readability') : 0;
+
+ $this->dbg('Cleaning Conditionally ' . $tagsList->item($i)->tagName . ' (' . $tagsList->item($i)->getAttribute('class') . ':' . $tagsList->item($i)->getAttribute('id') . ')' . (($tagsList->item($i)->hasAttribute('readability')) ? (' with score ' . $tagsList->item($i)->getAttribute('readability')) : ''));
+
+ if ($weight + $contentScore < 0) {
+ $tagsList->item($i)->parentNode->removeChild($tagsList->item($i));
+ }
+ else if ( $this->getCharCount($tagsList->item($i), ',') < 10) {
+ /**
+ * If there are not very many commas, and the number of
+ * non-paragraph elements is more than paragraphs or other ominous signs, remove the element.
+ **/
+ $p = $tagsList->item($i)->getElementsByTagName('p')->length;
+ $img = $tagsList->item($i)->getElementsByTagName('img')->length;
+ $li = $tagsList->item($i)->getElementsByTagName('li')->length-100;
+ $input = $tagsList->item($i)->getElementsByTagName('input')->length;
+ $a = $tagsList->item($i)->getElementsByTagName('a')->length;
+
+ $embedCount = 0;
+ $embeds = $tagsList->item($i)->getElementsByTagName('embed');
+ for ($ei=0, $il=$embeds->length; $ei < $il; $ei++) {
+ if (preg_match($this->regexps['video'], $embeds->item($ei)->getAttribute('src'))) {
+ $embedCount++;
+ }
+ }
+ $embeds = $tagsList->item($i)->getElementsByTagName('iframe');
+ for ($ei=0, $il=$embeds->length; $ei < $il; $ei++) {
+ if (preg_match($this->regexps['video'], $embeds->item($ei)->getAttribute('src'))) {
+ $embedCount++;
+ }
+ }
+
+ $linkDensity = $this->getLinkDensity($tagsList->item($i));
+ $contentLength = strlen($this->getInnerText($tagsList->item($i)));
+ $toRemove = false;
+
+ if ($this->lightClean) {
+ $this->dbg('Light clean...');
+ if ( ($img > $p) && ($img > 4) ) {
+ $this->dbg(' more than 4 images and more image elements than paragraph elements');
+ $toRemove = true;
+ } else if ($li > $p && $tag != 'ul' && $tag != 'ol') {
+ $this->dbg(' too many
[\s\h\v]*
!u', '', $html); if ($links == 'remove') { @@ -671,130 +703,155 @@ foreach ($items as $key => $item) { } } - if ($valid_key && isset($_GET['pubsub'])) { // used only on fivefilters.org at the moment - $newitem->addElement('guid', 'http://fivefilters.org/content-only/redirect.php?url='.urlencode($item->get_permalink()), array('isPermaLink'=>'false')); + if ($valid_key && isset($_GET['pubsub'])) { // used only on fivefilters.org at the moment + $newitem->addElement('guid', 'http://fivefilters.org/content-only/redirect.php?url='.urlencode($item->get_permalink()), array('isPermaLink'=>'false')); + } else { + $newitem->addElement('guid', $item->get_permalink(), array('isPermaLink'=>'true')); + } + // filter xss? + if ($xss_filter) { + debug('Filtering HTML to remove XSS'); + $html = htmLawed::hl($html, array('safe'=>1, 'deny_attribute'=>'style', 'comment'=>1, 'cdata'=>1)); + } + + // add content + if ($options->summary === true) { + // get summary + $summary = ''; + if (!$do_content_extraction) { + $summary = $html; } else { - $newitem->addElement('guid', $item->get_permalink(), array('isPermaLink'=>'true')); - } - // filter xss? - if ($xss_filter) { - debug('Filtering HTML to remove XSS'); - $html = htmLawed::hl($html, array('safe'=>1, 'deny_attribute'=>'style', 'comment'=>1, 'cdata'=>1)); - } - $newitem->setDescription($html); - - // set date - if ((int)$item->get_date('U') > 0) { - $newitem->setDate((int)$item->get_date('U')); - } elseif ($extractor->getDate()) { - $newitem->setDate($extractor->getDate()); - } - - // add authors - if ($authors = $item->get_authors()) { - foreach ($authors as $author) { - // for some feeds, SimplePie stores author's name as email, e.g. http://feeds.feedburner.com/nymag/intel - if ($author->get_name() !== null) { - $newitem->addElement('dc:creator', $author->get_name()); - } elseif ($author->get_email() !== null) { - $newitem->addElement('dc:creator', $author->get_email()); + // Try to get first few paragraphs + if (isset($content_block) && ($content_block instanceof DOMElement)) { + $_paras = $content_block->getElementsByTagName('p'); + foreach ($_paras as $_para) { + $summary .= preg_replace("/[\n\r\t ]+/", ' ', $_para->textContent).' '; + if (strlen($summary) > 200) break; } - } - } elseif ($authors = $extractor->getAuthors()) { - //TODO: make sure the list size is reasonable - foreach ($authors as $author) { - // TODO: xpath often selects authors from other articles linked from the page. - // for now choose first item - $newitem->addElement('dc:creator', $author); - break; + } else { + $summary = $html; } } - - // add language - if ($detect_language) { - $language = $extractor->getLanguage(); - if (!$language) $language = $feed->get_language(); - if (($detect_language == 3 || (!$language && $detect_language == 2)) && $text_sample) { - try { - if ($use_cld) { - // Use PHP-CLD extension - $php_cld = 'CLD\detect'; // in quotes to prevent PHP 5.2 parse error - $res = $php_cld($text_sample); - if (is_array($res) && count($res) > 0) { - $language = $res[0]['code']; - } - } else { - //die('what'); - // Use PEAR's Text_LanguageDetect - if (!isset($l)) { - $l = new Text_LanguageDetect('libraries/language-detect/lang.dat', 'libraries/language-detect/unicode_blocks.dat'); - } - $l_result = $l->detect($text_sample, 1); - if (count($l_result) > 0) { - $language = $language_codes[key($l_result)]; - } + unset($_paras, $_para); + $summary = get_excerpt($summary); + $newitem->setDescription($summary); + if ($options->content) $newitem->setElement('content:encoded', $html); + } else { + if ($options->content) $newitem->setDescription($html); + } + + // set date + if ((int)$item->get_date('U') > 0) { + $newitem->setDate((int)$item->get_date('U')); + } elseif ($extractor->getDate()) { + $newitem->setDate($extractor->getDate()); + } + + // add authors + if ($authors = $item->get_authors()) { + foreach ($authors as $author) { + // for some feeds, SimplePie stores author's name as email, e.g. http://feeds.feedburner.com/nymag/intel + if ($author->get_name() !== null) { + $newitem->addElement('dc:creator', $author->get_name()); + } elseif ($author->get_email() !== null) { + $newitem->addElement('dc:creator', $author->get_email()); + } + } + } elseif ($authors = $extractor->getAuthors()) { + //TODO: make sure the list size is reasonable + foreach ($authors as $author) { + // TODO: xpath often selects authors from other articles linked from the page. + // for now choose first item + $newitem->addElement('dc:creator', $author); + break; + } + } + + // add language + if ($detect_language) { + $language = $extractor->getLanguage(); + if (!$language) $language = $feed->get_language(); + if (($detect_language == 3 || (!$language && $detect_language == 2)) && $text_sample) { + try { + if ($use_cld) { + // Use PHP-CLD extension + $php_cld = 'CLD\detect'; // in quotes to prevent PHP 5.2 parse error + $res = $php_cld($text_sample); + if (is_array($res) && count($res) > 0) { + $language = $res[0]['code']; } - } catch (Exception $e) { - //die('error: '.$e); - // do nothing - } - } - if ($language && (strlen($language) < 7)) { - $newitem->addElement('dc:language', $language); - } - } - - // add MIME type (if it appeared in our exclusions lists) - if (isset($mime_info['mime'])) $newitem->addElement('dc:format', $mime_info['mime']); - // add effective URL (URL after redirects) - if (isset($effective_url)) { - //TODO: ensure $effective_url is valid witout - sometimes it causes problems, e.g. - //http://www.siasat.pk/forum/showthread.php?108883-Pakistan-Chowk-by-Rana-Mubashir-�-25th-March-2012-Special-Program-from-Liari-(Karachi) - //temporary measure: use utf8_encode() - $newitem->addElement('dc:identifier', remove_url_cruft(utf8_encode($effective_url))); - } else { - $newitem->addElement('dc:identifier', remove_url_cruft($item->get_permalink())); - } - - // add categories - if ($categories = $item->get_categories()) { - foreach ($categories as $category) { - if ($category->get_label() !== null) { - $newitem->addElement('category', $category->get_label()); - } - } - } - - // check for enclosures - if ($options->keep_enclosures) { - if ($enclosures = $item->get_enclosures()) { - foreach ($enclosures as $enclosure) { - // thumbnails - foreach ((array)$enclosure->get_thumbnails() as $thumbnail) { - $newitem->addElement('media:thumbnail', '', array('url'=>$thumbnail)); + } else { + //die('what'); + // Use PEAR's Text_LanguageDetect + if (!isset($l)) { + $l = new Text_LanguageDetect(); + $l->setNameMode(2); // return ISO 639-1 codes (e.g. "en") + } + $l_result = $l->detect($text_sample, 1); + if (count($l_result) > 0) { + $language = key($l_result); } - if (!$enclosure->get_link()) continue; - $enc = array(); - // Media RSS spec ($enc): http://search.yahoo.com/mrss - // SimplePie methods ($enclosure): http://simplepie.org/wiki/reference/start#methods4 - $enc['url'] = $enclosure->get_link(); - if ($enclosure->get_length()) $enc['fileSize'] = $enclosure->get_length(); - if ($enclosure->get_type()) $enc['type'] = $enclosure->get_type(); - if ($enclosure->get_medium()) $enc['medium'] = $enclosure->get_medium(); - if ($enclosure->get_expression()) $enc['expression'] = $enclosure->get_expression(); - if ($enclosure->get_bitrate()) $enc['bitrate'] = $enclosure->get_bitrate(); - if ($enclosure->get_framerate()) $enc['framerate'] = $enclosure->get_framerate(); - if ($enclosure->get_sampling_rate()) $enc['samplingrate'] = $enclosure->get_sampling_rate(); - if ($enclosure->get_channels()) $enc['channels'] = $enclosure->get_channels(); - if ($enclosure->get_duration()) $enc['duration'] = $enclosure->get_duration(); - if ($enclosure->get_height()) $enc['height'] = $enclosure->get_height(); - if ($enclosure->get_width()) $enc['width'] = $enclosure->get_width(); - if ($enclosure->get_language()) $enc['lang'] = $enclosure->get_language(); - $newitem->addElement('media:content', '', $enc); } + } catch (Exception $e) { + //die('error: '.$e); + // do nothing } } - /* } */ + if ($language && (strlen($language) < 7)) { + $newitem->addElement('dc:language', $language); + } + } + + // add MIME type (if it appeared in our exclusions lists) + if (isset($mime_info['mime'])) $newitem->addElement('dc:format', $mime_info['mime']); + // add effective URL (URL after redirects) + if (isset($effective_url)) { + //TODO: ensure $effective_url is valid witout - sometimes it causes problems, e.g. + //http://www.siasat.pk/forum/showthread.php?108883-Pakistan-Chowk-by-Rana-Mubashir-�-25th-March-2012-Special-Program-from-Liari-(Karachi) + //temporary measure: use utf8_encode() + $newitem->addElement('dc:identifier', remove_url_cruft(utf8_encode($effective_url))); + } else { + $newitem->addElement('dc:identifier', remove_url_cruft($item->get_permalink())); + } + + // add categories + if ($categories = $item->get_categories()) { + foreach ($categories as $category) { + if ($category->get_label() !== null) { + $newitem->addElement('category', $category->get_label()); + } + } + } + + // check for enclosures + if ($options->keep_enclosures) { + if ($enclosures = $item->get_enclosures()) { + foreach ($enclosures as $enclosure) { + // thumbnails + foreach ((array)$enclosure->get_thumbnails() as $thumbnail) { + $newitem->addElement('media:thumbnail', '', array('url'=>$thumbnail)); + } + if (!$enclosure->get_link()) continue; + $enc = array(); + // Media RSS spec ($enc): http://search.yahoo.com/mrss + // SimplePie methods ($enclosure): http://simplepie.org/wiki/reference/start#methods4 + $enc['url'] = $enclosure->get_link(); + if ($enclosure->get_length()) $enc['fileSize'] = $enclosure->get_length(); + if ($enclosure->get_type()) $enc['type'] = $enclosure->get_type(); + if ($enclosure->get_medium()) $enc['medium'] = $enclosure->get_medium(); + if ($enclosure->get_expression()) $enc['expression'] = $enclosure->get_expression(); + if ($enclosure->get_bitrate()) $enc['bitrate'] = $enclosure->get_bitrate(); + if ($enclosure->get_framerate()) $enc['framerate'] = $enclosure->get_framerate(); + if ($enclosure->get_sampling_rate()) $enc['samplingrate'] = $enclosure->get_sampling_rate(); + if ($enclosure->get_channels()) $enc['channels'] = $enclosure->get_channels(); + if ($enclosure->get_duration()) $enc['duration'] = $enclosure->get_duration(); + if ($enclosure->get_height()) $enc['height'] = $enclosure->get_height(); + if ($enclosure->get_width()) $enc['width'] = $enclosure->get_width(); + if ($enclosure->get_language()) $enc['lang'] = $enclosure->get_language(); + $newitem->addElement('media:content', '', $enc); + } + } + } $output->addItem($newitem); unset($html); $item_count++; diff --git a/inc/3rdparty/makefulltextfeedHelpers.php b/inc/3rdparty/makefulltextfeedHelpers.php index 1c11b8f..4e98537 100755 --- a/inc/3rdparty/makefulltextfeedHelpers.php +++ b/inc/3rdparty/makefulltextfeedHelpers.php @@ -66,6 +66,38 @@ class DummySingleItem { // HELPER FUNCTIONS /////////////////////////////// +// Adapted from WordPress +// http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/formatting.php#L2173 +function get_excerpt($text, $num_words=55, $more=null) { + if (null === $more) $more = '…'; + $text = strip_tags($text); + //TODO: Check if word count is based on single characters (East Asian characters) + /* + if (1==2) { + $text = trim(preg_replace("/[\n\r\t ]+/", ' ', $text), ' '); + preg_match_all('/./u', $text, $words_array); + $words_array = array_slice($words_array[0], 0, $num_words + 1); + $sep = ''; + } else { + $words_array = preg_split("/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY); + $sep = ' '; + } + */ + $words_array = preg_split("/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY); + $sep = ' '; + if (count($words_array) > $num_words) { + array_pop($words_array); + $text = implode($sep, $words_array); + $text = $text.$more; + } else { + $text = implode($sep, $words_array); + } + // trim whitespace at beginning or end of string + // See: http://stackoverflow.com/questions/4166896/trim-unicode-whitespace-in-php-5-2 + $text = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $text); + return $text; +} + function url_allowed($url) { global $options; if (!empty($options->allowed_urls)) { @@ -165,14 +197,6 @@ function convert_to_utf8($html, $header=null) if (strtolower($encoding) != 'utf-8') { debug('Converting to UTF-8'); $html = SimplePie_Misc::change_encoding($html, $encoding, 'utf-8'); - /* - if (function_exists('iconv')) { - // iconv appears to handle certain character encodings better than mb_convert_encoding - $html = iconv($encoding, 'utf-8', $html); - } else { - $html = mb_convert_encoding($html, 'utf-8', $encoding); - } - */ } } } @@ -196,7 +220,7 @@ function makeAbsolute($base, $elem) { } function makeAbsoluteAttr($base, $e, $attr) { if ($e->hasAttribute($attr)) { - // Trim leading and trailing white space. I don't really like this but + // Trim leading and trailing white space. I don't really like this but // unfortunately it does appear on some sites. e.g. $url = trim(str_replace('%20', ' ', $e->getAttribute($attr))); $url = str_replace(' ', '%20', $url); diff --git a/inc/3rdparty/site_config/custom/dailymotion.com.txt b/inc/3rdparty/site_config/custom/dailymotion.com.txt new file mode 100755 index 0000000..0cad808 --- /dev/null +++ b/inc/3rdparty/site_config/custom/dailymotion.com.txt @@ -0,0 +1,12 @@ +title: //title +body: //iframe + +replace_string(): _ + +single_page_link: //link[@type='application/xml+oembed'] + +prune: no +tidy: no + +http://www.dailymotion.com/video/x1vk5oh_before-they-were-on-game-of-thrones_people diff --git a/inc/3rdparty/site_config/custom/index.php b/inc/3rdparty/site_config/custom/index.php new file mode 100644 index 0000000..a3d5f73 --- /dev/null +++ b/inc/3rdparty/site_config/custom/index.php @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/inc/3rdparty/site_config/custom/ted.com.txt b/inc/3rdparty/site_config/custom/ted.com.txt new file mode 100755 index 0000000..4940d2b --- /dev/null +++ b/inc/3rdparty/site_config/custom/ted.com.txt @@ -0,0 +1,11 @@ +title: //title +body: //div[@class='talk-article__body talk-transcript__body'] | //div[@class='media__image media__image--thumb talk-link__image'] + +strip_id_or_class: talk-transcript__para__time + +single_page_link: //a[@id='hero-transcript-link'] + +#prune: no +tidy: no + +test_url: http://www.ted.com/talks/andrew_solomon_how_the_worst_moments_in_our_lives_make_us_who_we_are diff --git a/inc/3rdparty/site_config/index.php b/inc/3rdparty/site_config/index.php index a1b767f..76ca8b3 100644 --- a/inc/3rdparty/site_config/index.php +++ b/inc/3rdparty/site_config/index.php @@ -1,3 +1,2 @@ - \ No newline at end of file +set('Cache.SerializerPath', CACHE); - $config->set('HTML.SafeIframe', true); - $config->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%'); //allow YouTube and Vimeo$purifier = new HTMLPurifier($config); - + $config = HTMLPurifier_Config::createDefault(); + $config->set('Cache.SerializerPath', CACHE); + $config->set('HTML.SafeIframe', true); + //allow YouTube, Vimeo and dailymotion videos + $config->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/|www\.dailymotion\.com/embed/video/)%'); + return new HTMLPurifier($config); } diff --git a/themes/default/_search-form.twig b/themes/default/_search-form.twig old mode 100644 new mode 100755 index 74f420d..33bea20 --- a/themes/default/_search-form.twig +++ b/themes/default/_search-form.twig @@ -7,17 +7,3 @@ - \ No newline at end of file diff --git a/themes/default/css/style.css b/themes/default/css/style.css index 11a8ea1..e58ef81 100755 --- a/themes/default/css/style.css +++ b/themes/default/css/style.css @@ -384,8 +384,8 @@ a#bagit-form-close { background-color: #000; color: #fff; padding: 0 4px 1px 3px; - font-weight: bold; - font-size: 0.7em; + font-weight: bold; + font-size: 0.7em; border-radius: 4px; } .add-to-wallabag-link-after:hover, .add-to-wallabag-link-after:active { @@ -394,6 +394,23 @@ a#bagit-form-close { .add-to-wallabag-link-after:visited { color: #999; } +a.add-to-wallabag-link-after { + visibility: hidden; + position: absolute; + opacity: 0; + transition-duration: 2s; + transition-timing-function: ease-out; +} +#article article a:hover + a.add-to-wallabag-link-after, a.add-to-wallabag-link-after:hover { + opacity: 1; + visibility: visible; + transition-duration: .3s; + transition-timing-function: ease-in; +} +a.add-to-wallabag-link-after:after { + content: "w"; +} + #add-link-result { display: inline;