2 function update_feedbrowser_cache($link) {
4 $result = db_query($link, "SELECT feed_url, site_url, title, COUNT(id) AS subscribers
5 FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
6 WHERE tf.feed_url = ttrss_feeds.feed_url
7 AND (private IS true OR auth_login != '' OR auth_pass != '' OR feed_url LIKE '%:%@%/%'))
8 GROUP BY feed_url, site_url, title ORDER BY subscribers DESC LIMIT 1000");
10 db_query($link, "BEGIN");
12 db_query($link, "DELETE FROM ttrss_feedbrowser_cache");
16 while ($line = db_fetch_assoc($result)) {
17 $subscribers = db_escape_string($line["subscribers"]);
18 $feed_url = db_escape_string($line["feed_url"]);
19 $title = db_escape_string($line["title"]);
20 $site_url = db_escape_string($line["site_url"]);
22 $tmp_result = db_query($link, "SELECT subscribers FROM
23 ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'");
25 if (db_num_rows($tmp_result) == 0) {
27 db_query($link, "INSERT INTO ttrss_feedbrowser_cache
28 (feed_url, site_url, title, subscribers) VALUES ('$feed_url',
29 '$site_url', '$title', '$subscribers')");
37 db_query($link, "COMMIT");
45 * Update a feed batch.
46 * Used by daemons to update n feeds by run.
47 * Only update feed needing a update, and not being processed
50 * @param mixed $link Database link
51 * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT.
52 * @param boolean $from_http Set to true if you call this function from http to disable cli specific code.
53 * @param boolean $debug Set to false to disable debug output. Default to true.
56 function update_daemon_common($link, $limit = DAEMON_FEED_LIMIT
, $from_http = false, $debug = true) {
57 // Process all other feeds using last_updated and interval parameters
59 // Test if the user has loggued in recently. If not, it does not update its feeds.
60 if (DAEMON_UPDATE_LOGIN_LIMIT
> 0) {
61 if (DB_TYPE
== "pgsql") {
62 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT
." days'";
64 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT
." DAY)";
67 $login_thresh_qpart = "";
70 // Test if the feed need a update (update interval exceded).
71 if (DB_TYPE
== "pgsql") {
72 $update_limit_qpart = "AND ((
73 ttrss_feeds.update_interval = 0
74 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
76 ttrss_feeds.update_interval > 0
77 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
78 ) OR ttrss_feeds.last_updated IS NULL)";
80 $update_limit_qpart = "AND ((
81 ttrss_feeds.update_interval = 0
82 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
84 ttrss_feeds.update_interval > 0
85 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
86 ) OR ttrss_feeds.last_updated IS NULL)";
89 // Test if feed is currently being updated by another process.
90 if (DB_TYPE
== "pgsql") {
91 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
93 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
96 // Test if there is a limit to number of updated feeds
98 if($limit) $query_limit = sprintf("LIMIT %d", $limit);
100 $random_qpart = sql_random_function();
102 // We search for feed needing update.
103 $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid,
104 ".SUBSTRING_FOR_DATE
."(ttrss_feeds.last_updated,1,19) AS last_updated,
105 ttrss_feeds.update_interval
107 ttrss_feeds, ttrss_users, ttrss_user_prefs
109 ttrss_feeds.owner_uid = ttrss_users.id
110 AND ttrss_users.id = ttrss_user_prefs.owner_uid
111 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
112 $login_thresh_qpart $update_limit_qpart
113 $updstart_thresh_qpart
114 ORDER BY $random_qpart $query_limit");
116 $user_prefs_cache = array();
118 if($debug) _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
120 // Here is a little cache magic in order to minimize risk of double feed updates.
121 $feeds_to_update = array();
122 while ($line = db_fetch_assoc($result)) {
123 $feeds_to_update[$line['id']] = $line;
126 // We update the feed last update started date before anything else.
127 // There is no lag due to feed contents downloads
128 // It prevent an other process to update the same feed.
129 $feed_ids = array_keys($feeds_to_update);
131 db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
132 WHERE id IN (%s)", implode(',', $feed_ids)));
135 // For each feed, we call the feed update function.
136 while ($line = array_pop($feeds_to_update)) {
138 if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
140 update_rss_feed($link, $line["id"], true);
142 sleep(1); // prevent flood (FIXME make this an option?)
145 // Send feed digests by email if needed.
146 if (DAEMON_SENDS_DIGESTS
) send_headlines_digests($link);
148 } // function update_daemon_common
150 function fetch_twitter_rss($link, $url, $owner_uid) {
152 require_once 'lib/tmhoauth/tmhOAuth.php';
154 $result = db_query($link, "SELECT twitter_oauth FROM ttrss_users
155 WHERE id = $owner_uid");
157 $access_token = json_decode(db_fetch_result($result, 0, 'twitter_oauth'), true);
158 $url_escaped = db_escape_string($url);
162 $tmhOAuth = new tmhOAuth(array(
163 'consumer_key' => CONSUMER_KEY
,
164 'consumer_secret' => CONSUMER_SECRET
,
165 'user_token' => $access_token['oauth_token'],
166 'user_secret' => $access_token['oauth_token_secret'],
169 $code = $tmhOAuth->request('GET', $url);
173 $content = $tmhOAuth->response
['response'];
175 define('MAGPIE_CACHE_ON', false);
177 $rss = new MagpieRSS($content, MAGPIE_OUTPUT_ENCODING
,
178 MAGPIE_INPUT_ENCODING
, MAGPIE_DETECT_ENCODING
);
184 db_query($link, "UPDATE ttrss_feeds
185 SET last_error = 'OAuth authorization failed ($code).'
186 WHERE feed_url = '$url_escaped' AND owner_uid = $owner_uid");
191 db_query($link, "UPDATE ttrss_feeds
192 SET last_error = 'OAuth information not found.'
193 WHERE feed_url = '$url_escaped' AND owner_uid = $owner_uid");
199 function update_rss_feed($link, $feed, $ignore_daemon = false, $no_cache = false) {
203 /* Update all feeds with the same URL to utilize memcache */
206 $result = db_query($link, "SELECT f1.id
207 FROM ttrss_feeds AS f1, ttrss_feeds AS f2
208 WHERE f2.feed_url = f1.feed_url AND f2.id = '$feed'");
210 while ($line = db_fetch_assoc($result)) {
211 update_rss_feed_real($link, $line["id"], $ignore_daemon, $no_cache);
214 update_rss_feed_real($link, $feed, $ignore_daemon, $no_cache);
218 function update_rss_feed_real($link, $feed, $ignore_daemon = false, $no_cache = false,
219 $override_url = false) {
221 require_once "lib/simplepie/simplepie.inc";
222 require_once "lib/magpierss/rss_fetch.inc";
223 require_once 'lib/magpierss/rss_utils.inc';
227 $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') ||
$_REQUEST['xdebug'];
229 if (!$_REQUEST["daemon"] && !$ignore_daemon) {
233 if ($debug_enabled) {
234 _debug("update_rss_feed: start");
237 if (!$ignore_daemon) {
239 if (DB_TYPE
== "pgsql") {
240 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
242 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
245 $result = db_query($link, "SELECT id,update_interval,auth_login,
246 auth_pass,cache_images,update_method
247 FROM ttrss_feeds WHERE id = '$feed' AND $updstart_thresh_qpart");
251 $result = db_query($link, "SELECT id,update_interval,auth_login,
252 feed_url,auth_pass,cache_images,update_method,last_updated,
253 mark_unread_on_update, owner_uid, update_on_checksum_change,
255 FROM ttrss_feeds WHERE id = '$feed'");
259 if (db_num_rows($result) == 0) {
260 if ($debug_enabled) {
261 _debug("update_rss_feed: feed $feed NOT FOUND/SKIPPED");
266 $update_method = db_fetch_result($result, 0, "update_method");
267 $last_updated = db_fetch_result($result, 0, "last_updated");
268 $owner_uid = db_fetch_result($result, 0, "owner_uid");
269 $mark_unread_on_update = sql_bool_to_bool(db_fetch_result($result,
270 0, "mark_unread_on_update"));
271 $update_on_checksum_change = sql_bool_to_bool(db_fetch_result($result,
272 0, "update_on_checksum_change"));
273 $pubsub_state = db_fetch_result($result, 0, "pubsub_state");
275 db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
276 WHERE id = '$feed'");
278 $auth_login = db_fetch_result($result, 0, "auth_login");
279 $auth_pass = db_fetch_result($result, 0, "auth_pass");
281 if ($update_method == 0)
282 $update_method = DEFAULT_UPDATE_METHOD +
1;
288 if ($update_method == 2)
289 $use_simplepie = true;
291 $use_simplepie = false;
293 if ($debug_enabled) {
294 _debug("update method: $update_method (feed setting: $update_method) (use simplepie: $use_simplepie)\n");
297 if ($update_method == 1) {
298 $auth_login = urlencode($auth_login);
299 $auth_pass = urlencode($auth_pass);
302 $update_interval = db_fetch_result($result, 0, "update_interval");
303 $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
304 $fetch_url = db_fetch_result($result, 0, "feed_url");
306 if ($update_interval < 0) { return false; }
308 $feed = db_escape_string($feed);
310 if ($auth_login && $auth_pass ){
311 $url_parts = array();
312 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
314 if ($url_parts[1] && $url_parts[2]) {
315 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
321 $fetch_url = $override_url;
323 if ($debug_enabled) {
324 _debug("update_rss_feed: fetching [$fetch_url]...");
327 $obj_id = md5("FDATA:$use_simplepie:$fetch_url");
329 if ($memcache && $obj = $memcache->get($obj_id)) {
331 if ($debug_enabled) {
332 _debug("update_rss_feed: data found in memcache.");
339 if ($update_method == 3) {
340 $rss = fetch_twitter_rss($link, $fetch_url, $owner_uid);
341 } else if ($update_method == 1) {
343 define('MAGPIE_CACHE_AGE', get_feed_update_interval($link, $feed) * 60);
344 define('MAGPIE_CACHE_ON', !$no_cache);
345 define('MAGPIE_FETCH_TIME_OUT', 60);
346 define('MAGPIE_CACHE_DIR', CACHE_DIR
. "/magpie");
348 $rss = @fetch_rss
($fetch_url);
350 $simplepie_cache_dir = CACHE_DIR
. "/simplepie";
352 if (!is_dir($simplepie_cache_dir)) {
353 mkdir($simplepie_cache_dir);
356 $rss = new SimplePie();
357 $rss->set_useragent(SELF_USER_AGENT
);
358 # $rss->set_timeout(10);
359 $rss->set_feed_url($fetch_url);
360 $rss->set_output_encoding('UTF-8');
361 $rss->force_feed(true);
363 if (SIMPLEPIE_CACHE_IMAGES
&& $cache_images) {
365 if ($debug_enabled) {
366 _debug("enabling image cache");
369 $rss->set_image_handler("image.php", 'i');
372 if ($debug_enabled) {
373 _debug("feed update interval (sec): " .
374 get_feed_update_interval($link, $feed)*60);
377 $rss->enable_cache(!$no_cache);
380 $rss->set_cache_location($simplepie_cache_dir);
381 $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60);
387 if ($memcache && $rss) $memcache->add($obj_id, $rss, 0, 300);
392 if ($debug_enabled) {
393 _debug("update_rss_feed: fetch done, parsing...");
396 $feed = db_escape_string($feed);
398 if ($update_method == 2) {
399 $fetch_ok = !$rss->error();
406 if ($debug_enabled) {
407 _debug("update_rss_feed: processing feed data...");
410 // db_query($link, "BEGIN");
412 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
413 FROM ttrss_feeds WHERE id = '$feed'");
415 $registered_title = db_fetch_result($result, 0, "title");
416 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
417 $orig_site_url = db_fetch_result($result, 0, "site_url");
419 $owner_uid = db_fetch_result($result, 0, "owner_uid");
421 if ($use_simplepie) {
422 $site_url = $rss->get_link();
424 $site_url = $rss->channel
["link"];
427 $site_url = rewrite_relative_url($fetch_url, $site_url);
429 if ($debug_enabled) {
430 _debug("update_rss_feed: checking favicon...");
433 check_feed_favicon($site_url, $feed, $link);
435 if (!$registered_title ||
$registered_title == "[Unknown]") {
437 if ($use_simplepie) {
438 $feed_title = db_escape_string($rss->get_title());
440 $feed_title = db_escape_string($rss->channel
["title"]);
443 if ($debug_enabled) {
444 _debug("update_rss_feed: registering title: $feed_title");
447 db_query($link, "UPDATE ttrss_feeds SET
448 title = '$feed_title' WHERE id = '$feed'");
451 // weird, weird Magpie
452 if (!$use_simplepie) {
453 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
456 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
457 db_query($link, "UPDATE ttrss_feeds SET
458 site_url = '$site_url' WHERE id = '$feed'");
461 // print "I: " . $rss->channel["image"]["url"];
463 if (!$use_simplepie) {
464 $icon_url = db_escape_string($rss->image
["url"]);
466 $icon_url = db_escape_string($rss->get_image_url());
469 $icon_url = substr($icon_url, 0, 250);
471 if ($icon_url && $orig_icon_url != $icon_url) {
472 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
475 if ($debug_enabled) {
476 _debug("update_rss_feed: loading filters...");
479 $filters = load_filters($link, $feed, $owner_uid);
481 // if ($debug_enabled) {
482 // print_r($filters);
485 if ($use_simplepie) {
486 $iterator = $rss->get_items();
488 $iterator = $rss->items
;
489 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
490 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
493 if (!is_array($iterator)) {
494 /* db_query($link, "UPDATE ttrss_feeds
495 SET last_error = 'Parse error: can\'t find any articles.'
496 WHERE id = '$feed'"); */
498 // clear any errors and mark feed as updated if fetched okay
499 // even if it's blank
501 if ($debug_enabled) {
502 _debug("update_rss_feed: entry iterator is not an array, no articles?");
505 db_query($link, "UPDATE ttrss_feeds
506 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
508 return; // no articles
511 if ($pubsub_state != 2 && PUBSUBHUBBUB_ENABLED
) {
513 if ($debug_enabled) _debug("update_rss_feed: checking for PUSH hub...");
515 $feed_hub_url = false;
516 if ($use_simplepie) {
517 $links = $rss->get_links('hub');
519 if ($links && is_array($links)) {
520 foreach ($links as $l) {
527 $atom = $rss->channel
['atom'];
530 if ($atom['link@rel'] == 'hub') {
531 $feed_hub_url = $atom['link@href'];
534 if (!$feed_hub_url && $atom['link#'] > 1) {
535 for ($i = 2; $i <= $atom['link#']; $i++
) {
536 if ($atom["link#$i@rel"] == 'hub') {
537 $feed_hub_url = $atom["link#$i@href"];
543 $feed_hub_url = $rss->channel
['link_hub'];
547 if ($debug_enabled) _debug("update_rss_feed: feed hub url: $feed_hub_url");
549 if ($feed_hub_url && function_exists('curl_init') &&
550 !ini_get("open_basedir")) {
552 require_once 'lib/pubsubhubbub/subscriber.php';
554 $callback_url = get_self_url_prefix() .
555 "/public.php?op=pubsub&id=$feed";
557 $s = new Subscriber($feed_hub_url, $callback_url);
559 $rc = $s->subscribe($fetch_url);
562 _debug("update_rss_feed: feed hub url found, subscribe request sent.");
564 db_query($link, "UPDATE ttrss_feeds SET pubsub_state = 1
565 WHERE id = '$feed'");
569 if ($debug_enabled) {
570 _debug("update_rss_feed: processing articles...");
573 foreach ($iterator as $item) {
575 if ($_REQUEST['xdebug'] == 2) {
579 if ($use_simplepie) {
580 $entry_guid = $item->get_id();
581 if (!$entry_guid) $entry_guid = $item->get_link();
582 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
586 $entry_guid = $item["id"];
588 if (!$entry_guid) $entry_guid = $item["guid"];
589 if (!$entry_guid) $entry_guid = $item["about"];
590 if (!$entry_guid) $entry_guid = $item["link"];
591 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
594 if ($debug_enabled) {
595 _debug("update_rss_feed: guid $entry_guid");
598 if (!$entry_guid) continue;
600 $entry_timestamp = "";
602 if ($use_simplepie) {
603 $entry_timestamp = strtotime($item->get_date());
605 $rss_2_date = $item['pubdate'];
606 $rss_1_date = $item['dc']['date'];
607 $atom_date = $item['issued'];
608 if (!$atom_date) $atom_date = $item['updated'];
610 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
611 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
612 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
616 if ($entry_timestamp == "" ||
$entry_timestamp == -1 ||
!$entry_timestamp) {
617 $entry_timestamp = time();
618 $no_orig_date = 'true';
620 $no_orig_date = 'false';
623 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
625 if ($debug_enabled) {
626 _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]");
629 if ($use_simplepie) {
630 $entry_title = $item->get_title();
632 $entry_title = trim(strip_tags($item["title"]));
635 if ($use_simplepie) {
636 $entry_link = $item->get_link();
638 // strange Magpie workaround
639 $entry_link = $item["link_"];
640 if (!$entry_link) $entry_link = $item["link"];
643 $entry_link = rewrite_relative_url($site_url, $entry_link);
645 if ($debug_enabled) {
646 _debug("update_rss_feed: title $entry_title");
647 _debug("update_rss_feed: link $entry_link");
650 if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
652 $entry_link = strip_tags($entry_link);
654 if ($use_simplepie) {
655 $entry_content = $item->get_content();
656 if (!$entry_content) $entry_content = $item->get_description();
658 $entry_content = $item["content:escaped"];
660 if (!$entry_content) $entry_content = $item["content:encoded"];
661 if (!$entry_content) $entry_content = $item["content"]["encoded"];
662 if (!$entry_content) $entry_content = $item["content"];
664 if (is_array($entry_content)) $entry_content = $entry_content[0];
666 // Magpie bugs are getting ridiculous
667 if (trim($entry_content) == "Array") $entry_content = false;
669 if (!$entry_content) $entry_content = $item["atom_content"];
670 if (!$entry_content) $entry_content = $item["summary"];
672 if (!$entry_content ||
673 strlen($entry_content) < strlen($item["description"])) {
674 $entry_content = $item["description"];
678 if (is_array($entry_content)) {
679 $entry_content = $entry_content["encoded"];
680 if (!$entry_content) $entry_content = $entry_content["escaped"];
684 if ($_REQUEST["xdebug"] == 2) {
685 print "update_rss_feed: content: ";
686 print_r(htmlspecialchars($entry_content));
689 $entry_content_unescaped = $entry_content;
691 if ($use_simplepie) {
692 $entry_comments = strip_tags($item->data
["comments"]);
693 if ($item->get_author()) {
694 $entry_author_item = $item->get_author();
695 $entry_author = $entry_author_item->get_name();
696 if (!$entry_author) $entry_author = $entry_author_item->get_email();
698 $entry_author = db_escape_string($entry_author);
701 $entry_comments = strip_tags($item["comments"]);
703 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
705 if ($item['author']) {
707 if (is_array($item['author'])) {
709 if (!$entry_author) {
710 $entry_author = db_escape_string(strip_tags($item['author']['name']));
713 if (!$entry_author) {
714 $entry_author = db_escape_string(strip_tags($item['author']['email']));
718 if (!$entry_author) {
719 $entry_author = db_escape_string(strip_tags($item['author']));
724 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
726 $entry_guid = db_escape_string(strip_tags($entry_guid));
727 $entry_guid = mb_substr($entry_guid, 0, 250);
729 $result = db_query($link, "SELECT id FROM ttrss_entries
730 WHERE guid = '$entry_guid'");
732 $entry_content = db_escape_string($entry_content, false);
734 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
736 $entry_title = db_escape_string($entry_title);
737 $entry_link = db_escape_string($entry_link);
738 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
739 $entry_author = mb_substr($entry_author, 0, 250);
741 if ($use_simplepie) {
742 $num_comments = 0; #FIXME#
744 $num_comments = db_escape_string($item["slash"]["comments"]);
747 if (!$num_comments) $num_comments = 0;
749 if ($debug_enabled) {
750 _debug("update_rss_feed: looking for tags [1]...");
753 // parse <category> entries into tags
755 $additional_tags = array();
757 if ($use_simplepie) {
759 $additional_tags_src = $item->get_categories();
761 if (is_array($additional_tags_src)) {
762 foreach ($additional_tags_src as $tobj) {
763 array_push($additional_tags, $tobj->get_term());
767 if ($debug_enabled) {
768 _debug("update_rss_feed: category tags:");
769 print_r($additional_tags);
774 $t_ctr = $item['category#'];
777 $additional_tags = array();
778 } else if ($t_ctr > 0) {
779 $additional_tags = array($item['category']);
781 if ($item['category@term']) {
782 array_push($additional_tags, $item['category@term']);
785 for ($i = 0; $i <= $t_ctr; $i++
) {
786 if ($item["category#$i"]) {
787 array_push($additional_tags, $item["category#$i"]);
790 if ($item["category#$i@term"]) {
791 array_push($additional_tags, $item["category#$i@term"]);
796 // parse <dc:subject> elements
798 $t_ctr = $item['dc']['subject#'];
801 array_push($additional_tags, $item['dc']['subject']);
803 for ($i = 0; $i <= $t_ctr; $i++
) {
804 if ($item['dc']["subject#$i"]) {
805 array_push($additional_tags, $item['dc']["subject#$i"]);
811 if ($debug_enabled) {
812 _debug("update_rss_feed: looking for tags [2]...");
816 // <a href="..." rel="tag">Xorg</a>, //
820 preg_match_all("/<a.*?rel=['\"]tag['\"].*?\>([^<]+)<\/a>/i",
821 $entry_content_unescaped, $entry_tags);
823 $entry_tags = $entry_tags[1];
825 $entry_tags = array_merge($entry_tags, $additional_tags);
826 $entry_tags = array_unique($entry_tags);
828 for ($i = 0; $i < count($entry_tags); $i++
)
829 $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8');
831 if ($debug_enabled) {
832 _debug("update_rss_feed: unfiltered tags found:");
833 print_r($entry_tags);
838 $entry_content = sanitize_article_content($entry_content);
839 $entry_title = sanitize_article_content($entry_title);
841 if ($debug_enabled) {
842 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
845 db_query($link, "BEGIN");
847 if (db_num_rows($result) == 0) {
849 if ($debug_enabled) {
850 _debug("update_rss_feed: base guid not found");
853 // base post entry does not exist, create it
855 $result = db_query($link,
856 "INSERT INTO ttrss_entries
873 '$entry_timestamp_fmt',
883 // we keep encountering the entry in feeds, so we need to
884 // update date_updated column so that we don't get horrible
885 // dupes when the entry gets purged and reinserted again e.g.
886 // in the case of SLOW SLOW OMG SLOW updating feeds
888 $base_entry_id = db_fetch_result($result, 0, "id");
890 db_query($link, "UPDATE ttrss_entries SET date_updated = NOW()
891 WHERE id = '$base_entry_id'");
894 // now it should exist, if not - bad luck then
896 $result = db_query($link, "SELECT
897 id,content_hash,no_orig_date,title,
898 ".SUBSTRING_FOR_DATE
."(date_updated,1,19) as date_updated,
899 ".SUBSTRING_FOR_DATE
."(updated,1,19) as updated,
903 WHERE guid = '$entry_guid'");
908 if (db_num_rows($result) == 1) {
910 if ($debug_enabled) {
911 _debug("update_rss_feed: base guid found, checking for user record");
914 // this will be used below in update handler
915 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
916 $orig_title = db_fetch_result($result, 0, "title");
917 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
918 $orig_date_updated = strtotime(db_fetch_result($result,
921 $ref_id = db_fetch_result($result, 0, "id");
922 $entry_ref_id = $ref_id;
924 // check for user post link to main table
926 // do we allow duplicate posts with same GUID in different feeds?
927 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
928 $dupcheck_qpart = "AND (feed_id = '$feed' OR feed_id IS NULL)";
930 $dupcheck_qpart = "";
933 /* Collect article tags here so we could filter by them: */
935 $article_filters = get_article_filters($filters, $entry_title,
936 $entry_content, $entry_link, $entry_timestamp, $entry_author,
939 if ($debug_enabled) {
940 _debug("update_rss_feed: article filters: ");
941 if (count($article_filters) != 0) {
942 print_r($article_filters);
946 if (find_article_filter($article_filters, "filter")) {
947 db_query($link, "COMMIT"); // close transaction in progress
951 $score = calculate_article_score($article_filters);
953 if ($debug_enabled) {
954 _debug("update_rss_feed: initial score: $score");
957 $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
958 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
961 // if ($_REQUEST["xdebug"]) print "$query\n";
963 $result = db_query($link, $query);
965 // okay it doesn't exist - create user entry
966 if (db_num_rows($result) == 0) {
968 if ($debug_enabled) {
969 _debug("update_rss_feed: user record not found, creating...");
972 if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) {
974 $last_read_qpart = 'NULL';
977 $last_read_qpart = 'NOW()';
980 if (find_article_filter($article_filters, 'mark') ||
$score > 1000) {
986 if (find_article_filter($article_filters, 'publish')) {
989 $published = 'false';
992 $result = db_query($link,
993 "INSERT INTO ttrss_user_entries
994 (ref_id, owner_uid, feed_id, unread, last_read, marked,
995 published, score, tag_cache, label_cache, uuid)
996 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
997 $last_read_qpart, $marked, $published, '$score', '', '', '')");
999 if (PUBSUBHUBBUB_HUB
&& $published == 'true') {
1000 $rss_link = get_self_url_prefix() .
1001 "/public.php?op=rss&id=-2&key=" .
1002 get_feed_access_key($link, -2, false, $owner_uid);
1004 $p = new Publisher(PUBSUBHUBBUB_HUB
);
1006 $pubsub_result = $p->publish_update($rss_link);
1009 $result = db_query($link,
1010 "SELECT int_id FROM ttrss_user_entries WHERE
1011 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
1012 feed_id = '$feed' LIMIT 1");
1014 if (db_num_rows($result) == 1) {
1015 $entry_int_id = db_fetch_result($result, 0, "int_id");
1018 if ($debug_enabled) {
1019 _debug("update_rss_feed: user record FOUND");
1022 $entry_ref_id = db_fetch_result($result, 0, "ref_id");
1023 $entry_int_id = db_fetch_result($result, 0, "int_id");
1026 if ($debug_enabled) {
1027 _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
1030 $post_needs_update = false;
1031 $update_insignificant = false;
1033 if ($orig_num_comments != $num_comments) {
1034 $post_needs_update = true;
1035 $update_insignificant = true;
1038 if ($content_hash != $orig_content_hash) {
1039 $post_needs_update = true;
1040 $update_insignificant = false;
1043 if (db_escape_string($orig_title) != $entry_title) {
1044 $post_needs_update = true;
1045 $update_insignificant = false;
1048 // if post needs update, update it and mark all user entries
1049 // linking to this post as updated
1050 if ($post_needs_update) {
1052 if (defined('DAEMON_EXTENDED_DEBUG')) {
1053 _debug("update_rss_feed: post $entry_guid needs update...");
1056 // print "<!-- post $orig_title needs update : $post_needs_update -->";
1058 db_query($link, "UPDATE ttrss_entries
1059 SET title = '$entry_title', content = '$entry_content',
1060 content_hash = '$content_hash',
1061 updated = '$entry_timestamp_fmt',
1062 num_comments = '$num_comments'
1063 WHERE id = '$ref_id'");
1065 if (!$update_insignificant) {
1066 if ($mark_unread_on_update) {
1067 db_query($link, "UPDATE ttrss_user_entries
1068 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
1069 } else if ($update_on_checksum_change) {
1070 db_query($link, "UPDATE ttrss_user_entries
1071 SET last_read = null WHERE ref_id = '$ref_id'
1072 AND unread = false");
1078 db_query($link, "COMMIT");
1080 if ($debug_enabled) {
1081 _debug("update_rss_feed: assigning labels...");
1084 assign_article_to_labels($link, $entry_ref_id, $article_filters,
1087 if ($debug_enabled) {
1088 _debug("update_rss_feed: looking for enclosures...");
1093 $enclosures = array();
1095 if ($use_simplepie) {
1096 $encs = $item->get_enclosures();
1098 if (is_array($encs)) {
1099 foreach ($encs as $e) {
1101 $e->link
, $e->type
, $e->length
);
1103 array_push($enclosures, $e_item);
1110 $e_ctr = $item['enclosure#'];
1113 $e_item = array($item['enclosure@url'],
1114 $item['enclosure@type'],
1115 $item['enclosure@length']);
1117 array_push($enclosures, $e_item);
1119 for ($i = 0; $i <= $e_ctr; $i++
) {
1121 if ($item["enclosure#$i@url"]) {
1122 $e_item = array($item["enclosure#$i@url"],
1123 $item["enclosure#$i@type"],
1124 $item["enclosure#$i@length"]);
1125 array_push($enclosures, $e_item);
1131 // can there be many of those? yes -fox
1133 $m_ctr = $item['media']['content#'];
1136 $e_item = array($item['media']['content@url'],
1137 $item['media']['content@medium'],
1138 $item['media']['content@length']);
1140 array_push($enclosures, $e_item);
1142 for ($i = 0; $i <= $m_ctr; $i++
) {
1144 if ($item["media"]["content#$i@url"]) {
1145 $e_item = array($item["media"]["content#$i@url"],
1146 $item["media"]["content#$i@medium"],
1147 $item["media"]["content#$i@length"]);
1148 array_push($enclosures, $e_item);
1156 if ($debug_enabled) {
1157 _debug("update_rss_feed: article enclosures:");
1158 print_r($enclosures);
1161 db_query($link, "BEGIN");
1163 foreach ($enclosures as $enc) {
1164 $enc_url = db_escape_string($enc[0]);
1165 $enc_type = db_escape_string($enc[1]);
1166 $enc_dur = db_escape_string($enc[2]);
1168 $result = db_query($link, "SELECT id FROM ttrss_enclosures
1169 WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'");
1171 if (db_num_rows($result) == 0) {
1172 db_query($link, "INSERT INTO ttrss_enclosures
1173 (content_url, content_type, title, duration, post_id) VALUES
1174 ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')");
1178 db_query($link, "COMMIT");
1180 // check for manual tags (we have to do it here since they're loaded from filters)
1182 foreach ($article_filters as $f) {
1183 if ($f[0] == "tag") {
1185 $manual_tags = trim_array(explode(",", $f[1]));
1187 foreach ($manual_tags as $tag) {
1188 if (tag_is_valid($tag)) {
1189 array_push($entry_tags, $tag);
1197 $boring_tags = trim_array(explode(",", mb_strtolower(get_pref($link,
1198 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
1200 $filtered_tags = array();
1201 $tags_to_cache = array();
1203 if ($entry_tags && is_array($entry_tags)) {
1204 foreach ($entry_tags as $tag) {
1205 if (array_search($tag, $boring_tags) === false) {
1206 array_push($filtered_tags, $tag);
1211 $filtered_tags = array_unique($filtered_tags);
1213 if ($debug_enabled) {
1214 _debug("update_rss_feed: filtered article tags:");
1215 print_r($filtered_tags);
1218 // Save article tags in the database
1220 if (count($filtered_tags) > 0) {
1222 db_query($link, "BEGIN");
1224 foreach ($filtered_tags as $tag) {
1226 $tag = sanitize_tag($tag);
1227 $tag = db_escape_string($tag);
1229 if (!tag_is_valid($tag)) continue;
1231 $result = db_query($link, "SELECT id FROM ttrss_tags
1232 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1233 owner_uid = '$owner_uid' LIMIT 1");
1235 if ($result && db_num_rows($result) == 0) {
1237 db_query($link, "INSERT INTO ttrss_tags
1238 (owner_uid,tag_name,post_int_id)
1239 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1242 array_push($tags_to_cache, $tag);
1245 /* update the cache */
1247 $tags_to_cache = array_unique($tags_to_cache);
1249 $tags_str = db_escape_string(join(",", $tags_to_cache));
1251 db_query($link, "UPDATE ttrss_user_entries
1252 SET tag_cache = '$tags_str' WHERE ref_id = '$entry_ref_id'
1253 AND owner_uid = $owner_uid");
1255 db_query($link, "COMMIT");
1258 if ($debug_enabled) {
1259 _debug("update_rss_feed: article processed");
1263 if (!$last_updated) {
1264 if ($debug_enabled) {
1265 _debug("update_rss_feed: new feed, catching it up...");
1267 catchup_feed($link, $feed, false, $owner_uid);
1270 if ($debug_enabled) {
1271 _debug("purging feed...");
1274 purge_feed($link, $feed, 0, $debug_enabled);
1276 db_query($link, "UPDATE ttrss_feeds
1277 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
1279 // db_query($link, "COMMIT");
1283 if ($use_simplepie) {
1284 $error_msg = mb_substr($rss->error(), 0, 250);
1286 $error_msg = mb_substr(magpie_error(), 0, 250);
1289 if ($debug_enabled) {
1290 _debug("update_rss_feed: error fetching feed: $error_msg");
1293 $error_msg = db_escape_string($error_msg);
1296 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1297 last_updated = NOW() WHERE id = '$feed'");
1300 if ($use_simplepie) {
1304 if ($debug_enabled) {
1305 _debug("update_rss_feed: done");