From 42c80841c846610be280218d53fcde06b0f0063b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 6 Dec 2013 09:45:27 +0100 Subject: [change] we now use Full-Text RSS 3.1, thank you so much @fivefilters --- inc/poche/Poche.class.php | 9 +- inc/poche/PocheReadability.php | 46 ------ inc/poche/Url.class.php | 364 ----------------------------------------- inc/poche/global.inc.php | 16 +- 4 files changed, 8 insertions(+), 427 deletions(-) delete mode 100644 inc/poche/PocheReadability.php (limited to 'inc/poche') diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index a8a54ff0..5862befb 100644 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php @@ -332,9 +332,12 @@ class Poche switch ($action) { case 'add': - $content = $url->extract(); + $json = file_get_contents(Tools::getPocheUrl() . '/inc/3rdparty/makefulltextfeed.php?url='.urlencode($url->getUrl()).'&max=5&links=preserve&exc=&format=json&submit=Create+Feed'); + $content = json_decode($json, true); + $title = $content['rss']['channel']['item']['title']; + $body = $content['rss']['channel']['item']['description']; - if ($this->store->add($url->getUrl(), $content['title'], $content['body'], $this->user->getId())) { + if ($this->store->add($url->getUrl(), $title, $body, $this->user->getId())) { Tools::logm('add link ' . $url->getUrl()); $sequence = ''; if (STORAGE == 'postgres') { @@ -342,7 +345,7 @@ class Poche } $last_id = $this->store->getLastId($sequence); if (DOWNLOAD_PICTURES) { - $content = filtre_picture($content['body'], $url->getUrl(), $last_id); + $content = filtre_picture($body, $url->getUrl(), $last_id); Tools::logm('updating content article'); $this->store->updateContent($last_id, $content, $this->user->getId()); } diff --git a/inc/poche/PocheReadability.php b/inc/poche/PocheReadability.php deleted file mode 100644 index 48ae90d0..00000000 --- a/inc/poche/PocheReadability.php +++ /dev/null @@ -1,46 +0,0 @@ -getInnerText($this->dom->getElementsByTagName('title')->item(0)); - } catch(Exception $e) {} - - if (preg_match('/ [\|\-] /', $curTitle)) - { - $curTitle = preg_replace('/(.*)[\|\-] .*/i', '$1', $origTitle); - - if (count(explode(' ', $curTitle)) < 3) { - $curTitle = preg_replace('/[^\|\-]*[\|\-](.*)/i', '$1', $origTitle); - } - } - else if(strlen($curTitle) > 150 || strlen($curTitle) < 15) - { - $hOnes = $this->dom->getElementsByTagName('h1'); - if($hOnes->length == 1) - { - $curTitle = $this->getInnerText($hOnes->item(0)); - } - } - - $curTitle = trim($curTitle); - - if (count(explode(' ', $curTitle)) <= 4) { - $curTitle = $origTitle; - } - - $articleTitle = $this->dom->createElement('h1'); - $articleTitle->innerHTML = $curTitle; - - return $articleTitle; - } -} \ No newline at end of file diff --git a/inc/poche/Url.class.php b/inc/poche/Url.class.php index 600a2166..8b3468c3 100644 --- a/inc/poche/Url.class.php +++ b/inc/poche/Url.class.php @@ -12,45 +12,6 @@ class Url { public $url; - private $fingerprints = array( - // Posterous - ' array('hostname'=>'fingerprint.posterous.com', 'head'=>true), - // Blogger - ' array('hostname'=>'fingerprint.blogspot.com', 'head'=>true), - ' array('hostname'=>'fingerprint.blogspot.com', 'head'=>true), - // WordPress (self-hosted and hosted) - '\"$_name\""; - } else { - $html = "Download $_name"; - } - $title = $_name; - $do_content_extraction = false; - break; - } - } - } - unset($_mime, $_act, $_name, $match); - } - if ($do_content_extraction) { - $html = $response['body']; - // remove strange things - $html = str_replace('', '', $html); - $html = $this->convert_to_utf8($html, $response['headers']); - - // check site config for single page URL - fetch it if found - if ($single_page_response = $this->getSinglePage($item, $html, $effective_url)) { - $html = $single_page_response['body']; - // remove strange things - $html = str_replace('', '', $html); - $html = $this->convert_to_utf8($html, $single_page_response['headers']); - $effective_url = $single_page_response['effective_url']; - unset($single_page_response); - } - $extract_result = $extractor->process($html, $effective_url); - $readability = $extractor->readability; - $content_block = ($extract_result) ? $extractor->getContent() : null; - } - } - if ($do_content_extraction) { - // if we failed to extract content... - if (!$extract_result) { - $html = $this->error_message; - // keep the original item description - $html .= $item->get_description(); - } else { - $readability->clean($content_block, 'select'); - if ($this->rewrite_relative_urls) $this->makeAbsolute($effective_url, $content_block); - if ($content_block->childNodes->length == 1 && $content_block->firstChild->nodeType === XML_ELEMENT_NODE) { - $html = $content_block->firstChild->innerHTML; - } else { - $html = $content_block->innerHTML; - } - // post-processing cleanup - $html = preg_replace('!

[\s\h\v]*

!u', '', $html); - } - } - } - - $title = ($extractor->getTitle() != '' ? $extractor->getTitle() : _('Untitled')); - $content = array ('title' => $title, 'body' => $html); - - return $content; - } - - private function convert_to_utf8($html, $header=null) - { - $encoding = null; - if ($html || $header) { - if (is_array($header)) $header = implode("\n", $header); - if (!$header || !preg_match_all('/^Content-Type:\s+([^;]+)(?:;\s*charset=["\']?([^;"\'\n]*))?/im', $header, $match, PREG_SET_ORDER)) { - // error parsing the response - } else { - $match = end($match); // get last matched element (in case of redirects) - if (isset($match[2])) $encoding = trim($match[2], "\"' \r\n\0\x0B\t"); - } - // TODO: check to see if encoding is supported (can we convert it?) - // If it's not, result will be empty string. - // For now we'll check for invalid encoding types returned by some sites, e.g. 'none' - // Problem URL: http://facta.co.jp/blog/archives/20111026001026.html - if (!$encoding || $encoding == 'none') { - // search for encoding in HTML - only look at the first 35000 characters - $html_head = substr($html, 0, 40000); - if (preg_match('/^<\?xml\s+version=(?:"[^"]*"|\'[^\']*\')\s+encoding=("[^"]*"|\'[^\']*\')/s', $html_head, $match)) { - $encoding = trim($match[1], '"\''); - } elseif (preg_match('/]+)/i', $html_head, $match)) { - $encoding = trim($match[1]); - } elseif (preg_match_all('/]+)>/i', $html_head, $match)) { - foreach ($match[1] as $_test) { - if (preg_match('/charset=["\']?([^"\']+)/i', $_test, $_m)) { - $encoding = trim($_m[1]); - break; - } - } - } - } - if (isset($encoding)) $encoding = trim($encoding); - // trim is important here! - if (!$encoding || (strtolower($encoding) == 'iso-8859-1')) { - // replace MS Word smart qutoes - $trans = array(); - $trans[chr(130)] = '‚'; // Single Low-9 Quotation Mark - $trans[chr(131)] = 'ƒ'; // Latin Small Letter F With Hook - $trans[chr(132)] = '„'; // Double Low-9 Quotation Mark - $trans[chr(133)] = '…'; // Horizontal Ellipsis - $trans[chr(134)] = '†'; // Dagger - $trans[chr(135)] = '‡'; // Double Dagger - $trans[chr(136)] = 'ˆ'; // Modifier Letter Circumflex Accent - $trans[chr(137)] = '‰'; // Per Mille Sign - $trans[chr(138)] = 'Š'; // Latin Capital Letter S With Caron - $trans[chr(139)] = '‹'; // Single Left-Pointing Angle Quotation Mark - $trans[chr(140)] = 'Œ'; // Latin Capital Ligature OE - $trans[chr(145)] = '‘'; // Left Single Quotation Mark - $trans[chr(146)] = '’'; // Right Single Quotation Mark - $trans[chr(147)] = '“'; // Left Double Quotation Mark - $trans[chr(148)] = '”'; // Right Double Quotation Mark - $trans[chr(149)] = '•'; // Bullet - $trans[chr(150)] = '–'; // En Dash - $trans[chr(151)] = '—'; // Em Dash - $trans[chr(152)] = '˜'; // Small Tilde - $trans[chr(153)] = '™'; // Trade Mark Sign - $trans[chr(154)] = 'š'; // Latin Small Letter S With Caron - $trans[chr(155)] = '›'; // Single Right-Pointing Angle Quotation Mark - $trans[chr(156)] = 'œ'; // Latin Small Ligature OE - $trans[chr(159)] = 'Ÿ'; // Latin Capital Letter Y With Diaeresis - $html = strtr($html, $trans); - } - if (!$encoding) { - $encoding = 'utf-8'; - } else { - if (strtolower($encoding) != 'utf-8') { - $html = SimplePie_Misc::change_encoding($html, $encoding, 'utf-8'); - /* - if (function_exists('iconv')) { - // iconv appears to handle certain character encodings better than mb_convert_encoding - $html = iconv($encoding, 'utf-8', $html); - } else { - $html = mb_convert_encoding($html, 'utf-8', $encoding); - } - */ - } - } - } - return $html; - } - - private function makeAbsolute($base, $elem) { - $base = new SimplePie_IRI($base); - // remove '//' in URL path (used to prevent URLs from resolving properly) - // TODO: check if this is still the case - if (isset($base->path)) $base->path = preg_replace('!//+!', '/', $base->path); - foreach(array('a'=>'href', 'img'=>'src') as $tag => $attr) { - $elems = $elem->getElementsByTagName($tag); - for ($i = $elems->length-1; $i >= 0; $i--) { - $e = $elems->item($i); - //$e->parentNode->replaceChild($articleContent->ownerDocument->createTextNode($e->textContent), $e); - $this->makeAbsoluteAttr($base, $e, $attr); - } - if (strtolower($elem->tagName) == $tag) $this->makeAbsoluteAttr($base, $elem, $attr); - } - } - - private function makeAbsoluteAttr($base, $e, $attr) { - if ($e->hasAttribute($attr)) { - // Trim leading and trailing white space. I don't really like this but - // unfortunately it does appear on some sites. e.g. - $url = trim(str_replace('%20', ' ', $e->getAttribute($attr))); - $url = str_replace(' ', '%20', $url); - if (!preg_match('!https?://!i', $url)) { - if ($absolute = SimplePie_IRI::absolutize($base, $url)) { - $e->setAttribute($attr, $absolute); - } - } - } - } - - private function makeAbsoluteStr($base, $url) { - $base = new SimplePie_IRI($base); - // remove '//' in URL path (causes URLs not to resolve properly) - if (isset($base->path)) $base->path = preg_replace('!//+!', '/', $base->path); - if (preg_match('!^https?://!i', $url)) { - // already absolute - return $url; - } else { - if ($absolute = SimplePie_IRI::absolutize($base, $url)) { - return $absolute; - } - return false; - } - } - - // returns single page response, or false if not found - private function getSinglePage($item, $html, $url) { - global $http, $extractor; - $host = @parse_url($url, PHP_URL_HOST); - $site_config = SiteConfig::build($host); - if ($site_config === false) { - // check for fingerprints - if (!empty($extractor->fingerprints) && ($_fphost = $extractor->findHostUsingFingerprints($html))) { - $site_config = SiteConfig::build($_fphost); - } - if ($site_config === false) $site_config = new SiteConfig(); - SiteConfig::add_to_cache($host, $site_config); - return false; - } else { - SiteConfig::add_to_cache($host, $site_config); - } - $splink = null; - if (!empty($site_config->single_page_link)) { - $splink = $site_config->single_page_link; - } elseif (!empty($site_config->single_page_link_in_feed)) { - // single page link xpath is targeted at feed - $splink = $site_config->single_page_link_in_feed; - // so let's replace HTML with feed item description - $html = $item->get_description(); - } - if (isset($splink)) { - // Build DOM tree from HTML - $readability = new PocheReadability($html, $url); - $xpath = new DOMXPath($readability->dom); - // Loop through single_page_link xpath expressions - $single_page_url = null; - foreach ($splink as $pattern) { - $elems = @$xpath->evaluate($pattern, $readability->dom); - if (is_string($elems)) { - $single_page_url = trim($elems); - break; - } elseif ($elems instanceof DOMNodeList && $elems->length > 0) { - foreach ($elems as $item) { - if ($item instanceof DOMElement && $item->hasAttribute('href')) { - $single_page_url = $item->getAttribute('href'); - break; - } elseif ($item instanceof DOMAttr && $item->value) { - $single_page_url = $item->value; - break; - } - } - } - } - // If we've got URL, resolve against $url - if (isset($single_page_url) && ($single_page_url = $this->makeAbsoluteStr($url, $single_page_url))) { - // check it's not what we have already! - if ($single_page_url != $url) { - // it's not, so let's try to fetch it... - $_prev_ref = $http->referer; - $http->referer = $single_page_url; - if (($response = $http->get($single_page_url, true)) && $response['status_code'] < 300) { - $http->referer = $_prev_ref; - return $response; - } - $http->referer = $_prev_ref; - } - } - } - return false; - } } \ No newline at end of file diff --git a/inc/poche/global.inc.php b/inc/poche/global.inc.php index 65a026a7..846699d3 100644 --- a/inc/poche/global.inc.php +++ b/inc/poche/global.inc.php @@ -20,25 +20,13 @@ require_once INCLUDES . '/poche/Url.class.php'; require_once INCLUDES . '/3rdparty/class.messages.php'; require_once INCLUDES . '/poche/Poche.class.php'; -require_once INCLUDES . '/3rdparty/Readability.php'; -require_once INCLUDES . '/poche/PocheReadability.php'; - -require_once INCLUDES . '/3rdparty/Encoding.php'; require_once INCLUDES . '/poche/Database.class.php'; require_once INCLUDES . '/3rdparty/simple_html_dom.php'; require_once INCLUDES . '/3rdparty/paginator.php'; require_once INCLUDES . '/3rdparty/Session.class.php'; -require_once INCLUDES . '/3rdparty/simplepie/SimplePieAutoloader.php'; -require_once INCLUDES . '/3rdparty/simplepie/SimplePie/Core.php'; -require_once INCLUDES . '/3rdparty/content-extractor/ContentExtractor.php'; -require_once INCLUDES . '/3rdparty/content-extractor/SiteConfig.php'; -require_once INCLUDES . '/3rdparty/humble-http-agent/HumbleHttpAgent.php'; -require_once INCLUDES . '/3rdparty/humble-http-agent/SimplePie_HumbleHttpAgent.php'; -require_once INCLUDES . '/3rdparty/humble-http-agent/CookieJar.php'; -require_once INCLUDES . '/3rdparty/feedwriter/FeedItem.php'; -require_once INCLUDES . '/3rdparty/feedwriter/FeedWriter.php'; -require_once INCLUDES . '/3rdparty/feedwriter/DummySingleItemFeed.php'; +require_once INCLUDES . '/3rdparty/libraries/feedwriter/FeedItem.php'; +require_once INCLUDES . '/3rdparty/libraries/feedwriter/FeedWriter.php'; require_once INCLUDES . '/3rdparty/FlattrItem.class.php'; # Composer its autoloader for automatically loading Twig -- cgit v1.2.3