]> git.wh0rd.org - tt-rss.git/blob - update.php
8cca3daa0937b0ae3de0c0c3897dc260283be857
[tt-rss.git] / update.php
1 #!/usr/bin/php
2 <?php
3 set_include_path(get_include_path() . PATH_SEPARATOR . "include");
4
5 define('DISABLE_SESSIONS', true);
6
7 chdir(dirname(__FILE__));
8
9 require_once "functions.php";
10 require_once "rssfuncs.php";
11 require_once "sanity_check.php";
12 require_once "config.php";
13 require_once "db.php";
14 require_once "db-prefs.php";
15
16 if (!defined('PHP_EXECUTABLE'))
17 define('PHP_EXECUTABLE', '/usr/bin/php');
18
19 $op = $argv[1];
20
21 if (!$op || $op == "-help") {
22 print "Tiny Tiny RSS data update script.\n\n";
23 print "Options:\n";
24 print " -feeds - update feeds\n";
25 print " -feedbrowser - update feedbrowser\n";
26 print " -daemon - start single-process update daemon\n";
27 print " -cleanup-tags - perform tags table maintenance\n";
28 print " -get-feeds - receive popular feeds from linked instances\n";
29 print " -help - show this help\n";
30 return;
31 }
32
33 if ($op != "-daemon") {
34 $lock_filename = "update.lock";
35 } else {
36 $lock_filename = "update_daemon.lock";
37 }
38
39 $lock_handle = make_lockfile($lock_filename);
40 $must_exit = false;
41
42 // Try to lock a file in order to avoid concurrent update.
43 if (!$lock_handle) {
44 die("error: Can't create lockfile ($lock_filename). ".
45 "Maybe another update process is already running.\n");
46 }
47
48 // Create a database connection.
49 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
50
51 init_connection($link);
52
53 if ($op == "-feeds") {
54 // Update all feeds needing a update.
55 update_daemon_common($link);
56
57 // Update feedbrowser
58 $count = update_feedbrowser_cache($link);
59 _debug("Feedbrowser updated, $count feeds processed.");
60
61 // Purge orphans and cleanup tags
62 purge_orphans($link, true);
63
64 $rc = cleanup_tags($link, 14, 50000);
65 _debug("Cleaned $rc cached tags.");
66
67 get_linked_feeds($link);
68 }
69
70 if ($op == "-feedbrowser") {
71 $count = update_feedbrowser_cache($link);
72 print "Finished, $count feeds processed.\n";
73 }
74
75 if ($op == "-daemon") {
76 while (true) {
77 passthru(PHP_EXECUTABLE . " " . $argv[0] . " -daemon-loop");
78 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
79 sleep(DAEMON_SLEEP_INTERVAL);
80 }
81 }
82
83 if ($op == "-daemon-loop") {
84 if (!make_stampfile('update_daemon.stamp')) {
85 die("error: unable to create stampfile\n");
86 }
87
88 // Call to the feed batch update function
89 // or regenerate feedbrowser cache
90
91 if (rand(0,100) > 30) {
92 update_daemon_common($link);
93 } else {
94 $count = update_feedbrowser_cache($link);
95 _debug("Feedbrowser updated, $count feeds processed.");
96
97 purge_orphans($link, true);
98
99 $rc = cleanup_tags($link, 14, 50000);
100
101 _debug("Cleaned $rc cached tags.");
102
103 get_linked_feeds($link);
104 }
105
106 }
107
108 if ($op == "-cleanup-tags") {
109 $rc = cleanup_tags($link, 14, 50000);
110 print "$rc tags deleted.\n";
111 }
112
113 if ($op == "-get-feeds") {
114 get_linked_feeds($link);
115 }
116
117 db_close($link);
118
119 if ($lock_handle != false) {
120 fclose($lock_handle);
121 }
122
123 unlink(LOCK_DIRECTORY . "/$lock_filename");
124 ?>