3 // define('DEFAULT_ERROR_LEVEL', E_ALL);
4 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE
);
6 $start_timestamp = time();
8 if ($argv[1] != "SRV_RUN_OK") {
9 die("This script should be started by update_daemon2.php.\n");
14 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
15 define('SIMPLEPIE_CACHE_DIR', '/var/tmp/simplepie-ttrss-cache-daemon');
16 define('DISABLE_SESSIONS', true);
18 require_once "version.php";
20 if (strpos(VERSION
, ".99") !== false) {
21 define('DAEMON_EXTENDED_DEBUG', true);
24 define('PURGE_INTERVAL', 3600); // seconds
26 require_once "sanity_check.php";
27 require_once "config.php";
29 if (!ENABLE_UPDATE_DAEMON
) {
30 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
33 require_once "db.php";
34 require_once "db-prefs.php";
35 require_once "functions.php";
36 require_once "magpierss/rss_fetch.inc";
38 error_reporting(DEFAULT_ERROR_LEVEL
);
40 function sigalrm_handler() {
41 die("received SIGALRM, hang in feed update?\n");
44 pcntl_signal(SIGALRM
, sigalrm_handler
);
46 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
49 if (DB_TYPE
== "mysql") {
52 // PG seems to display its own errors just fine by default.
56 if (DB_TYPE
== "pgsql") {
57 pg_query("set client_encoding = 'utf-8'");
58 pg_set_client_encoding("UNICODE");
60 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET
) {
61 db_query($link, "SET NAMES " . MYSQL_CHARSET
);
62 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
68 if (!make_stampfile('update_daemon.stamp')) {
69 print "warning: unable to create stampfile";
72 if (time() - $last_purge > PURGE_INTERVAL
) {
73 _debug("Purging old posts (random 30 feeds)...");
74 global_purge_old_posts($link, true, 30);
78 // Process all other feeds using last_updated and interval parameters
80 $random_qpart = sql_random_function();
82 if (DAEMON_UPDATE_LOGIN_LIMIT
> 0) {
83 if (DB_TYPE
== "pgsql") {
84 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT
." days'";
86 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT
." DAY)";
89 $login_thresh_qpart = "";
92 if (DB_TYPE
== "pgsql") {
93 $update_limit_qpart = "AND ttrss_feeds.last_updated < NOW() - INTERVAL '".(DAEMON_SLEEP_INTERVAL
*2)." seconds'";
95 $update_limit_qpart = "AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ".(DAEMON_SLEEP_INTERVAL
*2)." SECOND)";
98 if (DB_TYPE
== "pgsql") {
99 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
101 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
104 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
105 SUBSTRING(last_updated,1,19) AS last_updated,
108 ttrss_feeds,ttrss_users
110 ttrss_users.id = owner_uid $login_thresh_qpart $update_limit_qpart
111 $updstart_thresh_qpart
112 ORDER BY $random_qpart DESC LIMIT " . DAEMON_FEED_LIMIT
);
114 $user_prefs_cache = array();
116 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
118 while ($line = db_fetch_assoc($result)) {
120 $upd_intl = $line["update_interval"];
121 $user_id = $line["owner_uid"];
123 if (!$upd_intl ||
$upd_intl == 0) {
124 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
125 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
126 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
128 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
133 # print "Updates disabled.\n";
137 _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
139 // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
140 // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
142 if (!$line["last_updated"] ||
143 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
145 _debug("Updating...");
149 update_rss_feed($link, $line["feed_url"], $line["id"], true);
153 sleep(1); // prevent flood (FIXME make this an option?)
155 _debug("Update not needed.");
159 if (DAEMON_SENDS_DIGESTS
) send_headlines_digests($link);
161 print "Elapsed time: " . (time() - $start_timestamp) . " second(s)\n";