3 // define('DEFAULT_ERROR_LEVEL', E_ALL);
4 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE
);
6 if ($argv[1] != "SRV_RUN_OK") {
7 die("This script should be started by update_daemon2.php.\n");
12 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
13 define('SIMPLEPIE_CACHE_DIR', '/var/tmp/simplepie-ttrss-cache-daemon');
14 define('DISABLE_SESSIONS', true);
16 require_once "version.php";
18 if (strpos(VERSION
, ".99") !== false) {
19 define('DAEMON_EXTENDED_DEBUG', true);
22 define('PURGE_INTERVAL', 3600); // seconds
24 require_once "sanity_check.php";
25 require_once "config.php";
27 if (!ENABLE_UPDATE_DAEMON
) {
28 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
31 require_once "db.php";
32 require_once "db-prefs.php";
33 require_once "functions.php";
34 require_once "magpierss/rss_fetch.inc";
36 error_reporting(DEFAULT_ERROR_LEVEL
);
38 function sigalrm_handler() {
39 die("received SIGALRM, hang in feed update?\n");
42 pcntl_signal(SIGALRM
, sigalrm_handler
);
44 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
47 if (DB_TYPE
== "mysql") {
50 // PG seems to display its own errors just fine by default.
54 if (DB_TYPE
== "pgsql") {
55 pg_query("set client_encoding = 'utf-8'");
56 pg_set_client_encoding("UNICODE");
58 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET
) {
59 db_query($link, "SET NAMES " . MYSQL_CHARSET
);
60 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
66 // if (!make_stampfile('update_daemon.stamp')) {
67 // print "error: unable to create stampfile";
71 if (time() - $last_purge > PURGE_INTERVAL
) {
72 _debug("Purging old posts (random 30 feeds)...");
73 global_purge_old_posts($link, true, 30);
77 // Process all other feeds using last_updated and interval parameters
79 $random_qpart = sql_random_function();
81 if (DAEMON_UPDATE_LOGIN_LIMIT
> 0) {
82 if (DB_TYPE
== "pgsql") {
83 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT
." days'";
85 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT
." DAY)";
88 $login_thresh_qpart = "";
91 if (DB_TYPE
== "pgsql") {
92 $update_limit_qpart = "AND ttrss_feeds.last_updated < NOW() - INTERVAL '".(DAEMON_SLEEP_INTERVAL
*2)." seconds'";
94 $update_limit_qpart = "AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ".(DAEMON_SLEEP_INTERVAL
*2)." SECOND)";
97 if (DB_TYPE
== "pgsql") {
98 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started >= NOW() - INTERVAL '120 seconds')";
100 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started >= DATE_SUB(NOW(), INTERVAL 120 SECOND))";
103 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
104 SUBSTRING(last_updated,1,19) AS last_updated,
107 ttrss_feeds,ttrss_users
109 ttrss_users.id = owner_uid $login_thresh_qpart $update_limit_qpart
110 $updstart_thresh_qpart
111 ORDER BY $random_qpart DESC LIMIT " . DAEMON_FEED_LIMIT
);
113 $user_prefs_cache = array();
115 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
117 while ($line = db_fetch_assoc($result)) {
119 $upd_intl = $line["update_interval"];
120 $user_id = $line["owner_uid"];
122 if (!$upd_intl ||
$upd_intl == 0) {
123 if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) {
124 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
125 $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl;
127 $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
132 # print "Updates disabled.\n";
136 _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
138 // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
139 // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
141 if (!$line["last_updated"] ||
142 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
144 _debug("Updating...");
148 update_rss_feed($link, $line["feed_url"], $line["id"], true);
152 sleep(1); // prevent flood (FIXME make this an option?)
154 _debug("Update not needed.");
158 if (DAEMON_SENDS_DIGESTS
) send_headlines_digests($link);