From 2a77e426f9a26a2584da78c90daf8ec1d49fcc00 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 27 Jan 2017 17:00:34 +0000 Subject: [PATCH 1/4] Split poco discovery in smaller function calls --- include/socgraph.php | 124 ++++++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 54 deletions(-) diff --git a/include/socgraph.php b/include/socgraph.php index 32c151c04..b787452b8 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -1260,6 +1260,71 @@ function poco_discover_federation() { set_config('poco','last_federation_discovery', time()); } +function poco_discover_single_server($id) { + $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `id` = %d", intval($id)); + if (!dbm::is_result($r)) { + return false; + } + + $server = $r[0]; + + if (!poco_check_server($server["url"], $server["network"])) { + // The server is not reachable? Okay, then we will try it later + q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); + return false; + } + + // Fetch all users from the other server + $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation"; + + logger("Fetch all users from the server ".$server["nurl"], LOGGER_DEBUG); + + $retdata = z_fetch_url($url); + if ($retdata["success"]) { + $data = json_decode($retdata["body"]); + + poco_discover_server($data, 2); + + if (get_config('system','poco_discovery') > 1) { + + $timeframe = get_config('system','poco_discovery_since'); + if ($timeframe == 0) { + $timeframe = 30; + } + + $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400); + + // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3) + $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation"; + + $success = false; + + $retdata = z_fetch_url($url); + if ($retdata["success"]) { + logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG); + $success = poco_discover_server(json_decode($retdata["body"])); + } + + if (!$success AND (get_config('system','poco_discovery') > 2)) { + logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG); + poco_discover_server_users($data, $server); + } + } + + q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); + + return true; + } else { + // If the server hadn't replied correctly, then force a sanity check + poco_check_server($server["url"], $server["network"], true); + + // If we couldn't reach the server, we will try it some time later + q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); + + return false; + } +} + function poco_discover($complete = false) { // Update the server list @@ -1274,63 +1339,14 @@ function poco_discover($complete = false) { $last_update = date("c", time() - (60 * 60 * 24 * $requery_days)); - $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update)); - if ($r) + $r = q("SELECT `id` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update)); + if (dbm::is_result($r)) { foreach ($r AS $server) { - - if (!poco_check_server($server["url"], $server["network"])) { - // The server is not reachable? Okay, then we will try it later - q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); - continue; - } - - // Fetch all users from the other server - $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation"; - - logger("Fetch all users from the server ".$server["nurl"], LOGGER_DEBUG); - - $retdata = z_fetch_url($url); - if ($retdata["success"]) { - $data = json_decode($retdata["body"]); - - poco_discover_server($data, 2); - - if (get_config('system','poco_discovery') > 1) { - - $timeframe = get_config('system','poco_discovery_since'); - if ($timeframe == 0) - $timeframe = 30; - - $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400); - - // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3) - $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation"; - - $success = false; - - $retdata = z_fetch_url($url); - if ($retdata["success"]) { - logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG); - $success = poco_discover_server(json_decode($retdata["body"])); - } - - if (!$success AND (get_config('system','poco_discovery') > 2)) { - logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG); - poco_discover_server_users($data, $server); - } - } - - q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); - if (!$complete AND (--$no_of_queries == 0)) - break; - } else { - // If the server hadn't replied correctly, then force a sanity check - poco_check_server($server["url"], $server["network"], true); - - // If we couldn't reach the server, we will try it some time later - q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); + if (poco_discover_single_server($server['id']) AND !$complete AND (--$no_of_queries == 0)) { + break; } } + } } function poco_discover_server_users($data, $server) { From 2615d40d6b3d594c648616c28583d00139e89118 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 19 Mar 2017 17:43:59 +0000 Subject: [PATCH 2/4] Split the POCO discovery process in several small processes --- include/discover_poco.php | 6 +++++- include/socgraph.php | 24 ++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/include/discover_poco.php b/include/discover_poco.php index f0bfb646b..e3f1e2903 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -27,6 +27,8 @@ function discover_poco_run(&$argv, &$argc) { $mode = 4; } elseif (($argc == 2) && ($argv[1] == "update_server")) { $mode = 5; + } elseif (($argc == 3) && ($argv[1] == "update_server_directory")) { + $mode = 6; } elseif ($argc == 1) { $search = ""; $mode = 0; @@ -36,7 +38,9 @@ function discover_poco_run(&$argv, &$argc) { logger('start '.$search); - if ($mode == 5) { + if ($mode == 6) { + poco_discover_single_server(intval($argv[2])); + } elseif ($mode == 5) { update_server(); } elseif ($mode == 4) { $server_url = base64_decode($argv[2]); diff --git a/include/socgraph.php b/include/socgraph.php index 67f9e8aa6..c55111626 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -1676,19 +1676,13 @@ function poco_discover_single_server($id) { $server = $r[0]; - if (!poco_check_server($server["url"], $server["network"])) { - // The server is not reachable? Okay, then we will try it later - q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); - return false; - } - - // Discover new servers out there + // Discover new servers out there (Works from Friendica version 3.5.2) poco_fetch_serverlist($server["poco"]); // Fetch all users from the other server $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation"; - logger("Fetch all users from the server ".$server["nurl"], LOGGER_DEBUG); + logger("Fetch all users from the server ".$server["url"], LOGGER_DEBUG); $retdata = z_fetch_url($url); if ($retdata["success"]) { @@ -1750,10 +1744,20 @@ function poco_discover($complete = false) { $last_update = date("c", time() - (60 * 60 * 24 * $requery_days)); - $r = q("SELECT `id` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update)); + $r = q("SELECT `id`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update)); if (dbm::is_result($r)) { foreach ($r AS $server) { - if (poco_discover_single_server($server['id']) AND !$complete AND (--$no_of_queries == 0)) { + + if (!poco_check_server($server["url"], $server["network"])) { + // The server is not reachable? Okay, then we will try it later + q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); + continue; + } + + logger('Update directory from server '.$server['url'].' with ID '.$server['id'], LOGGER_DEBUG); + proc_run(PRIORITY_LOW, "include/discover_poco.php", "update_server_directory", $server['id']); + + if (!$complete AND (--$no_of_queries == 0)) { break; } } From e684fff477a3b9b9d4cd942fd94037b8080b7c31 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 19 Mar 2017 18:24:49 +0000 Subject: [PATCH 3/4] Added documention --- include/discover_poco.php | 1 + 1 file changed, 1 insertion(+) diff --git a/include/discover_poco.php b/include/discover_poco.php index e3f1e2903..416337a2e 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -14,6 +14,7 @@ function discover_poco_run(&$argv, &$argc) { - suggestions: Discover other servers for their contacts. - server : Searches for the poco server list. "poco url" is base64 encoded. - update_server: Frequently check the first 250 servers for vitality. + - update_server_directory: Discover the given server id for their contacts */ if (($argc > 2) && ($argv[1] == "dirsearch")) { From 41ee4ec60a19dc194f639dd185003c6390488719 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 19 Mar 2017 21:32:11 +0000 Subject: [PATCH 4/4] Standards and just some more poco slitting --- include/discover_poco.php | 12 +++++++++++- include/socgraph.php | 32 +++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/include/discover_poco.php b/include/discover_poco.php index 416337a2e..2923cd01f 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -15,6 +15,7 @@ function discover_poco_run(&$argv, &$argc) { - server : Searches for the poco server list. "poco url" is base64 encoded. - update_server: Frequently check the first 250 servers for vitality. - update_server_directory: Discover the given server id for their contacts + - poco_load: Load POCO data from a given POCO address */ if (($argc > 2) && ($argv[1] == "dirsearch")) { @@ -30,6 +31,8 @@ function discover_poco_run(&$argv, &$argc) { $mode = 5; } elseif (($argc == 3) && ($argv[1] == "update_server_directory")) { $mode = 6; + } elseif (($argc > 5) && ($argv[1] == "poco_load")) { + $mode = 7; } elseif ($argc == 1) { $search = ""; $mode = 0; @@ -39,7 +42,14 @@ function discover_poco_run(&$argv, &$argc) { logger('start '.$search); - if ($mode == 6) { + if ($mode == 7) { + if ($argc == 6) { + $url = base64_decode($argv[5]); + } else { + $url = ''; + } + poco_load_worker(intval($argv[2]), intval($argv[3]), intval($argv[4]), $url); + } elseif ($mode == 6) { poco_discover_single_server(intval($argv[2])); } elseif ($mode == 5) { update_server(); diff --git a/include/socgraph.php b/include/socgraph.php index c55111626..f43ad62d0 100644 --- a/include/socgraph.php +++ b/include/socgraph.php @@ -14,8 +14,13 @@ require_once("include/html2bbcode.php"); require_once("include/Contact.php"); require_once("include/Photo.php"); -/* - * poco_load +/** + * @brief Fetch POCO data + * + * @param integer $cid Contact ID + * @param integer $uid User ID + * @param integer $zcid Global Contact ID + * @param integer $url POCO address that should be polled * * Given a contact-id (minimum), load the PortableContacts friend list for that contact, * and add the entries to the gcontact (Global Contact) table, or update existing entries @@ -27,12 +32,21 @@ require_once("include/Photo.php"); * pointing to the same global contact id. * */ +function poco_load($cid, $uid = 0, $zcid = 0, $url = null) { + // Call the function "poco_load_worker" via the worker + proc_run(PRIORITY_LOW, "include/discover_poco.php", "poco_load", $cid, $uid, $zcid, base64_encode($url)); +} - - - -function poco_load($cid,$uid = 0,$zcid = 0,$url = null) { - +/** + * @brief Fetch POCO data from the worker + * + * @param integer $cid Contact ID + * @param integer $uid User ID + * @param integer $zcid Global Contact ID + * @param integer $url POCO address that should be polled + * + */ +function poco_load_worker($cid, $uid, $zcid, $url) { $a = get_app(); if($cid) { @@ -1739,9 +1753,9 @@ function poco_discover($complete = false) { $requery_days = intval(get_config("system", "poco_requery_days")); - if ($requery_days == 0) + if ($requery_days == 0) { $requery_days = 7; - + } $last_update = date("c", time() - (60 * 60 * 24 * $requery_days)); $r = q("SELECT `id`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));