Merge pull request #3580 from annando/issue-3571-2

Reworked "get_contact"
This commit is contained in:
Tobias Diekershoff 2017-07-13 08:34:59 +02:00 committed by GitHub
commit 86934119b1
2 changed files with 94 additions and 91 deletions

View file

@ -517,7 +517,7 @@ function contacts_not_grouped($uid,$start = 0,$count = 0) {
* @return integer Contact ID * @return integer Contact ID
*/ */
function get_contact($url, $uid = 0, $no_update = false) { function get_contact($url, $uid = 0, $no_update = false) {
logger("Get contact data for url ".$url." and user ".$uid." - ".App::callstack(), LOGGER_DEBUG);; logger("Get contact data for url ".$url." and user ".$uid." - ".App::callstack(), LOGGER_DEBUG);
$data = array(); $data = array();
$contact_id = 0; $contact_id = 0;
@ -527,39 +527,28 @@ function get_contact($url, $uid = 0, $no_update = false) {
} }
// We first try the nurl (http://server.tld/nick), most common case // We first try the nurl (http://server.tld/nick), most common case
$contacts = q("SELECT `id`, `avatar-date` FROM `contact` $contact = dba::select('contact', array('id', 'avatar-date'), array('nurl' => normalise_link($url), 'uid' => $uid), array('limit' => 1));
WHERE `nurl` = '%s'
AND `uid` = %d",
dbesc(normalise_link($url)),
intval($uid));
// Then the addr (nick@server.tld) // Then the addr (nick@server.tld)
if (! dbm::is_result($contacts)) { if (!dbm::is_result($contact)) {
$contacts = q("SELECT `id`, `avatar-date` FROM `contact` $contact = dba::select('contact', array('id', 'avatar-date'), array('addr' => $url, 'uid' => $uid), array('limit' => 1));
WHERE `addr` = '%s'
AND `uid` = %d",
dbesc($url),
intval($uid));
} }
// Then the alias (which could be anything) // Then the alias (which could be anything)
if (! dbm::is_result($contacts)) { if (!dbm::is_result($contact)) {
$contacts = q("SELECT `id`, `avatar-date` FROM `contact` $r = dba::p("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN (?, ?) AND `uid` = ? LIMIT 1",
WHERE `alias` IN ('%s', '%s') $url, normalise_link($url), $uid);
AND `uid` = %d", $contact = dba::fetch($r);
dbesc($url), dba::close($r);
dbesc(normalise_link($url)),
intval($uid));
} }
if (dbm::is_result($contacts)) { if (dbm::is_result($contact)) {
$contact_id = $contacts[0]["id"]; $contact_id = $contact["id"];
// Update the contact every 7 days // Update the contact every 7 days
$update_photo = ($contacts[0]['avatar-date'] < datetime_convert('','','now -7 days')); $update_contact = ($contact['avatar-date'] < datetime_convert('','','now -7 days'));
if (!$update_photo || $no_update) { if (!$update_contact || $no_update) {
return $contact_id; return $contact_id;
} }
} elseif ($uid != 0) { } elseif ($uid != 0) {
@ -576,45 +565,29 @@ function get_contact($url, $uid = 0, $no_update = false) {
} }
// Get data from the gcontact table // Get data from the gcontact table
$gcontacts = q("SELECT `name`, `nick`, `url`, `photo`, `addr`, `alias`, `network` FROM `gcontact` WHERE `nurl` = '%s'", $gcontacts = dba::select('gcontact', array('name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'),
dbesc(normalise_link($url))); array('nurl' => normalise_link($url)), array('limit' => 1));
if (!$gcontacts) { if (!dbm::is_result($gcontacts)) {
return 0; return 0;
} }
$data = $gcontacts[0]; $data = array_merge($data, $gcontacts);
} }
$url = $data["url"]; $url = $data["url"];
if (!$contact_id) { if (!$contact_id) {
q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`, dba::insert('contact', array('uid' => $uid, 'created' => datetime_convert(), 'url' => $data["url"],
`name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`, 'nurl' => normalise_link($data["url"]), 'addr' => $data["addr"],
`batch`, `request`, `confirm`, `poco`, `name-date`, `uri-date`, 'alias' => $data["alias"], 'notify' => $data["notify"], 'poll' => $data["poll"],
`writable`, `blocked`, `readonly`, `pending`) 'name' => $data["name"], 'nick' => $data["nick"], 'photo' => $data["photo"],
VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', 1, 0, 0, 0)", 'keywords' => $data["keywords"], 'location' => $data["location"], 'about' => $data["about"],
intval($uid), 'network' => $data["network"], 'pubkey' => $data["pubkey"],
dbesc(datetime_convert()), 'rel' => CONTACT_IS_SHARING, 'priority' => $data["priority"],
dbesc($data["url"]), 'batch' => $data["batch"], 'request' => $data["request"],
dbesc(normalise_link($data["url"])), 'confirm' => $data["confirm"], 'poco' => $data["poco"],
dbesc($data["addr"]), 'name-date' => datetime_convert(), 'uri-date' => datetime_convert(),
dbesc($data["alias"]), 'avatar-date' => datetime_convert(), 'writable' => 1, 'blocked' => 0,
dbesc($data["notify"]), 'readonly' => 0, 'pending' => 0));
dbesc($data["poll"]),
dbesc($data["name"]),
dbesc($data["nick"]),
dbesc($data["photo"]),
dbesc($data["network"]),
dbesc($data["pubkey"]),
intval(CONTACT_IS_SHARING),
intval($data["priority"]),
dbesc($data["batch"]),
dbesc($data["request"]),
dbesc($data["confirm"]),
dbesc($data["poco"]),
dbesc(datetime_convert()),
dbesc(datetime_convert())
);
$contacts = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2", $contacts = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
dbesc(normalise_link($data["url"])), dbesc(normalise_link($data["url"])),
@ -626,49 +599,65 @@ function get_contact($url, $uid = 0, $no_update = false) {
$contact_id = $contacts[0]["id"]; $contact_id = $contacts[0]["id"];
// Update the newly created contact from data in the gcontact table // Update the newly created contact from data in the gcontact table
$gcontacts = q("SELECT `location`, `about`, `keywords`, `gender` FROM `gcontact` WHERE `nurl` = '%s'", $gcontact = dba::select('gcontact', array('location', 'about', 'keywords', 'gender'),
dbesc(normalise_link($data["url"]))); array('nurl' => normalise_link($data["url"])), array('limit' => 1));
if (dbm::is_result($gcontacts)) { if (dbm::is_result($gcontact)) {
logger("Update contact " . $data["url"] . ' from gcontact'); // Only use the information when the probing hadn't fetched these values
q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `id` = %d", if ($data['keywords'] != '') {
dbesc($gcontacts[0]["location"]), dbesc($gcontacts[0]["about"]), dbesc($gcontacts[0]["keywords"]), unset($gcontact['keywords']);
dbesc($gcontacts[0]["gender"]), intval($contact_id));
} }
if ($data['location'] != '') {
unset($gcontact['location']);
}
if ($data['about'] != '') {
unset($gcontact['about']);
}
dba::update('contact', $gcontact, array('id' => $contact_id));
} }
if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $url != "") { if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") {
q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0 AND `id` != %d AND NOT `self`", dba::e("DELETE FROM `contact` WHERE `nurl` = ? AND `uid` = 0 AND `id` != ? AND NOT `self`",
dbesc(normalise_link($url)), normalise_link($data["url"]), $contact_id);
intval($contact_id)); }
} }
require_once "Photo.php"; require_once "Photo.php";
update_contact_avatar($data["photo"], $uid, $contact_id); update_contact_avatar($data["photo"], $uid, $contact_id);
$contacts = q("SELECT `addr`, `alias`, `name`, `nick` FROM `contact` WHERE `id` = %d", intval($contact_id)); $contact = dba::select('contact', array('addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date'),
array('id' => $contact_id), array('limit' => 1));
// This condition should always be true // This condition should always be true
if (!dbm::is_result($contacts)) { if (!dbm::is_result($contact)) {
return $contact_id; return $contact_id;
} }
// Only update if there had something been changed $updated = array('addr' => $data['addr'],
if ($data["addr"] != $contacts[0]["addr"] || 'alias' => $data['alias'],
$data["alias"] != $contacts[0]["alias"] || 'name' => $data['name'],
$data["name"] != $contacts[0]["name"] || 'nick' => $data['nick']);
$data["nick"] != $contacts[0]["nick"]) {
q("UPDATE `contact` SET `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s', if ($data['keywords'] != '') {
`name-date` = '%s', `uri-date` = '%s' WHERE `id` = %d", $updated['keywords'] = $data['keywords'];
dbesc($data["addr"]),
dbesc($data["alias"]),
dbesc($data["name"]),
dbesc($data["nick"]),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
intval($contact_id)
);
} }
if ($data['location'] != '') {
$updated['location'] = $data['location'];
}
if ($data['about'] != '') {
$updated['about'] = $data['about'];
}
if (($data["addr"] != $contact["addr"]) || ($data["alias"] != $contact["alias"])) {
$updated['uri-date'] = datetime_convert();
}
if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) {
$updated['name-date'] = datetime_convert();
}
$updated['avatar-date'] = datetime_convert();
dba::update('contact', $updated, array('id' => $contact_id), $contact);
return $contact_id; return $contact_id;
} }

