]> git.wh0rd.org - tt-rss.git/blob - update.php
overall directory tree cleanup
[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 if (!$link) {
51 if (DB_TYPE == "mysql") {
52 print mysql_error();
53 }
54 // PG seems to display its own errors just fine by default.
55 return;
56 }
57
58 init_connection($link);
59
60 if ($op == "-feeds") {
61 // Update all feeds needing a update.
62 update_daemon_common($link);
63
64 // Update feedbrowser
65 $count = update_feedbrowser_cache($link);
66 _debug("Feedbrowser updated, $count feeds processed.");
67
68 // Purge orphans and cleanup tags
69 purge_orphans($link, true);
70
71 $rc = cleanup_tags($link, 14, 50000);
72 _debug("Cleaned $rc cached tags.");
73
74 get_linked_feeds($link);
75 }
76
77 if ($op == "-feedbrowser") {
78 $count = update_feedbrowser_cache($link);
79 print "Finished, $count feeds processed.\n";
80 }
81
82 if ($op == "-daemon") {
83 while (true) {
84 passthru(PHP_EXECUTABLE . " " . $argv[0] . " -daemon-loop");
85 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
86 sleep(DAEMON_SLEEP_INTERVAL);
87 }
88 }
89
90 if ($op == "-daemon-loop") {
91 if (!make_stampfile('update_daemon.stamp')) {
92 die("error: unable to create stampfile\n");
93 }
94
95 // Call to the feed batch update function
96 // or regenerate feedbrowser cache
97
98 if (rand(0,100) > 30) {
99 update_daemon_common($link);
100 } else {
101 $count = update_feedbrowser_cache($link);
102 _debug("Feedbrowser updated, $count feeds processed.");
103
104 purge_orphans($link, true);
105
106 $rc = cleanup_tags($link, 14, 50000);
107
108 _debug("Cleaned $rc cached tags.");
109
110 get_linked_feeds($link);
111 }
112
113 }
114
115 if ($op == "-cleanup-tags") {
116 $rc = cleanup_tags($link, 14, 50000);
117 print "$rc tags deleted.\n";
118 }
119
120 if ($op == "-get-feeds") {
121 get_linked_feeds($link);
122 }
123
124 db_close($link);
125
126 if ($lock_handle != false) {
127 fclose($lock_handle);
128 }
129
130 unlink(LOCK_DIRECTORY . "/$lock_filename");
131 ?>