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';
*
*/
class ostatus {
- const OSTATUS_DEFAULT_POLL_INTERVAL = 30; // given in minutes
- const OSTATUS_DEFAULT_POLL_TIMEFRAME = 1440; // given in minutes
- const OSTATUS_DEFAULT_POLL_TIMEFRAME_MENTIONS = 14400; // given in minutes
private static $itemlist;
+ private static $conv_list = array();
/**
* @brief Fetches author data
*
* @param object $xpath The xpath object
- * @param object $context The xml context of the author detals
+ * @param object $context The xml context of the author details
* @param array $importer user record of the importing user
* @param array $contact Called by reference, will contain the fetched contact
* @param bool $onlyfetch Only fetch the header without updating the contact entries
}
}
}
-
$author["contact-id"] = $contact["id"];
- if ($author["author-link"] != "") {
+ $found = false;
+
+ if ($aliaslink != '') {
+ $condition = array("`uid` = ? AND `alias` = ? AND `network` != ?",
+ $importer["uid"], $aliaslink, NETWORK_STATUSNET);
+ $r = dba::select('contact', array(), $condition, array('limit' => 1));
+
+ if (dbm::is_result($r)) {
+ $found = true;
+ if ($r['blocked']) {
+ $r['id'] = -1;
+ }
+ $contact = $r;
+ $author["contact-id"] = $r["id"];
+ }
+ }
+
+ if (!$found && ($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;
+ if ($r['blocked']) {
+ $r['id'] = -1;
+ }
+ $contact = $r;
+ $author["contact-id"] = $r["id"];
}
- } 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)) {
+ $found = true;
+ if ($r['blocked']) {
+ $r['id'] = -1;
+ }
+ $contact = $r;
+ $author["contact-id"] = $r["id"];
}
}
$author["owner-avatar"] = $author["author-avatar"];
// Only update the contacts if it is an OStatus contact
- if ($r && !$onlyfetch && ($contact["network"] == NETWORK_OSTATUS)) {
+ if ($r && ($r['id'] > 0) && !$onlyfetch && ($contact["network"] == NETWORK_OSTATUS)) {
+
+ // This contact is vital, so we awake it from the dead
+ unmark_for_death($contact);
// Update contact data
+ $current = $contact;
+ unset($current['name-date']);
+
// This query doesn't seem to work
// $value = $xpath->query("atom:link[@rel='salmon']", $context)->item(0)->nodeValue;
// if ($value != "")
// if ($value != "")
// $contact["poll"] = $value;
+ $contact['url'] = $author["author-link"];
+ $contact['nurl'] = normalise_link($contact['url']);
+
$value = $xpath->evaluate('atom:author/atom:uri/text()', $context)->item(0)->nodeValue;
if ($value != "")
$contact["alias"] = $value;
if ($value != "")
$contact["location"] = $value;
- if (($contact["name"] != $r[0]["name"]) || ($contact["nick"] != $r[0]["nick"]) || ($contact["about"] != $r[0]["about"]) ||
- ($contact["alias"] != $r[0]["alias"]) || ($contact["location"] != $r[0]["location"])) {
+ $contact['name-date'] = datetime_convert();
- logger("Update contact data for contact ".$contact["id"], LOGGER_DEBUG);
-
- q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `alias` = '%s', `about` = '%s', `location` = '%s', `name-date` = '%s' WHERE `id` = %d",
- dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["alias"]),
- dbesc($contact["about"]), dbesc($contact["location"]),
- dbesc(datetime_convert()), intval($contact["id"]));
- }
+ dba::update('contact', $contact, array('id' => $contact["id"]), $current);
- if (isset($author["author-avatar"]) && ($author["author-avatar"] != $r[0]['avatar'])) {
+ if (!empty($author["author-avatar"]) && ($author["author-avatar"] != $current['avatar'])) {
logger("Update profile picture for contact ".$contact["id"], LOGGER_DEBUG);
-
update_contact_avatar($author["author-avatar"], $importer["uid"], $contact["id"]);
}
// Ensure that we are having this contact (with uid=0)
- $cid = get_contact($author["author-link"], 0);
+ $cid = get_contact($aliaslink, 0);
if ($cid) {
+ $fields = array('url', 'nurl', '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"],
+ 'nurl' => normalise_link($author["author-link"]),
+ '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);
*
* @param string $xml The XML
* @param array $importer user record of the importing user
- * @param $contact
- * @param array $hub Called by reference, returns the fetched hub data
+ * @param array $contact
+ * @param string $hub Called by reference, returns the fetched hub data
*/
public static function import($xml, $importer, &$contact, &$hub) {
self::process($xml, $importer, $contact, $hub);
}
/**
- * @brief Imports an XML string containing OStatus elements
+ * @brief Internal feed processing
*
* @param string $xml The XML
* @param array $importer user record of the importing user
- * @param $contact
- * @param array $hub Called by reference, returns the fetched hub data
+ * @param array $contact
+ * @param string $hub Called by reference, returns the fetched hub data
+ * @param boolean $stored Is the post fresh imported or from the database?
+ * @param boolean $initialize Is it the leading post so that data has to be initialized?
+ *
+ * @return boolean Could the XML be processed?
*/
private static function process($xml, $importer, &$contact, &$hub, $stored = false, $initialize = true) {
if ($initialize) {
self::$itemlist = array();
+ self::$conv_list = array();
}
logger("Import OStatus message", LOGGER_DEBUG);
if ($first_child == "feed") {
$entries = $xpath->query('/atom:feed/atom:entry');
- $header["protocol"] = PROTOCOL_OSTATUS_FEED;
} else {
$entries = $xpath->query('/atom:entry');
+ }
+
+ if ($entries->length == 1) {
+ // We reformat the XML to make it better readable
+ $doc2 = new DOMDocument();
+ $doc2->loadXML($xml);
+ $doc2->preserveWhiteSpace = false;
+ $doc2->formatOutput = true;
+ $xml2 = $doc2->saveXML();
+
$header["protocol"] = PROTOCOL_OSTATUS_SALMON;
+ $header["source"] = $xml2;
+ } elseif (!$initialize) {
+ return false;
}
// Fetch the first author
$author = self::fetchauthor($xpath, $authordata, $importer, $contact, $stored);
$entry = $xpath->query('/atom:entry');
- $header["protocol"] = PROTOCOL_OSTATUS_SALMON;
// Reverse the order of the entries
$entrylist = array();
$entrylist[] = $entry;
}
- if (!$initialize && (count($entrylist) > 1)) {
- return false;
- }
-
foreach (array_reverse($entrylist) AS $entry) {
// fetch the author
$authorelement = $xpath->query('/atom:entry/atom:author', $entry);
+
+ if ($authorelement->length == 0) {
+ $authorelement = $xpath->query('atom:author', $entry);
+ }
+
if ($authorelement->length > 0) {
$author = self::fetchauthor($xpath, $entry, $importer, $contact, $stored);
}
$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
- if ($item["verb"] == "qvitter-delete-notice" || $item["verb"] == ACTIVITY_DELETE) {
- // ignore "Delete" messages (by now)
- logger("Ignore delete message ".print_r($item, true));
+ // Delete a message
+ if (in_array($item["verb"], array('qvitter-delete-notice', ACTIVITY_DELETE, 'delete'))) {
+ 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
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);
}
- $doc2 = new DOMDocument();
- $doc2->loadXML($xml);
- $doc2->preserveWhiteSpace = false;
- $doc2->formatOutput = true;
- $xml2 = $doc2->saveXML();
-
- $item["source"] = $xml2;
-
self::processPost($xpath, $entry, $item, $importer);
if ($initialize && (count(self::$itemlist) > 0)) {
- // We will import it everytime, when it is started by our contacts
- $valid = !empty(self::$itemlist[0]['contact-id']);
- if (!$valid) {
- // If not, then it depends on this setting
- $valid = !Config::get('system','ostatus_full_threads');
- }
-
- if ($valid) {
- // But we will only import complete threads
- $valid = self::$itemlist[0]['uri'] == self::$itemlist[0]['parent-uri'];
- }
-
- if ($valid) {
- // Never post a thread when the only interaction by our contact was a like
- $valid = false;
- $verbs = array(ACTIVITY_POST, ACTIVITY_SHARE);
- foreach (self::$itemlist AS $item) {
- if (!empty($item['contact-id']) && in_array($item['verb'], $verbs)) {
- $valid = true;
+ if (self::$itemlist[0]['uri'] == self::$itemlist[0]['parent-uri']) {
+ // We will import it everytime, when it is started by our contacts
+ $valid = !empty(self::$itemlist[0]['contact-id']);
+ if (!$valid) {
+ // If not, then it depends on this setting
+ $valid = !Config::get('system','ostatus_full_threads');
+ }
+ if ($valid) {
+ // Never post a thread when the only interaction by our contact was a like
+ $valid = false;
+ $verbs = array(ACTIVITY_POST, ACTIVITY_SHARE);
+ foreach (self::$itemlist AS $item) {
+ if (!empty($item['contact-id']) && in_array($item['verb'], $verbs)) {
+ $valid = true;
+ }
}
}
+ } else {
+ // But we will only import complete threads
+ $valid = dba::exists('item', array('uid' => $importer["uid"], 'uri' => self::$itemlist[0]['parent-uri']));
}
if ($valid) {
$found = dba::exists('item', array('uid' => $importer["uid"], 'uri' => $item["uri"]));
if ($found) {
logger("Item with uri ".$item["uri"]." for user ".$importer["uid"]." already exists.", LOGGER_DEBUG);
+ } elseif ($item['contact-id'] < 0) {
+ logger("Item with uri ".$item["uri"]." is from a blocked contact.", 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 object $xpath The xpath object
+ * @param object $entry The xml entry that is processed
+ * @param array $item The item array
+ * @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;
$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 (empty($item['conversation-href']) && !empty($item['conversation-uri'])) {
- $item['conversation-href'] = $item['conversation-uri'];
- }
-
$related = "";
$inreplyto = $xpath->query('thr:in-reply-to', $entry);
$item["body"] = html2bbcode($clear_text) . '[spoiler]' . $item["body"] . '[/spoiler]';
}
+ if (($self != '') && empty($item['protocol'])) {
+ self::fetchSelf($self, $item);
+ }
+
+ if (!empty($item["conversation-href"])) {
+ self::fetchConversation($item['conversation-href'], $item['conversation-uri']);
+ }
+
if (isset($item["parent-uri"]) && ($related != '')) {
- self::FetchRelated($related, $item["parent-uri"], $importer);
+ 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_DEBUG);
+ }
+
$item["type"] = 'remote-comment';
$item["gravity"] = GRAVITY_COMMENT;
} else {
$item["parent-uri"] = $item["uri"];
}
- if ($item['author-link'] != '') {
+ if (($item['author-link'] != '') && !empty($item['protocol'])) {
$item = store_conversation($item);
}
self::$itemlist[] = $item;
}
+ /**
+ * @brief Fetch the conversation for posts
+ *
+ * @param string $conversation The link to the conversation
+ * @param string $conversation_uri The conversation in "uri" format
+ */
+ private static function fetchConversation($conversation, $conversation_uri) {
+
+ // Ensure that we only store a conversation once in a process
+ if (isset(self::$conv_list[$conversation])) {
+ return;
+ }
+
+ self::$conv_list[$conversation] = true;
+
+ $conversation_data = z_fetch_url($conversation, false, $redirects, array('accept_content' => 'application/atom+xml, text/html'));
+
+ if (!$conversation_data['success']) {
+ return;
+ }
+
+ $xml = '';
+
+ if (stristr($conversation_data['header'], 'Content-Type: application/atom+xml')) {
+ $xml = $conversation_data['body'];
+ }
+
+ if ($xml == '') {
+ $doc = new DOMDocument();
+ if (!@$doc->loadHTML($conversation_data['body'])) {
+ return;
+ }
+ $xpath = new DomXPath($doc);
+
+ $links = $xpath->query('//link');
+ if ($links) {
+ foreach ($links AS $link) {
+ $attribute = ostatus::read_attributes($link);
+ if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
+ $file = $attribute['href'];
+ }
+ }
+ if ($file != '') {
+ $conversation_atom = z_fetch_url($attribute['href']);
+
+ if ($conversation_atom['success']) {
+ $xml = $conversation_atom['body'];
+ }
+ }
+ }
+ }
+
+ if ($xml == '') {
+ return;
+ }
+
+ self::storeConversation($xml, $conversation, $conversation_uri);
+ }
+
+ /**
+ * @brief Store a feed in several conversation entries
+ *
+ * @param string $xml The feed
+ */
+ private static function storeConversation($xml, $conversation = '', $conversation_uri = '') {
+ $doc = new DOMDocument();
+ @$doc->loadXML($xml);
+
+ $xpath = new DomXPath($doc);
+ $xpath->registerNamespace('atom', NAMESPACE_ATOM1);
+ $xpath->registerNamespace('thr', NAMESPACE_THREAD);
+ $xpath->registerNamespace('ostatus', NAMESPACE_OSTATUS);
+
+ $entries = $xpath->query('/atom:feed/atom:entry');
+
+ // Now store the entries
+ foreach ($entries AS $entry) {
+ $doc2 = new DOMDocument();
+ $doc2->preserveWhiteSpace = false;
+ $doc2->formatOutput = true;
+
+ $conv_data = array();
+
+ $conv_data['protocol'] = PROTOCOL_SPLITTED_CONV;
+ $conv_data['network'] = NETWORK_OSTATUS;
+ $conv_data['uri'] = $xpath->query('atom:id/text()', $entry)->item(0)->nodeValue;
+
+ $inreplyto = $xpath->query('thr:in-reply-to', $entry);
+ if (is_object($inreplyto->item(0))) {
+ foreach ($inreplyto->item(0)->attributes AS $attributes) {
+ if ($attributes->name == "ref") {
+ $conv_data['reply-to-uri'] = $attributes->textContent;
+ }
+ }
+ }
+
+ $conv = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue;
+ $conv_data['conversation-uri'] = $conv;
+
+ $conv = $xpath->query('ostatus:conversation', $entry);
+ if (is_object($conv->item(0))) {
+ foreach ($conv->item(0)->attributes AS $attributes) {
+ if ($attributes->name == "ref") {
+ $conv_data['conversation-uri'] = $attributes->textContent;
+ }
+ if ($attributes->name == "href") {
+ $conv_data['conversation-href'] = $attributes->textContent;
+ }
+ }
+ }
+
+ if ($conversation != '') {
+ $conv_data['conversation-uri'] = $conversation;
+ }
+
+ if ($conversation_uri != '') {
+ $conv_data['conversation-uri'] = $conversation_uri;
+ }
+
+ $entry = $doc2->importNode($entry, true);
+
+ $doc2->appendChild($entry);
+
+ $conv_data['source'] = $doc2->saveXML();
+
+ $condition = array('item-uri' => $conv_data['uri'],'protocol' => PROTOCOL_OSTATUS_FEED);
+ if (dba::exists('conversation', $condition)) {
+ logger('Delete deprecated entry for URI '.$conv_data['uri'], LOGGER_DEBUG);
+ dba::delete('conversation', array('item-uri' => $conv_data['uri']));
+ }
+
+ logger('Store conversation data for uri '.$conv_data['uri'], LOGGER_DEBUG);
+ store_conversation($conv_data);
+ }
+ }
+
+ /**
+ * @brief Fetch the own post so that it can be stored later
+ * @param array $item The item array
+ *
+ * We want to store the original data for later processing.
+ * This function is meant for cases where we process a feed with multiple entries.
+ * In that case we need to fetch the single posts here.
+ *
+ * @param string $self The link to the self item
+ */
+ 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_DEBUG);
+ return;
+ }
+
+ $self_data = z_fetch_url($self);
+
+ if (!$self_data['success']) {
+ return;
+ }
+
+ // We reformat the XML to make it better readable
+ $doc = new DOMDocument();
+ $doc->loadXML($self_data['body']);
+ $doc->preserveWhiteSpace = false;
+ $doc->formatOutput = true;
+ $xml = $doc->saveXML();
+
+ $item["protocol"] = PROTOCOL_OSTATUS_SALMON;
+ $item["source"] = $xml;
+
+ logger('Conversation '.$item['uri'].' is now fetched.', LOGGER_DEBUG);
+ }
+
+ /**
+ * @brief Fetch related posts and processes them
+ *
+ * @param string $related The link to the related item
+ * @param string $related_uri The related item in "uri" format
+ * @param array $importer user record of the importing user
+ */
private static function fetchRelated($related, $related_uri, $importer) {
$condition = array('`item-uri` = ? AND `protocol` IN (?, ?)', $related_uri, PROTOCOL_DFRN, PROTOCOL_OSTATUS_SALMON);
$conversation = dba::select('conversation', array('source', 'protocol'), $condition, array('limit' => 1));
$stored = true;
$xml = $conversation['source'];
if (self::process($xml, $importer, $contact, $hub, $stored, false)) {
+ logger('Got valid cached XML for URI '.$related_uri, LOGGER_DEBUG);
return;
}
if ($conversation['protocol'] == PROTOCOL_OSTATUS_SALMON) {
+ logger('Delete invalid cached XML for URI '.$related_uri, LOGGER_DEBUG);
dba::delete('conversation', array('item-uri' => $related_uri));
}
}
$stored = false;
- $related_data = z_fetch_url($related);
+ $related_data = z_fetch_url($related, false, $redirects, array('accept_content' => 'application/atom+xml, text/html'));
if (!$related_data['success']) {
return;
$xml = '';
if (stristr($related_data['header'], 'Content-Type: application/atom+xml')) {
+ logger('Directly fetched XML for URI '.$related_uri, LOGGER_DEBUG);
$xml = $related_data['body'];
}
}
$xpath = new DomXPath($doc);
+ $atom_file = '';
+
$links = $xpath->query('//link');
if ($links) {
foreach ($links AS $link) {
$attribute = self::read_attributes($link);
if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
- $related_atom = z_fetch_url($attribute['href']);
+ $atom_file = $attribute['href'];
+ }
+ }
+ if ($atom_file != '') {
+ $related_atom = z_fetch_url($atom_file);
- if ($related_atom['success']) {
- $xml = $related_atom['body'];
- }
+ if ($related_atom['success']) {
+ logger('Fetched XML for URI '.$related_uri, LOGGER_DEBUG);
+ $xml = $related_atom['body'];
}
}
}
// Workaround for older GNU Social servers
if (($xml == '') && strstr($related, '/notice/')) {
- $related_atom = z_fetch_url(str_replace('/notice/', '/api/statuses/show/', $related).',atom');
+ $related_atom = z_fetch_url(str_replace('/notice/', '/api/statuses/show/', $related).'.atom');
if ($related_atom['success']) {
+ logger('GNU Social workaround to fetch XML for URI '.$related_uri, LOGGER_DEBUG);
$xml = $related_atom['body'];
}
}
+ // Even more worse workaround for GNU Social ;-)
+ if ($xml == '') {
+ $related_guess = ostatus::convert_href($related_uri);
+ $related_atom = z_fetch_url(str_replace('/notice/', '/api/statuses/show/', $related_guess).'.atom');
+
+ if ($related_atom['success']) {
+ logger('GNU Social workaround 2 to fetch XML for URI '.$related_uri, LOGGER_DEBUG);
+ $xml = $related_atom['body'];
+ }
+ }
+
+ // Finally we take the data that we fetched from "ostatus:conversation"
+ if ($xml == '') {
+ $condition = array('item-uri' => $related_uri, 'protocol' => PROTOCOL_SPLITTED_CONV);
+ $conversation = dba::select('conversation', array('source'), $condition, array('limit' => 1));
+ if (dbm::is_result($conversation)) {
+ $stored = true;
+ logger('Got cached XML from conversation for URI '.$related_uri, LOGGER_DEBUG);
+ $xml = $conversation['source'];
+ }
+ }
+
if ($xml != '') {
self::process($xml, $importer, $contact, $hub, $stored, false);
+ } else {
+ logger("XML couldn't be fetched for URI: ".$related_uri." - href: ".$related, LOGGER_DEBUG);
}
return;
}
+ /**
+ * @brief Processes the XML for a repeated post
+ *
+ * @param object $xpath The xpath object
+ * @param object $entry The xml entry that is processed
+ * @param array $item The item array
+ * @param array $importer user record of the importing user
+ *
+ * @return array with data from links
+ */
private static function processRepeatedItem($xpath, $entry, &$item, $importer) {
$activityobjects = $xpath->query('activity:object', $entry)->item(0);
return $link_data;
}
+ /**
+ * @brief Processes links in the XML
+ *
+ * @param object $links The xml data that contain links
+ * @param array $item The item array
+ *
+ * @return array with data from the links
+ */
private static function processLinks($links, &$item) {
$link_data = array('add_body' => '', 'self' => '');
return $link_data;
}
+/**
+ * @brief Create an url out of an uri
+ *
+ * @param string $href URI in the format "parameter1:parameter1:..."
+ *
+ * @return string URL in the format http(s)://....
+ */
+ public static function convert_href($href) {
+ $elements = explode(":",$href);
+
+ if ((count($elements) <= 2) || ($elements[0] != "tag"))
+ return $href;
+
+ $server = explode(",", $elements[1]);
+ $conversation = explode("=", $elements[2]);
+
+ if ((count($elements) == 4) && ($elements[2] == "post"))
+ return "http://".$server[0]."/notice/".$elements[3];
+
+ if ((count($conversation) != 2) || ($conversation[1] =="")) {
+ return $href;
+ }
+ if ($elements[3] == "objectType=thread") {
+ return "http://".$server[0]."/conversation/".$conversation[1];
+ } else {
+ return "http://".$server[0]."/notice/".$conversation[1];
+ }
+ return $href;
+ }
+
/**
* @brief Checks if the current post is a reshare
*