]> git.wh0rd.org - tt-rss.git/blame - update_daemon.php
update translations
[tt-rss.git] / update_daemon.php
CommitLineData
cd2cd415 1#!/usr/bin/php
1d3a17c7 2<?php
de696427
AD
3 // this daemon runs in the background and updates all feeds
4 // continuously
5
550e37d5 6 define('DEFAULT_ERROR_LEVEL', E_ALL);
aba609e0 7 error_reporting(E_ALL);
550e37d5 8
6eafcac6 9 declare(ticks = 1);
de696427
AD
10
11 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
de696427
AD
12 define('DISABLE_SESSIONS', true);
13
3b3d116e
AD
14 define('PURGE_INTERVAL', 3600); // seconds
15
de696427
AD
16 require_once "sanity_check.php";
17 require_once "config.php";
c6784aea
AD
18
19 if (!ENABLE_UPDATE_DAEMON) {
69ff793a 20 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
c6784aea
AD
21 }
22
de696427
AD
23 require_once "db.php";
24 require_once "db-prefs.php";
25 require_once "functions.php";
26 require_once "magpierss/rss_fetch.inc";
27
6eafcac6
AD
28 function sigint_handler() {
29 unlink("update_daemon.lock");
30 die("Received SIGINT. Exiting.\n");
31 }
32
33 pcntl_signal(SIGINT, sigint_handler);
34
35 $lock_handle = make_lockfile("update_daemon.lock");
36
37 if (!$lock_handle) {
38 die("error: Can't create lockfile ($lock_filename). ".
69ff793a 39 "Maybe another daemon is already running.\n");
6eafcac6
AD
40 }
41
de696427
AD
42 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
43
44 if (!$link) {
45 if (DB_TYPE == "mysql") {
46 print mysql_error();
47 }
48 // PG seems to display its own errors just fine by default.
49 return;
50 }
51
52 if (DB_TYPE == "pgsql") {
53 pg_query("set client_encoding = 'utf-8'");
ef063748 54 pg_set_client_encoding("UNICODE");
de696427
AD
55 }
56
3b3d116e
AD
57 $last_purge = 0;
58
1f4ad53c 59 while (true) {
a545b564 60
3b3d116e 61 if (time() - $last_purge > PURGE_INTERVAL) {
6f9e33e4 62 _debug("Purging old posts (random 30 feeds)...");
894ebcf5 63 global_purge_old_posts($link, true, 30);
3b3d116e
AD
64 $last_purge = time();
65 }
1f4ad53c 66
085a5a74 67 // FIXME: get all scheduled updates w/forced refetch
6eafcac6 68 // Stub, until I figure out if it is really needed.
1f4ad53c 69
085a5a74
AD
70# $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
71# while ($line = db_fetch_assoc($result)) {
72# print "Scheduled feed update: " . $line["feed_id"] . ", UID: " .
73# $line["owner_uid"] . "\n";
74# }
1f4ad53c
AD
75
76 // Process all other feeds using last_updated and interval parameters
77
894ebcf5
AD
78 $random_qpart = sql_random_function();
79
eed55fd3
AD
80/*
81 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
82 }
83
84 $rows = pg_affected_rows($result);
85
86 } else {
87
88 $result = db_query($link, "DELETE FROM ttrss_user_entries
89 USING ttrss_user_entries, ttrss_entries
90 WHERE ttrss_entries.id = ref_id AND
91 marked = false AND
92 feed_id = '$feed_id' AND
93 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
94
95 if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
96 if (DB_TYPE == "pgsql") {
97 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
98 } else {
99 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
100 }
101 } else {
102 $login_thresh_qpart = "";
103 }
104
105 $result = db_query($link, "SELECT feed_url,ttrss_feeds.id,owner_uid,
106 SUBSTRING(last_updated,1,19) AS last_updated,
107 update_interval
108 FROM
109 ttrss_feeds,ttrss_users
110 WHERE
111 ttrss_users.id = owner_uid $login_thresh_qpart
112 ORDER BY $random_qpart DESC");
44e241cb
AD
113
114 $user_prefs_cache = array();
115
6f9e33e4 116 _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
eed55fd3 117
1f4ad53c
AD
118 while ($line = db_fetch_assoc($result)) {
119
1f4ad53c 120 $upd_intl = $line["update_interval"];
1f4ad53c
AD
121 $user_id = $line["owner_uid"];
122
123 if (!$upd_intl || $upd_intl == 0) {
44e241cb
AD
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 }
1f4ad53c 130 }
c1e202b7
AD
131
132 if ($upd_intl < 0) {
1011b66a 133# print "Updates disabled.\n";
c1e202b7
AD
134 continue;
135 }
1f4ad53c 136
6f9e33e4 137 _debug("Feed: " . $line["feed_url"]);
1011b66a 138
6f9e33e4
AD
139// _debug(sprintf("\tLU: %d, INTL: %d, UID: %d) ",
140// time() - strtotime($line["last_updated"]), $upd_intl*60, $user_id));
1f4ad53c
AD
141
142 if (!$line["last_updated"] ||
143 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
6f9e33e4 144
072bd2cc 145 _debug("Updating...");
6f9e33e4 146
6eafcac6 147 update_rss_feed($link, $line["feed_url"], $line["id"], true);
44e241cb 148 sleep(1); // prevent flood (FIXME make this an option?)
6eafcac6 149 } else {
072bd2cc 150 _debug("Update not needed.");
1f4ad53c 151 }
de696427
AD
152 }
153
99018440 154 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
9cd7c995 155
6f9e33e4 156 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
1f4ad53c 157
78ea1de0 158 sleep(DAEMON_SLEEP_INTERVAL);
de696427
AD
159 }
160
de696427
AD
161 db_close($link);
162
163?>