]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/DFRN.php
Replace old database queries with the new ones
[friendica.git] / src / Protocol / DFRN.php
index d738adecb8b2d95f245f8371abbcdaf2e7aa96a8..ea14aa6ffad950b008470598936e11b008a496a1 100644 (file)
@@ -50,9 +50,9 @@ require_once "include/text.php";
 class DFRN
 {
 
-       const DFRN_TOP_LEVEL = 0;       // Top level posting
-       const DFRN_REPLY = 1;           // Regular reply that is stored locally
-       const DFRN_REPLY_RC = 2;        // Reply that will be relayed
+       const TOP_LEVEL = 0;    // Top level posting
+       const REPLY = 1;                // Regular reply that is stored locally
+       const REPLY_RC = 2;     // Reply that will be relayed
 
        /**
         * @brief Generates the atom entries for delivery.php
@@ -228,17 +228,10 @@ class DFRN
                $check_date = DateTimeFormat::utc($last_update);
 
                $r = q(
-                       "SELECT `item`.*, `item`.`id` AS `item_id`,
-                       `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
-                       `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
-                       `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
-                       `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
+                       "SELECT `item`.`id`
                        FROM `item` USE INDEX (`uid_wall_changed`) $sql_post_table
                        STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                       AND (NOT `contact`.`blocked` OR `contact`.`pending`)
-                       LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
-                       WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`moderated` AND `item`.`parent` != 0
-                       AND `item`.`wall` AND `item`.`changed` > '%s'
+                       WHERE `item`.`uid` = %d AND `item`.`wall` AND `item`.`changed` > '%s'
                        $sql_extra
                        ORDER BY `item`.`parent` ".$sort.", `item`.`created` ASC LIMIT 0, 300",
                        intval($owner_id),
@@ -246,13 +239,23 @@ class DFRN
                        dbesc($sort)
                );
 
+               $ids = [];
+               foreach ($r as $item) {
+                       $ids[] = $item['id'];
+               }
+
+               if (!empty($ids)) {
+                       $ret = Item::select(Item::DELIVER_FIELDLIST, ['id' => $ids]);
+                       $items = dba::inArray($ret);
+               } else {
+                       $items = [];
+               }
+
                /*
                 * Will check further below if this actually returned results.
                 * We will provide an empty feed if that is the case.
                 */
 
-               $items = $r;
-
                $doc = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;
 
@@ -321,33 +324,18 @@ class DFRN
        public static function itemFeed($item_id, $conversation = false)
        {
                if ($conversation) {
-                       $condition = '`item`.`parent`';
+                       $condition = ['parent' => $item_id];
                } else {
-                       $condition = '`item`.`id`';
+                       $condition = ['id' => $item_id];
                }
 
-               $r = q(
-                       "SELECT `item`.*, `item`.`id` AS `item_id`,
-                       `contact`.`name`, `contact`.`network`, `contact`.`photo`, `contact`.`url`,
-                       `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
-                       `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
-                       `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer`
-                       FROM `item`
-                       STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                               AND (NOT `contact`.`blocked` OR `contact`.`pending`)
-                       LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id`
-                       WHERE %s = %d AND `item`.`visible` AND NOT `item`.`moderated` AND `item`.`parent` != 0
-                       AND NOT `item`.`private`",
-                       $condition,
-                       intval($item_id)
-               );
-
-               if (!DBM::is_result($r)) {
+               $ret = Item::select(Item::DELIVER_FIELDLIST, $condition);
+               $items = dba::inArray($ret);
+               if (!DBM::is_result($items)) {
                        killme();
                }
 
-               $items = $r;
-               $item = $r[0];
+               $item = $items[0];
 
                if ($item['uid'] != 0) {
                        $owner = User::getOwnerDataById($item['uid']);
@@ -962,13 +950,13 @@ class DFRN
                $conversation_uri = $conversation_href;
 
                if (isset($parent_item)) {
-                       $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $item['parent-uri']);
-                       if (DBM::is_result($r)) {
+                       $conversation = dba::selectFirst('conversation', ['conversation-uri', 'conversation-href'], ['item-uri' => $item['parent-uri']]);
+                       if (DBM::is_result($conversation)) {
                                if ($r['conversation-uri'] != '') {
-                                       $conversation_uri = $r['conversation-uri'];
+                                       $conversation_uri = $conversation['conversation-uri'];
                                }
                                if ($r['conversation-href'] != '') {
-                                       $conversation_href = $r['conversation-href'];
+                                       $conversation_href = $conversation['conversation-href'];
                                }
                        }
                }
@@ -1549,13 +1537,11 @@ class DFRN
                $author["name"] = $xpath->evaluate($element."/atom:name/text()", $context)->item(0)->nodeValue;
                $author["link"] = $xpath->evaluate($element."/atom:uri/text()", $context)->item(0)->nodeValue;
 
-               $contact_old = dba::fetch_first("SELECT `id`, `uid`, `url`, `network`, `avatar-date`, `avatar`, `name-date`, `uri-date`, `addr`,
-                               `name`, `nick`, `about`, `location`, `keywords`, `xmpp`, `bdyear`, `bd`, `hidden`, `contact-type`
-                               FROM `contact` WHERE `uid` = ? AND `nurl` = ? AND `network` != ?",
-                       $importer["importer_uid"],
-                       normalise_link($author["link"]),
-                       NETWORK_STATUSNET
-               );
+               $fields = ['id', 'uid', 'url', 'network', 'avatar-date', 'avatar', 'name-date', 'uri-date', 'addr',
+                       'name', 'nick', 'about', 'location', 'keywords', 'xmpp', 'bdyear', 'bd', 'hidden', 'contact-type'];
+               $condition = ["`uid` = ? AND `nurl` = ? AND `network` != ?",
+                       $importer["importer_uid"], normalise_link($author["link"]), NETWORK_STATUSNET];
+               $contact_old = dba::selectFirst('contact', $fields, $condition);
 
                if (DBM::is_result($contact_old)) {
                        $author["contact-id"] = $contact_old["id"];
@@ -2094,7 +2080,6 @@ class DFRN
                        'confirm' => $relocate["confirm"], 'notify' => $relocate["notify"],
                        'poll' => $relocate["poll"], 'site-pubkey' => $relocate["sitepubkey"]];
                $condition = ["(`id` = ?) OR (`nurl` = ?)", $importer["id"], normalise_link($old["url"])];
-               dba::update('contact', $fields, $condition);
 
                // @TODO No dba:update here?
                dba::update('contact', $fields, $condition);
@@ -2146,10 +2131,6 @@ class DFRN
                        Item::update($fields, $condition);
 
                        $changed = true;
-
-                       if ($entrytype == DFRN_REPLY_RC) {
-                               Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $current["id"]);
-                       }
                }
                return $changed;
        }
@@ -2217,12 +2198,12 @@ class DFRN
                        }
 
                        if ($is_a_remote_action) {
-                               return DFRN_REPLY_RC;
+                               return DFRN::REPLY_RC;
                        } else {
-                               return DFRN_REPLY;
+                               return DFRN::REPLY;
                        }
                } else {
-                       return DFRN_TOP_LEVEL;
+                       return DFRN::TOP_LEVEL;
                }
        }
 
@@ -2257,6 +2238,8 @@ class DFRN
                        }
 
                        if ($Blink && link_compare($Blink, System::baseUrl() . "/profile/" . $importer["nickname"])) {
+                               $author = dba::selectFirst('contact', ['name', 'thumb', 'url'], ['id' => $item['author-id']]);
+
                                // send a notification
                                notification(
                                        [
@@ -2268,10 +2251,9 @@ class DFRN
                                        "uid"          => $importer["importer_uid"],
                                        "item"         => $item,
                                        "link"         => System::baseUrl()."/display/".urlencode(Item::getGuidById($posted_id)),
-                                       "source_name"  => stripslashes($item["author-name"]),
-                                       "source_link"  => $item["author-link"],
-                                       "source_photo" => ((link_compare($item["author-link"], $importer["url"]))
-                                               ? $importer["thumb"] : $item["author-avatar"]),
+                                       "source_name"  => $author["name"],
+                                       "source_link"  => $author["url"],
+                                       "source_photo" => $author["thumb"],
                                        "verb"         => $item["verb"],
                                        "otype"        => "person",
                                        "activity"     => $verb,
@@ -2296,7 +2278,7 @@ class DFRN
        {
                logger("Process verb ".$item["verb"]." and object-type ".$item["object-type"]." for entrytype ".$entrytype, LOGGER_DEBUG);
 
-               if (($entrytype == DFRN_TOP_LEVEL)) {
+               if (($entrytype == DFRN::TOP_LEVEL)) {
                        // The filling of the the "contact" variable is done for legcy reasons
                        // The functions below are partly used by ostatus.php as well - where we have this variable
                        $r = q("SELECT * FROM `contact` WHERE `id` = %d", intval($importer["id"]));
@@ -2338,9 +2320,9 @@ class DFRN
                                // only one like or dislike per person
                                // splitted into two queries for performance issues
                                $r = q(
-                                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `parent-uri` = '%s' AND NOT `deleted` LIMIT 1",
+                                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-id` = %d AND `verb` = '%s' AND `parent-uri` = '%s' AND NOT `deleted` LIMIT 1",
                                        intval($item["uid"]),
-                                       dbesc($item["author-link"]),
+                                       intval($item["author-id"]),
                                        dbesc($item["verb"]),
                                        dbesc($item["parent-uri"])
                                );
@@ -2349,9 +2331,9 @@ class DFRN
                                }
 
                                $r = q(
-                                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1",
+                                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `author-id` = %d AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1",
                                        intval($item["uid"]),
-                                       dbesc($item["author-link"]),
+                                       intval($item["author-id"]),
                                        dbesc($item["verb"]),
                                        dbesc($item["parent-uri"])
                                );
@@ -2473,16 +2455,14 @@ class DFRN
                // Fetch the owner
                $owner = self::fetchauthor($xpath, $entry, $importer, "dfrn:owner", true);
 
-               $item["owner-name"] = $owner["name"];
                $item["owner-link"] = $owner["link"];
-               $item["owner-avatar"] = $owner["avatar"];
+               $item["owner-id"] = Contact::getIdForURL($owner["link"], 0);
 
                // fetch the author
                $author = self::fetchauthor($xpath, $entry, $importer, "atom:author", true);
 
-               $item["author-name"] = $author["name"];
                $item["author-link"] = $author["link"];
-               $item["author-avatar"] = $author["avatar"];
+               $item["author-id"] = Contact::getIdForURL($author["link"], 0);
 
                $item["title"] = $xpath->query("atom:title/text()", $entry)->item(0)->nodeValue;
 
@@ -2640,7 +2620,7 @@ class DFRN
                $entrytype = self::getEntryType($importer, $item);
 
                // Now assign the rest of the values that depend on the type of the message
-               if (in_array($entrytype, [DFRN_REPLY, DFRN_REPLY_RC])) {
+               if (in_array($entrytype, [DFRN::REPLY, DFRN::REPLY_RC])) {
                        if (!isset($item["object-type"])) {
                                $item["object-type"] = ACTIVITY_OBJ_COMMENT;
                        }
@@ -2662,10 +2642,10 @@ class DFRN
                        }
                }
 
-               if ($entrytype == DFRN_REPLY_RC) {
+               if ($entrytype == DFRN::REPLY_RC) {
                        $item["type"] = "remote-comment";
                        $item["wall"] = 1;
-               } elseif ($entrytype == DFRN_TOP_LEVEL) {
+               } elseif ($entrytype == DFRN::TOP_LEVEL) {
                        if (!isset($item["object-type"])) {
                                $item["object-type"] = ACTIVITY_OBJ_NOTE;
                        }
@@ -2714,7 +2694,7 @@ class DFRN
                        return;
                }
 
-               if (in_array($entrytype, [DFRN_REPLY, DFRN_REPLY_RC])) {
+               if (in_array($entrytype, [DFRN::REPLY, DFRN::REPLY_RC])) {
                        $posted_id = Item::insert($item);
                        $parent = 0;
 
@@ -2725,26 +2705,9 @@ class DFRN
                                        Item::distribute($posted_id);
                                }
 
-                               $item["id"] = $posted_id;
-
-                               $r = q(
-                                       "SELECT `parent`, `parent-uri` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-                                       intval($posted_id),
-                                       intval($importer["importer_uid"])
-                               );
-                               if (DBM::is_result($r)) {
-                                       $parent = $r[0]["parent"];
-                                       $parent_uri = $r[0]["parent-uri"];
-                               }
-
-                               if ($posted_id && $parent && ($entrytype == DFRN_REPLY_RC)) {
-                                       logger("Notifying followers about comment ".$posted_id, LOGGER_DEBUG);
-                                       Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $posted_id);
-                               }
-
                                return true;
                        }
-               } else { // $entrytype == DFRN_TOP_LEVEL
+               } else { // $entrytype == DFRN::TOP_LEVEL
                        if (($importer["uid"] == 0) && ($importer["importer_uid"] != 0)) {
                                logger("Contact ".$importer["id"]." isn't known to user ".$importer["importer_uid"].". The post will be ignored.", LOGGER_DEBUG);
                                return;
@@ -2757,9 +2720,8 @@ class DFRN
                                 * but we're going to unconditionally correct it here so that the post will always be owned by our contact.
                                 */
                                logger('Correcting item owner.', LOGGER_DEBUG);
-                               $item["owner-name"]   = $importer["senderName"];
-                               $item["owner-link"]   = $importer["url"];
-                               $item["owner-avatar"] = $importer["thumb"];
+                               $item["owner-link"] = $importer["url"];
+                               $item["owner-id"] = Contact::getIdForURL($importer["url"], 0);
                        }
 
                        if (($importer["rel"] == CONTACT_IS_FOLLOWER) && (!self::tgroupCheck($importer["importer_uid"], $item))) {
@@ -2835,23 +2797,13 @@ class DFRN
                        }
                }
 
-               $entrytype = self::getEntryType($importer, $item);
-
-               if (!$item["deleted"]) {
-                       logger('deleting item '.$item["id"].' uri='.$uri, LOGGER_DEBUG);
-               } else {
+               if ($item["deleted"]) {
                        return;
                }
 
-               Item::deleteById($item["id"]);
+               logger('deleting item '.$item['id'].' uri='.$uri, LOGGER_DEBUG);
 
-               if ($entrytype != DFRN_TOP_LEVEL) {
-                       // if this is a relayed delete, propagate it to other recipients
-                       if ($entrytype == DFRN_REPLY_RC) {
-                               logger("Notifying followers about deletion of post " . $item["id"], LOGGER_DEBUG);
-                               Worker::add(PRIORITY_HIGH, "Notifier", "drop", $item["id"]);
-                       }
-               }
+               Item::delete(['id' => $item['id']]);
        }
 
        /**