3 // this daemon runs in the background and updates all feeds
6 if ($argv[1] != "SRV_RUN_OK") {
7 die("This script should be run by update_daemon.php\n");
10 // define('DEFAULT_ERROR_LEVEL', E_ALL);
11 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE
);
15 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
16 define('SIMPLEPIE_CACHE_DIR', '/var/tmp/simplepie-ttrss-cache-daemon');
17 define('DISABLE_SESSIONS', true);
19 require_once "version.php";
21 if (strpos(VERSION
, ".99") !== false) {
22 define('DAEMON_EXTENDED_DEBUG', true);
25 define('PURGE_INTERVAL', 3600); // seconds
27 require_once "sanity_check.php";
28 require_once "config.php";
30 if (!ENABLE_UPDATE_DAEMON
) {
31 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
34 require_once "db.php";
35 require_once "db-prefs.php";
36 require_once "functions.php";
37 require_once "magpierss/rss_fetch.inc";
39 error_reporting(DEFAULT_ERROR_LEVEL
);
41 function sigalrm_handler() {
42 die("received SIGALRM, hang in feed update?\n");
45 pcntl_signal(SIGALRM
, sigalrm_handler
);
47 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
50 if (DB_TYPE
== "mysql") {
53 // PG seems to display its own errors just fine by default.
57 if (DB_TYPE
== "pgsql") {
58 pg_query("set client_encoding = 'utf-8'");
59 pg_set_client_encoding("UNICODE");
61 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET
) {
62 db_query($link, "SET NAMES " . MYSQL_CHARSET
);
63 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
69 if (!make_stampfile('update_daemon.stamp')) {
70 print "error: unable to create stampfile";
74 if (time() - $last_purge > PURGE_INTERVAL
) {
75 _debug("Purging old posts (random 30 feeds)...");
76 global_purge_old_posts($link, true, 30);
80 // FIXME: get all scheduled updates w/forced refetch
81 // Stub, until I figure out if it is really needed.
83 # $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
84 # while ($line = db_fetch_assoc($result)) {
85 # print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
86 # $line["owner_uid"] . "\n";
89 // Process all other feeds using last_updated and interval parameters
91 $random_qpart = sql_random_function();
94 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
97 $rows = pg_affected_rows($result);
101 $result = db_query($link, "DELETE FROM ttrss_user_entries
102 USING ttrss_user_entries, ttrss_entries
103 WHERE ttrss_entries.id = ref_id AND
105 feed_id = '$feed_id' AND
106 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
108 if (DAEMON_UPDATE_LOGIN_LIMIT
> 0) {
109 if (DB_TYPE
== "pgsql") {
110 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT
." days'";
112 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT
." DAY)";
115 $login_thresh_qpart = "";
118 if (DB_TYPE
== "pgsql") {
119 $update_limit_qpart = "AND ttrss_feeds.last_updated < NOW() - INTERVAL '".(DAEMON_SLEEP_INTERVAL
*2)." seconds'";
121 $update_limit_qpart = "AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ".(DAEMON_SLEEP_INTERVAL
*2)." SECOND)";
124 if (DB_TYPE
== "pgsql") {
125 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
127 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
130 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
131 SUBSTRING(last_updated,1,19) AS last_updated,
134 ttrss_feeds,ttrss_users
136 ttrss_users.id = owner_uid $login_thresh_qpart $update_limit_qpart
137 $updstart_thresh_qpart
138 ORDER BY $random_qpart DESC LIMIT " . DAEMON_FEED_LIMIT
);
140 $user_prefs_cache = array();
142 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
144 while ($line = db_fetch_assoc($result)) {
146 $upd_intl = $line["update_interval"];
147 $user_id = $line["owner_uid"];
149 if (!$upd_intl ||
$upd_intl == 0) {
150 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
151 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
152 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
154 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
159 # print "Updates disabled.\n";
163 _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
165 // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
166 // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
168 if (!$line["last_updated"] ||
169 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
171 _debug("Updating...");
175 update_rss_feed($link, $line["feed_url"], $line["id"], true);
179 sleep(1); // prevent flood (FIXME make this an option?)
181 _debug("Update not needed.");
185 if (DAEMON_SENDS_DIGESTS
) send_headlines_digests($link);