diff options
Diffstat (limited to 'inc/3rdparty/libraries/content-extractor')
-rw-r--r-- | inc/3rdparty/libraries/content-extractor/ContentExtractor.php | 728 | ||||
-rw-r--r-- | inc/3rdparty/libraries/content-extractor/SiteConfig.php | 338 |
2 files changed, 1066 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 | ||
diff --git a/inc/3rdparty/libraries/content-extractor/SiteConfig.php b/inc/3rdparty/libraries/content-extractor/SiteConfig.php new file mode 100644 index 00000000..c5e300d7 --- /dev/null +++ b/inc/3rdparty/libraries/content-extractor/SiteConfig.php | |||
@@ -0,0 +1,338 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Site Config | ||
4 | * | ||
5 | * Each instance of this class should hold extraction patterns and other directives | ||
6 | * for a website. See ContentExtractor class to see how it's used. | ||
7 | * | ||
8 | * @version 0.7 | ||
9 | * @date 2012-08-27 | ||
10 | * @author Keyvan Minoukadeh | ||
11 | * @copyright 2012 Keyvan Minoukadeh | ||
12 | * @license http://www.gnu.org/licenses/agpl-3.0.html AGPL v3 | ||
13 | */ | ||
14 | |||
15 | class SiteConfig | ||
16 | { | ||
17 | // Use first matching element as title (0 or more xpath expressions) | ||
18 | public $title = array(); | ||
19 | |||
20 | // Use first matching element as body (0 or more xpath expressions) | ||
21 | public $body = array(); | ||
22 | |||
23 | // Use first matching element as author (0 or more xpath expressions) | ||
24 | public $author = array(); | ||
25 | |||
26 | // Use first matching element as date (0 or more xpath expressions) | ||
27 | public $date = array(); | ||
28 | |||
29 | // Strip elements matching these xpath expressions (0 or more) | ||
30 | public $strip = array(); | ||
31 | |||
32 | // Strip elements which contain these strings (0 or more) in the id or class attribute | ||
33 | public $strip_id_or_class = array(); | ||
34 | |||
35 | // Strip images which contain these strings (0 or more) in the src attribute | ||
36 | public $strip_image_src = array(); | ||
37 | |||
38 | // Additional HTTP headers to send | ||
39 | // NOT YET USED | ||
40 | public $http_header = array(); | ||
41 | |||
42 | // Process HTML with tidy before creating DOM (bool or null if undeclared) | ||
43 | public $tidy = null; | ||
44 | |||
45 | protected $default_tidy = true; // used if undeclared | ||
46 | |||
47 | // Autodetect title/body if xpath expressions fail to produce results. | ||
48 | // Note that this applies to title and body separately, ie. | ||
49 | // * if we get a body match but no title match, this option will determine whether we autodetect title | ||
50 | // * if neither match, this determines whether we autodetect title and body. | ||
51 | // Also note that this only applies when there is at least one xpath expression in title or body, ie. | ||
52 | // * if title and body are both empty (no xpath expressions), this option has no effect (both title and body will be auto-detected) | ||
53 | // * if there's an xpath expression for title and none for body, body will be auto-detected and this option will determine whether we auto-detect title if the xpath expression for it fails to produce results. | ||
54 | // Usage scenario: you want to extract something specific from a set of URLs, e.g. a table, and if the table is not found, you want to ignore the entry completely. Auto-detection is unlikely to succeed here, so you construct your patterns and set this option to false. Another scenario may be a site where auto-detection has proven to fail (or worse, picked up the wrong content). | ||
55 | // bool or null if undeclared | ||
56 | public $autodetect_on_failure = null; | ||
57 | protected $default_autodetect_on_failure = true; // used if undeclared | ||
58 | |||
59 | // Clean up content block - attempt to remove elements that appear to be superfluous | ||
60 | // bool or null if undeclared | ||
61 | public $prune = null; | ||
62 | protected $default_prune = true; // used if undeclared | ||
63 | |||
64 | // Test URL - if present, can be used to test the config above | ||
65 | public $test_url = array(); | ||
66 | |||
67 | // Single-page link - should identify a link element or URL pointing to the page holding the entire article | ||
68 | // This is useful for sites which split their articles across multiple pages. Links to such pages tend to | ||
69 | // display the first page with links to the other pages at the bottom. Often there is also a link to a page | ||
70 | // which displays the entire article on one page (e.g. 'print view'). | ||
71 | // This should be an XPath expression identifying the link to that page. If present and we find a match, | ||
72 | // we will retrieve that page and the rest of the options in this config will be applied to the new page. | ||
73 | public $single_page_link = array(); | ||
74 | |||
75 | public $next_page_link = array(); | ||
76 | |||
77 | // Single-page link in feed? - same as above, but patterns applied to item description HTML taken from feed | ||
78 | public $single_page_link_in_feed = array(); | ||
79 | |||
80 | // Which parser to use for turning raw HTML into a DOMDocument (either 'libxml' or 'html5lib') | ||
81 | // string or null if undeclared | ||
82 | public $parser = null; | ||
83 | protected $default_parser = 'libxml'; // used if undeclared | ||
84 | |||
85 | // Strings to search for in HTML before processing begins (used with $replace_string) | ||
86 | public $find_string = array(); | ||
87 | // Strings to replace those found in $find_string before HTML processing begins | ||
88 | public $replace_string = array(); | ||
89 | |||
90 | // the options below cannot be set in the config files which this class represents | ||
91 | |||
92 | //public $cache_in_apc = false; // used to decide if we should cache in apc or not | ||
93 | public $cache_key = null; | ||
94 | public static $debug = false; | ||
95 | protected static $apc = false; | ||
96 | protected static $config_path; | ||
97 | protected static $config_path_fallback; | ||
98 | protected static $config_cache = array(); | ||
99 | const HOSTNAME_REGEX = '/^(([a-zA-Z0-9-]*[a-zA-Z0-9])\.)*([A-Za-z0-9-]*[A-Za-z0-9])$/'; | ||
100 | |||
101 | protected static function debug($msg) { | ||
102 | if (self::$debug) { | ||
103 | //$mem = round(memory_get_usage()/1024, 2); | ||
104 | //$memPeak = round(memory_get_peak_usage()/1024, 2); | ||
105 | echo '* ',$msg; | ||
106 | //echo ' - mem used: ',$mem," (peak: $memPeak)\n"; | ||
107 | echo "\n"; | ||
108 | ob_flush(); | ||
109 | flush(); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | // enable APC caching of certain site config files? | ||
114 | // If enabled the following site config files will be | ||
115 | // cached in APC cache (when requested for first time): | ||
116 | // * anything in site_config/custom/ and its corresponding file in site_config/standard/ | ||
117 | // * the site config files associated with HTML fingerprints | ||
118 | // * the global site config file | ||
119 | // returns true if enabled, false otherwise | ||
120 | public static function use_apc($apc=true) { | ||
121 | if (!function_exists('apc_add')) { | ||
122 | if ($apc) self::debug('APC will not be used (function apc_add does not exist)'); | ||
123 | return false; | ||
124 | } | ||
125 | self::$apc = $apc; | ||
126 | return $apc; | ||
127 | } | ||
128 | |||
129 | // return bool or null | ||
130 | public function tidy($use_default=true) { | ||
131 | if ($use_default) return (isset($this->tidy)) ? $this->tidy : $this->default_tidy; | ||
132 | return $this->tidy; | ||
133 | } | ||
134 | |||
135 | // return bool or null | ||
136 | public function prune($use_default=true) { | ||
137 | if ($use_default) return (isset($this->prune)) ? $this->prune : $this->default_prune; | ||
138 | return $this->prune; | ||
139 | } | ||
140 | |||
141 | // return string or null | ||
142 | public function parser($use_default=true) { | ||
143 | if ($use_default) return (isset($this->parser)) ? $this->parser : $this->default_parser; | ||
144 | return $this->parser; | ||
145 | } | ||
146 | |||
147 | // return bool or null | ||
148 | public function autodetect_on_failure($use_default=true) { | ||
149 | if ($use_default) return (isset($this->autodetect_on_failure)) ? $this->autodetect_on_failure : $this->default_autodetect_on_failure; | ||
150 | return $this->autodetect_on_failure; | ||
151 | } | ||
152 | |||
153 | public static function set_config_path($path, $fallback=null) { | ||
154 | self::$config_path = $path; | ||
155 | self::$config_path_fallback = $fallback; | ||
156 | } | ||
157 | |||
158 | public static function add_to_cache($key, SiteConfig $config, $use_apc=true) { | ||
159 | $key = strtolower($key); | ||
160 | if (substr($key, 0, 4) == 'www.') $key = substr($key, 4); | ||
161 | if ($config->cache_key) $key = $config->cache_key; | ||
162 | self::$config_cache[$key] = $config; | ||
163 | if (self::$apc && $use_apc) { | ||
164 | self::debug("Adding site config to APC cache with key sc.$key"); | ||
165 | apc_add("sc.$key", $config); | ||
166 | } | ||
167 | self::debug("Cached site config with key $key"); | ||
168 | } | ||
169 | |||
170 | public static function is_cached($key) { | ||
171 | $key = strtolower($key); | ||
172 | if (substr($key, 0, 4) == 'www.') $key = substr($key, 4); | ||
173 | if (array_key_exists($key, self::$config_cache)) { | ||
174 | return true; | ||
175 | } elseif (self::$apc && (bool)apc_fetch("sc.$key")) { | ||
176 | return true; | ||
177 | } | ||
178 | return false; | ||
179 | } | ||
180 | |||
181 | public function append(SiteConfig $newconfig) { | ||
182 | // check for commands where we accept multiple statements (no test_url) | ||
183 | foreach (array('title', 'body', 'author', 'date', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'single_page_link_in_feed', 'next_page_link', 'http_header', 'find_string', 'replace_string') as $var) { | ||
184 | // append array elements for this config variable from $newconfig to this config | ||
185 | //$this->$var = $this->$var + $newconfig->$var; | ||
186 | $this->$var = array_unique(array_merge($this->$var, $newconfig->$var)); | ||
187 | } | ||
188 | // check for single statement commands | ||
189 | // we do not overwrite existing non null values | ||
190 | foreach (array('tidy', 'prune', 'parser', 'autodetect_on_failure') as $var) { | ||
191 | if ($this->$var === null) $this->$var = $newconfig->$var; | ||
192 | } | ||
193 | } | ||
194 | |||
195 | // returns SiteConfig instance if an appropriate one is found, false otherwise | ||
196 | // if $exact_host_match is true, we will not look for wildcard config matches | ||
197 | // by default if host is 'test.example.org' we will look for and load '.example.org.txt' if it exists | ||
198 | public static function build($host, $exact_host_match=false) { | ||
199 | $host = strtolower($host); | ||
200 | if (substr($host, 0, 4) == 'www.') $host = substr($host, 4); | ||
201 | if (!$host || (strlen($host) > 200) || !preg_match(self::HOSTNAME_REGEX, ltrim($host, '.'))) return false; | ||
202 | // check for site configuration | ||
203 | $try = array($host); | ||
204 | // should we look for wildcard matches | ||
205 | if (!$exact_host_match) { | ||
206 | $split = explode('.', $host); | ||
207 | if (count($split) > 1) { | ||
208 | array_shift($split); | ||
209 | $try[] = '.'.implode('.', $split); | ||
210 | } | ||
211 | } | ||
212 | |||
213 | // look for site config file in primary folder | ||
214 | self::debug(". looking for site config for $host in primary folder"); | ||
215 | foreach ($try as $h) { | ||
216 | if (array_key_exists($h, self::$config_cache)) { | ||
217 | self::debug("... site config for $h already loaded in this request"); | ||
218 | return self::$config_cache[$h]; | ||
219 | } elseif (self::$apc && ($sconfig = apc_fetch("sc.$h"))) { | ||
220 | self::debug("... site config for $h in APC cache"); | ||
221 | return $sconfig; | ||
222 | } elseif (file_exists(self::$config_path."/$h.txt")) { | ||
223 | self::debug("... found site config ($h.txt)"); | ||
224 | $file_primary = self::$config_path."/$h.txt"; | ||
225 | $matched_name = $h; | ||
226 | break; | ||
227 | } | ||
228 | } | ||
229 | |||
230 | // if we found site config, process it | ||
231 | if (isset($file_primary)) { | ||
232 | $config_lines = file($file_primary, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | ||
233 | if (!$config_lines || !is_array($config_lines)) return false; | ||
234 | $config = self::build_from_array($config_lines); | ||
235 | // if APC caching is available and enabled, mark this for cache | ||
236 | //$config->cache_in_apc = true; | ||
237 | $config->cache_key = $matched_name; | ||
238 | |||
239 | // if autodetec on failure is off (on by default) we do not need to look | ||
240 | // in secondary folder | ||
241 | if (!$config->autodetect_on_failure()) { | ||
242 | self::debug('... autodetect on failure is disabled (no other site config files will be loaded)'); | ||
243 | return $config; | ||
244 | } | ||
245 | } | ||
246 | |||
247 | // look for site config file in secondary folder | ||
248 | if (isset(self::$config_path_fallback)) { | ||
249 | self::debug(". looking for site config for $host in secondary folder"); | ||
250 | foreach ($try as $h) { | ||
251 | if (file_exists(self::$config_path_fallback."/$h.txt")) { | ||
252 | self::debug("... found site config in secondary folder ($h.txt)"); | ||
253 | $file_secondary = self::$config_path_fallback."/$h.txt"; | ||
254 | $matched_name = $h; | ||
255 | break; | ||
256 | } | ||
257 | } | ||
258 | if (!isset($file_secondary)) { | ||
259 | self::debug("... no site config match in secondary folder"); | ||
260 | } | ||
261 | } | ||
262 | |||
263 | // return false if no config file found | ||
264 | if (!isset($file_primary) && !isset($file_secondary)) { | ||
265 | self::debug("... no site config match for $host"); | ||
266 | return false; | ||
267 | } | ||
268 | |||
269 | // return primary config if secondary not found | ||
270 | if (!isset($file_secondary) && isset($config)) { | ||
271 | return $config; | ||
272 | } | ||
273 | |||
274 | // process secondary config file | ||
275 | $config_lines = file($file_secondary, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | ||
276 | if (!$config_lines || !is_array($config_lines)) { | ||
277 | // failed to process secondary | ||
278 | if (isset($config)) { | ||
279 | // return primary config | ||
280 | return $config; | ||
281 | } else { | ||
282 | return false; | ||
283 | } | ||
284 | } | ||
285 | |||
286 | // merge with primary and return | ||
287 | if (isset($config)) { | ||
288 | self::debug('. merging config files'); | ||
289 | $config->append(self::build_from_array($config_lines)); | ||
290 | return $config; | ||
291 | } else { | ||
292 | // return just secondary | ||
293 | $config = self::build_from_array($config_lines); | ||
294 | // if APC caching is available and enabled, mark this for cache | ||
295 | //$config->cache_in_apc = true; | ||
296 | $config->cache_key = $matched_name; | ||
297 | return $config; | ||
298 | } | ||
299 | } | ||
300 | |||
301 | public static function build_from_array(array $lines) { | ||
302 | $config = new SiteConfig(); | ||
303 | foreach ($lines as $line) { | ||
304 | $line = trim($line); | ||
305 | |||
306 | // skip comments, empty lines | ||
307 | if ($line == '' || $line[0] == '#') continue; | ||
308 | |||
309 | // get command | ||
310 | $command = explode(':', $line, 2); | ||
311 | // if there's no colon ':', skip this line | ||
312 | if (count($command) != 2) continue; | ||
313 | $val = trim($command[1]); | ||
314 | $command = trim($command[0]); | ||
315 | if ($command == '' || $val == '') continue; | ||
316 | |||
317 | // check for commands where we accept multiple statements | ||
318 | if (in_array($command, array('title', 'body', 'author', 'date', 'strip', 'strip_id_or_class', 'strip_image_src', 'single_page_link', 'single_page_link_in_feed', 'next_page_link', 'http_header', 'test_url', 'find_string', 'replace_string'))) { | ||
319 | array_push($config->$command, $val); | ||
320 | // check for single statement commands that evaluate to true or false | ||
321 | } elseif (in_array($command, array('tidy', 'prune', 'autodetect_on_failure'))) { | ||
322 | $config->$command = ($val == 'yes'); | ||
323 | // check for single statement commands stored as strings | ||
324 | } elseif (in_array($command, array('parser'))) { | ||
325 | $config->$command = $val; | ||
326 | // check for replace_string(find): replace | ||
327 | } elseif ((substr($command, -1) == ')') && preg_match('!^([a-z0-9_]+)\((.*?)\)$!i', $command, $match)) { | ||
328 | if (in_array($match[1], array('replace_string'))) { | ||
329 | $command = $match[1]; | ||
330 | array_push($config->find_string, $match[2]); | ||
331 | array_push($config->$command, $val); | ||
332 | } | ||
333 | } | ||
334 | } | ||
335 | return $config; | ||
336 | } | ||
337 | } | ||
338 | ?> \ No newline at end of file | ||