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