]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - inc/3rdparty/libraries/content-extractor/ContentExtractor.php
update to 3.2 version of full-text-rss, issue #694
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / content-extractor / ContentExtractor.php
index ddd33bb5a8859908509d4281520aa56e9f978b2c..21e693e7db6ed0c7bbb30f6a0ba885c79ebe1f9d 100644 (file)
-<?php\r
-/**\r
- * Content Extractor\r
- * \r
- * Uses patterns specified in site config files and auto detection (hNews/PHP Readability) \r
- * to extract content from HTML files.\r
- * \r
- * @version 1.0\r
- * @date 2013-02-05\r
- * @author Keyvan Minoukadeh\r
- * @copyright 2013 Keyvan Minoukadeh\r
- * @license http://www.gnu.org/licenses/agpl-3.0.html AGPL v3\r
- */\r
-\r
-class ContentExtractor\r
-{\r
-       protected static $tidy_config = array(\r
-                                'clean' => true,\r
-                                'output-xhtml' => true,\r
-                                'logical-emphasis' => true,\r
-                                'show-body-only' => false,\r
-                                'new-blocklevel-tags' => 'article, aside, footer, header, hgroup, menu, nav, section, details, datagrid',\r
-                                'new-inline-tags' => 'mark, time, meter, progress, data',\r
-                                'wrap' => 0,\r
-                                'drop-empty-paras' => true,\r
-                                'drop-proprietary-attributes' => false,\r
-                                'enclose-text' => true,\r
-                                'enclose-block-text' => true,\r
-                                'merge-divs' => true,\r
-                                'merge-spans' => true,\r
-                                'char-encoding' => 'utf8',\r
-                                'hide-comments' => true\r
-                                );\r
-       protected $html;\r
-       protected $config;\r
-       protected $title;\r
-       protected $author = array();\r
-       protected $language;\r
-       protected $date;\r
-       protected $body;\r
-       protected $success = false;\r
-       protected $nextPageUrl;\r
-       public $allowedParsers = array('libxml', 'html5lib');\r
-       public $fingerprints = array();\r
-       public $readability;\r
-       public $debug = false;\r
-       public $debugVerbose = false;\r
-\r
-       function __construct($path, $fallback=null) {\r
-               SiteConfig::set_config_path($path, $fallback);  \r
-       }\r
-       \r
-       protected function debug($msg) {\r
-               if ($this->debug) {\r
-                       $mem = round(memory_get_usage()/1024, 2);\r
-                       $memPeak = round(memory_get_peak_usage()/1024, 2);\r
-                       echo '* ',$msg;\r
-                       if ($this->debugVerbose) echo ' - mem used: ',$mem," (peak: $memPeak)";\r
-                       echo "\n";\r
-                       ob_flush();\r
-                       flush();\r
-               }\r
-       }\r
-       \r
-       public function reset() {\r
-               $this->html = null;\r
-               $this->readability = null;\r
-               $this->config = null;\r
-               $this->title = null;\r
-               $this->body = null;\r
-               $this->author = array();\r
-               $this->language = null;\r
-               $this->date = null;\r
-               $this->nextPageUrl = null;\r
-               $this->success = false;\r
-       }\r
-\r
-       public function findHostUsingFingerprints($html) {\r
-               $this->debug('Checking fingerprints...');\r
-               $head = substr($html, 0, 8000);\r
-               foreach ($this->fingerprints as $_fp => $_fphost) {\r
-                       $lookin = 'html';\r
-                       if (is_array($_fphost)) {\r
-                               if (isset($_fphost['head']) && $_fphost['head']) {\r
-                                       $lookin = 'head';\r
-                               }\r
-                               $_fphost = $_fphost['hostname'];\r
-                       }\r
-                       if (strpos($$lookin, $_fp) !== false) {\r
-                               $this->debug("Found match: $_fphost");\r
-                               return $_fphost;\r
-                       }\r
-               }\r
-               $this->debug('No fingerprint matches');\r
-               return false;\r
-       }\r
-       \r
-       // returns SiteConfig instance (joined in order: exact match, wildcard, fingerprint, global, default)\r
-       public function buildSiteConfig($url, $html='', $add_to_cache=true) {\r
-               // extract host name\r
-               $host = @parse_url($url, PHP_URL_HOST);\r
-               $host = strtolower($host);\r
-               if (substr($host, 0, 4) == 'www.') $host = substr($host, 4);\r
-               // is merged version already cached?\r
-               if (SiteConfig::is_cached("$host.merged")) {\r
-                       $this->debug("Returning cached and merged site config for $host");\r
-                       return SiteConfig::build("$host.merged");\r
-               }\r
-               // let's build from site_config/custom/ and standard/\r
-               $config = SiteConfig::build($host);\r
-               if ($add_to_cache && $config && !SiteConfig::is_cached("$host")) {\r
-                       SiteConfig::add_to_cache($host, $config);\r
-               }\r
-               // if no match, use defaults\r
-               if (!$config) $config = new SiteConfig();\r
-               // load fingerprint config?\r
-               if ($config->autodetect_on_failure()) {\r
-                       // check HTML for fingerprints\r
-                       if (!empty($this->fingerprints) && ($_fphost = $this->findHostUsingFingerprints($html))) {\r
-                               if ($config_fingerprint = SiteConfig::build($_fphost)) {\r
-                                       $this->debug("Appending site config settings from $_fphost (fingerprint match)");\r
-                                       $config->append($config_fingerprint);\r
-                                       if ($add_to_cache && !SiteConfig::is_cached($_fphost)) {\r
-                                               //$config_fingerprint->cache_in_apc = true;\r
-                                               SiteConfig::add_to_cache($_fphost, $config_fingerprint);\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-               // load global config?\r
-               if ($config->autodetect_on_failure()) {\r
-                       if ($config_global = SiteConfig::build('global', true)) {\r
-                               $this->debug('Appending site config settings from global.txt');\r
-                               $config->append($config_global);\r
-                               if ($add_to_cache && !SiteConfig::is_cached('global')) {\r
-                                       //$config_global->cache_in_apc = true;\r
-                                       SiteConfig::add_to_cache('global', $config_global);\r
-                               }\r
-                       }\r
-               }\r
-               // store copy of merged config\r
-               if ($add_to_cache) {\r
-                       // do not store in APC if wildcard match\r
-                       $use_apc = ($host == $config->cache_key);\r
-                       $config->cache_key = null;\r
-                       SiteConfig::add_to_cache("$host.merged", $config, $use_apc);\r
-               }\r
-               return $config;\r
-       }\r
-       \r
-       // returns true on success, false on failure\r
-       // $smart_tidy indicates that if tidy is used and no results are produced, we will\r
-       // try again without it. Tidy helps us deal with PHP's patchy HTML parsing most of the time\r
-       // but it has problems of its own which we try to avoid with this option.\r
-       public function process($html, $url, $smart_tidy=true) {\r
-               $this->reset();\r
-               $this->config = $this->buildSiteConfig($url, $html);\r
-               \r
-               // do string replacements\r
-               if (!empty($this->config->find_string)) {\r
-                       if (count($this->config->find_string) == count($this->config->replace_string)) {\r
-                               $html = str_replace($this->config->find_string, $this->config->replace_string, $html, $_count);\r
-                               $this->debug("Strings replaced: $_count (find_string and/or replace_string)");\r
-                       } else {\r
-                               $this->debug('Skipped string replacement - incorrect number of find-replace strings in site config');\r
-                       }\r
-                       unset($_count);\r
-               }\r
-               \r
-               // use tidy (if it exists)?\r
-               // This fixes problems with some sites which would otherwise\r
-               // trouble DOMDocument's HTML parsing. (Although sometimes it\r
-               // makes matters worse, which is why you can override it in site config files.)\r
-               $tidied = false;\r
-               if ($this->config->tidy() && function_exists('tidy_parse_string') && $smart_tidy) {\r
-                       $this->debug('Using Tidy');\r
-                       $tidy = tidy_parse_string($html, self::$tidy_config, 'UTF8');\r
-                       if (tidy_clean_repair($tidy)) {\r
-                               $original_html = $html;\r
-                               $tidied = true;\r
-                               $html = $tidy->value;\r
-                       }\r
-                       unset($tidy);\r
-               }\r
-               \r
-               // load and parse html\r
-               $_parser = $this->config->parser();\r
-               if (!in_array($_parser, $this->allowedParsers)) {\r
-                       $this->debug("HTML parser $_parser not listed, using libxml instead");\r
-                       $_parser = 'libxml';\r
-               }\r
-               $this->debug("Attempting to parse HTML with $_parser");\r
-               $this->readability = new Readability($html, $url, $_parser);\r
-               \r
-               // we use xpath to find elements in the given HTML document\r
-               // see http://en.wikipedia.org/wiki/XPath_1.0\r
-               $xpath = new DOMXPath($this->readability->dom);\r
-\r
-               // try to get next page link\r
-               foreach ($this->config->next_page_link as $pattern) {\r
-                       $elems = @$xpath->evaluate($pattern, $this->readability->dom);\r
-                       if (is_string($elems)) {\r
-                               $this->nextPageUrl = trim($elems);\r
-                               break;\r
-                       } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {\r
-                               foreach ($elems as $item) {\r
-                                       if ($item instanceof DOMElement && $item->hasAttribute('href')) {\r
-                                               $this->nextPageUrl = $item->getAttribute('href');\r
-                                               break 2;\r
-                                       } elseif ($item instanceof DOMAttr && $item->value) {\r
-                                               $this->nextPageUrl = $item->value;\r
-                                               break 2;\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               // try to get title\r
-               foreach ($this->config->title as $pattern) {\r
-                       // $this->debug("Trying $pattern");\r
-                       $elems = @$xpath->evaluate($pattern, $this->readability->dom);\r
-                       if (is_string($elems)) {\r
-                               $this->title = trim($elems);\r
-                               $this->debug('Title expression evaluated as string: '.$this->title);\r
-                               $this->debug("...XPath match: $pattern");\r
-                               break;\r
-                       } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {\r
-                               $this->title = $elems->item(0)->textContent;\r
-                               $this->debug('Title matched: '.$this->title);\r
-                               $this->debug("...XPath match: $pattern");\r
-                               // remove title from document\r
-                               try {\r
-                                       $elems->item(0)->parentNode->removeChild($elems->item(0));\r
-                               } catch (DOMException $e) {\r
-                                       // do nothing\r
-                               }\r
-                               break;\r
-                       }\r
-               }\r
-               \r
-               // try to get author (if it hasn't already been set)\r
-               if (empty($this->author)) {\r
-                       foreach ($this->config->author as $pattern) {\r
-                               $elems = @$xpath->evaluate($pattern, $this->readability->dom);\r
-                               if (is_string($elems)) {\r
-                                       if (trim($elems) != '') {\r
-                                               $this->author[] = trim($elems);\r
-                                               $this->debug('Author expression evaluated as string: '.trim($elems));\r
-                                               $this->debug("...XPath match: $pattern");\r
-                                               break;\r
-                                       }\r
-                               } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {\r
-                                       foreach ($elems as $elem) {\r
-                                               if (!isset($elem->parentNode)) continue;\r
-                                               $this->author[] = trim($elem->textContent);\r
-                                               $this->debug('Author matched: '.trim($elem->textContent));\r
-                                       }\r
-                                       if (!empty($this->author)) {\r
-                                               $this->debug("...XPath match: $pattern");\r
-                                               break;\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               // try to get language\r
-               $_lang_xpath = array('//html[@lang]/@lang', '//meta[@name="DC.language"]/@content');\r
-               foreach ($_lang_xpath as $pattern) {\r
-                       $elems = @$xpath->evaluate($pattern, $this->readability->dom);\r
-                       if (is_string($elems)) {\r
-                               if (trim($elems) != '') {\r
-                                       $this->language = trim($elems);\r
-                                       $this->debug('Language matched: '.$this->language);\r
-                                       break;\r
-                               }\r
-                       } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {\r
-                               foreach ($elems as $elem) {\r
-                                       if (!isset($elem->parentNode)) continue;\r
-                                       $this->language = trim($elem->textContent);\r
-                                       $this->debug('Language matched: '.$this->language);                                     \r
-                               }\r
-                               if ($this->language) break;\r
-                       }\r
-               }\r
-               \r
-               // try to get date\r
-               foreach ($this->config->date as $pattern) {\r
-                       $elems = @$xpath->evaluate($pattern, $this->readability->dom);\r
-                       if (is_string($elems)) {\r
-                               $this->date = strtotime(trim($elems, "; \t\n\r\0\x0B"));                                \r
-                       } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {\r
-                               $this->date = $elems->item(0)->textContent;\r
-                               $this->date = strtotime(trim($this->date, "; \t\n\r\0\x0B"));\r
-                               // remove date from document\r
-                               // $elems->item(0)->parentNode->removeChild($elems->item(0));\r
-                       }\r
-                       if (!$this->date) {\r
-                               $this->date = null;\r
-                       } else {\r
-                               $this->debug('Date matched: '.date('Y-m-d H:i:s', $this->date));\r
-                               $this->debug("...XPath match: $pattern");\r
-                               break;\r
-                       }\r
-               }\r
-\r
-               // strip elements (using xpath expressions)\r
-               foreach ($this->config->strip as $pattern) {\r
-                       $elems = @$xpath->query($pattern, $this->readability->dom);\r
-                       // check for matches\r
-                       if ($elems && $elems->length > 0) {\r
-                               $this->debug('Stripping '.$elems->length.' elements (strip)');\r
-                               for ($i=$elems->length-1; $i >= 0; $i--) {\r
-                                       $elems->item($i)->parentNode->removeChild($elems->item($i));\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               // strip elements (using id and class attribute values)\r
-               foreach ($this->config->strip_id_or_class as $string) {\r
-                       $string = strtr($string, array("'"=>'', '"'=>''));\r
-                       $elems = @$xpath->query("//*[contains(@class, '$string') or contains(@id, '$string')]", $this->readability->dom);\r
-                       // check for matches\r
-                       if ($elems && $elems->length > 0) {\r
-                               $this->debug('Stripping '.$elems->length.' elements (strip_id_or_class)');\r
-                               for ($i=$elems->length-1; $i >= 0; $i--) {\r
-                                       $elems->item($i)->parentNode->removeChild($elems->item($i));\r
-                               }\r
-                       }\r
-               }\r
-               \r
-               // strip images (using src attribute values)\r
-               foreach ($this->config->strip_image_src as $string) {\r
-                       $string = strtr($string, array("'"=>'', '"'=>''));\r
-                       $elems = @$xpath->query("//img[contains(@src, '$string')]", $this->readability->dom);\r
-                       // check for matches\r
-                       if ($elems && $elems->length > 0) {\r
-                               $this->debug('Stripping '.$elems->length.' image elements');\r
-                               for ($i=$elems->length-1; $i >= 0; $i--) {\r
-                                       $elems->item($i)->parentNode->removeChild($elems->item($i));\r
-                               }\r
-                       }\r
-               }\r
-               // strip elements using Readability.com and Instapaper.com ignore class names\r
-               // .entry-unrelated and .instapaper_ignore\r
-               // See https://www.readability.com/publishers/guidelines/#view-plainGuidelines\r
-               // and http://blog.instapaper.com/post/730281947\r
-               $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' entry-unrelated ') or contains(concat(' ',normalize-space(@class),' '),' instapaper_ignore ')]", $this->readability->dom);\r
-               // check for matches\r
-               if ($elems && $elems->length > 0) {\r
-                       $this->debug('Stripping '.$elems->length.' .entry-unrelated,.instapaper_ignore elements');\r
-                       for ($i=$elems->length-1; $i >= 0; $i--) {\r
-                               $elems->item($i)->parentNode->removeChild($elems->item($i));\r
-                       }\r
-               }\r
-               \r
-               // strip elements that contain style="display: none;"\r
-               $elems = @$xpath->query("//*[contains(@style,'display:none')]", $this->readability->dom);\r
-               // check for matches\r
-               if ($elems && $elems->length > 0) {\r
-                       $this->debug('Stripping '.$elems->length.' elements with inline display:none style');\r
-                       for ($i=$elems->length-1; $i >= 0; $i--) {\r
-                               $elems->item($i)->parentNode->removeChild($elems->item($i));\r
-                       }\r
-               }\r
-               \r
-               // try to get body\r
-               foreach ($this->config->body as $pattern) {\r
-                       $elems = @$xpath->query($pattern, $this->readability->dom);\r
-                       // check for matches\r
-                       if ($elems && $elems->length > 0) {\r
-                               $this->debug('Body matched');\r
-                               $this->debug("...XPath match: $pattern");\r
-                               if ($elems->length == 1) {                              \r
-                                       $this->body = $elems->item(0);\r
-                                       // prune (clean up elements that may not be content)\r
-                                       if ($this->config->prune()) {\r
-                                               $this->debug('...pruning content');\r
-                                               $this->readability->prepArticle($this->body);\r
-                                       }\r
-                                       break;\r
-                               } else {\r
-                                       $this->body = $this->readability->dom->createElement('div');\r
-                                       $this->debug($elems->length.' body elems found');\r
-                                       foreach ($elems as $elem) {\r
-                                               if (!isset($elem->parentNode)) continue;\r
-                                               $isDescendant = false;\r
-                                               foreach ($this->body->childNodes as $parent) {\r
-                                                       if ($this->isDescendant($parent, $elem)) {\r
-                                                               $isDescendant = true;\r
-                                                               break;\r
-                                                       }\r
-                                               }\r
-                                               if ($isDescendant) {\r
-                                                       $this->debug('...element is child of another body element, skipping.');\r
-                                               } else {\r
-                                                       // prune (clean up elements that may not be content)\r
-                                                       if ($this->config->prune()) {\r
-                                                               $this->debug('Pruning content');\r
-                                                               $this->readability->prepArticle($elem);\r
-                                                       }\r
-                                                       $this->debug('...element added to body');\r
-                                                       $this->body->appendChild($elem);\r
-                                               }\r
-                                       }\r
-                                       if ($this->body->hasChildNodes()) break;\r
-                               }\r
-                       }\r
-               }               \r
-               \r
-               // auto detect?\r
-               $detect_title = $detect_body = $detect_author = $detect_date = false;\r
-               // detect title?\r
-               if (!isset($this->title)) {\r
-                       if (empty($this->config->title) || $this->config->autodetect_on_failure()) {\r
-                               $detect_title = true;\r
-                       }\r
-               }\r
-               // detect body?\r
-               if (!isset($this->body)) {\r
-                       if (empty($this->config->body) || $this->config->autodetect_on_failure()) {\r
-                               $detect_body = true;\r
-                       }\r
-               }\r
-               // detect author?\r
-               if (empty($this->author)) {\r
-                       if (empty($this->config->author) || $this->config->autodetect_on_failure()) {\r
-                               $detect_author = true;\r
-                       }\r
-               }\r
-               // detect date?\r
-               if (!isset($this->date)) {\r
-                       if (empty($this->config->date) || $this->config->autodetect_on_failure()) {\r
-                               $detect_date = true;\r
-                       }\r
-               }\r
-\r
-               // check for hNews\r
-               if ($detect_title || $detect_body) {\r
-                       // check for hentry\r
-                       $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' hentry ')]", $this->readability->dom);\r
-                       if ($elems && $elems->length > 0) {\r
-                               $this->debug('hNews: found hentry');\r
-                               $hentry = $elems->item(0);\r
-                               \r
-                               if ($detect_title) {\r
-                                       // check for entry-title\r
-                                       $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' entry-title ')]", $hentry);\r
-                                       if ($elems && $elems->length > 0) {\r
-                                               $this->title = $elems->item(0)->textContent;\r
-                                               $this->debug('hNews: found entry-title: '.$this->title);\r
-                                               // remove title from document\r
-                                               $elems->item(0)->parentNode->removeChild($elems->item(0));\r
-                                               $detect_title = false;\r
-                                       }\r
-                               }\r
-                               \r
-                               if ($detect_date) {\r
-                                       // check for time element with pubdate attribute\r
-                                       $elems = @$xpath->query(".//time[@pubdate] | .//abbr[contains(concat(' ',normalize-space(@class),' '),' published ')]", $hentry);\r
-                                       if ($elems && $elems->length > 0) {\r
-                                               $this->date = strtotime(trim($elems->item(0)->textContent));\r
-                                               // remove date from document\r
-                                               //$elems->item(0)->parentNode->removeChild($elems->item(0));\r
-                                               if ($this->date) {\r
-                                                       $this->debug('hNews: found publication date: '.date('Y-m-d H:i:s', $this->date));\r
-                                                       $detect_date = false;\r
-                                               } else {\r
-                                                       $this->date = null;\r
-                                               }\r
-                                       }\r
-                               }\r
-\r
-                               if ($detect_author) {\r
-                                       // check for time element with pubdate attribute\r
-                                       $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' vcard ') and (contains(concat(' ',normalize-space(@class),' '),' author ') or contains(concat(' ',normalize-space(@class),' '),' byline '))]", $hentry);\r
-                                       if ($elems && $elems->length > 0) {\r
-                                               $author = $elems->item(0);\r
-                                               $fn = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' fn ')]", $author);\r
-                                               if ($fn && $fn->length > 0) {\r
-                                                       foreach ($fn as $_fn) {\r
-                                                               if (trim($_fn->textContent) != '') {\r
-                                                                       $this->author[] = trim($_fn->textContent);\r
-                                                                       $this->debug('hNews: found author: '.trim($_fn->textContent));\r
-                                                               }\r
-                                                       }\r
-                                               } else {\r
-                                                       if (trim($author->textContent) != '') {\r
-                                                               $this->author[] = trim($author->textContent);\r
-                                                               $this->debug('hNews: found author: '.trim($author->textContent));\r
-                                                       }\r
-                                               }\r
-                                               $detect_author = empty($this->author);\r
-                                       }\r
-                               }\r
-                               \r
-                               // check for entry-content.\r
-                               // according to hAtom spec, if there are multiple elements marked entry-content,\r
-                               // we include all of these in the order they appear - see http://microformats.org/wiki/hatom#Entry_Content\r
-                               if ($detect_body) {\r
-                                       $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' entry-content ')]", $hentry);\r
-                                       if ($elems && $elems->length > 0) {\r
-                                               $this->debug('hNews: found entry-content');\r
-                                               if ($elems->length == 1) {\r
-                                                       // what if it's empty? (some sites misuse hNews - place their content outside an empty entry-content element)\r
-                                                       $e = $elems->item(0);\r
-                                                       if (($e->tagName == 'img') || (trim($e->textContent) != '')) {\r
-                                                               $this->body = $elems->item(0);\r
-                                                               // prune (clean up elements that may not be content)\r
-                                                               if ($this->config->prune()) {\r
-                                                                       $this->debug('Pruning content');\r
-                                                                       $this->readability->prepArticle($this->body);\r
-                                                               }\r
-                                                               $detect_body = false;\r
-                                                       } else {\r
-                                                               $this->debug('hNews: skipping entry-content - appears not to contain content');\r
-                                                       }\r
-                                                       unset($e);\r
-                                               } else {\r
-                                                       $this->body = $this->readability->dom->createElement('div');\r
-                                                       $this->debug($elems->length.' entry-content elems found');\r
-                                                       foreach ($elems as $elem) {\r
-                                                               if (!isset($elem->parentNode)) continue;\r
-                                                               $isDescendant = false;\r
-                                                               foreach ($this->body->childNodes as $parent) {\r
-                                                                       if ($this->isDescendant($parent, $elem)) {\r
-                                                                               $isDescendant = true;\r
-                                                                               break;\r
-                                                                       }\r
-                                                               }\r
-                                                               if ($isDescendant) {\r
-                                                                       $this->debug('Element is child of another body element, skipping.');\r
-                                                               } else {\r
-                                                                       // prune (clean up elements that may not be content)\r
-                                                                       if ($this->config->prune()) {\r
-                                                                               $this->debug('Pruning content');\r
-                                                                               $this->readability->prepArticle($elem);\r
-                                                                       }                                                               \r
-                                                                       $this->debug('Element added to body');                                                                  \r
-                                                                       $this->body->appendChild($elem);\r
-                                                               }\r
-                                                       }\r
-                                                       $detect_body = false;\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-\r
-               // check for elements marked with instapaper_title\r
-               if ($detect_title) {\r
-                       // check for instapaper_title\r
-                       $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' instapaper_title ')]", $this->readability->dom);\r
-                       if ($elems && $elems->length > 0) {\r
-                               $this->title = $elems->item(0)->textContent;\r
-                               $this->debug('Title found (.instapaper_title): '.$this->title);\r
-                               // remove title from document\r
-                               $elems->item(0)->parentNode->removeChild($elems->item(0));\r
-                               $detect_title = false;\r
-                       }\r
-               }\r
-               // check for elements marked with instapaper_body\r
-               if ($detect_body) {\r
-                       $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' instapaper_body ')]", $this->readability->dom);\r
-                       if ($elems && $elems->length > 0) {\r
-                               $this->debug('body found (.instapaper_body)');\r
-                               $this->body = $elems->item(0);\r
-                               // prune (clean up elements that may not be content)\r
-                               if ($this->config->prune()) {\r
-                                       $this->debug('Pruning content');\r
-                                       $this->readability->prepArticle($this->body);\r
-                               }\r
-                               $detect_body = false;\r
-                       }\r
-               }\r
-               \r
-               // Find author in rel="author" marked element\r
-               // We only use this if there's exactly one.\r
-               // If there's more than one, it could indicate more than\r
-               // one author, but it could also indicate that we're processing\r
-               // a page listing different articles with different authors.\r
-               if ($detect_author) {\r
-                       $elems = @$xpath->query("//a[contains(concat(' ',normalize-space(@rel),' '),' author ')]", $this->readability->dom);\r
-                       if ($elems && $elems->length == 1) {\r
-                               $author = trim($elems->item(0)->textContent);\r
-                               if ($author != '') {\r
-                                       $this->debug("Author found (rel=\"author\"): $author");\r
-                                       $this->author[] = $author;\r
-                                       $detect_author = false;\r
-                               }\r
-                       }\r
-               }\r
-\r
-               // Find date in pubdate marked time element\r
-               // For the same reason given above, we only use this\r
-               // if there's exactly one element.\r
-               if ($detect_date) {\r
-                       $elems = @$xpath->query("//time[@pubdate]", $this->readability->dom);\r
-                       if ($elems && $elems->length == 1) {\r
-                               $this->date = strtotime(trim($elems->item(0)->textContent));\r
-                               // remove date from document\r
-                               //$elems->item(0)->parentNode->removeChild($elems->item(0));\r
-                               if ($this->date) {\r
-                                       $this->debug('Date found (pubdate marked time element): '.date('Y-m-d H:i:s', $this->date));\r
-                                       $detect_date = false;\r
-                               } else {\r
-                                       $this->date = null;\r
-                               }\r
-                       }\r
-               }\r
-\r
-               // still missing title or body, so we detect using Readability\r
-               if ($detect_title || $detect_body) {\r
-                       $this->debug('Using Readability');\r
-                       // clone body if we're only using Readability for title (otherwise it may interfere with body element)\r
-                       if (isset($this->body)) $this->body = $this->body->cloneNode(true);\r
-                       $success = $this->readability->init();\r
-               }\r
-               if ($detect_title) {\r
-                       $this->debug('Detecting title');\r
-                       $this->title = $this->readability->getTitle()->textContent;\r
-               }\r
-               if ($detect_body && $success) {\r
-                       $this->debug('Detecting body');\r
-                       $this->body = $this->readability->getContent();\r
-                       if ($this->body->childNodes->length == 1 && $this->body->firstChild->nodeType === XML_ELEMENT_NODE) {\r
-                               $this->body = $this->body->firstChild;\r
-                       }\r
-                       // prune (clean up elements that may not be content)\r
-                       if ($this->config->prune()) {\r
-                               $this->debug('Pruning content');\r
-                               $this->readability->prepArticle($this->body);\r
-                       }\r
-               }\r
-               if (isset($this->body)) {\r
-                       // remove scripts\r
-                       $this->readability->removeScripts($this->body);\r
-                       // remove any h1-h6 elements that appear as first thing in the body\r
-                       // and which match our title\r
-                       if (isset($this->title) && ($this->title != '')) {\r
-                               $firstChild = $this->body->firstChild;\r
-                               while ($firstChild->nodeType && ($firstChild->nodeType !== XML_ELEMENT_NODE)) {\r
-                                       $firstChild = $firstChild->nextSibling;\r
-                               }\r
-                               if (($firstChild->nodeType === XML_ELEMENT_NODE)\r
-                                       && in_array(strtolower($firstChild->tagName), array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))\r
-                                       && (strtolower(trim($firstChild->textContent)) == strtolower(trim($this->title)))) {\r
-                                               $this->body->removeChild($firstChild);\r
-                               }\r
-                       }\r
-                       // prevent self-closing iframes\r
-                       $elems = $this->body->getElementsByTagName('iframe');\r
-                       for ($i = $elems->length-1; $i >= 0; $i--) {\r
-                               $e = $elems->item($i);\r
-                               if (!$e->hasChildNodes()) {\r
-                                       $e->appendChild($this->body->ownerDocument->createTextNode('[embedded content]'));\r
-                               }\r
-                       }\r
-                       // remove image lazy loading - WordPress plugin http://wordpress.org/extend/plugins/lazy-load/\r
-                       // the plugin replaces the src attribute to point to a 1x1 gif and puts the original src\r
-                       // inside the data-lazy-src attribute. It also places the original image inside a noscript element \r
-                       // next to the amended one.\r
-                       $elems = @$xpath->query("//img[@data-lazy-src]", $this->body);\r
-                       for ($i = $elems->length-1; $i >= 0; $i--) {\r
-                               $e = $elems->item($i);\r
-                               // let's see if we can grab image from noscript\r
-                               if ($e->nextSibling !== null && $e->nextSibling->nodeName === 'noscript') {\r
-                                       $_new_elem = $e->ownerDocument->createDocumentFragment();\r
-                                       @$_new_elem->appendXML($e->nextSibling->innerHTML);\r
-                                       $e->nextSibling->parentNode->replaceChild($_new_elem, $e->nextSibling);\r
-                                       $e->parentNode->removeChild($e);\r
-                               } else {\r
-                                       // Use data-lazy-src as src value\r
-                                       $e->setAttribute('src', $e->getAttribute('data-lazy-src'));\r
-                                       $e->removeAttribute('data-lazy-src');\r
-                               }\r
-                       }\r
-               \r
-                       $this->success = true;\r
-               }\r
-               \r
-               // if we've had no success and we've used tidy, there's a chance\r
-               // that tidy has messed up. So let's try again without tidy...\r
-               if (!$this->success && $tidied && $smart_tidy) {\r
-                       $this->debug('Trying again without tidy');\r
-                       $this->process($original_html, $url, false);\r
-               }\r
-\r
-               return $this->success;\r
-       }\r
-       \r
-       private function isDescendant(DOMElement $parent, DOMElement $child) {\r
-               $node = $child->parentNode;\r
-               while ($node != null) {\r
-                       if ($node->isSameNode($parent)) return true;\r
-                       $node = $node->parentNode;\r
-               }\r
-               return false;\r
-       }\r
-\r
-       public function getContent() {\r
-               return $this->body;\r
-       }\r
-       \r
-       public function getTitle() {\r
-               return $this->title;\r
-       }\r
-       \r
-       public function getAuthors() {\r
-               return $this->author;\r
-       }\r
-       \r
-       public function getLanguage() {\r
-               return $this->language;\r
-       }\r
-       \r
-       public function getDate() {\r
-               return $this->date;\r
-       }\r
-       \r
-       public function getSiteConfig() {\r
-               return $this->config;\r
-       }\r
-       \r
-       public function getNextPageUrl() {\r
-               return $this->nextPageUrl;\r
-       }\r
-}\r
-?>
\ No newline at end of file
+<?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;
+       }
+}
\ No newline at end of file