]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Feed.php
Missing dba::selectFirst in Model\Item
[friendica.git] / src / Protocol / Feed.php
index 9f93287370e21088af22f921d66a488bf485e1fe..203a2e83565d27396dcb4dc7486e17e8a5bab5a8 100644 (file)
@@ -8,10 +8,15 @@ namespace Friendica\Protocol;
 
 use Friendica\Database\DBM;
 use Friendica\Core\System;
+use Friendica\Model\Item;
+use Friendica\Util\Network;
 use dba;
+use DOMDocument;
+use DOMXPath;
 
-require_once("include/html2bbcode.php");
-require_once("include/items.php");
+require_once 'include/dba.php';
+require_once 'include/html2bbcode.php';
+require_once 'include/items.php';
 
 /**
  * @brief This class contain functions to import feeds
@@ -29,7 +34,7 @@ class Feed {
         *
         * @return array In simulation mode it returns the header and the first item
         */
-       function import($xml, $importer, &$contact, &$hub, $simulate = false) {
+       public static function import($xml, $importer, &$contact, &$hub, $simulate = false) {
 
                $a = get_app();
 
@@ -53,7 +58,7 @@ class Feed {
 
                $doc = new DOMDocument();
                @$doc->loadXML(trim($xml));
-               $xpath = new DomXPath($doc);
+               $xpath = new DOMXPath($doc);
                $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
                $xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
                $xpath->registerNamespace('content', "http://purl.org/rss/1.0/modules/content/");
@@ -62,7 +67,7 @@ class Feed {
                $xpath->registerNamespace('media', "http://search.yahoo.com/mrss/");
                $xpath->registerNamespace('poco', NAMESPACE_POCO);
 
-               $author = array();
+               $author = [];
 
                // Is it RDF?
                if ($xpath->query('/rdf:RDF/rss:channel')->length > 0) {
@@ -124,7 +129,7 @@ class Feed {
                                if ($value != "") {
                                        $author["author-nick"] = $value;
                                }
-                               $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()', $context)->item(0)->nodeValue;
+                               $value = $xpath->evaluate('atom:author/poco:address/poco:formatted/text()')->item(0)->nodeValue;
                                if ($value != "") {
                                        $author["author-location"] = $value;
                                }
@@ -182,7 +187,7 @@ class Feed {
                        $author["owner-avatar"] = $contact["thumb"];
                }
 
-               $header = array();
+               $header = [];
                $header["uid"] = $importer["uid"];
                $header["network"] = NETWORK_FEED;
                $header["type"] = "remote";
@@ -195,19 +200,14 @@ class Feed {
 
                $header["contact-id"] = $contact["id"];
 
-               if (!strlen($contact["notify"])) {
-                       // one way feed - no remote comment ability
-                       $header["last-child"] = 0;
-               }
-
                if (!is_object($entries)) {
                        logger("There are no entries in this feed.", LOGGER_DEBUG);
                        return;
                }
 
-               $items = array();
+               $items = [];
 
-               $entrylist = array();
+               $entrylist = [];
 
                foreach ($entries AS $entry) {
                        $entrylist[] = $entry;
@@ -244,14 +244,14 @@ class Feed {
 
                        $orig_plink = $item["plink"];
 
-                       $item["plink"] = original_url($item["plink"]);
+                       $item["plink"] = Network::finalUrl($item["plink"]);
 
                        $item["parent-uri"] = $item["uri"];
 
                        if (!$simulate) {
                                $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
                                        $importer["uid"], $item["uri"], NETWORK_FEED, NETWORK_DFRN];
-                               $previous = dba::select('item', ['id'], $condition, ['limit' => 1]);
+                               $previous = dba::selectFirst('item', ['id'], $condition);
                                if (DBM::is_result($previous)) {
                                        logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already existed under id ".$previous["id"], LOGGER_DEBUG);
                                        continue;
@@ -296,9 +296,6 @@ class Feed {
                        if ($creator != "") {
                                $item["author-name"] = $creator;
                        }
-                       if ($pubDate != "") {
-                               $item["edited"] = $item["created"] = $pubDate;
-                       }
                        $creator = $xpath->query('dc:creator/text()', $entry)->item(0)->nodeValue;
 
                        if ($creator != "") {
@@ -309,7 +306,7 @@ class Feed {
                        // <category>Ausland</category>
                        // <media:thumbnail width="152" height="76" url="http://www.taz.de/picture/667875/192/14388767.jpg"/>
 
-                       $attachments = array();
+                       $attachments = [];
 
                        $enclosures = $xpath->query("enclosure", $entry);
                        foreach ($enclosures AS $enclosure) {
@@ -331,7 +328,7 @@ class Feed {
                                        $item["attach"] .= ',';
                                }
 
-                               $attachments[] = array("link" => $href, "type" => $type, "length" => $length);
+                               $attachments[] = ["link" => $href, "type" => $type, "length" => $length];
 
                                $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'"[/attach]';
                        }
@@ -362,7 +359,7 @@ class Feed {
 
                        // remove the content of the title if it is identically to the body
                        // This helps with auto generated titles e.g. from tumblr
-                       if (title_is_body($item["title"], $body)) {
+                       if (self::titleIsBody($item["title"], $body)) {
                                $item["title"] = "";
                        }
                        $item["body"] = html2bbcode($body, $basepath);
@@ -417,7 +414,8 @@ class Feed {
                                        }
                                        $item["body"] .= "\n".$item['tag'];
                                }
-                               if (!strstr($item["body"], '[url') && ($item['plink'] != '')) {
+                               // Add the link to the original feed entry if not present in feed
+                               if (($item['plink'] != '') && !strstr($item["body"], $item['plink'])) {
                                        $item["body"] .= "[hr][url]".$item['plink']."[/url]";
                                }
                        }
@@ -425,17 +423,17 @@ class Feed {
                        if (!$simulate) {
                                logger("Stored feed: ".print_r($item, true), LOGGER_DEBUG);
 
-                               $notify = item_is_remote_self($contact, $item);
+                               $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'] = uri_to_guid($orig_plink, $a->get_hostname());
+                                       $item['guid'] = Item::guidFromUri($orig_plink, $a->get_hostname());
                                        unset($item['uri']);
                                        unset($item['parent-uri']);
                                }
 
-                               $id = item_store($item, false, $notify);
+                               $id = Item::insert($item, false, $notify);
 
                                logger("Feed for contact ".$contact["url"]." stored under id ".$id);
                        } else {
@@ -447,7 +445,33 @@ class Feed {
                }
 
                if ($simulate) {
-                       return array("header" => $author, "items" => $items);
+                       return ["header" => $author, "items" => $items];
+               }
+       }
+
+       private static function titleIsBody($title, $body)
+       {
+               $title = strip_tags($title);
+               $title = trim($title);
+               $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
+               $title = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $title);
+
+               $body = strip_tags($body);
+               $body = trim($body);
+               $body = html_entity_decode($body, ENT_QUOTES, 'UTF-8');
+               $body = str_replace(["\n", "\r", "\t", " "], ["", "", "", ""], $body);
+
+               if (strlen($title) < strlen($body)) {
+                       $body = substr($body, 0, strlen($title));
+               }
+
+               if (($title != $body) && (substr($title, -3) == "...")) {
+                       $pos = strrpos($title, "...");
+                       if ($pos > 0) {
+                               $title = substr($title, 0, $pos);
+                               $body = substr($body, 0, $pos);
+                       }
                }
+               return ($title == $body);
        }
 }