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