]>
Commit | Line | Data |
---|---|---|
02008cb1 AD |
1 | #!/usr/bin/php |
2 | <?php | |
3 | // define('DEFAULT_ERROR_LEVEL', E_ALL); | |
4 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
5 | ||
6 | if ($argv[1] != "SRV_RUN_OK") { | |
7 | die("This script should be started by update_daemon2.php.\n"); | |
8 | } | |
9 | ||
10 | declare(ticks = 1); | |
11 | ||
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); | |
15 | ||
16 | require_once "version.php"; | |
17 | ||
18 | if (strpos(VERSION, ".99") !== false) { | |
19 | define('DAEMON_EXTENDED_DEBUG', true); | |
20 | } | |
21 | ||
22 | define('PURGE_INTERVAL', 3600); // seconds | |
23 | ||
24 | require_once "sanity_check.php"; | |
25 | require_once "config.php"; | |
26 | ||
27 | if (!ENABLE_UPDATE_DAEMON) { | |
28 | die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n"); | |
29 | } | |
30 | ||
31 | require_once "db.php"; | |
32 | require_once "db-prefs.php"; | |
33 | require_once "functions.php"; | |
34 | require_once "magpierss/rss_fetch.inc"; | |
35 | ||
36 | error_reporting(DEFAULT_ERROR_LEVEL); | |
37 | ||
38 | function sigalrm_handler() { | |
39 | die("received SIGALRM, hang in feed update?\n"); | |
40 | } | |
41 | ||
42 | pcntl_signal(SIGALRM, sigalrm_handler); | |
43 | ||
44 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
45 | ||
46 | if (!$link) { | |
47 | if (DB_TYPE == "mysql") { | |
48 | print mysql_error(); | |
49 | } | |
50 | // PG seems to display its own errors just fine by default. | |
51 | return; | |
52 | } | |
53 | ||
54 | if (DB_TYPE == "pgsql") { | |
55 | pg_query("set client_encoding = 'utf-8'"); | |
56 | pg_set_client_encoding("UNICODE"); | |
57 | } else { | |
58 | if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { | |
59 | db_query($link, "SET NAMES " . MYSQL_CHARSET); | |
60 | // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); | |
61 | } | |
62 | } | |
63 | ||
64 | $last_purge = 0; | |
65 | ||
0bd6e68a AD |
66 | if (!make_stampfile('update_daemon.stamp')) { |
67 | print "warning: unable to create stampfile"; | |
68 | } | |
02008cb1 AD |
69 | |
70 | if (time() - $last_purge > PURGE_INTERVAL) { | |
71 | _debug("Purging old posts (random 30 feeds)..."); | |
72 | global_purge_old_posts($link, true, 30); | |
73 | $last_purge = time(); | |
74 | } | |
75 | ||
76 | // Process all other feeds using last_updated and interval parameters | |
77 | ||
78 | $random_qpart = sql_random_function(); | |
79 | ||
80 | if (DAEMON_UPDATE_LOGIN_LIMIT > 0) { | |
81 | if (DB_TYPE == "pgsql") { | |
82 | $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'"; | |
83 | } else { | |
84 | $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)"; | |
85 | } | |
86 | } else { | |
87 | $login_thresh_qpart = ""; | |
88 | } | |
89 | ||
90 | if (DB_TYPE == "pgsql") { | |
91 | $update_limit_qpart = "AND ttrss_feeds.last_updated < NOW() - INTERVAL '".(DAEMON_SLEEP_INTERVAL*2)." seconds'"; | |
92 | } else { | |
93 | $update_limit_qpart = "AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ".(DAEMON_SLEEP_INTERVAL*2)." SECOND)"; | |
94 | } | |
95 | ||
96 | if (DB_TYPE == "pgsql") { | |
e620f72f | 97 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')"; |
02008cb1 | 98 | } else { |
e620f72f | 99 | $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))"; |
02008cb1 AD |
100 | } |
101 | ||
102 | $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid, | |
103 | SUBSTRING(last_updated,1,19) AS last_updated, | |
104 | update_interval | |
105 | FROM | |
106 | ttrss_feeds,ttrss_users | |
107 | WHERE | |
108 | ttrss_users.id = owner_uid $login_thresh_qpart $update_limit_qpart | |
109 | $updstart_thresh_qpart | |
110 | ORDER BY $random_qpart DESC LIMIT " . DAEMON_FEED_LIMIT); | |
111 | ||
112 | $user_prefs_cache = array(); | |
113 | ||
114 | _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result))); | |
115 | ||
116 | while ($line = db_fetch_assoc($result)) { | |
117 | ||
118 | $upd_intl = $line["update_interval"]; | |
119 | $user_id = $line["owner_uid"]; | |
120 | ||
121 | if (!$upd_intl || $upd_intl == 0) { | |
122 | if (!$user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']) { | |
123 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id); | |
124 | $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'] = $upd_intl; | |
125 | } else { | |
126 | $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL']; | |
127 | } | |
128 | } | |
129 | ||
130 | if ($upd_intl < 0) { | |
131 | # print "Updates disabled.\n"; | |
132 | continue; | |
133 | } | |
134 | ||
135 | _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]); | |
136 | ||
137 | // _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ", | |
138 | // time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id)); | |
139 | ||
140 | if (!$line["last_updated"] || | |
141 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) { | |
142 | ||
143 | _debug("Updating..."); | |
144 | ||
145 | pcntl_alarm(300); | |
146 | ||
147 | update_rss_feed($link, $line["feed_url"], $line["id"], true); | |
148 | ||
149 | pcntl_alarm(0); | |
150 | ||
151 | sleep(1); // prevent flood (FIXME make this an option?) | |
152 | } else { | |
153 | _debug("Update not needed."); | |
154 | } | |
155 | } | |
156 | ||
157 | if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link); | |
158 | ||
159 | db_close($link); | |
160 | ?> |