diff options
Diffstat (limited to 'inc/3rdparty/makefulltextfeed.php')
-rwxr-xr-x | inc/3rdparty/makefulltextfeed.php | 349 |
1 files changed, 203 insertions, 146 deletions
diff --git a/inc/3rdparty/makefulltextfeed.php b/inc/3rdparty/makefulltextfeed.php index 4faad6d9..7a56be8c 100755 --- a/inc/3rdparty/makefulltextfeed.php +++ b/inc/3rdparty/makefulltextfeed.php | |||
@@ -3,8 +3,8 @@ | |||
3 | // Author: Keyvan Minoukadeh | 3 | // Author: Keyvan Minoukadeh |
4 | // Copyright (c) 2013 Keyvan Minoukadeh | 4 | // Copyright (c) 2013 Keyvan Minoukadeh |
5 | // License: AGPLv3 | 5 | // License: AGPLv3 |
6 | // Version: 3.1 | 6 | // Version: 3.2 |
7 | // Date: 2013-03-05 | 7 | // Date: 2013-05-13 |
8 | // More info: http://fivefilters.org/content-only/ | 8 | // More info: http://fivefilters.org/content-only/ |
9 | // Help: http://help.fivefilters.org | 9 | // Help: http://help.fivefilters.org |
10 | 10 | ||
@@ -25,12 +25,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
25 | 25 | ||
26 | // Usage | 26 | // Usage |
27 | // ----- | 27 | // ----- |
28 | // Request this file passing it your feed in the querystring: makefulltextfeed.php?url=mysite.org | 28 | // Request this file passing it a web page or feed URL in the querystring: makefulltextfeed.php?url=example.org/article |
29 | // The following options can be passed in the querystring: | 29 | // For more request parameters, see http://help.fivefilters.org/customer/portal/articles/226660-usage |
30 | // * URL: url=[feed or website url] (required, should be URL-encoded - in php: urlencode($url)) | ||
31 | // * URL points to HTML (not feed): html=true (optional, by default it's automatically detected) | ||
32 | // * API key: key=[api key] (optional, refer to config.php) | ||
33 | // * Max entries to process: max=[max number of items] (optional) | ||
34 | 30 | ||
35 | error_reporting(E_ALL ^ E_NOTICE); | 31 | error_reporting(E_ALL ^ E_NOTICE); |
36 | ini_set("display_errors", 1); | 32 | ini_set("display_errors", 1); |
@@ -76,8 +72,8 @@ header('X-Robots-Tag: noindex, nofollow'); | |||
76 | //////////////////////////////// | 72 | //////////////////////////////// |
77 | // Check if service is enabled | 73 | // Check if service is enabled |
78 | //////////////////////////////// | 74 | //////////////////////////////// |
79 | if (!$options->enabled) { | 75 | if (!$options->enabled) { |
80 | die('The full-text RSS service is currently disabled'); | 76 | die('The full-text RSS service is currently disabled'); |
81 | } | 77 | } |
82 | 78 | ||
83 | //////////////////////////////// | 79 | //////////////////////////////// |
@@ -121,8 +117,8 @@ $options->smart_cache = $options->smart_cache && function_exists('apc_inc'); | |||
121 | //////////////////////////////// | 117 | //////////////////////////////// |
122 | // Check for feed URL | 118 | // Check for feed URL |
123 | //////////////////////////////// | 119 | //////////////////////////////// |
124 | if (!isset($_GET['url'])) { | 120 | if (!isset($_GET['url'])) { |
125 | die('No URL supplied'); | 121 | die('No URL supplied'); |
126 | } | 122 | } |
127 | $url = trim($_GET['url']); | 123 | $url = trim($_GET['url']); |
128 | if (strtolower(substr($url, 0, 7)) == 'feed://') { | 124 | if (strtolower(substr($url, 0, 7)) == 'feed://') { |
@@ -161,10 +157,12 @@ if (isset($_GET['key']) && ($key_index = array_search($_GET['key'], $options->ap | |||
161 | if (isset($_GET['links'])) $redirect .= '&links='.urlencode($_GET['links']); | 157 | if (isset($_GET['links'])) $redirect .= '&links='.urlencode($_GET['links']); |
162 | if (isset($_GET['exc'])) $redirect .= '&exc='.urlencode($_GET['exc']); | 158 | if (isset($_GET['exc'])) $redirect .= '&exc='.urlencode($_GET['exc']); |
163 | if (isset($_GET['format'])) $redirect .= '&format='.urlencode($_GET['format']); | 159 | if (isset($_GET['format'])) $redirect .= '&format='.urlencode($_GET['format']); |
164 | if (isset($_GET['callback'])) $redirect .= '&callback='.urlencode($_GET['callback']); | 160 | if (isset($_GET['callback'])) $redirect .= '&callback='.urlencode($_GET['callback']); |
165 | if (isset($_GET['l'])) $redirect .= '&l='.urlencode($_GET['l']); | 161 | if (isset($_GET['l'])) $redirect .= '&l='.urlencode($_GET['l']); |
166 | if (isset($_GET['xss'])) $redirect .= '&xss'; | 162 | if (isset($_GET['xss'])) $redirect .= '&xss'; |
167 | if (isset($_GET['use_extracted_title'])) $redirect .= '&use_extracted_title'; | 163 | if (isset($_GET['use_extracted_title'])) $redirect .= '&use_extracted_title'; |
164 | if (isset($_GET['content'])) $redirect .= '&content='.urlencode($_GET['content']); | ||
165 | if (isset($_GET['summary'])) $redirect .= '&summary='.urlencode($_GET['summary']); | ||
168 | if (isset($_GET['debug'])) $redirect .= '&debug'; | 166 | if (isset($_GET['debug'])) $redirect .= '&debug'; |
169 | if ($debug_mode) { | 167 | if ($debug_mode) { |
170 | debug('Redirecting to hide access key, follow URL below to continue'); | 168 | debug('Redirecting to hide access key, follow URL below to continue'); |
@@ -177,7 +175,7 @@ if (isset($_GET['key']) && ($key_index = array_search($_GET['key'], $options->ap | |||
177 | 175 | ||
178 | /////////////////////////////////////////////// | 176 | /////////////////////////////////////////////// |
179 | // Set timezone. | 177 | // Set timezone. |
180 | // Prevents warnings, but needs more testing - | 178 | // Prevents warnings, but needs more testing - |
181 | // perhaps if timezone is set in php.ini we | 179 | // perhaps if timezone is set in php.ini we |
182 | // don't need to set it at all... | 180 | // don't need to set it at all... |
183 | /////////////////////////////////////////////// | 181 | /////////////////////////////////////////////// |
@@ -199,7 +197,7 @@ if (isset($_GET['key']) && isset($_GET['hash']) && isset($options->api_keys[(int | |||
199 | } | 197 | } |
200 | $key_index = ($valid_key) ? (int)$_GET['key'] : 0; | 198 | $key_index = ($valid_key) ? (int)$_GET['key'] : 0; |
201 | if (!$valid_key && $options->key_required) { | 199 | if (!$valid_key && $options->key_required) { |
202 | die('A valid key must be supplied'); | 200 | die('A valid key must be supplied'); |
203 | } | 201 | } |
204 | if (!$valid_key && isset($_GET['key']) && $_GET['key'] != '') { | 202 | if (!$valid_key && isset($_GET['key']) && $_GET['key'] != '') { |
205 | die('The entered key is invalid'); | 203 | die('The entered key is invalid'); |
@@ -251,6 +249,28 @@ if ($options->favour_feed_titles == 'user') { | |||
251 | } | 249 | } |
252 | 250 | ||
253 | /////////////////////////////////////////////// | 251 | /////////////////////////////////////////////// |
252 | // Include full content in output? | ||
253 | /////////////////////////////////////////////// | ||
254 | if ($options->content === 'user') { | ||
255 | if (isset($_GET['content']) && $_GET['content'] === '0') { | ||
256 | $options->content = false; | ||
257 | } else { | ||
258 | $options->content = true; | ||
259 | } | ||
260 | } | ||
261 | |||
262 | /////////////////////////////////////////////// | ||
263 | // Include summaries in output? | ||
264 | /////////////////////////////////////////////// | ||
265 | if ($options->summary === 'user') { | ||
266 | if (isset($_GET['summary']) && $_GET['summary'] === '1') { | ||
267 | $options->summary = true; | ||
268 | } else { | ||
269 | $options->summary = false; | ||
270 | } | ||
271 | } | ||
272 | |||
273 | /////////////////////////////////////////////// | ||
254 | // Exclude items if extraction fails | 274 | // Exclude items if extraction fails |
255 | /////////////////////////////////////////////// | 275 | /////////////////////////////////////////////// |
256 | if ($options->exclude_items_on_fail === 'user') { | 276 | if ($options->exclude_items_on_fail === 'user') { |
@@ -272,15 +292,6 @@ if ($options->detect_language === 'user') { | |||
272 | $detect_language = $options->detect_language; | 292 | $detect_language = $options->detect_language; |
273 | } | 293 | } |
274 | 294 | ||
275 | if ($detect_language >= 2) { | ||
276 | $language_codes = array('albanian' => 'sq','arabic' => 'ar','azeri' => 'az','bengali' => 'bn','bulgarian' => 'bg', | ||
277 | 'cebuano' => 'ceb', // ISO 639-2 | ||
278 | 'croatian' => 'hr','czech' => 'cs','danish' => 'da','dutch' => 'nl','english' => 'en','estonian' => 'et','farsi' => 'fa','finnish' => 'fi','french' => 'fr','german' => 'de','hausa' => 'ha', | ||
279 | 'hawaiian' => 'haw', // ISO 639-2 | ||
280 | 'hindi' => 'hi','hungarian' => 'hu','icelandic' => 'is','indonesian' => 'id','italian' => 'it','kazakh' => 'kk','kyrgyz' => 'ky','latin' => 'la','latvian' => 'lv','lithuanian' => 'lt','macedonian' => 'mk','mongolian' => 'mn','nepali' => 'ne','norwegian' => 'no','pashto' => 'ps', | ||
281 | 'pidgin' => 'cpe', // ISO 639-2 | ||
282 | 'polish' => 'pl','portuguese' => 'pt','romanian' => 'ro','russian' => 'ru','serbian' => 'sr','slovak' => 'sk','slovene' => 'sl','somali' => 'so','spanish' => 'es','swahili' => 'sw','swedish' => 'sv','tagalog' => 'tl','turkish' => 'tr','ukrainian' => 'uk','urdu' => 'ur','uzbek' => 'uz','vietnamese' => 'vi','welsh' => 'cy'); | ||
283 | } | ||
284 | $use_cld = extension_loaded('cld') && (version_compare(PHP_VERSION, '5.3.0') >= 0); | 295 | $use_cld = extension_loaded('cld') && (version_compare(PHP_VERSION, '5.3.0') >= 0); |
285 | 296 | ||
286 | ///////////////////////////////////// | 297 | ///////////////////////////////////// |
@@ -330,7 +341,7 @@ if ($options->cors) header('Access-Control-Allow-Origin: *'); | |||
330 | ////////////////////////////////// | 341 | ////////////////////////////////// |
331 | if ($options->caching) { | 342 | if ($options->caching) { |
332 | debug('Caching is enabled...'); | 343 | debug('Caching is enabled...'); |
333 | $cache_id = md5($max.$url.$valid_key.$links.$favour_feed_titles.$xss_filter.$exclude_on_fail.$format.$detect_language.(int)isset($_GET['pubsub'])); | 344 | $cache_id = md5($max.$url.(int)$valid_key.$links.(int)$favour_feed_titles.(int)$options->content.(int)$options->summary.(int)$xss_filter.(int)$exclude_on_fail.$format.$detect_language.(int)isset($_GET['pubsub'])); |
334 | $check_cache = true; | 345 | $check_cache = true; |
335 | if ($options->apc && $options->smart_cache) { | 346 | if ($options->apc && $options->smart_cache) { |
336 | apc_add("cache.$cache_id", 0, 10*60); | 347 | apc_add("cache.$cache_id", 0, 10*60); |
@@ -468,7 +479,7 @@ if ($img_url = $feed->get_image_url()) { | |||
468 | //////////////////////////////////////////// | 479 | //////////////////////////////////////////// |
469 | // Loop through feed items | 480 | // Loop through feed items |
470 | //////////////////////////////////////////// | 481 | //////////////////////////////////////////// |
471 | $items = $feed->get_items(0, $max); | 482 | $items = $feed->get_items(0, $max); |
472 | // Request all feed items in parallel (if supported) | 483 | // Request all feed items in parallel (if supported) |
473 | $urls_sanitized = array(); | 484 | $urls_sanitized = array(); |
474 | $urls = array(); | 485 | $urls = array(); |
@@ -550,24 +561,43 @@ foreach ($items as $key => $item) { | |||
550 | $is_single_page = false; | 561 | $is_single_page = false; |
551 | if ($single_page_response = getSinglePage($item, $html, $effective_url)) { | 562 | if ($single_page_response = getSinglePage($item, $html, $effective_url)) { |
552 | $is_single_page = true; | 563 | $is_single_page = true; |
553 | $html = $single_page_response['body']; | ||
554 | // remove strange things | ||
555 | $html = str_replace('</[>', '', $html); | ||
556 | $html = convert_to_utf8($html, $single_page_response['headers']); | ||
557 | $effective_url = $single_page_response['effective_url']; | 564 | $effective_url = $single_page_response['effective_url']; |
558 | debug("Retrieved single-page view from $effective_url"); | 565 | // check if action defined for returned Content-Type |
566 | $mime_info = get_mime_action_info($single_page_response['headers']); | ||
567 | if (isset($mime_info['action'])) { | ||
568 | if ($mime_info['action'] == 'exclude') { | ||
569 | continue; // skip this feed item entry | ||
570 | } elseif ($mime_info['action'] == 'link') { | ||
571 | if ($mime_info['type'] == 'image') { | ||
572 | $html = "<a href=\"$effective_url\"><img src=\"$effective_url\" alt=\"{$mime_info['name']}\" /></a>"; | ||
573 | } else { | ||
574 | $html = "<a href=\"$effective_url\">Download {$mime_info['name']}</a>"; | ||
575 | } | ||
576 | $extracted_title = $mime_info['name']; | ||
577 | $do_content_extraction = false; | ||
578 | } | ||
579 | } | ||
580 | if ($do_content_extraction) { | ||
581 | $html = $single_page_response['body']; | ||
582 | // remove strange things | ||
583 | $html = str_replace('</[>', '', $html); | ||
584 | $html = convert_to_utf8($html, $single_page_response['headers']); | ||
585 | debug("Retrieved single-page view from $effective_url"); | ||
586 | } | ||
559 | unset($single_page_response); | 587 | unset($single_page_response); |
560 | } | 588 | } |
589 | } | ||
590 | if ($do_content_extraction) { | ||
561 | debug('--------'); | 591 | debug('--------'); |
562 | debug('Attempting to extract content'); | 592 | debug('Attempting to extract content'); |
563 | $extract_result = $extractor->process($html, $effective_url); | 593 | $extract_result = $extractor->process($html, $effective_url); |
564 | $readability = $extractor->readability; | 594 | $readability = $extractor->readability; |
565 | $content_block = ($extract_result) ? $extractor->getContent() : null; | 595 | $content_block = ($extract_result) ? $extractor->getContent() : null; |
566 | $extracted_title = ($extract_result) ? $extractor->getTitle() : ''; | 596 | $extracted_title = ($extract_result) ? $extractor->getTitle() : ''; |
567 | // Deal with multi-page articles | 597 | // Deal with multi-page articles |
568 | //die('Next: '.$extractor->getNextPageUrl()); | 598 | //die('Next: '.$extractor->getNextPageUrl()); |
569 | $is_multi_page = (!$is_single_page && $extract_result && $extractor->getNextPageUrl()); | 599 | $is_multi_page = (!$is_single_page && $extract_result && $extractor->getNextPageUrl()); |
570 | if ($options->multipage && $is_multi_page) { | 600 | if ($options->multipage && $is_multi_page && $options->content) { |
571 | debug('--------'); | 601 | debug('--------'); |
572 | debug('Attempting to process multi-page article'); | 602 | debug('Attempting to process multi-page article'); |
573 | $multi_page_urls = array(); | 603 | $multi_page_urls = array(); |
@@ -580,7 +610,7 @@ foreach ($items as $key => $item) { | |||
580 | // check it's not what we have already! | 610 | // check it's not what we have already! |
581 | if (!in_array($next_page_url, $multi_page_urls)) { | 611 | if (!in_array($next_page_url, $multi_page_urls)) { |
582 | // it's not, so let's attempt to fetch it | 612 | // it's not, so let's attempt to fetch it |
583 | $multi_page_urls[] = $next_page_url; | 613 | $multi_page_urls[] = $next_page_url; |
584 | $_prev_ref = $http->referer; | 614 | $_prev_ref = $http->referer; |
585 | if (($response = $http->get($next_page_url, true)) && $response['status_code'] < 300) { | 615 | if (($response = $http->get($next_page_url, true)) && $response['status_code'] < 300) { |
586 | // make sure mime type is not something with a different action associated | 616 | // make sure mime type is not something with a different action associated |
@@ -605,13 +635,15 @@ foreach ($items as $key => $item) { | |||
605 | // did we successfully deal with this multi-page article? | 635 | // did we successfully deal with this multi-page article? |
606 | if (empty($multi_page_content)) { | 636 | if (empty($multi_page_content)) { |
607 | debug('Failed to extract all parts of multi-page article, so not going to include them'); | 637 | debug('Failed to extract all parts of multi-page article, so not going to include them'); |
608 | $multi_page_content[] = $readability->dom->createElement('p')->innerHTML = '<em>This article appears to continue on subsequent pages which we could not extract</em>'; | 638 | $_page = $readability->dom->createElement('p'); |
639 | $_page->innerHTML = '<em>This article appears to continue on subsequent pages which we could not extract</em>'; | ||
640 | $multi_page_content[] = $_page; | ||
609 | } | 641 | } |
610 | foreach ($multi_page_content as $_page) { | 642 | foreach ($multi_page_content as $_page) { |
611 | $_page = $content_block->ownerDocument->importNode($_page, true); | 643 | $_page = $content_block->ownerDocument->importNode($_page, true); |
612 | $content_block->appendChild($_page); | 644 | $content_block->appendChild($_page); |
613 | } | 645 | } |
614 | unset($multi_page_urls, $multi_page_content, $page_mime_info, $next_page_url); | 646 | unset($multi_page_urls, $multi_page_content, $page_mime_info, $next_page_url, $_page); |
615 | } | 647 | } |
616 | } | 648 | } |
617 | // use extracted title for both feed and item title if we're using single-item dummy feed | 649 | // use extracted title for both feed and item title if we're using single-item dummy feed |
@@ -658,7 +690,7 @@ foreach ($items as $key => $item) { | |||
658 | } else { | 690 | } else { |
659 | $html = $content_block->ownerDocument->saveXML($content_block); // essentially outerHTML | 691 | $html = $content_block->ownerDocument->saveXML($content_block); // essentially outerHTML |
660 | } | 692 | } |
661 | unset($content_block); | 693 | //unset($content_block); |
662 | // post-processing cleanup | 694 | // post-processing cleanup |
663 | $html = preg_replace('!<p>[\s\h\v]*</p>!u', '', $html); | 695 | $html = preg_replace('!<p>[\s\h\v]*</p>!u', '', $html); |
664 | if ($links == 'remove') { | 696 | if ($links == 'remove') { |
@@ -671,130 +703,155 @@ foreach ($items as $key => $item) { | |||
671 | } | 703 | } |
672 | } | 704 | } |
673 | 705 | ||
674 | if ($valid_key && isset($_GET['pubsub'])) { // used only on fivefilters.org at the moment | 706 | if ($valid_key && isset($_GET['pubsub'])) { // used only on fivefilters.org at the moment |
675 | $newitem->addElement('guid', 'http://fivefilters.org/content-only/redirect.php?url='.urlencode($item->get_permalink()), array('isPermaLink'=>'false')); | 707 | $newitem->addElement('guid', 'http://fivefilters.org/content-only/redirect.php?url='.urlencode($item->get_permalink()), array('isPermaLink'=>'false')); |
708 | } else { | ||
709 | $newitem->addElement('guid', $item->get_permalink(), array('isPermaLink'=>'true')); | ||
710 | } | ||
711 | // filter xss? | ||
712 | if ($xss_filter) { | ||
713 | debug('Filtering HTML to remove XSS'); | ||
714 | $html = htmLawed::hl($html, array('safe'=>1, 'deny_attribute'=>'style', 'comment'=>1, 'cdata'=>1)); | ||
715 | } | ||
716 | |||
717 | // add content | ||
718 | if ($options->summary === true) { | ||
719 | // get summary | ||
720 | $summary = ''; | ||
721 | if (!$do_content_extraction) { | ||
722 | $summary = $html; | ||
676 | } else { | 723 | } else { |
677 | $newitem->addElement('guid', $item->get_permalink(), array('isPermaLink'=>'true')); | 724 | // Try to get first few paragraphs |
678 | } | 725 | if (isset($content_block) && ($content_block instanceof DOMElement)) { |
679 | // filter xss? | 726 | $_paras = $content_block->getElementsByTagName('p'); |
680 | if ($xss_filter) { | 727 | foreach ($_paras as $_para) { |
681 | debug('Filtering HTML to remove XSS'); | 728 | $summary .= preg_replace("/[\n\r\t ]+/", ' ', $_para->textContent).' '; |
682 | $html = htmLawed::hl($html, array('safe'=>1, 'deny_attribute'=>'style', 'comment'=>1, 'cdata'=>1)); | 729 | if (strlen($summary) > 200) break; |
683 | } | ||
684 | $newitem->setDescription($html); | ||
685 | |||
686 | // set date | ||
687 | if ((int)$item->get_date('U') > 0) { | ||
688 | $newitem->setDate((int)$item->get_date('U')); | ||
689 | } elseif ($extractor->getDate()) { | ||
690 | $newitem->setDate($extractor->getDate()); | ||
691 | } | ||
692 | |||
693 | // add authors | ||
694 | if ($authors = $item->get_authors()) { | ||
695 | foreach ($authors as $author) { | ||
696 | // for some feeds, SimplePie stores author's name as email, e.g. http://feeds.feedburner.com/nymag/intel | ||
697 | if ($author->get_name() !== null) { | ||
698 | $newitem->addElement('dc:creator', $author->get_name()); | ||
699 | } elseif ($author->get_email() !== null) { | ||
700 | $newitem->addElement('dc:creator', $author->get_email()); | ||
701 | } | 730 | } |
731 | } else { | ||
732 | $summary = $html; | ||
702 | } | 733 | } |
703 | } elseif ($authors = $extractor->getAuthors()) { | 734 | } |
704 | //TODO: make sure the list size is reasonable | 735 | unset($_paras, $_para); |
705 | foreach ($authors as $author) { | 736 | $summary = get_excerpt($summary); |
706 | // TODO: xpath often selects authors from other articles linked from the page. | 737 | $newitem->setDescription($summary); |
707 | // for now choose first item | 738 | if ($options->content) $newitem->setElement('content:encoded', $html); |
708 | $newitem->addElement('dc:creator', $author); | 739 | } else { |
709 | break; | 740 | if ($options->content) $newitem->setDescription($html); |
741 | } | ||
742 | |||
743 | // set date | ||
744 | if ((int)$item->get_date('U') > 0) { | ||
745 | $newitem->setDate((int)$item->get_date('U')); | ||
746 | } elseif ($extractor->getDate()) { | ||
747 | $newitem->setDate($extractor->getDate()); | ||
748 | } | ||
749 | |||
750 | // add authors | ||
751 | if ($authors = $item->get_authors()) { | ||
752 | foreach ($authors as $author) { | ||
753 | // for some feeds, SimplePie stores author's name as email, e.g. http://feeds.feedburner.com/nymag/intel | ||
754 | if ($author->get_name() !== null) { | ||
755 | $newitem->addElement('dc:creator', $author->get_name()); | ||
756 | } elseif ($author->get_email() !== null) { | ||
757 | $newitem->addElement('dc:creator', $author->get_email()); | ||
710 | } | 758 | } |
711 | } | 759 | } |
712 | 760 | } elseif ($authors = $extractor->getAuthors()) { | |
713 | // add language | 761 | //TODO: make sure the list size is reasonable |
714 | if ($detect_language) { | 762 | foreach ($authors as $author) { |
715 | $language = $extractor->getLanguage(); | 763 | // TODO: xpath often selects authors from other articles linked from the page. |
716 | if (!$language) $language = $feed->get_language(); | 764 | // for now choose first item |
717 | if (($detect_language == 3 || (!$language && $detect_language == 2)) && $text_sample) { | 765 | $newitem->addElement('dc:creator', $author); |
718 | try { | 766 | break; |
719 | if ($use_cld) { | 767 | } |
720 | // Use PHP-CLD extension | 768 | } |
721 | $php_cld = 'CLD\detect'; // in quotes to prevent PHP 5.2 parse error | 769 | |
722 | $res = $php_cld($text_sample); | 770 | // add language |
723 | if (is_array($res) && count($res) > 0) { | 771 | if ($detect_language) { |
724 | $language = $res[0]['code']; | 772 | $language = $extractor->getLanguage(); |
725 | } | 773 | if (!$language) $language = $feed->get_language(); |
726 | } else { | 774 | if (($detect_language == 3 || (!$language && $detect_language == 2)) && $text_sample) { |
727 | //die('what'); | 775 | try { |
728 | // Use PEAR's Text_LanguageDetect | 776 | if ($use_cld) { |
729 | if (!isset($l)) { | 777 | // Use PHP-CLD extension |
730 | $l = new Text_LanguageDetect('libraries/language-detect/lang.dat', 'libraries/language-detect/unicode_blocks.dat'); | 778 | $php_cld = 'CLD\detect'; // in quotes to prevent PHP 5.2 parse error |
731 | } | 779 | $res = $php_cld($text_sample); |
732 | $l_result = $l->detect($text_sample, 1); | 780 | if (is_array($res) && count($res) > 0) { |
733 | if (count($l_result) > 0) { | 781 | $language = $res[0]['code']; |
734 | $language = $language_codes[key($l_result)]; | 782 | } |
735 | } | 783 | } else { |
784 | //die('what'); | ||
785 | // Use PEAR's Text_LanguageDetect | ||
786 | if (!isset($l)) { | ||
787 | $l = new Text_LanguageDetect(); | ||
788 | $l->setNameMode(2); // return ISO 639-1 codes (e.g. "en") | ||
789 | } | ||
790 | $l_result = $l->detect($text_sample, 1); | ||
791 | if (count($l_result) > 0) { | ||
792 | $language = key($l_result); | ||
736 | } | 793 | } |
737 | } catch (Exception $e) { | ||
738 | //die('error: '.$e); | ||
739 | // do nothing | ||
740 | } | 794 | } |
741 | } | 795 | } catch (Exception $e) { |
742 | if ($language && (strlen($language) < 7)) { | 796 | //die('error: '.$e); |
743 | $newitem->addElement('dc:language', $language); | 797 | // do nothing |
744 | } | 798 | } |
745 | } | 799 | } |
746 | 800 | if ($language && (strlen($language) < 7)) { | |
747 | // add MIME type (if it appeared in our exclusions lists) | 801 | $newitem->addElement('dc:language', $language); |
748 | if (isset($mime_info['mime'])) $newitem->addElement('dc:format', $mime_info['mime']); | ||
749 | // add effective URL (URL after redirects) | ||
750 | if (isset($effective_url)) { | ||
751 | //TODO: ensure $effective_url is valid witout - sometimes it causes problems, e.g. | ||
752 | //http://www.siasat.pk/forum/showthread.php?108883-Pakistan-Chowk-by-Rana-Mubashir-�-25th-March-2012-Special-Program-from-Liari-(Karachi) | ||
753 | //temporary measure: use utf8_encode() | ||
754 | $newitem->addElement('dc:identifier', remove_url_cruft(utf8_encode($effective_url))); | ||
755 | } else { | ||
756 | $newitem->addElement('dc:identifier', remove_url_cruft($item->get_permalink())); | ||
757 | } | 802 | } |
758 | 803 | } | |
759 | // add categories | 804 | |
760 | if ($categories = $item->get_categories()) { | 805 | // add MIME type (if it appeared in our exclusions lists) |
761 | foreach ($categories as $category) { | 806 | if (isset($mime_info['mime'])) $newitem->addElement('dc:format', $mime_info['mime']); |
762 | if ($category->get_label() !== null) { | 807 | // add effective URL (URL after redirects) |
763 | $newitem->addElement('category', $category->get_label()); | 808 | if (isset($effective_url)) { |
764 | } | 809 | //TODO: ensure $effective_url is valid witout - sometimes it causes problems, e.g. |
810 | //http://www.siasat.pk/forum/showthread.php?108883-Pakistan-Chowk-by-Rana-Mubashir-�-25th-March-2012-Special-Program-from-Liari-(Karachi) | ||
811 | //temporary measure: use utf8_encode() | ||
812 | $newitem->addElement('dc:identifier', remove_url_cruft(utf8_encode($effective_url))); | ||
813 | } else { | ||
814 | $newitem->addElement('dc:identifier', remove_url_cruft($item->get_permalink())); | ||
815 | } | ||
816 | |||
817 | // add categories | ||
818 | if ($categories = $item->get_categories()) { | ||
819 | foreach ($categories as $category) { | ||
820 | if ($category->get_label() !== null) { | ||
821 | $newitem->addElement('category', $category->get_label()); | ||
765 | } | 822 | } |
766 | } | 823 | } |
767 | 824 | } | |
768 | // check for enclosures | 825 | |
769 | if ($options->keep_enclosures) { | 826 | // check for enclosures |
770 | if ($enclosures = $item->get_enclosures()) { | 827 | if ($options->keep_enclosures) { |
771 | foreach ($enclosures as $enclosure) { | 828 | if ($enclosures = $item->get_enclosures()) { |
772 | // thumbnails | 829 | foreach ($enclosures as $enclosure) { |
773 | foreach ((array)$enclosure->get_thumbnails() as $thumbnail) { | 830 | // thumbnails |
774 | $newitem->addElement('media:thumbnail', '', array('url'=>$thumbnail)); | 831 | foreach ((array)$enclosure->get_thumbnails() as $thumbnail) { |
775 | } | 832 | $newitem->addElement('media:thumbnail', '', array('url'=>$thumbnail)); |
776 | if (!$enclosure->get_link()) continue; | ||
777 | $enc = array(); | ||
778 | // Media RSS spec ($enc): http://search.yahoo.com/mrss | ||
779 | // SimplePie methods ($enclosure): http://simplepie.org/wiki/reference/start#methods4 | ||
780 | $enc['url'] = $enclosure->get_link(); | ||
781 | if ($enclosure->get_length()) $enc['fileSize'] = $enclosure->get_length(); | ||
782 | if ($enclosure->get_type()) $enc['type'] = $enclosure->get_type(); | ||
783 | if ($enclosure->get_medium()) $enc['medium'] = $enclosure->get_medium(); | ||
784 | if ($enclosure->get_expression()) $enc['expression'] = $enclosure->get_expression(); | ||
785 | if ($enclosure->get_bitrate()) $enc['bitrate'] = $enclosure->get_bitrate(); | ||
786 | if ($enclosure->get_framerate()) $enc['framerate'] = $enclosure->get_framerate(); | ||
787 | if ($enclosure->get_sampling_rate()) $enc['samplingrate'] = $enclosure->get_sampling_rate(); | ||
788 | if ($enclosure->get_channels()) $enc['channels'] = $enclosure->get_channels(); | ||
789 | if ($enclosure->get_duration()) $enc['duration'] = $enclosure->get_duration(); | ||
790 | if ($enclosure->get_height()) $enc['height'] = $enclosure->get_height(); | ||
791 | if ($enclosure->get_width()) $enc['width'] = $enclosure->get_width(); | ||
792 | if ($enclosure->get_language()) $enc['lang'] = $enclosure->get_language(); | ||
793 | $newitem->addElement('media:content', '', $enc); | ||
794 | } | 833 | } |
834 | if (!$enclosure->get_link()) continue; | ||
835 | $enc = array(); | ||
836 | // Media RSS spec ($enc): http://search.yahoo.com/mrss | ||
837 | // SimplePie methods ($enclosure): http://simplepie.org/wiki/reference/start#methods4 | ||
838 | $enc['url'] = $enclosure->get_link(); | ||
839 | if ($enclosure->get_length()) $enc['fileSize'] = $enclosure->get_length(); | ||
840 | if ($enclosure->get_type()) $enc['type'] = $enclosure->get_type(); | ||
841 | if ($enclosure->get_medium()) $enc['medium'] = $enclosure->get_medium(); | ||
842 | if ($enclosure->get_expression()) $enc['expression'] = $enclosure->get_expression(); | ||
843 | if ($enclosure->get_bitrate()) $enc['bitrate'] = $enclosure->get_bitrate(); | ||
844 | if ($enclosure->get_framerate()) $enc['framerate'] = $enclosure->get_framerate(); | ||
845 | if ($enclosure->get_sampling_rate()) $enc['samplingrate'] = $enclosure->get_sampling_rate(); | ||
846 | if ($enclosure->get_channels()) $enc['channels'] = $enclosure->get_channels(); | ||
847 | if ($enclosure->get_duration()) $enc['duration'] = $enclosure->get_duration(); | ||
848 | if ($enclosure->get_height()) $enc['height'] = $enclosure->get_height(); | ||
849 | if ($enclosure->get_width()) $enc['width'] = $enclosure->get_width(); | ||
850 | if ($enclosure->get_language()) $enc['lang'] = $enclosure->get_language(); | ||
851 | $newitem->addElement('media:content', '', $enc); | ||
795 | } | 852 | } |
796 | } | 853 | } |
797 | /* } */ | 854 | } |
798 | $output->addItem($newitem); | 855 | $output->addItem($newitem); |
799 | unset($html); | 856 | unset($html); |
800 | $item_count++; | 857 | $item_count++; |