+ /**
+ * 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