mirror of
https://github.com/wallabag/wallabag.git
synced 2025-08-26 18:21:02 +00:00
[change] we now use Full-Text RSS 3.1, thank you so much @fivefilters
This commit is contained in:
parent
59cc585271
commit
42c80841c8
83 changed files with 23898 additions and 7845 deletions
728
inc/3rdparty/libraries/content-extractor/ContentExtractor.php
vendored
Normal file
728
inc/3rdparty/libraries/content-extractor/ContentExtractor.php
vendored
Normal file
|
@ -0,0 +1,728 @@
|
|||
<?php
|
||||
/**
|
||||
* Content Extractor
|
||||
*
|
||||
* Uses patterns specified in site config files and auto detection (hNews/PHP Readability)
|
||||
* to extract content from HTML files.
|
||||
*
|
||||
* @version 1.0
|
||||
* @date 2013-02-05
|
||||
* @author Keyvan Minoukadeh
|
||||
* @copyright 2013 Keyvan Minoukadeh
|
||||
* @license http://www.gnu.org/licenses/agpl-3.0.html AGPL v3
|
||||
*/
|
||||
|
||||
class ContentExtractor
|
||||
{
|
||||
protected static $tidy_config = array(
|
||||
'clean' => 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;
|
||||
}
|
||||
}
|
||||
?>
|
338
inc/3rdparty/libraries/content-extractor/SiteConfig.php
vendored
Normal file
338
inc/3rdparty/libraries/content-extractor/SiteConfig.php
vendored
Normal file
|
@ -0,0 +1,338 @@
|
|||
<?php
|
||||
/**
|
||||
* Site Config
|
||||
*
|
||||
* Each instance of this class should hold extraction patterns and other directives
|
||||
* for a website. See ContentExtractor class to see how it's used.
|
||||
*
|
||||
* @version 0.7
|
||||
* @date 2012-08-27
|
||||
* @author Keyvan Minoukadeh
|
||||
* @copyright 2012 Keyvan Minoukadeh
|
||||
* @license http://www.gnu.org/licenses/agpl-3.0.html AGPL v3
|
||||
*/
|
||||
|
||||
class SiteConfig
|
||||
{
|
||||
// Use first matching element as title (0 or more xpath expressions)
|
||||
public $title = array();
|
||||
|
||||
// Use first matching element as body (0 or more xpath expressions)
|
||||
public $body = array();
|
||||
|
||||
// Use first matching element as author (0 or more xpath expressions)
|
||||
public $author = array();
|
||||
|
||||
// Use first matching element as date (0 or more xpath expressions)
|
||||
public $date = array();
|
||||
|
||||
// Strip elements matching these xpath expressions (0 or more)
|
||||
public $strip = array();
|
||||
|
||||
// Strip elements which contain these strings (0 or more) in the id or class attribute
|
||||
public $strip_id_or_class = array();
|
||||
|
||||
// Strip images which contain these strings (0 or more) in the src attribute
|
||||
public $strip_image_src = array();
|
||||
|
||||
// Additional HTTP headers to send
|
||||
// NOT YET USED
|
||||
public $http_header = array();
|
||||
|
||||
// Process HTML with tidy before creating DOM (bool or null if undeclared)
|
||||
public $tidy = null;
|
||||
|
||||
protected $default_tidy = true; // used if undeclared
|
||||
|
||||
// Autodetect title/body if xpath expressions fail to produce results.
|
||||
// Note that this applies to title and body separately, ie.
|
||||
// * if we get a body match but no title match, this option will determine whether we autodetect title
|
||||
// * if neither match, this determines whether we autodetect title and body.
|
||||
// Also note that this only applies when there is at least one xpath expression in title or body, ie.
|
||||
// * if title and body are both empty (no xpath expressions), this option has no effect (both title and body will be auto-detected)
|
||||
// * if there's an xpath expression for title and none for body, body will be auto-detected and this option will determine whether we auto-detect title if the xpath expression for it fails to produce results.
|
||||
// Usage scenario: you want to extract something specific from a set of URLs, e.g. a table, and if the table is not found, you want to ignore the entry completely. Auto-detection is unlikely to succeed here, so you construct your patterns and set this option to false. Another scenario may be a site where auto-detection has proven to fail (or worse, picked up the wrong content).
|
||||
// bool or null if undeclared
|
||||
public $autodetect_on_failure = null;
|
||||
protected $default_autodetect_on_failure = true; // used if undeclared
|
||||
|
||||
// Clean up content block - attempt to remove elements that appear to be superfluous
|
||||
// bool or null if undeclared
|
||||
public $prune = null;
|
||||
protected $default_prune = true; // used if undeclared
|
||||
|
||||
// Test URL - if present, can be used to test the config above
|
||||
public $test_url = array();
|
||||
|
||||
// Single-page link - should identify a link element or URL pointing to the page holding the entire article
|
||||
// This is useful for sites which split their articles across multiple pages. Links to such pages tend to
|
||||
// display the first page with links to the other pages at the bottom. Often there is also a link to a page
|
||||
// which displays the entire article on one page (e.g. 'print view').
|
||||
// This should be an XPath expression identifying the link to that page. If present and we find a match,
|
||||
// we will retrieve that page and the rest of the options in this config will be applied to the new page.
|
||||
public $single_page_link = array();
|
||||
|
||||
public $next_page_link = array();
|
||||
|
||||
// Single-page link in feed? - same as above, but patterns applied to item description HTML taken from feed
|
||||
public $single_page_link_in_feed = array();
|
||||
|
||||
// Which parser to use for turning raw HTML into a DOMDocument (either 'libxml' or 'html5lib')
|
||||
// string or null if undeclared
|
||||
public $parser = null;
|
||||
protected $default_parser = 'libxml'; // used if undeclared
|
||||
|
||||
// Strings to search for in HTML before processing begins (used with $replace_string)
|
||||
public $find_string = array();
|
||||
// Strings to replace those found in $find_string before HTML processing begins
|
||||
public $replace_string = array();
|
||||
|
||||
// the options below cannot be set in the config files which this class represents
|
||||
|
||||
//public $cache_in_apc = false; // used to decide if we should cache in apc or not
|
||||
public $cache_key = null;
|
||||
public static $debug = false;
|
||||
protected static $apc = false;
|
||||
protected static $config_path;
|
||||
protected static $config_path_fallback;
|
||||
protected static $config_cache = array();
|
||||
const HOSTNAME_REGEX = '/^(([a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9-]*[A-Za-z0-9])$/';
|
||||
|
||||
protected static function debug($msg) {
|
||||
if (self::$debug) {
|
||||
//$mem = round(memory_get_usage()/1024, 2);
|
||||
//$memPeak = round(memory_get_peak_usage()/1024, 2);
|
||||
echo '* ',$msg;
|
||||
//echo ' - mem used: ',$mem," (peak: $memPeak)\n";
|
||||
echo "\n";
|
||||
ob_flush();
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
// enable APC caching of certain site config files?
|
||||
// If enabled the following site config files will be
|
||||
// cached in APC cache (when requested for first time):
|
||||
// * anything in site_config/custom/ and its corresponding file in site_config/standard/
|
||||
// * the site config files associated with HTML fingerprints
|
||||
// * the global site config file
|
||||
// returns true if enabled, false otherwise
|
||||
public static function use_apc($apc=true) {
|
||||
if (!function_exists('apc_add')) {
|
||||
if ($apc) self::debug('APC will not be used (function apc_add does not exist)');
|
||||
return false;
|
||||
}
|
||||
self::$apc = $apc;
|
||||
return $apc;
|
||||
}
|
||||
|
||||
// return bool or null
|
||||
public function tidy($use_default=true) {
|
||||
if ($use_default) return (isset($this->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;
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue