]> git.wh0rd.org Git - tt-rss.git/blob - update_daemon.php
add tt-rss forum as default subscription for new users
[tt-rss.git] / update_daemon.php
1 #!/usr/bin/php
2 <?php
3         // this daemon runs in the background and updates all feeds
4         // continuously
5
6         define('DEFAULT_ERROR_LEVEL', E_ALL);
7
8         declare(ticks = 1);
9
10         define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
11         define('DISABLE_SESSIONS', true);
12
13         define('PURGE_INTERVAL', 3600); // seconds
14
15         require_once "sanity_check.php";
16         require_once "config.php";
17
18         if (!ENABLE_UPDATE_DAEMON) {
19                 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
20         }
21         
22         require_once "db.php";
23         require_once "db-prefs.php";
24         require_once "functions.php";
25         require_once "magpierss/rss_fetch.inc";
26
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.\n");
39         }
40
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                 pg_set_client_encoding("UNICODE");
54         }
55
56         $last_purge = 0;
57
58         while (true) {
59
60                 if (time() - $last_purge > PURGE_INTERVAL) {
61                         print "Purging old posts (random 30 feeds)...\n";
62                         global_purge_old_posts($link, true, 30);
63                         $last_purge = time();
64                 }
65
66                 // FIXME: get all scheduled updates w/forced refetch
67                 // Stub, until I figure out if it is really needed.
68
69 #               $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id");
70 #               while ($line = db_fetch_assoc($result)) {
71 #                       print "Scheduled feed update: " . $line["feed_id"] . ", UID: " . 
72 #                               $line["owner_uid"] . "\n";
73 #               }
74         
75                 // Process all other feeds using last_updated and interval parameters
76
77                 $random_qpart = sql_random_function();
78
79 /*              
80                                         ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
81                         }
82
83                         $rows = pg_affected_rows($result);
84                         
85                 } else {
86
87                         $result = db_query($link, "DELETE FROM ttrss_user_entries 
88                                 USING ttrss_user_entries, ttrss_entries 
89                                 WHERE ttrss_entries.id = ref_id AND 
90                                 marked = false AND 
91                                 feed_id = '$feed_id' AND 
92                                 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */                
93                 
94                 if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
95                         if (DB_TYPE == "pgsql") {
96                                 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
97                         } else {
98                                 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
99                         }                       
100                 } else {
101                         $login_thresh_qpart = "";
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 
111                         ORDER BY $random_qpart DESC");
112
113                 $user_prefs_cache = array();
114
115                 printf("Scheduled %d feeds to update...\n", db_num_rows($result));
116                 
117                 while ($line = db_fetch_assoc($result)) {
118         
119                         $upd_intl = $line["update_interval"];
120                         $user_id = $line["owner_uid"];
121         
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;
126                                 } else {
127                                         $upd_intl = $user_prefs_cache[$user_id]['DEFAULT_UPDATE_INTERVAL'];
128                                 }
129                         }
130
131                         if ($upd_intl < 0) { 
132 #                               print "Updates disabled.\n";
133                                 continue; 
134                         }
135         
136                         print "Feed: " . $line["feed_url"] . ": ";
137
138                         printf("(%d/%d, %d) ", time() - strtotime($line["last_updated"]),
139                                 $upd_intl*60, $user_id);
140         
141                         if (!$line["last_updated"] || 
142                                 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
143         
144                                 print "Updating...\n";  
145                                 update_rss_feed($link, $line["feed_url"], $line["id"], true);   
146                                 sleep(1); // prevent flood (FIXME make this an option?)
147                         } else {
148                                 print "Update not needed.\n";
149                         }
150                 }
151
152                 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
153
154                 print "Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...\n";
155                 
156                 sleep(DAEMON_SLEEP_INTERVAL);
157         }
158
159         db_close($link);
160
161 ?>