Updated database calls

This commit is contained in:
Michael 2017-12-19 06:47:39 +00:00
parent dab83c8b40
commit ddc25583be

View File

@ -65,22 +65,19 @@ class PortableContact
$a = get_app(); $a = get_app();
if ($cid) { if ($cid) {
if ((! $url) || (! $uid)) { if (!$url || !$uid) {
$r = q( $r = dba::select('contact', ['poco', 'uid'], ['id' => $cid], ['limit' => 1]);
"select `poco`, `uid` from `contact` where `id` = %d limit 1",
intval($cid)
);
if (DBM::is_result($r)) { if (DBM::is_result($r)) {
$url = $r[0]['poco']; $url = $r['poco'];
$uid = $r[0]['uid']; $uid = $r['uid'];
} }
} }
if (! $uid) { if (!$uid) {
return; return;
} }
} }
if (! $url) { if (!$url) {
return; return;
} }
@ -203,12 +200,8 @@ class PortableContact
} }
logger("load: loaded $total entries", LOGGER_DEBUG); logger("load: loaded $total entries", LOGGER_DEBUG);
q( $condition = ["`cid` = ? AND `uid` = ? AND `zcid` = ? AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", $cid, $uid, $zcid];
"DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", dba::delete('glink', $condition);
intval($cid),
intval($uid),
intval($zcid)
);
} }
public static function reachable($profile, $server = "", $network = "", $force = false) public static function reachable($profile, $server = "", $network = "", $force = false)
@ -344,11 +337,8 @@ class PortableContact
if ($server_url != "") { if ($server_url != "") {
if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) { if (!self::checkServer($server_url, $gcontacts[0]["network"], $force)) {
if ($force) { if ($force) {
q( $fields = ['last_failure' => datetime_convert()];
"UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
dbesc(datetime_convert()),
dbesc(normalise_link($profile))
);
} }
logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG); logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
@ -428,11 +418,8 @@ class PortableContact
GContact::update($contact); GContact::update($contact);
if (trim($noscrape["updated"]) != "") { if (trim($noscrape["updated"]) != "") {
q( $fields = ['last_contact' => datetime_convert()];
"UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'", dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
dbesc(datetime_convert()),
dbesc(normalise_link($profile))
);
logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG); logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
@ -460,8 +447,7 @@ class PortableContact
&& (normalise_link($profile) != normalise_link($data["url"])) && (normalise_link($profile) != normalise_link($data["url"]))
) { ) {
// Delete the old entry // Delete the old entry
q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile))); dba::delete('gcontact', ['nurl' => normalise_link($profile)]);
q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
$gcontact = array_merge($gcontacts[0], $data); $gcontact = array_merge($gcontacts[0], $data);
@ -481,11 +467,8 @@ class PortableContact
} }
if (($data["poll"] == "") || (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) { if (($data["poll"] == "") || (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
q( $fields = ['last_failure' => datetime_convert()];
"UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
dbesc(datetime_convert()),
dbesc(normalise_link($profile))
);
logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG); logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
return false; return false;
@ -500,11 +483,8 @@ class PortableContact
$feedret = z_fetch_url($data["poll"]); $feedret = z_fetch_url($data["poll"]);
if (!$feedret["success"]) { if (!$feedret["success"]) {
q( $fields = ['last_failure' => datetime_convert()];
"UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
dbesc(datetime_convert()),
dbesc(normalise_link($profile))
);
logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG); logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
return false; return false;
@ -537,18 +517,12 @@ class PortableContact
$last_updated = NULL_DATE; $last_updated = NULL_DATE;
} }
} }
q( $fields = ['updated' => DBM::date($last_updated), 'last_contact' => DBM::date()];
"UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'", dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
dbesc(DBM::date($last_updated)),
dbesc(DBM::date()),
dbesc(normalise_link($profile))
);
if (($gcontacts[0]["generation"] == 0)) { if (($gcontacts[0]["generation"] == 0)) {
q( $fields = ['generation' => 9];
"UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'", dba::update('gcontact', $fields, ['nurl' => normalise_link($profile)]);
dbesc(normalise_link($profile))
);
} }
logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG); logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
@ -834,32 +808,30 @@ class PortableContact
return false; return false;
} }
$servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url))); $servers = dba::select('gserver', [], ['nurl' => normalise_link($server_url)], ['limit' => 1]);
if (DBM::is_result($servers)) { if (DBM::is_result($servers)) {
if ($servers[0]["created"] <= NULL_DATE) { if ($servers["created"] <= NULL_DATE) {
q( $fields = ['created' => datetime_convert()];
"UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'", $condition = ['nurl' => normalise_link($server_url)];
dbesc(datetime_convert()), dba::update('gserver', $fields, $condition);
dbesc(normalise_link($server_url))
);
} }
$poco = $servers[0]["poco"]; $poco = $servers["poco"];
$noscrape = $servers[0]["noscrape"]; $noscrape = $servers["noscrape"];
if ($network == "") { if ($network == "") {
$network = $servers[0]["network"]; $network = $servers["network"];
} }
$last_contact = $servers[0]["last_contact"]; $last_contact = $servers["last_contact"];
$last_failure = $servers[0]["last_failure"]; $last_failure = $servers["last_failure"];
$version = $servers[0]["version"]; $version = $servers["version"];
$platform = $servers[0]["platform"]; $platform = $servers["platform"];
$site_name = $servers[0]["site_name"]; $site_name = $servers["site_name"];
$info = $servers[0]["info"]; $info = $servers["info"];
$register_policy = $servers[0]["register_policy"]; $register_policy = $servers["register_policy"];
$registered_users = $servers[0]["registered-users"]; $registered_users = $servers["registered-users"];
if (!$force && !self::updateNeeded($servers[0]["created"], "", $last_failure, $last_contact)) { if (!$force && !self::updateNeeded($servers["created"], "", $last_failure, $last_contact)) {
logger("Use cached data for server ".$server_url, LOGGER_DEBUG); logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
return ($last_contact >= $last_failure); return ($last_contact >= $last_failure);
} }
@ -876,7 +848,7 @@ class PortableContact
$last_contact = NULL_DATE; $last_contact = NULL_DATE;
$last_failure = NULL_DATE; $last_failure = NULL_DATE;
} }
logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers[0]["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG); logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
$failure = false; $failure = false;
$possible_failure = false; $possible_failure = false;
@ -1241,27 +1213,24 @@ class PortableContact
} }
// Check again if the server exists // Check again if the server exists
$servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url))); $found = dba::exists('gserver', array('nurl' => normalise_link($server_url)));
$version = strip_tags($version); $version = strip_tags($version);
$site_name = strip_tags($site_name); $site_name = strip_tags($site_name);
$info = strip_tags($info); $info = strip_tags($info);
$platform = strip_tags($platform); $platform = strip_tags($platform);
if ($servers) { $fields = ['url' => $server_url, 'version' => $version,
$fields = ['url' => $server_url, 'version' => $version, 'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy,
'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy, 'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network, 'platform' => $platform, 'registered-users' => $registered_users,
'platform' => $platform, 'registered-users' => $registered_users, 'last_contact' => $last_contact, 'last_failure' => last_failure];
'last_contact' => $last_contact, 'last_failure' => last_failure];
if ($found) {
dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]); dba::update('gserver', $fields, ['nurl' => normalise_link($server_url)]);
} elseif (!$failure) { } elseif (!$failure) {
$fields = ['url' => $server_url, 'nurl' => normalise_link($server_url), 'version' => $version, $fields['nurl'] = normalise_link($server_url);
'site_name' => $site_name, 'info' => $info, 'register_policy' => $register_policy, $fields['created'] = datetime_convert();
'poco' => $poco, 'noscrape' => $noscrape, 'network' => $network,
'platform' => $platform, 'registered-users' => $registered_users,
'created' => datetime_convert(),
'last_contact' => $last_contact, 'last_failure' => last_failure];
dba::insert('gserver', $fields); dba::insert('gserver', $fields);
} }
logger("End discovery for server " . $server_url, LOGGER_DEBUG); logger("End discovery for server " . $server_url, LOGGER_DEBUG);
@ -1425,7 +1394,8 @@ class PortableContact
} }
} }
q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"])); $fields = ['last_poco_query' => datetime_convert()];
dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
return true; return true;
} else { } else {
@ -1433,7 +1403,8 @@ class PortableContact
self::checkServer($server["url"], $server["network"], true); self::checkServer($server["url"], $server["network"], true);
// If we couldn't reach the server, we will try it some time later // 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"])); $fields = ['last_poco_query' => datetime_convert()];
dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
return false; return false;
} }
@ -1458,7 +1429,8 @@ class PortableContact
foreach ($r as $server) { foreach ($r as $server) {
if (!self::checkServer($server["url"], $server["network"])) { if (!self::checkServer($server["url"], $server["network"])) {
// The server is not reachable? Okay, then we will try it later // 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"])); $fields = ['last_poco_query' => datetime_convert()];
dba::update('gserver', $fields, ['nurl' => $server["nurl"]]);
continue; continue;
} }