diff options
Diffstat (limited to 'inc/3rdparty/libraries/content-extractor/ContentExtractor.php')
-rw-r--r-- | inc/3rdparty/libraries/content-extractor/ContentExtractor.php | 728 |
1 files changed, 728 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/content-extractor/ContentExtractor.php b/inc/3rdparty/libraries/content-extractor/ContentExtractor.php new file mode 100644 index 00000000..ddd33bb5 --- /dev/null +++ b/inc/3rdparty/libraries/content-extractor/ContentExtractor.php | |||
@@ -0,0 +1,728 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Content Extractor | ||
4 | * | ||
5 | * Uses patterns specified in site config files and auto detection (hNews/PHP Readability) | ||
6 | * to extract content from HTML files. | ||
7 | * | ||
8 | * @version 1.0 | ||
9 | * @date 2013-02-05 | ||
10 | * @author Keyvan Minoukadeh | ||
11 | * @copyright 2013 Keyvan Minoukadeh | ||
12 | * @license http://www.gnu.org/licenses/agpl-3.0.html AGPL v3 | ||
13 | */ | ||
14 | |||
15 | class ContentExtractor | ||
16 | { | ||
17 | protected static $tidy_config = array( | ||
18 | 'clean' => true, | ||
19 | 'output-xhtml' => true, | ||
20 | 'logical-emphasis' => true, | ||
21 | 'show-body-only' => false, | ||
22 | 'new-blocklevel-tags' => 'article, aside, footer, header, hgroup, menu, nav, section, details, datagrid', | ||
23 | 'new-inline-tags' => 'mark, time, meter, progress, data', | ||
24 | 'wrap' => 0, | ||
25 | 'drop-empty-paras' => true, | ||
26 | 'drop-proprietary-attributes' => false, | ||
27 | 'enclose-text' => true, | ||
28 | 'enclose-block-text' => true, | ||
29 | 'merge-divs' => true, | ||
30 | 'merge-spans' => true, | ||
31 | 'char-encoding' => 'utf8', | ||
32 | 'hide-comments' => true | ||
33 | ); | ||
34 | protected $html; | ||
35 | protected $config; | ||
36 | protected $title; | ||
37 | protected $author = array(); | ||
38 | protected $language; | ||
39 | protected $date; | ||
40 | protected $body; | ||
41 | protected $success = false; | ||
42 | protected $nextPageUrl; | ||
43 | public $allowedParsers = array('libxml', 'html5lib'); | ||
44 | public $fingerprints = array(); | ||
45 | public $readability; | ||
46 | public $debug = false; | ||
47 | public $debugVerbose = false; | ||
48 | |||
49 | function __construct($path, $fallback=null) { | ||
50 | SiteConfig::set_config_path($path, $fallback); | ||
51 | } | ||
52 | |||
53 | protected function debug($msg) { | ||
54 | if ($this->debug) { | ||
55 | $mem = round(memory_get_usage()/1024, 2); | ||
56 | $memPeak = round(memory_get_peak_usage()/1024, 2); | ||
57 | echo '* ',$msg; | ||
58 | if ($this->debugVerbose) echo ' - mem used: ',$mem," (peak: $memPeak)"; | ||
59 | echo "\n"; | ||
60 | ob_flush(); | ||
61 | flush(); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | public function reset() { | ||
66 | $this->html = null; | ||
67 | $this->readability = null; | ||
68 | $this->config = null; | ||
69 | $this->title = null; | ||
70 | $this->body = null; | ||
71 | $this->author = array(); | ||
72 | $this->language = null; | ||
73 | $this->date = null; | ||
74 | $this->nextPageUrl = null; | ||
75 | $this->success = false; | ||
76 | } | ||
77 | |||
78 | public function findHostUsingFingerprints($html) { | ||
79 | $this->debug('Checking fingerprints...'); | ||
80 | $head = substr($html, 0, 8000); | ||
81 | foreach ($this->fingerprints as $_fp => $_fphost) { | ||
82 | $lookin = 'html'; | ||
83 | if (is_array($_fphost)) { | ||
84 | if (isset($_fphost['head']) && $_fphost['head']) { | ||
85 | $lookin = 'head'; | ||
86 | } | ||
87 | $_fphost = $_fphost['hostname']; | ||
88 | } | ||
89 | if (strpos($$lookin, $_fp) !== false) { | ||
90 | $this->debug("Found match: $_fphost"); | ||
91 | return $_fphost; | ||
92 | } | ||
93 | } | ||
94 | $this->debug('No fingerprint matches'); | ||
95 | return false; | ||
96 | } | ||
97 | |||
98 | // returns SiteConfig instance (joined in order: exact match, wildcard, fingerprint, global, default) | ||
99 | public function buildSiteConfig($url, $html='', $add_to_cache=true) { | ||
100 | // extract host name | ||
101 | $host = @parse_url($url, PHP_URL_HOST); | ||
102 | $host = strtolower($host); | ||
103 | if (substr($host, 0, 4) == 'www.') $host = substr($host, 4); | ||
104 | // is merged version already cached? | ||
105 | if (SiteConfig::is_cached("$host.merged")) { | ||
106 | $this->debug("Returning cached and merged site config for $host"); | ||
107 | return SiteConfig::build("$host.merged"); | ||
108 | } | ||
109 | // let's build from site_config/custom/ and standard/ | ||
110 | $config = SiteConfig::build($host); | ||
111 | if ($add_to_cache && $config && !SiteConfig::is_cached("$host")) { | ||
112 | SiteConfig::add_to_cache($host, $config); | ||
113 | } | ||
114 | // if no match, use defaults | ||
115 | if (!$config) $config = new SiteConfig(); | ||
116 | // load fingerprint config? | ||
117 | if ($config->autodetect_on_failure()) { | ||
118 | // check HTML for fingerprints | ||
119 | if (!empty($this->fingerprints) && ($_fphost = $this->findHostUsingFingerprints($html))) { | ||
120 | if ($config_fingerprint = SiteConfig::build($_fphost)) { | ||
121 | $this->debug("Appending site config settings from $_fphost (fingerprint match)"); | ||
122 | $config->append($config_fingerprint); | ||
123 | if ($add_to_cache && !SiteConfig::is_cached($_fphost)) { | ||
124 | //$config_fingerprint->cache_in_apc = true; | ||
125 | SiteConfig::add_to_cache($_fphost, $config_fingerprint); | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | // load global config? | ||
131 | if ($config->autodetect_on_failure()) { | ||
132 | if ($config_global = SiteConfig::build('global', true)) { | ||
133 | $this->debug('Appending site config settings from global.txt'); | ||
134 | $config->append($config_global); | ||
135 | if ($add_to_cache && !SiteConfig::is_cached('global')) { | ||
136 | //$config_global->cache_in_apc = true; | ||
137 | SiteConfig::add_to_cache('global', $config_global); | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | // store copy of merged config | ||
142 | if ($add_to_cache) { | ||
143 | // do not store in APC if wildcard match | ||
144 | $use_apc = ($host == $config->cache_key); | ||
145 | $config->cache_key = null; | ||
146 | SiteConfig::add_to_cache("$host.merged", $config, $use_apc); | ||
147 | } | ||
148 | return $config; | ||
149 | } | ||
150 | |||
151 | // returns true on success, false on failure | ||
152 | // $smart_tidy indicates that if tidy is used and no results are produced, we will | ||
153 | // try again without it. Tidy helps us deal with PHP's patchy HTML parsing most of the time | ||
154 | // but it has problems of its own which we try to avoid with this option. | ||
155 | public function process($html, $url, $smart_tidy=true) { | ||
156 | $this->reset(); | ||
157 | $this->config = $this->buildSiteConfig($url, $html); | ||
158 | |||
159 | // do string replacements | ||
160 | if (!empty($this->config->find_string)) { | ||
161 | if (count($this->config->find_string) == count($this->config->replace_string)) { | ||
162 | $html = str_replace($this->config->find_string, $this->config->replace_string, $html, $_count); | ||
163 | $this->debug("Strings replaced: $_count (find_string and/or replace_string)"); | ||
164 | } else { | ||
165 | $this->debug('Skipped string replacement - incorrect number of find-replace strings in site config'); | ||
166 | } | ||
167 | unset($_count); | ||
168 | } | ||
169 | |||
170 | // use tidy (if it exists)? | ||
171 | // This fixes problems with some sites which would otherwise | ||
172 | // trouble DOMDocument's HTML parsing. (Although sometimes it | ||
173 | // makes matters worse, which is why you can override it in site config files.) | ||
174 | $tidied = false; | ||
175 | if ($this->config->tidy() && function_exists('tidy_parse_string') && $smart_tidy) { | ||
176 | $this->debug('Using Tidy'); | ||
177 | $tidy = tidy_parse_string($html, self::$tidy_config, 'UTF8'); | ||
178 | if (tidy_clean_repair($tidy)) { | ||
179 | $original_html = $html; | ||
180 | $tidied = true; | ||
181 | $html = $tidy->value; | ||
182 | } | ||
183 | unset($tidy); | ||
184 | } | ||
185 | |||
186 | // load and parse html | ||
187 | $_parser = $this->config->parser(); | ||
188 | if (!in_array($_parser, $this->allowedParsers)) { | ||
189 | $this->debug("HTML parser $_parser not listed, using libxml instead"); | ||
190 | $_parser = 'libxml'; | ||
191 | } | ||
192 | $this->debug("Attempting to parse HTML with $_parser"); | ||
193 | $this->readability = new Readability($html, $url, $_parser); | ||
194 | |||
195 | // we use xpath to find elements in the given HTML document | ||
196 | // see http://en.wikipedia.org/wiki/XPath_1.0 | ||
197 | $xpath = new DOMXPath($this->readability->dom); | ||
198 | |||
199 | // try to get next page link | ||
200 | foreach ($this->config->next_page_link as $pattern) { | ||
201 | $elems = @$xpath->evaluate($pattern, $this->readability->dom); | ||
202 | if (is_string($elems)) { | ||
203 | $this->nextPageUrl = trim($elems); | ||
204 | break; | ||
205 | } elseif ($elems instanceof DOMNodeList && $elems->length > 0) { | ||
206 | foreach ($elems as $item) { | ||
207 | if ($item instanceof DOMElement && $item->hasAttribute('href')) { | ||
208 | $this->nextPageUrl = $item->getAttribute('href'); | ||
209 | break 2; | ||
210 | } elseif ($item instanceof DOMAttr && $item->value) { | ||
211 | $this->nextPageUrl = $item->value; | ||
212 | break 2; | ||
213 | } | ||
214 | } | ||
215 | } | ||
216 | } | ||
217 | |||
218 | // try to get title | ||
219 | foreach ($this->config->title as $pattern) { | ||
220 | // $this->debug("Trying $pattern"); | ||
221 | $elems = @$xpath->evaluate($pattern, $this->readability->dom); | ||
222 | if (is_string($elems)) { | ||
223 | $this->title = trim($elems); | ||
224 | $this->debug('Title expression evaluated as string: '.$this->title); | ||
225 | $this->debug("...XPath match: $pattern"); | ||
226 | break; | ||
227 | } elseif ($elems instanceof DOMNodeList && $elems->length > 0) { | ||
228 | $this->title = $elems->item(0)->textContent; | ||
229 | $this->debug('Title matched: '.$this->title); | ||
230 | $this->debug("...XPath match: $pattern"); | ||
231 | // remove title from document | ||
232 | try { | ||
233 | $elems->item(0)->parentNode->removeChild($elems->item(0)); | ||
234 | } catch (DOMException $e) { | ||
235 | // do nothing | ||
236 | } | ||
237 | break; | ||
238 | } | ||
239 | } | ||
240 | |||
241 | // try to get author (if it hasn't already been set) | ||
242 | if (empty($this->author)) { | ||
243 | foreach ($this->config->author as $pattern) { | ||
244 | $elems = @$xpath->evaluate($pattern, $this->readability->dom); | ||
245 | if (is_string($elems)) { | ||
246 | if (trim($elems) != '') { | ||
247 | $this->author[] = trim($elems); | ||
248 | $this->debug('Author expression evaluated as string: '.trim($elems)); | ||
249 | $this->debug("...XPath match: $pattern"); | ||
250 | break; | ||
251 | } | ||
252 | } elseif ($elems instanceof DOMNodeList && $elems->length > 0) { | ||
253 | foreach ($elems as $elem) { | ||
254 | if (!isset($elem->parentNode)) continue; | ||
255 | $this->author[] = trim($elem->textContent); | ||
256 | $this->debug('Author matched: '.trim($elem->textContent)); | ||
257 | } | ||
258 | if (!empty($this->author)) { | ||
259 | $this->debug("...XPath match: $pattern"); | ||
260 | break; | ||
261 | } | ||
262 | } | ||
263 | } | ||
264 | } | ||
265 | |||
266 | // try to get language | ||
267 | $_lang_xpath = array('//html[@lang]/@lang', '//meta[@name="DC.language"]/@content'); | ||
268 | foreach ($_lang_xpath as $pattern) { | ||
269 | $elems = @$xpath->evaluate($pattern, $this->readability->dom); | ||
270 | if (is_string($elems)) { | ||
271 | if (trim($elems) != '') { | ||
272 | $this->language = trim($elems); | ||
273 | $this->debug('Language matched: '.$this->language); | ||
274 | break; | ||
275 | } | ||
276 | } elseif ($elems instanceof DOMNodeList && $elems->length > 0) { | ||
277 | foreach ($elems as $elem) { | ||
278 | if (!isset($elem->parentNode)) continue; | ||
279 | $this->language = trim($elem->textContent); | ||
280 | $this->debug('Language matched: '.$this->language); | ||
281 | } | ||
282 | if ($this->language) break; | ||
283 | } | ||
284 | } | ||
285 | |||
286 | // try to get date | ||
287 | foreach ($this->config->date as $pattern) { | ||
288 | $elems = @$xpath->evaluate($pattern, $this->readability->dom); | ||
289 | if (is_string($elems)) { | ||
290 | $this->date = strtotime(trim($elems, "; \t\n\r\0\x0B")); | ||
291 | } elseif ($elems instanceof DOMNodeList && $elems->length > 0) { | ||
292 | $this->date = $elems->item(0)->textContent; | ||
293 | $this->date = strtotime(trim($this->date, "; \t\n\r\0\x0B")); | ||
294 | // remove date from document | ||
295 | // $elems->item(0)->parentNode->removeChild($elems->item(0)); | ||
296 | } | ||
297 | if (!$this->date) { | ||
298 | $this->date = null; | ||
299 | } else { | ||
300 | $this->debug('Date matched: '.date('Y-m-d H:i:s', $this->date)); | ||
301 | $this->debug("...XPath match: $pattern"); | ||
302 | break; | ||
303 | } | ||
304 | } | ||
305 | |||
306 | // strip elements (using xpath expressions) | ||
307 | foreach ($this->config->strip as $pattern) { | ||
308 | $elems = @$xpath->query($pattern, $this->readability->dom); | ||
309 | // check for matches | ||
310 | if ($elems && $elems->length > 0) { | ||
311 | $this->debug('Stripping '.$elems->length.' elements (strip)'); | ||
312 | for ($i=$elems->length-1; $i >= 0; $i--) { | ||
313 | $elems->item($i)->parentNode->removeChild($elems->item($i)); | ||
314 | } | ||
315 | } | ||
316 | } | ||
317 | |||
318 | // strip elements (using id and class attribute values) | ||
319 | foreach ($this->config->strip_id_or_class as $string) { | ||
320 | $string = strtr($string, array("'"=>'', '"'=>'')); | ||
321 | $elems = @$xpath->query("//*[contains(@class, '$string') or contains(@id, '$string')]", $this->readability->dom); | ||
322 | // check for matches | ||
323 | if ($elems && $elems->length > 0) { | ||
324 | $this->debug('Stripping '.$elems->length.' elements (strip_id_or_class)'); | ||
325 | for ($i=$elems->length-1; $i >= 0; $i--) { | ||
326 | $elems->item($i)->parentNode->removeChild($elems->item($i)); | ||
327 | } | ||
328 | } | ||
329 | } | ||
330 | |||
331 | // strip images (using src attribute values) | ||
332 | foreach ($this->config->strip_image_src as $string) { | ||
333 | $string = strtr($string, array("'"=>'', '"'=>'')); | ||
334 | $elems = @$xpath->query("//img[contains(@src, '$string')]", $this->readability->dom); | ||
335 | // check for matches | ||
336 | if ($elems && $elems->length > 0) { | ||
337 | $this->debug('Stripping '.$elems->length.' image elements'); | ||
338 | for ($i=$elems->length-1; $i >= 0; $i--) { | ||
339 | $elems->item($i)->parentNode->removeChild($elems->item($i)); | ||
340 | } | ||
341 | } | ||
342 | } | ||
343 | // strip elements using Readability.com and Instapaper.com ignore class names | ||
344 | // .entry-unrelated and .instapaper_ignore | ||
345 | // See https://www.readability.com/publishers/guidelines/#view-plainGuidelines | ||
346 | // and http://blog.instapaper.com/post/730281947 | ||
347 | $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' entry-unrelated ') or contains(concat(' ',normalize-space(@class),' '),' instapaper_ignore ')]", $this->readability->dom); | ||
348 | // check for matches | ||
349 | if ($elems && $elems->length > 0) { | ||
350 | $this->debug('Stripping '.$elems->length.' .entry-unrelated,.instapaper_ignore elements'); | ||
351 | for ($i=$elems->length-1; $i >= 0; $i--) { | ||
352 | $elems->item($i)->parentNode->removeChild($elems->item($i)); | ||
353 | } | ||
354 | } | ||
355 | |||
356 | // strip elements that contain style="display: none;" | ||
357 | $elems = @$xpath->query("//*[contains(@style,'display:none')]", $this->readability->dom); | ||
358 | // check for matches | ||
359 | if ($elems && $elems->length > 0) { | ||
360 | $this->debug('Stripping '.$elems->length.' elements with inline display:none style'); | ||
361 | for ($i=$elems->length-1; $i >= 0; $i--) { | ||
362 | $elems->item($i)->parentNode->removeChild($elems->item($i)); | ||
363 | } | ||
364 | } | ||
365 | |||
366 | // try to get body | ||
367 | foreach ($this->config->body as $pattern) { | ||
368 | $elems = @$xpath->query($pattern, $this->readability->dom); | ||
369 | // check for matches | ||
370 | if ($elems && $elems->length > 0) { | ||
371 | $this->debug('Body matched'); | ||
372 | $this->debug("...XPath match: $pattern"); | ||
373 | if ($elems->length == 1) { | ||
374 | $this->body = $elems->item(0); | ||
375 | // prune (clean up elements that may not be content) | ||
376 | if ($this->config->prune()) { | ||
377 | $this->debug('...pruning content'); | ||
378 | $this->readability->prepArticle($this->body); | ||
379 | } | ||
380 | break; | ||
381 | } else { | ||
382 | $this->body = $this->readability->dom->createElement('div'); | ||
383 | $this->debug($elems->length.' body elems found'); | ||
384 | foreach ($elems as $elem) { | ||
385 | if (!isset($elem->parentNode)) continue; | ||
386 | $isDescendant = false; | ||
387 | foreach ($this->body->childNodes as $parent) { | ||
388 | if ($this->isDescendant($parent, $elem)) { | ||
389 | $isDescendant = true; | ||
390 | break; | ||
391 | } | ||
392 | } | ||
393 | if ($isDescendant) { | ||
394 | $this->debug('...element is child of another body element, skipping.'); | ||
395 | } else { | ||
396 | // prune (clean up elements that may not be content) | ||
397 | if ($this->config->prune()) { | ||
398 | $this->debug('Pruning content'); | ||
399 | $this->readability->prepArticle($elem); | ||
400 | } | ||
401 | $this->debug('...element added to body'); | ||
402 | $this->body->appendChild($elem); | ||
403 | } | ||
404 | } | ||
405 | if ($this->body->hasChildNodes()) break; | ||
406 | } | ||
407 | } | ||
408 | } | ||
409 | |||
410 | // auto detect? | ||
411 | $detect_title = $detect_body = $detect_author = $detect_date = false; | ||
412 | // detect title? | ||
413 | if (!isset($this->title)) { | ||
414 | if (empty($this->config->title) || $this->config->autodetect_on_failure()) { | ||
415 | $detect_title = true; | ||
416 | } | ||
417 | } | ||
418 | // detect body? | ||
419 | if (!isset($this->body)) { | ||
420 | if (empty($this->config->body) || $this->config->autodetect_on_failure()) { | ||
421 | $detect_body = true; | ||
422 | } | ||
423 | } | ||
424 | // detect author? | ||
425 | if (empty($this->author)) { | ||
426 | if (empty($this->config->author) || $this->config->autodetect_on_failure()) { | ||
427 | $detect_author = true; | ||
428 | } | ||
429 | } | ||
430 | // detect date? | ||
431 | if (!isset($this->date)) { | ||
432 | if (empty($this->config->date) || $this->config->autodetect_on_failure()) { | ||
433 | $detect_date = true; | ||
434 | } | ||
435 | } | ||
436 | |||
437 | // check for hNews | ||
438 | if ($detect_title || $detect_body) { | ||
439 | // check for hentry | ||
440 | $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' hentry ')]", $this->readability->dom); | ||
441 | if ($elems && $elems->length > 0) { | ||
442 | $this->debug('hNews: found hentry'); | ||
443 | $hentry = $elems->item(0); | ||
444 | |||
445 | if ($detect_title) { | ||
446 | // check for entry-title | ||
447 | $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' entry-title ')]", $hentry); | ||
448 | if ($elems && $elems->length > 0) { | ||
449 | $this->title = $elems->item(0)->textContent; | ||
450 | $this->debug('hNews: found entry-title: '.$this->title); | ||
451 | // remove title from document | ||
452 | $elems->item(0)->parentNode->removeChild($elems->item(0)); | ||
453 | $detect_title = false; | ||
454 | } | ||
455 | } | ||
456 | |||
457 | if ($detect_date) { | ||
458 | // check for time element with pubdate attribute | ||
459 | $elems = @$xpath->query(".//time[@pubdate] | .//abbr[contains(concat(' ',normalize-space(@class),' '),' published ')]", $hentry); | ||
460 | if ($elems && $elems->length > 0) { | ||
461 | $this->date = strtotime(trim($elems->item(0)->textContent)); | ||
462 | // remove date from document | ||
463 | //$elems->item(0)->parentNode->removeChild($elems->item(0)); | ||
464 | if ($this->date) { | ||
465 | $this->debug('hNews: found publication date: '.date('Y-m-d H:i:s', $this->date)); | ||
466 | $detect_date = false; | ||
467 | } else { | ||
468 | $this->date = null; | ||
469 | } | ||
470 | } | ||
471 | } | ||
472 | |||
473 | if ($detect_author) { | ||
474 | // check for time element with pubdate attribute | ||
475 | $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' vcard ') and (contains(concat(' ',normalize-space(@class),' '),' author ') or contains(concat(' ',normalize-space(@class),' '),' byline '))]", $hentry); | ||
476 | if ($elems && $elems->length > 0) { | ||
477 | $author = $elems->item(0); | ||
478 | $fn = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' fn ')]", $author); | ||
479 | if ($fn && $fn->length > 0) { | ||
480 | foreach ($fn as $_fn) { | ||
481 | if (trim($_fn->textContent) != '') { | ||
482 | $this->author[] = trim($_fn->textContent); | ||
483 | $this->debug('hNews: found author: '.trim($_fn->textContent)); | ||
484 | } | ||
485 | } | ||
486 | } else { | ||
487 | if (trim($author->textContent) != '') { | ||
488 | $this->author[] = trim($author->textContent); | ||
489 | $this->debug('hNews: found author: '.trim($author->textContent)); | ||
490 | } | ||
491 | } | ||
492 | $detect_author = empty($this->author); | ||
493 | } | ||
494 | } | ||
495 | |||
496 | // check for entry-content. | ||
497 | // according to hAtom spec, if there are multiple elements marked entry-content, | ||
498 | // we include all of these in the order they appear - see http://microformats.org/wiki/hatom#Entry_Content | ||
499 | if ($detect_body) { | ||
500 | $elems = @$xpath->query(".//*[contains(concat(' ',normalize-space(@class),' '),' entry-content ')]", $hentry); | ||
501 | if ($elems && $elems->length > 0) { | ||
502 | $this->debug('hNews: found entry-content'); | ||
503 | if ($elems->length == 1) { | ||
504 | // what if it's empty? (some sites misuse hNews - place their content outside an empty entry-content element) | ||
505 | $e = $elems->item(0); | ||
506 | if (($e->tagName == 'img') || (trim($e->textContent) != '')) { | ||
507 | $this->body = $elems->item(0); | ||
508 | // prune (clean up elements that may not be content) | ||
509 | if ($this->config->prune()) { | ||
510 | $this->debug('Pruning content'); | ||
511 | $this->readability->prepArticle($this->body); | ||
512 | } | ||
513 | $detect_body = false; | ||
514 | } else { | ||
515 | $this->debug('hNews: skipping entry-content - appears not to contain content'); | ||
516 | } | ||
517 | unset($e); | ||
518 | } else { | ||
519 | $this->body = $this->readability->dom->createElement('div'); | ||
520 | $this->debug($elems->length.' entry-content elems found'); | ||
521 | foreach ($elems as $elem) { | ||
522 | if (!isset($elem->parentNode)) continue; | ||
523 | $isDescendant = false; | ||
524 | foreach ($this->body->childNodes as $parent) { | ||
525 | if ($this->isDescendant($parent, $elem)) { | ||
526 | $isDescendant = true; | ||
527 | break; | ||
528 | } | ||
529 | } | ||
530 | if ($isDescendant) { | ||
531 | $this->debug('Element is child of another body element, skipping.'); | ||
532 | } else { | ||
533 | // prune (clean up elements that may not be content) | ||
534 | if ($this->config->prune()) { | ||
535 | $this->debug('Pruning content'); | ||
536 | $this->readability->prepArticle($elem); | ||
537 | } | ||
538 | $this->debug('Element added to body'); | ||
539 | $this->body->appendChild($elem); | ||
540 | } | ||
541 | } | ||
542 | $detect_body = false; | ||
543 | } | ||
544 | } | ||
545 | } | ||
546 | } | ||
547 | } | ||
548 | |||
549 | // check for elements marked with instapaper_title | ||
550 | if ($detect_title) { | ||
551 | // check for instapaper_title | ||
552 | $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' instapaper_title ')]", $this->readability->dom); | ||
553 | if ($elems && $elems->length > 0) { | ||
554 | $this->title = $elems->item(0)->textContent; | ||
555 | $this->debug('Title found (.instapaper_title): '.$this->title); | ||
556 | // remove title from document | ||
557 | $elems->item(0)->parentNode->removeChild($elems->item(0)); | ||
558 | $detect_title = false; | ||
559 | } | ||
560 | } | ||
561 | // check for elements marked with instapaper_body | ||
562 | if ($detect_body) { | ||
563 | $elems = @$xpath->query("//*[contains(concat(' ',normalize-space(@class),' '),' instapaper_body ')]", $this->readability->dom); | ||
564 | if ($elems && $elems->length > 0) { | ||
565 | $this->debug('body found (.instapaper_body)'); | ||
566 | $this->body = $elems->item(0); | ||
567 | // prune (clean up elements that may not be content) | ||
568 | if ($this->config->prune()) { | ||
569 | $this->debug('Pruning content'); | ||
570 | $this->readability->prepArticle($this->body); | ||
571 | } | ||
572 | $detect_body = false; | ||
573 | } | ||
574 | } | ||
575 | |||
576 | // Find author in rel="author" marked element | ||
577 | // We only use this if there's exactly one. | ||
578 | // If there's more than one, it could indicate more than | ||
579 | // one author, but it could also indicate that we're processing | ||
580 | // a page listing different articles with different authors. | ||
581 | if ($detect_author) { | ||
582 | $elems = @$xpath->query("//a[contains(concat(' ',normalize-space(@rel),' '),' author ')]", $this->readability->dom); | ||
583 | if ($elems && $elems->length == 1) { | ||
584 | $author = trim($elems->item(0)->textContent); | ||
585 | if ($author != '') { | ||
586 | $this->debug("Author found (rel=\"author\"): $author"); | ||
587 | $this->author[] = $author; | ||
588 | $detect_author = false; | ||
589 | } | ||
590 | } | ||
591 | } | ||
592 | |||
593 | // Find date in pubdate marked time element | ||
594 | // For the same reason given above, we only use this | ||
595 | // if there's exactly one element. | ||
596 | if ($detect_date) { | ||
597 | $elems = @$xpath->query("//time[@pubdate]", $this->readability->dom); | ||
598 | if ($elems && $elems->length == 1) { | ||
599 | $this->date = strtotime(trim($elems->item(0)->textContent)); | ||
600 | // remove date from document | ||
601 | //$elems->item(0)->parentNode->removeChild($elems->item(0)); | ||
602 | if ($this->date) { | ||
603 | $this->debug('Date found (pubdate marked time element): '.date('Y-m-d H:i:s', $this->date)); | ||
604 | $detect_date = false; | ||
605 | } else { | ||
606 | $this->date = null; | ||
607 | } | ||
608 | } | ||
609 | } | ||
610 | |||
611 | // still missing title or body, so we detect using Readability | ||
612 | if ($detect_title || $detect_body) { | ||
613 | $this->debug('Using Readability'); | ||
614 | // clone body if we're only using Readability for title (otherwise it may interfere with body element) | ||
615 | if (isset($this->body)) $this->body = $this->body->cloneNode(true); | ||
616 | $success = $this->readability->init(); | ||
617 | } | ||
618 | if ($detect_title) { | ||
619 | $this->debug('Detecting title'); | ||
620 | $this->title = $this->readability->getTitle()->textContent; | ||
621 | } | ||
622 | if ($detect_body && $success) { | ||
623 | $this->debug('Detecting body'); | ||
624 | $this->body = $this->readability->getContent(); | ||
625 | if ($this->body->childNodes->length == 1 && $this->body->firstChild->nodeType === XML_ELEMENT_NODE) { | ||
626 | $this->body = $this->body->firstChild; | ||
627 | } | ||
628 | // prune (clean up elements that may not be content) | ||
629 | if ($this->config->prune()) { | ||
630 | $this->debug('Pruning content'); | ||
631 | $this->readability->prepArticle($this->body); | ||
632 | } | ||
633 | } | ||
634 | if (isset($this->body)) { | ||
635 | // remove scripts | ||
636 | $this->readability->removeScripts($this->body); | ||
637 | // remove any h1-h6 elements that appear as first thing in the body | ||
638 | // and which match our title | ||
639 | if (isset($this->title) && ($this->title != '')) { | ||
640 | $firstChild = $this->body->firstChild; | ||
641 | while ($firstChild->nodeType && ($firstChild->nodeType !== XML_ELEMENT_NODE)) { | ||
642 | $firstChild = $firstChild->nextSibling; | ||
643 | } | ||
644 | if (($firstChild->nodeType === XML_ELEMENT_NODE) | ||
645 | && in_array(strtolower($firstChild->tagName), array('h1', 'h2', 'h3', 'h4', 'h5', 'h6')) | ||
646 | && (strtolower(trim($firstChild->textContent)) == strtolower(trim($this->title)))) { | ||
647 | $this->body->removeChild($firstChild); | ||
648 | } | ||
649 | } | ||
650 | // prevent self-closing iframes | ||
651 | $elems = $this->body->getElementsByTagName('iframe'); | ||
652 | for ($i = $elems->length-1; $i >= 0; $i--) { | ||
653 | $e = $elems->item($i); | ||
654 | if (!$e->hasChildNodes()) { | ||
655 | $e->appendChild($this->body->ownerDocument->createTextNode('[embedded content]')); | ||
656 | } | ||
657 | } | ||
658 | // remove image lazy loading - WordPress plugin http://wordpress.org/extend/plugins/lazy-load/ | ||
659 | // the plugin replaces the src attribute to point to a 1x1 gif and puts the original src | ||
660 | // inside the data-lazy-src attribute. It also places the original image inside a noscript element | ||
661 | // next to the amended one. | ||
662 | $elems = @$xpath->query("//img[@data-lazy-src]", $this->body); | ||
663 | for ($i = $elems->length-1; $i >= 0; $i--) { | ||
664 | $e = $elems->item($i); | ||
665 | // let's see if we can grab image from noscript | ||
666 | if ($e->nextSibling !== null && $e->nextSibling->nodeName === 'noscript') { | ||
667 | $_new_elem = $e->ownerDocument->createDocumentFragment(); | ||
668 | @$_new_elem->appendXML($e->nextSibling->innerHTML); | ||
669 | $e->nextSibling->parentNode->replaceChild($_new_elem, $e->nextSibling); | ||
670 | $e->parentNode->removeChild($e); | ||
671 | } else { | ||
672 | // Use data-lazy-src as src value | ||
673 | $e->setAttribute('src', $e->getAttribute('data-lazy-src')); | ||
674 | $e->removeAttribute('data-lazy-src'); | ||
675 | } | ||
676 | } | ||
677 | |||
678 | $this->success = true; | ||
679 | } | ||
680 | |||
681 | // if we've had no success and we've used tidy, there's a chance | ||
682 | // that tidy has messed up. So let's try again without tidy... | ||
683 | if (!$this->success && $tidied && $smart_tidy) { | ||
684 | $this->debug('Trying again without tidy'); | ||
685 | $this->process($original_html, $url, false); | ||
686 | } | ||
687 | |||
688 | return $this->success; | ||
689 | } | ||
690 | |||
691 | private function isDescendant(DOMElement $parent, DOMElement $child) { | ||
692 | $node = $child->parentNode; | ||
693 | while ($node != null) { | ||
694 | if ($node->isSameNode($parent)) return true; | ||
695 | $node = $node->parentNode; | ||
696 | } | ||
697 | return false; | ||
698 | } | ||
699 | |||
700 | public function getContent() { | ||
701 | return $this->body; | ||
702 | } | ||
703 | |||
704 | public function getTitle() { | ||
705 | return $this->title; | ||
706 | } | ||
707 | |||
708 | public function getAuthors() { | ||
709 | return $this->author; | ||
710 | } | ||
711 | |||
712 | public function getLanguage() { | ||
713 | return $this->language; | ||
714 | } | ||
715 | |||
716 | public function getDate() { | ||
717 | return $this->date; | ||
718 | } | ||
719 | |||
720 | public function getSiteConfig() { | ||
721 | return $this->config; | ||
722 | } | ||
723 | |||
724 | public function getNextPageUrl() { | ||
725 | return $this->nextPageUrl; | ||
726 | } | ||
727 | } | ||
728 | ?> \ No newline at end of file | ||