New handling for filling the gcontact table / duplicate prevention

This commit is contained in:
Michael Vogel 2016-01-23 22:36:15 +01:00
parent e18bb43918
commit 60470cfafa
2 changed files with 61 additions and 159 deletions

View File

@ -33,7 +33,7 @@ function gprobe_run(&$argv, &$argc){
$url = hex2bin($argv[1]); $url = hex2bin($argv[1]);
$r = q("select * from gcontact where nurl = '%s' limit 1", $r = q("SELECT `id`, `url`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 1",
dbesc(normalise_link($url)) dbesc(normalise_link($url))
); );
@ -58,21 +58,16 @@ function gprobe_run(&$argv, &$argc){
if (is_null($result)) if (is_null($result))
Cache::set("gprobe:".$urlparts["host"],serialize($arr)); Cache::set("gprobe:".$urlparts["host"],serialize($arr));
if(count($arr) && x($arr,'network') && $arr['network'] === NETWORK_DFRN) { if (!in_array($result["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))
q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`) update_gcontact($arr);
values ( '%s', '%s', '%s', '%s') ",
dbesc($arr['name']), $r = q("SELECT `id`, `url`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 1",
dbesc($arr['url']),
dbesc(normalise_link($arr['url'])),
dbesc($arr['photo'])
);
}
$r = q("select * from gcontact where nurl = '%s' limit 1",
dbesc(normalise_link($url)) dbesc(normalise_link($url))
); );
} }
if(count($r)) if(count($r))
poco_load(0,0,$r[0]['id'], str_replace('/profile/','/poco/',$r[0]['url'])); if ($r[0]["network"] == NETWORK_DFRN)
poco_load(0,0,$r[0]['id'], str_replace('/profile/','/poco/',$r[0]['url']));
logger("gprobe end for ".normalise_link($url), LOGGER_DEBUG); logger("gprobe end for ".normalise_link($url), LOGGER_DEBUG);
return; return;

View File

@ -289,93 +289,25 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca
poco_check_server($server_url, $network); poco_check_server($server_url, $network);
if(count($x)) { $gcontact = array("url" => $profile_url,
$gcid = $x[0]['id']; "addr" => $addr,
"alias" => $alias,
"name" => $name,
"network" => $network,
"photo" => $profile_photo,
"about" => $about,
"location" => $location,
"gender" => $gender,
"keywords" => $keywords,
"server_url" => $server_url,
"connect" => $connect_url,
"notify" => $notify,
"updated" => $updated,
"generation" => $generation);
if (($location == "") AND ($x[0]['location'] != "")) $gcid = update_gcontact($gcontact);
$location = $x[0]['location'];
if (($about == "") AND ($x[0]['about'] != "")) if(!$gcid)
$about = $x[0]['about'];
if (($gender == "") AND ($x[0]['gender'] != ""))
$gender = $x[0]['gender'];
if (($keywords == "") AND ($x[0]['keywords'] != ""))
$keywords = $x[0]['keywords'];
if (($addr == "") AND ($x[0]['addr'] != ""))
$addr = $x[0]['addr'];
if (($alias == "") AND ($x[0]['alias'] != ""))
$alias = $x[0]['alias'];
if (($notify == "") AND ($x[0]['notify'] != ""))
$notify = $x[0]['notify'];
if (($generation == 0) AND ($x[0]['generation'] > 0))
$generation = $x[0]['generation'];
if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
q("UPDATE `gcontact` SET `name` = '%s', `addr` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s', `server_url` = '%s',
`updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d,
`alias` = '$s', `notify` = '%s'
WHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'",
dbesc($name),
dbesc($addr),
dbesc($network),
dbesc($profile_photo),
dbesc($connect_url),
dbesc($profile_url),
dbesc($server_url),
dbesc($updated),
dbesc($location),
dbesc($about),
dbesc($keywords),
dbesc($gender),
dbesc($alias),
dbesc($notify),
intval($generation),
intval($generation),
dbesc(normalise_link($profile_url))
);
}
} else {
// Maybe another process had inserted the entry after the first check, so it again
$x = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
dbesc(normalise_link($profile_url))
);
if(!$x) {
q("INSERT INTO `gcontact` (`name`, `nick`, `addr`, `network`, `url`, `nurl`, `photo`, `connect`, `server_url`, `created`, `updated`, `location`, `about`, `keywords`, `gender`, `alias`, `notify`, `generation`)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)",
dbesc($name),
dbesc($nick),
dbesc($addr),
dbesc($network),
dbesc($profile_url),
dbesc(normalise_link($profile_url)),
dbesc($profile_photo),
dbesc($connect_url),
dbesc($server_url),
dbesc(datetime_convert()),
dbesc($updated),
dbesc($location),
dbesc($about),
dbesc($keywords),
dbesc($gender),
dbesc($alias),
dbesc($notify),
intval($generation)
);
$x = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
dbesc(normalise_link($profile_url))
);
}
if(count($x))
$gcid = $x[0]['id'];
}
if(! $gcid)
return $gcid; return $gcid;
$r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1", $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
@ -402,13 +334,6 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca
); );
} }
// For unknown reasons there are sometimes duplicates
//q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND
// NOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)",
// dbesc(normalise_link($profile_url)),
// intval($gcid)
//);
return $gcid; return $gcid;
} }
@ -1540,7 +1465,7 @@ function get_gcontact_id($contact) {
if ($contact["network"] == NETWORK_STATUSNET) if ($contact["network"] == NETWORK_STATUSNET)
$contact["network"] = NETWORK_OSTATUS; $contact["network"] = NETWORK_OSTATUS;
$r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1", $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
dbesc(normalise_link($contact["url"]))); dbesc(normalise_link($contact["url"])));
if ($r) if ($r)
@ -1562,13 +1487,18 @@ function get_gcontact_id($contact) {
intval($contact["generation"]) intval($contact["generation"])
); );
$r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1", $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
dbesc(normalise_link($contact["url"]))); dbesc(normalise_link($contact["url"])));
if ($r) if ($r)
$gcontact_id = $r[0]["id"]; $gcontact_id = $r[0]["id"];
} }
if ((count($r) > 1) AND ($gcontact_id > 0) AND ($contact["url"] != ""))
q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d",
dbesc(normalise_link($contact["url"])),
intval($gcontact_id));
return $gcontact_id; return $gcontact_id;
} }
@ -1587,78 +1517,55 @@ function update_gcontact($contact) {
if (!$gcontact_id) if (!$gcontact_id)
return false; return false;
$r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`, `hide`, `nsfw`, `network`, `alias`, `notify`, `url` $r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`,
`hide`, `nsfw`, `network`, `alias`, `notify`, `server_url`, `connect`, `updated`, `url`
FROM `gcontact` WHERE `id` = %d LIMIT 1", FROM `gcontact` WHERE `id` = %d LIMIT 1",
intval($gcontact_id)); intval($gcontact_id));
if ($contact["generation"] == 0) // Get all field names
$contact["generation"] = $r[0]["generation"]; $fields = array();
foreach ($r[0] AS $field => $data)
$fields[$field] = $data;
if ($contact["photo"] == "") unset($fields["url"]);
$contact["photo"] = $r[0]["photo"]; unset($fields["updated"]);
if ($contact["name"] == "") // assign all unassigned fields from the database entry
$contact["name"] = $r[0]["name"]; foreach ($fields AS $field => $data)
if (!isset($contact[$field]))
if ($contact["nick"] == "") $contact[$field] = $r[0][$field];
$contact["nick"] = $r[0]["nick"];
if ($contact["addr"] == "")
$contact["addr"] = $r[0]["addr"];
if ($contact["location"] =="")
$contact["location"] = $r[0]["location"];
if ($contact["about"] =="")
$contact["about"] = $r[0]["about"];
if ($contact["birthday"] =="")
$contact["birthday"] = $r[0]["birthday"];
if ($contact["gender"] =="")
$contact["gender"] = $r[0]["gender"];
if ($contact["keywords"] =="")
$contact["keywords"] = $r[0]["keywords"];
if (!isset($contact["hide"]))
$contact["hide"] = $r[0]["hide"];
if (!isset($contact["nsfw"]))
$contact["nsfw"] = $r[0]["nsfw"];
if ($contact["network"] =="")
$contact["network"] = $r[0]["network"];
if ($contact["alias"] =="")
$contact["alias"] = $r[0]["alias"];
if ($contact["url"] =="")
$contact["url"] = $r[0]["url"];
if ($contact["notify"] =="")
$contact["notify"] = $r[0]["notify"];
if ($contact["network"] == NETWORK_STATUSNET) if ($contact["network"] == NETWORK_STATUSNET)
$contact["network"] = NETWORK_OSTATUS; $contact["network"] = NETWORK_OSTATUS;
if (($contact["photo"] != $r[0]["photo"]) OR ($contact["name"] != $r[0]["name"]) OR ($contact["nick"] != $r[0]["nick"]) OR ($contact["addr"] != $r[0]["addr"]) OR if (!isset($contact["updated"]))
($contact["birthday"] != $r[0]["birthday"]) OR ($contact["gender"] != $r[0]["gender"]) OR ($contact["keywords"] != $r[0]["keywords"]) OR $contact["updated"] = datetime_convert();
($contact["hide"] != $r[0]["hide"]) OR ($contact["nsfw"] != $r[0]["nsfw"]) OR ($contact["network"] != $r[0]["network"]) OR
($contact["alias"] != $r[0]["alias"]) OR ($contact["notify"] != $r[0]["notify"]) OR ($contact["url"] != $r[0]["url"]) OR
($contact["location"] != $r[0]["location"]) OR ($contact["about"] != $r[0]["about"]) OR ($contact["generation"] < $r[0]["generation"])) {
// Check if any field changed
$update = false;
unset($fields["generation"]);
foreach ($fields AS $field => $data)
if ($contact[$field] != $r[0][$field])
$update = true;
if ($contact["generation"] < $r[0]["generation"])
$update = true;
if ($update) {
q("UPDATE `gcontact` SET `photo` = '%s', `name` = '%s', `nick` = '%s', `addr` = '%s', `network` = '%s', q("UPDATE `gcontact` SET `photo` = '%s', `name` = '%s', `nick` = '%s', `addr` = '%s', `network` = '%s',
`birthday` = '%s', `gender` = '%s', `keywords` = %d, `hide` = %d, `nsfw` = %d, `birthday` = '%s', `gender` = '%s', `keywords` = %d, `hide` = %d, `nsfw` = %d,
`alias` = '%s', `notify` = '%s', `url` = '%s', `alias` = '%s', `notify` = '%s', `url` = '%s',
`location` = '%s', `about` = '%s', `generation` = %d, `updated` = '%s' `location` = '%s', `about` = '%s', `generation` = %d, `updated` = '%s',
`server_url` = '%s', `connect` = '%s'
WHERE `nurl` = '%s' AND (`generation` = 0 OR `generation` >= %d)", WHERE `nurl` = '%s' AND (`generation` = 0 OR `generation` >= %d)",
dbesc($contact["photo"]), dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["photo"]), dbesc($contact["name"]), dbesc($contact["nick"]),
dbesc($contact["addr"]), dbesc($contact["network"]), dbesc($contact["birthday"]), dbesc($contact["addr"]), dbesc($contact["network"]), dbesc($contact["birthday"]),
dbesc($contact["gender"]), dbesc($contact["keywords"]), intval($contact["hide"]), dbesc($contact["gender"]), dbesc($contact["keywords"]), intval($contact["hide"]),
intval($contact["nsfw"]), dbesc($contact["alias"]), dbesc($contact["notify"]), intval($contact["nsfw"]), dbesc($contact["alias"]), dbesc($contact["notify"]),
dbesc($contact["url"]), dbesc($contact["location"]), dbesc($contact["about"]), dbesc($contact["url"]), dbesc($contact["location"]), dbesc($contact["about"]),
intval($contact["generation"]), dbesc(datetime_convert()), intval($contact["generation"]), dbesc($contact["updated"]),
dbesc($contact["server_url"]), dbesc($contact["connect"]),
dbesc(normalise_link($contact["url"])), intval($contact["generation"])); dbesc(normalise_link($contact["url"])), intval($contact["generation"]));
} }