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