return $cache[$url][$uid];
}
+ $ssl_url = str_replace('http://', 'https://', $url);
+
// Fetch contact data from the contact table for the given user
$s = dba::p("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
`keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
normalise_link($url), $uid);
$r = dba::inArray($s);
+ // Fetch contact data from the contact table for the given user, checking with the alias
+ if (!dbm::is_result($r)) {
+ $s = dba::p("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
+ `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
+ FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ?",
+ normalise_link($url), $url, $ssl_url, $uid);
+ $r = dba::inArray($s);
+ }
+
// Fetch the data from the contact table with "uid=0" (which is filled automatically)
if (!dbm::is_result($r)) {
$s = dba::p("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
$r = dba::inArray($s);
}
+ // Fetch the data from the contact table with "uid=0" (which is filled automatically) - checked with the alias
+ if (!dbm::is_result($r)) {
+ $s = dba::p("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
+ `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
+ FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = 0",
+ normalise_link($url), $url, $ssl_url);
+ $r = dba::inArray($s);
+ }
+
// Fetch the data from the gcontact table
if (!dbm::is_result($r)) {
$s = dba::p("SELECT 0 AS `id`, 0 AS `cid`, `id` AS `gid`, 0 AS `zid`, 0 AS `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, '' AS `xmpp`,
// Then the alias (which could be anything)
if (!dbm::is_result($contact)) {
- $r = dba::p("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN (?, ?) AND `uid` = ? LIMIT 1",
- $url, normalise_link($url), $uid);
+ // The link could be provided as http although we stored it as https
+ $ssl_url = str_replace('http://', 'https://', $url);
+ $r = dba::p("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ? LIMIT 1",
+ $url, normalise_link($url), $ssl_url, $uid);
$contact = dba::fetch($r);
dba::close($r);
}
use Friendica\Core\System;
use Friendica\Core\Config;
use Friendica\Network\Probe;
+use Friendica\Util\Lock;
require_once 'include/Contact.php';
require_once 'include/threads.php';
$author["contact-id"] = $contact["id"];
+ $found = false;
+
if ($author["author-link"] != "") {
if ($aliaslink == "") {
$aliaslink = $author["author-link"];
}
- $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` IN ('%s', '%s') AND `network` != '%s'",
- intval($importer["uid"]), dbesc(normalise_link($author["author-link"])),
- dbesc(normalise_link($aliaslink)), dbesc(NETWORK_STATUSNET));
+ $condition = array("`uid` = ? AND `nurl` IN (?, ?) AND `network` != ?", $importer["uid"],
+ normalise_link($author["author-link"]), normalise_link($aliaslink), NETWORK_STATUSNET);
+ $r = dba::select('contact', array(), $condition, array('limit' => 1));
if (dbm::is_result($r)) {
- $contact = $r[0];
- $author["contact-id"] = $r[0]["id"];
- $author["author-link"] = $r[0]["url"];
+ $found = true;
+ $contact = $r;
+ $author["contact-id"] = $r["id"];
+ $author["author-link"] = $r["url"];
}
- } elseif ($addr != "") {
- // Should not happen
- $contact = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `addr` = ? AND `network` != ?",
+ }
+
+ if (!$found && ($addr != "")) {
+ $condition = array("`uid` = ? AND `addr` = ? AND `network` != ?",
$importer["uid"], $addr, NETWORK_STATUSNET);
+ $r = dba::select('contact', array(), $condition, array('limit' => 1));
- if (dbm::is_result($contact)) {
- $author["contact-id"] = $contact["id"];
- $author["author-link"] = $contact["url"];
+ if (dbm::is_result($r)) {
+ $contact = $r;
+ $author["contact-id"] = $r["id"];
+ $author["author-link"] = $r["url"];
}
}
$cid = get_contact($author["author-link"], 0);
if ($cid) {
+ $fields = array('url', 'name', 'nick', 'alias', 'about', 'location');
+ $old_contact = dba::select('contact', $fields, array('id' => $cid), array('limit' => 1));
+
// Update it with the current values
- q("UPDATE `contact` SET `url` = '%s', `name` = '%s', `nick` = '%s', `alias` = '%s',
- `about` = '%s', `location` = '%s',
- `success_update` = '%s', `last-update` = '%s'
- WHERE `id` = %d",
- dbesc($author["author-link"]), dbesc($contact["name"]), dbesc($contact["nick"]),
- dbesc($contact["alias"]), dbesc($contact["about"]), dbesc($contact["location"]),
- dbesc(datetime_convert()), dbesc(datetime_convert()), intval($cid));
+ $fields = array('url' => $author["author-link"], 'name' => $contact["name"],
+ 'nick' => $contact["nick"], 'alias' => $contact["alias"],
+ 'about' => $contact["about"], 'location' => $contact["location"],
+ 'success_update' => datetime_convert(), 'last-update' => datetime_convert());
+
+ dba::update('contact', $fields, array('id' => $cid), $old_contact);
// Update the avatar
update_contact_avatar($author["author-avatar"], 0, $cid);
$item = array_merge($header, $author);
+ $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
+
$item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
- /// Delete a message
+ // Delete a message
if (in_array($item["verb"], array('qvitter-delete-notice', ACTIVITY_DELETE, 'delete'))) {
- // ignore "Delete" messages (by now)
- logger("Ignore delete message ".print_r($item, true));
+ self::deleteNotice($item);
+ continue;
+ }
+
+ if (in_array($item["verb"], array(NAMESPACE_OSTATUS."/unfavorite", ACTIVITY_UNFAVORITE))) {
+ // Ignore "Unfavorite" message
+ logger("Ignore unfavorite message ".print_r($item, true), LOGGER_DEBUG);
continue;
}
+ // Deletions come with the same uri, so we check for duplicates after processing deletions
+ if (dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item["uri"]))) {
+ logger('Post with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.', LOGGER_DEBUG);
+ continue;
+ } else {
+ logger('Processing post with URI '.$item["uri"].' for user '.$importer["uid"].'.', LOGGER_DEBUG);
+ }
+
if ($item["verb"] == ACTIVITY_JOIN) {
// ignore "Join" messages
- logger("Ignore join message ".print_r($item, true));
+ logger("Ignore join message ".print_r($item, true), LOGGER_DEBUG);
+ continue;
+ }
+
+ if ($item["verb"] == "http://mastodon.social/schema/1.0/block") {
+ // ignore mastodon "block" messages
+ logger("Ignore block message ".print_r($item, true), LOGGER_DEBUG);
continue;
}
continue;
}
- if ($item["verb"] == NAMESPACE_OSTATUS."/unfavorite") {
- // Ignore "Unfavorite" message
- logger("Ignore unfavorite message ".print_r($item, true));
- continue;
- }
-
if ($item["verb"] == ACTIVITY_FAVORITE) {
$orig_uri = $xpath->query("activity:object/atom:id", $entry)->item(0)->nodeValue;
logger("Favorite ".$orig_uri." ".print_r($item, true));
}
// http://activitystrea.ms/schema/1.0/rsvp-yes
- // http://activitystrea.ms/schema/1.0/unfavorite
- // http://mastodon.social/schema/1.0/block
if (!in_array($item["verb"], array(ACTIVITY_POST, ACTIVITY_LIKE, ACTIVITY_SHARE))) {
- logger("Unhandled verb ".$item["verb"]." ".print_r($item, true));
+ logger("Unhandled verb ".$item["verb"]." ".print_r($item, true), LOGGER_DEBUG);
}
self::processPost($xpath, $entry, $item, $importer);
if ($found) {
logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already exists.", LOGGER_DEBUG);
} else {
- $ret = item_store($item);
- logger('Item was stored with return value '.$ret);
+ // We are having duplicated entries. Hopefully this solves it.
+ if (Lock::set('ostatus_process_item_store')) {
+ $ret = item_store($item);
+ Lock::remove('ostatus_process_item_store');
+ logger("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);
+ } else {
+ $ret = item_store($item);
+ logger("We couldn't lock - but tried to store the item anyway. Return value is ".$ret);
+ }
}
}
}
self::$itemlist = array();
}
+ logger('Processing done for post with URI '.$item["uri"].' for user '.$importer["uid"].'.', LOGGER_DEBUG);
}
return true;
}
+ private static function deleteNotice($item) {
+
+ $condition = array('uid' => $item['uid'], 'author-link' => $item['author-link'], 'uri' => $item['uri']);
+ $deleted = dba::select('item', array('id', 'parent-uri'), $condition, array('limit' => 1));
+ if (!dbm::is_result($deleted)) {
+ logger('Item from '.$item['author-link'].' with uri '.$item['uri'].' for user '.$item['uid']." wasn't found. We don't delete it. ");
+ return;
+ }
+
+ // Currently we don't have a central deletion function that we could use in this case. The function "item_drop" doesn't work for that case
+ dba::update('item', array('deleted' => true, 'title' => '', 'body' => '',
+ 'edited' => datetime_convert(), 'changed' => datetime_convert()),
+ array('id' => $deleted["id"]));
+
+ delete_thread($deleted["id"], $deleted["parent-uri"]);
+
+ logger('Deleted item with uri '.$item['uri'].' for user '.$item['uid']);
+ }
+
/**
* @brief Processes the XML for a post
*
* @param array $importer user record of the importing user
*/
private static function processPost($xpath, $entry, &$item, $importer) {
- $item["uri"] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
-
- if (dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item["uri"]))) {
- logger('Post with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.');
- return;
- }
-
$item["body"] = html2bbcode($xpath->query('atom:content/text()', $entry)->item(0)->nodeValue);
$item["object-type"] = $xpath->query('activity:object-type/text()', $entry)->item(0)->nodeValue;
if (($item["object-type"] == ACTIVITY_OBJ_BOOKMARK) || ($item["object-type"] == ACTIVITY_OBJ_EVENT)) {
if (!dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item['parent-uri']))) {
self::fetchRelated($related, $item["parent-uri"], $importer);
} else {
- logger('Reply with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.');
+ logger('Reply with URI '.$item["uri"].' already existed for user '.$importer["uid"].'.', LOGGER_DEBUG);
}
$item["type"] = 'remote-comment';
private static function fetchSelf($self, &$item) {
$condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $self, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
if (dba::exists('conversation', $condition)) {
- logger('Conversation '.$item['uri'].' is already stored.');
+ logger('Conversation '.$item['uri'].' is already stored.', LOGGER_DEBUG);
return;
}
$item["protocol"] = PROTOCOL_OSTATUS_SALMON;
$item["source"] = $xml;
- logger('Conversation '.$item['uri'].' is now fetched.');
+ logger('Conversation '.$item['uri'].' is now fetched.', LOGGER_DEBUG);
}
/**