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