}
$items = [];
+ $creation_dates = [];
// Limit the number of items that are about to be fetched
$total_items = ($entries->length - 1);
$orig_plink = $item["plink"];
- $item["plink"] = Network::finalUrl($item["plink"]);
+ $item["plink"] = DI::httpRequest()->finalUrl($item["plink"]);
$item["parent-uri"] = $item["uri"];
- if (!$dryRun) {
- $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
- $importer["uid"], $item["uri"], Protocol::FEED, Protocol::DFRN];
- $previous = Item::selectFirst(['id'], $condition);
- if (DBA::isResult($previous)) {
- Logger::info("Item with uri " . $item["uri"] . " for user " . $importer["uid"] . " already existed under id " . $previous["id"]);
- continue;
- }
- }
-
$item["title"] = XML::getFirstNodeValue($xpath, 'atom:title/text()', $entry);
if (empty($item["title"])) {
$item["edited"] = $updated;
}
+ if (!$dryRun) {
+ $condition = ["`uid` = ? AND `uri` = ? AND `network` IN (?, ?)",
+ $importer["uid"], $item["uri"], Protocol::FEED, Protocol::DFRN];
+ $previous = Item::selectFirst(['id', 'created'], $condition);
+ if (DBA::isResult($previous)) {
+ // Use the creation date when the post had been stored. It can happen this date changes in the feed.
+ $creation_dates[] = $previous['created'];
+ Logger::info("Item with uri " . $item["uri"] . " for user " . $importer["uid"] . " already existed under id " . $previous["id"]);
+ continue;
+ }
+ $creation_dates[] = DateTimeFormat::utc($item['created']);
+ }
+
$creator = XML::getFirstNodeValue($xpath, 'author/text()', $entry);
if (empty($creator)) {
}
$item["body"] = HTML::toBBCode($body, $basepath);
+ // Remove tracking pixels
+ $item["body"] = preg_replace("/\[img=1x1\]([^\[\]]*)\[\/img\]/Usi", '', $item["body"]);
+
if (($item["body"] == '') && ($item["title"] != '')) {
$item["body"] = $item["title"];
$item["title"] = '';
}
+ if ($dryRun) {
+ $items[] = $item;
+ break;
+ } elseif (!Item::isValid($item)) {
+ Logger::info('Feed is invalid', ['created' => $item['created'], 'uid' => $item['uid'], 'uri' => $item['uri']]);
+ continue;
+ }
+
$preview = '';
if (!empty($contact["fetch_further_information"]) && ($contact["fetch_further_information"] < 3)) {
// Handle enclosures and treat them as preview picture
$replace = true;
}
+ $saved_body = $item["body"];
+ $saved_title = $item["title"];
+
if ($replace) {
$item["body"] = trim($item["title"]);
}
}
}
+ $data = PageInfo::queryUrl($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"] ?? '');
+
+ // Take the data that was provided by the feed if the query is empty
+ if (($data['type'] == 'link') && empty($data['title']) && empty($data['text'])) {
+ $data['title'] = $saved_title;
+ $item["body"] = $saved_body;
+ }
+
+ $data_text = strip_tags(trim($data['text'] ?? ''));
+ $item_body = strip_tags(trim($item['body'] ?? ''));
+
+ if (!empty($data_text) && (($data_text == $item_body) || strstr($item_body, $data_text))) {
+ $data['text'] = '';
+ }
+
// We always strip the title since it will be added in the page information
$item["title"] = "";
- $item["body"] = $item["body"] . "\n" . PageInfo::getFooterFromUrl($item["plink"], false, $preview, ($contact["fetch_further_information"] == 2), $contact["ffi_keyword_denylist"] ?? '');
+ $item["body"] = $item["body"] . "\n" . PageInfo::getFooterFromData($data, false);
$taglist = $contact["fetch_further_information"] == 2 ? PageInfo::getTagsFromUrl($item["plink"], $preview, $contact["ffi_keyword_denylist"] ?? '') : [];
$item["object-type"] = Activity\ObjectType::BOOKMARK;
unset($item["attach"]);
}
}
- if ($dryRun) {
- $items[] = $item;
- break;
- } else {
- Logger::info('Stored feed', ['item' => $item]);
+ Logger::info('Stored feed', ['item' => $item]);
- $notify = Item::isRemoteSelf($contact, $item);
+ $notify = Item::isRemoteSelf($contact, $item);
- // Distributed items should have a well formatted URI.
- // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
- if ($notify) {
- $item['guid'] = Item::guidFromUri($orig_plink, DI::baseUrl()->getHostname());
- unset($item['uri']);
- unset($item['parent-uri']);
+ // Distributed items should have a well formatted URI.
+ // Additionally we have to avoid conflicts with identical URI between imported feeds and these items.
+ if ($notify) {
+ $item['guid'] = Item::guidFromUri($orig_plink, DI::baseUrl()->getHostname());
+ unset($item['uri']);
+ unset($item['parent-uri']);
- // Set the delivery priority for "remote self" to "medium"
- $notify = PRIORITY_MEDIUM;
- }
+ // Set the delivery priority for "remote self" to "medium"
+ $notify = PRIORITY_MEDIUM;
+ }
- $id = Item::insert($item, $notify);
+ $id = Item::insert($item, $notify);
- Logger::info("Feed for contact " . $contact["url"] . " stored under id " . $id);
+ Logger::info("Feed for contact " . $contact["url"] . " stored under id " . $id);
- if (!empty($id) && !empty($taglist)) {
- $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
- foreach ($taglist as $tag) {
- Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
- }
+ if (!empty($id) && !empty($taglist)) {
+ $feeditem = Item::selectFirst(['uri-id'], ['id' => $id]);
+ foreach ($taglist as $tag) {
+ Tag::store($feeditem['uri-id'], Tag::HASHTAG, $tag);
}
}
}
+ if (!$dryRun && DI::config()->get('system', 'adjust_poll_frequency')) {
+ self::adjustPollFrequency($contact, $creation_dates);
+ }
+
return ["header" => $author, "items" => $items];
}
+ /**
+ * Automatically adjust the poll frequency according to the post frequency
+ *
+ * @param array $contact
+ * @param array $creation_dates
+ * @return void
+ */
+ private static function adjustPollFrequency(array $contact, array $creation_dates)
+ {
+ if ($contact['network'] != Protocol::FEED) {
+ Logger::info('Contact is no feed, skip.', ['id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url'], 'network' => $contact['network']]);
+ return;
+ }
+
+ if (!empty($creation_dates)) {
+ // Count the post frequency and the earliest and latest post date
+ $frequency = [];
+ $oldest = time();
+ $newest = 0;
+ $oldest_date = $newest_date = '';
+
+ foreach ($creation_dates as $date) {
+ $timestamp = strtotime($date);
+ $day = intdiv($timestamp, 86400);
+ $hour = $timestamp % 86400;
+
+ // Only have a look at values from the last seven days
+ if (((time() / 86400) - $day) < 7) {
+ if (empty($frequency[$day])) {
+ $frequency[$day] = ['count' => 1, 'low' => $hour, 'high' => $hour];
+ } else {
+ ++$frequency[$day]['count'];
+ if ($frequency[$day]['low'] > $hour) {
+ $frequency[$day]['low'] = $hour;
+ }
+ if ($frequency[$day]['high'] < $hour) {
+ $frequency[$day]['high'] = $hour;
+ }
+ }
+ }
+ if ($oldest > $day) {
+ $oldest = $day;
+ $oldest_date = $date;
+ }
+
+ if ($newest < $day) {
+ $newest = $day;
+ $newest_date = $date;
+ }
+ }
+
+ if (count($creation_dates) == 1) {
+ Logger::info('Feed had posted a single time, switching to daily polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+ $priority = 8; // Poll once a day
+ }
+
+ if (empty($priority) && (((time() / 86400) - $newest) > 730)) {
+ Logger::info('Feed had not posted for two years, switching to monthly polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+ $priority = 10; // Poll every month
+ }
+
+ if (empty($priority) && (((time() / 86400) - $newest) > 365)) {
+ Logger::info('Feed had not posted for a year, switching to weekly polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+ $priority = 9; // Poll every week
+ }
+
+ if (empty($priority) && empty($frequency)) {
+ Logger::info('Feed had not posted for at least a week, switching to daily polling', ['newest' => $newest_date, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+ $priority = 8; // Poll once a day
+ }
+
+ if (empty($priority)) {
+ // Calculate the highest "posts per day" value
+ $max = 0;
+ foreach ($frequency as $entry) {
+ if (($entry['count'] == 1) || ($entry['high'] == $entry['low'])) {
+ continue;
+ }
+
+ // We take the earliest and latest post day and interpolate the number of post per day
+ // that would had been created with this post frequency
+
+ // Assume at least four hours between oldest and newest post per day - should be okay for news outlets
+ $duration = max($entry['high'] - $entry['low'], 14400);
+ $ppd = (86400 / $duration) * $entry['count'];
+ if ($ppd > $max) {
+ $max = $ppd;
+ }
+ }
+ if ($max > 48) {
+ $priority = 1; // Poll every quarter hour
+ } elseif ($max > 24) {
+ $priority = 2; // Poll half an hour
+ } elseif ($max > 12) {
+ $priority = 3; // Poll hourly
+ } elseif ($max > 8) {
+ $priority = 4; // Poll every two hours
+ } elseif ($max > 4) {
+ $priority = 5; // Poll every three hours
+ } elseif ($max > 2) {
+ $priority = 6; // Poll every six hours
+ } else {
+ $priority = 7; // Poll twice a day
+ }
+ Logger::info('Calculated priority by the posts per day', ['priority' => $priority, 'max' => round($max, 2), 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+ }
+ } else {
+ Logger::info('No posts, switching to daily polling', ['id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+ $priority = 8; // Poll once a day
+ }
+
+ if ($contact['rating'] != $priority) {
+ Logger::notice('Adjusting priority', ['old' => $contact['rating'], 'new' => $priority, 'id' => $contact['id'], 'uid' => $contact['uid'], 'url' => $contact['url']]);
+ DBA::update('contact', ['rating' => $priority], ['id' => $contact['id']]);
+ }
+ }
+
/**
* Convert a tag array to a tag string
*
return ($title == $body);
}
- /**
+ /**
* Creates the Atom feed for a given nickname
*
* Supported filters:
}
$check_date = empty($last_update) ? '' : DateTimeFormat::utc($last_update);
- $authorid = Contact::getIdForURL($owner["url"], 0, false);
+ $authorid = Contact::getIdForURL($owner["url"]);
$condition = ["`uid` = ? AND `received` > ? AND NOT `deleted` AND `gravity` IN (?, ?)
AND `private` != ? AND `visible` AND `wall` AND `parent-network` IN (?, ?, ?, ?)",
return false;
}
- $contact = Contact::getByURL($repeated_item['author-link']) ?: $owner;
-
- $title = $owner["nick"]." repeated a notice by ".$contact["nick"];
-
- self::entryContent($doc, $entry, $item, $owner, $title, Activity::SHARE, false);
+ self::entryContent($doc, $entry, $item, self::getTitle($repeated_item), Activity::SHARE, false);
self::entryFooter($doc, $entry, $item, $owner);
Logger::info('Feed entry author does not match feed owner', ['owner' => $owner["url"], 'author' => $item["author-link"]]);
}
- if (!empty($item['title'])) {
- $title = BBCode::convert($item['title'], false, BBCode::OSTATUS);
- } else {
- $title = sprintf("New note by %s", $owner["nick"]);
- }
-
$entry = OStatus::entryHeader($doc, $owner, $item, false);
- self::entryContent($doc, $entry, $item, $title, '', true);
+ self::entryContent($doc, $entry, $item, self::getTitle($item), '', true);
self::entryFooter($doc, $entry, $item, $owner);
OStatus::getAttachment($doc, $entry, $item);
}
+
+ /**
+ * Fetch or create title for feed entry
+ *
+ * @param array $item
+ * @return string title
+ */
+ private static function getTitle(array $item)
+ {
+ if ($item['title'] != '') {
+ return BBCode::convert($item['title'], false, BBCode::OSTATUS);
+ }
+
+ // Fetch information about the post
+ $siteinfo = BBCode::getAttachedData($item["body"]);
+ if (isset($siteinfo["title"])) {
+ return $siteinfo["title"];
+ }
+
+ // If no bookmark is found then take the first line
+ // Remove the share element before fetching the first line
+ $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$item['body']));
+
+ $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
+ $pos = strpos($title, "\n");
+ $trailer = "";
+ if (($pos == 0) || ($pos > 100)) {
+ $pos = 100;
+ $trailer = "...";
+ }
+
+ return substr($title, 0, $pos) . $trailer;
+ }
}