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