]> git.mxchange.org Git - friendica.git/blobdiff - include/Contact.php
Merge remote-tracking branch 'upstream/develop' into 1607-performance
[friendica.git] / include / Contact.php
index 9cbfdaa173899bc6743efc94c08827d1d018b49e..05e5dc62fcf84a957f5dde7a571330ae89bad951 100644 (file)
@@ -196,6 +196,7 @@ function unmark_for_death($contact) {
  * @brief Get contact data for a given profile link
  *
  * The function looks at several places (contact table and gcontact table) for the contact
+ * It caches its result for the same script execution to prevent duplicate calls
  *
  * @param string $url The profile link
  * @param int $uid User id
@@ -204,8 +205,15 @@ function unmark_for_death($contact) {
  * @return array Contact data
  */
 function get_contact_details_by_url($url, $uid = -1, $default = array()) {
-       if ($uid == -1)
+       static $cache = array();
+
+       if ($uid == -1) {
                $uid = local_user();
+       }
+
+       if (isset($cache[$url][$uid])) {
+               return $cache[$url][$uid];
+       }
 
        // Fetch contact data from the contact table for the given user
        $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
@@ -229,10 +237,13 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) {
 
        if ($r) {
                // If there is more than one entry we filter out the connector networks
-               if (count($r) > 1)
-                       foreach ($r AS $id => $result)
-                               if ($result["network"] == NETWORK_STATUSNET)
+               if (count($r) > 1) {
+                       foreach ($r AS $id => $result) {
+                               if ($result["network"] == NETWORK_STATUSNET) {
                                        unset($r[$id]);
+                               }
+                       }
+               }
 
                $profile = array_shift($r);
 
@@ -251,31 +262,40 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) {
                        $profile["bd"] = $current_year."-".$month."-".$day;
                        $current = $current_year."-".$current_month."-".$current_day;
 
-                       if ($profile["bd"] < $current)
+                       if ($profile["bd"] < $current) {
                                $profile["bd"] = (++$current_year)."-".$month."-".$day;
-               } else
+                       }
+               } else {
                        $profile["bd"] = "0000-00-00";
-       } else
+               }
+       } else {
                $profile = $default;
+       }
 
-       if (($profile["photo"] == "") AND isset($default["photo"]))
+       if (($profile["photo"] == "") AND isset($default["photo"])) {
                $profile["photo"] = $default["photo"];
+       }
 
-       if (($profile["name"] == "") AND isset($default["name"]))
+       if (($profile["name"] == "") AND isset($default["name"])) {
                $profile["name"] = $default["name"];
+       }
 
-       if (($profile["network"] == "") AND isset($default["network"]))
+       if (($profile["network"] == "") AND isset($default["network"])) {
                $profile["network"] = $default["network"];
+       }
 
-       if (($profile["thumb"] == "") AND isset($profile["photo"]))
+       if (($profile["thumb"] == "") AND isset($profile["photo"])) {
                $profile["thumb"] = $profile["photo"];
+       }
 
-       if (($profile["micro"] == "") AND isset($profile["thumb"]))
+       if (($profile["micro"] == "") AND isset($profile["thumb"])) {
                $profile["micro"] = $profile["thumb"];
+       }
 
        if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND
-               in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
+               in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
                proc_run(PRIORITY_LOW, "include/update_gcontact.php", $profile["gid"]);
+       }
 
        // Show contact details of Diaspora contacts only if connected
        if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
@@ -285,7 +305,9 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) {
                $profile["birthday"] = "0000-00-00";
        }
 
-       return($profile);
+       $cache[$url][$uid] = $profile;
+
+       return $profile;
 }
 
 if (! function_exists('contact_photo_menu')) {
@@ -630,57 +652,64 @@ function posts_from_gcontact($a, $gcontact_id) {
 
        return $o;
 }
-
 /**
- * @brief Returns posts from a given contact
+ * @brief Returns posts from a given contact url
  *
  * @param App $a argv application class
- * @param int $contact_id contact
+ * @param int $contact_url Contact URL
  *
  * @return string posts in HTML
  */
-function posts_from_contact($a, $contact_id) {
+function posts_from_contact_url($a, $contact_url) {
 
        require_once('include/conversation.php');
 
-       $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", intval($contact_id));
-       if (!$r)
-               return false;
+       // There are no posts with "uid = 0" with connector networks
+       // This speeds up the query a lot
+       $r = q("SELECT `network`, `id` AS `author-id` FROM `contact`
+               WHERE `contact`.`nurl` = '%s' AND `contact`.`uid` = 0",
+               dbesc(normalise_link($contact_url)));
+       if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
+               $sql = "(`item`.`uid` = 0 OR  (`item`.`uid` = %d AND `item`.`private`))";
+       else
+               $sql = "`item`.`uid` = %d";
 
-       $contact = $r[0];
+       $author_id = intval($r[0]["author-id"]);
 
-       if(get_config('system', 'old_pager')) {
+       if (get_config('system', 'old_pager')) {
                $r = q("SELECT COUNT(*) AS `total` FROM `item`
-                       WHERE `item`.`uid` = %d AND `author-link` IN ('%s', '%s')",
-                       intval(local_user()),
-                       dbesc(str_replace("https://", "http://", $contact["url"])),
-                       dbesc(str_replace("http://", "https://", $contact["url"])));
+                       WHERE `author-id` = %d and $sql",
+                       intval($author_id),
+                       intval(local_user()));
 
                $a->set_pager_total($r[0]['total']);
        }
 
-       $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
+/*
+"SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
                        `author-name` AS `name`, `owner-avatar` AS `photo`,
                        `owner-link` AS `url`, `owner-avatar` AS `thumb`
-               FROM `item` FORCE INDEX (`uid_contactid_id`)
-               WHERE `item`.`uid` = %d AND `contact-id` = %d
-                       AND `author-link` IN ('%s', '%s')
-                       AND NOT `deleted` AND NOT `moderated` AND `visible`
-               ORDER BY `item`.`id` DESC LIMIT %d, %d",
+               FROM `item` FORCE INDEX (`authorid_created`)
+               WHERE `item`.`author-id` = %d AND $sql
+               AND NOT `deleted` AND NOT `moderated` AND `visible`
+
+*/
+
+       $r = q(item_query()." AND `item`.`author-id` = %d AND ".$sql.
+               " ORDER BY `item`.`created` DESC LIMIT %d, %d",
+               intval($author_id),
                intval(local_user()),
-               intval($contact_id),
-               dbesc(str_replace("https://", "http://", $contact["url"])),
-               dbesc(str_replace("http://", "https://", $contact["url"])),
                intval($a->pager['start']),
                intval($a->pager['itemspage'])
        );
 
-       $o .= conversation($a,$r,'community',false);
+       $o = conversation($a,$r,'community',false);
 
-       if(!get_config('system', 'old_pager'))
+       if(!get_config('system', 'old_pager')) {
                $o .= alt_pager($a,count($r));
-       else
+       } else {
                $o .= paginate($a);
+       }
 
        return $o;
 }