X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FFeed.php;h=705a094c38149b433b454fc2b5626181ebb9b598;hb=51ee24f65220f4d5e3b0717767fa399e1a5a58e5;hp=0709a9550ea23b44c20468930c1d30369f1b65ea;hpb=a6996601d5260e4dde621f4d04b35adbbc3c10e0;p=friendica.git diff --git a/src/Protocol/Feed.php b/src/Protocol/Feed.php index 0709a9550e..705a094c38 100644 --- a/src/Protocol/Feed.php +++ b/src/Protocol/Feed.php @@ -9,16 +9,16 @@ namespace Friendica\Protocol; use DOMDocument; use DOMXPath; use Friendica\Content\Text\HTML; +use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Model\Item; +use Friendica\Protocol\ActivityNamespace; +use Friendica\Util\ParseUrl; use Friendica\Util\Network; use Friendica\Util\XML; -require_once 'include/dba.php'; -require_once 'include/items.php'; - /** * @brief This class contain functions to import feeds * @@ -27,25 +27,26 @@ class Feed { /** * @brief Read a RSS/RDF/Atom feed and create an item entry for it * - * @param string $xml The feed data - * @param array $importer The user record of the importer - * @param array $contact The contact record of the feed - * @param string $hub Unused dummy value for compatibility reasons - * @param bool $simulate If enabled, no data is imported + * @param string $xml The feed data + * @param array $importer The user record of the importer + * @param array $contact The contact record of the feed + * @param string $hub Unused dummy value for compatibility reasons + * @param bool $simulate If enabled, no data is imported * * @return array In simulation mode it returns the header and the first item + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function import($xml, $importer, &$contact, &$hub, $simulate = false) { - $a = get_app(); + $a = \get_app(); if (!$simulate) { - logger("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], LOGGER_DEBUG); + Logger::log("Import Atom/RSS feed '".$contact["name"]."' (Contact ".$contact["id"].") for user ".$importer["uid"], Logger::DEBUG); } else { - logger("Test Atom/RSS feed", LOGGER_DEBUG); + Logger::log("Test Atom/RSS feed", Logger::DEBUG); } if (empty($xml)) { - logger('XML is empty.', LOGGER_DEBUG); + Logger::log('XML is empty.', Logger::DEBUG); return; } @@ -60,13 +61,13 @@ class Feed { $doc = new DOMDocument(); @$doc->loadXML(trim($xml)); $xpath = new DOMXPath($doc); - $xpath->registerNamespace('atom', NAMESPACE_ATOM1); + $xpath->registerNamespace('atom', ActivityNamespace::ATOM1); $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/"); $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/"); $xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); $xpath->registerNamespace('rss', "http://purl.org/rss/1.0/"); $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/"); - $xpath->registerNamespace('poco', NAMESPACE_POCO); + $xpath->registerNamespace('poco', ActivityNamespace::POCO); $author = []; $entries = null; @@ -122,7 +123,13 @@ class Feed { $author["author-name"] = $value; } if ($simulate) { - $author["author-id"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:author/atom:uri/text()'); + $author["author-id"] = XML::getFirstNodeValue($xpath, '/atom:feed/atom:author/atom:id/text()'); + + // See https://tools.ietf.org/html/rfc4287#section-3.2.2 + $value = XML::getFirstNodeValue($xpath, 'atom:author/atom:uri/text()'); + if ($value != "") { + $author["author-link"] = $value; + } $value = XML::getFirstNodeValue($xpath, 'atom:author/poco:preferredUsername/text()'); if ($value != "") { @@ -193,13 +200,13 @@ class Feed { $header["origin"] = 0; $header["gravity"] = GRAVITY_PARENT; $header["private"] = 2; - $header["verb"] = ACTIVITY_POST; - $header["object-type"] = ACTIVITY_OBJ_NOTE; + $header["verb"] = Activity::POST; + $header["object-type"] = Activity\ObjectType::NOTE; $header["contact-id"] = $contact["id"]; if (!is_object($entries)) { - logger("There are no entries in this feed.", LOGGER_DEBUG); + Logger::log("There are no entries in this feed.", Logger::DEBUG); return; } @@ -248,7 +255,7 @@ class Feed { $importer["uid"], $item["uri"], Protocol::FEED, Protocol::DFRN]; $previous = Item::selectFirst(['id'], $condition); if (DBA::isResult($previous)) { - logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$previous["id"], LOGGER_DEBUG); + Logger::log("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$previous["id"], Logger::DEBUG); continue; } } @@ -261,6 +268,9 @@ class Feed { if (empty($item["title"])) { $item["title"] = XML::getFirstNodeValue($xpath, 'rss:title/text()', $entry); } + + $item["title"] = html_entity_decode($item["title"], ENT_QUOTES, 'UTF-8'); + $published = XML::getFirstNodeValue($xpath, 'atom:published/text()', $entry); if (empty($published)) { @@ -313,7 +323,6 @@ class Feed { $href = ""; $length = ""; $type = ""; - $title = ""; foreach ($enclosure->attributes AS $attribute) { if (in_array($attribute->name, ["url", "href"])) { @@ -343,7 +352,7 @@ class Feed { $tags .= ', '; } - $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . rawurlencode($hashtag) . "]" . $hashtag . "[/url]"; + $taglink = "#[url=" . System::baseUrl() . "/search?tag=" . $hashtag . "]" . $hashtag . "[/url]"; $tags .= $taglink; } @@ -352,11 +361,20 @@ class Feed { if (empty($body)) { $body = trim(XML::getFirstNodeValue($xpath, 'content:encoded/text()', $entry)); } - if (empty($body)) { - $body = trim(XML::getFirstNodeValue($xpath, 'description/text()', $entry)); + + $summary = trim(XML::getFirstNodeValue($xpath, 'atom:summary/text()', $entry)); + + if (empty($summary)) { + $summary = trim(XML::getFirstNodeValue($xpath, 'description/text()', $entry)); } + if (empty($body)) { - $body = trim(XML::getFirstNodeValue($xpath, 'atom:summary/text()', $entry)); + $body = $summary; + $summary = ''; + } + + if ($body == $summary) { + $summary = ''; } // remove the content of the title if it is identically to the body @@ -382,7 +400,7 @@ class Feed { // Remove a possible link to the item itself $item["body"] = str_replace($item["plink"], '', $item["body"]); - $item["body"] = preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"]); + $item["body"] = trim(preg_replace('/\[url\=\](\w+.*?)\[\/url\]/i', '', $item["body"])); // Replace the content when the title is longer than the body $replace = (strlen($item["title"]) > strlen($item["body"])); @@ -398,15 +416,32 @@ class Feed { } if ($replace) { - $item["body"] = $item["title"]; + $item["body"] = trim($item["title"]); } + + $data = ParseUrl::getSiteinfoCached($item['plink'], true); + if (!empty($data['text']) && !empty($data['title']) && (mb_strlen($item['body']) < mb_strlen($data['text']))) { + // When the fetched page info text is longer than the body, we do try to enhance the body + if (!empty($item['body']) && (strpos($data['title'], $item['body']) === false) && (strpos($data['text'], $item['body']) === false)) { + // The body is not part of the fetched page info title or page info text. So we add the text to the body + $item['body'] .= "\n\n" . $data['text']; + } else { + // Else we replace the body with the page info text + $item['body'] = $data['text']; + } + } + // We always strip the title since it will be added in the page information $item["title"] = ""; $item["body"] = $item["body"].add_page_info($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]); $item["tag"] = add_page_keywords($item["plink"], $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_blacklist"]); - $item["object-type"] = ACTIVITY_OBJ_BOOKMARK; + $item["object-type"] = Activity\ObjectType::BOOKMARK; unset($item["attach"]); } else { + if (!empty($summary)) { + $item["body"] = '[abstract]' . HTML::toBBCode($summary, $basepath) . "[/abstract]\n" . $item["body"]; + } + if ($contact["fetch_further_information"] == 3) { if (!empty($tags)) { $item["tag"] = $tags; @@ -423,14 +458,14 @@ class Feed { } if (!$simulate) { - logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG); + Logger::log("Stored feed: ".print_r($item, true), Logger::DEBUG); $notify = Item::isRemoteSelf($contact, $item); // Distributed items should have a well formatted URI. // Additionally we have to avoid conflicts with identical URI between imported feeds and these items. if ($notify) { - $item['guid'] = Item::guidFromUri($orig_plink, $a->get_hostname()); + $item['guid'] = Item::guidFromUri($orig_plink, $a->getHostName()); unset($item['uri']); unset($item['parent-uri']); @@ -440,7 +475,7 @@ class Feed { $id = Item::insert($item, false, $notify); - logger("Feed for contact ".$contact["url"]." stored under id ".$id); + Logger::log("Feed for contact ".$contact["url"]." stored under id ".$id); } else { $items[] = $item; }