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