View file

@ -638,17 +638,22 @@ class dba {
} }
if (self::$dbo->errorno != 0) { if (self::$dbo->errorno != 0) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
$called_from = array_shift($trace);
// We are having an own error logging in the function "p" if (isset($trace[2])) {
if ($called_from['function'] != 'p') { $called_from = $trace[2];
} else {
// We use just something that is defined to avoid warnings
$called_from = $trace[0];
}
// We are having an own error logging in the function "e"
if ($called_from['function'] != 'e') {
// We have to preserve the error code, somewhere in the logging it get lost // We have to preserve the error code, somewhere in the logging it get lost
$error = self::$dbo->error; $error = self::$dbo->error;
$errorno = self::$dbo->errorno; $errorno = self::$dbo->errorno;
logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n". logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
$a->callstack(8))."\n".self::replace_parameters($sql, $args); $a->callstack(8)."\n".self::replace_parameters($sql, $params));
self::$dbo->error = $error; self::$dbo->error = $error;
self::$dbo->errorno = $errorno; self::$dbo->errorno = $errorno;
@ -711,8 +716,17 @@ class dba {
$error = self::$dbo->error; $error = self::$dbo->error;
$errorno = self::$dbo->errorno; $errorno = self::$dbo->errorno;
array_shift($args);
// When the second function parameter is an array then use this as the parameter array
if ((count($args) > 0) && (is_array($args[0]))) {
$params = $args[0];
} else {
$params = $args;
}
logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n". logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
$a->callstack(8))."\n".self::replace_parameters($sql, $args); $a->callstack(8)."\n".self::replace_parameters($sql, $params));
self::$dbo->error = $error; self::$dbo->error = $error;
self::$dbo->errorno = $errorno; self::$dbo->errorno = $errorno;