]> git.wh0rd.org - tt-rss.git/blame - update.php
update.php & daemon: chdir to script directory using __FILE__ magic constant
[tt-rss.git] / update.php
CommitLineData
661135c7 1#!/usr/bin/php
fecd57c8 2<?php
661135c7
AD
3 define('DISABLE_SESSIONS', true);
4
9b27cec8
AD
5 chdir(dirname(__FILE__));
6
fb074239 7 require_once "functions.php";
81596c66 8 require_once "sanity_check.php";
81596c66
AD
9 require_once "config.php";
10 require_once "db.php";
661135c7 11 require_once "db-prefs.php";
661135c7 12
86268d8b
AD
13 if (!defined('PHP_EXECUTABLE'))
14 define('PHP_EXECUTABLE', '/usr/bin/php');
15
661135c7 16 $op = $argv[1];
70dcff6b 17
661135c7
AD
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";
868650e4 24 print " -cleanup-tags - perform tags table maintenance\n";
661135c7
AD
25 print " -help - show this help\n";
26 return;
81596c66 27 }
87b9fb65 28
661135c7
AD
29 if ($op != "-daemon") {
30 $lock_filename = "update.lock";
31 } else {
32 $lock_filename = "update_daemon.lock";
33 }
fecd57c8 34
661135c7
AD
35 $lock_handle = make_lockfile($lock_filename);
36 $must_exit = false;
fecd57c8 37
661135c7
AD
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 }
fecd57c8 43
661135c7 44 // Create a database connection.
dbaa4e4a 45 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
fecd57c8 46
661135c7
AD
47 if (!$link) {
48 if (DB_TYPE == "mysql") {
49 print mysql_error();
50 }
dbaa4e4a 51 // PG seems to display its own errors just fine by default.
661135c7
AD
52 return;
53 }
b4c27af7 54
661135c7 55 init_connection($link);
ef59e6e8 56
661135c7
AD
57 if ($op == "-feeds") {
58 // Update all feeds needing a update.
59 update_daemon_common($link);
84e9a8c7
AD
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.");
661135c7 70 }
fecd57c8 71
661135c7
AD
72 if ($op == "-feedbrowser") {
73 $count = update_feedbrowser_cache($link);
74 print "Finished, $count feeds processed.\n";
fecd57c8 75 }
661135c7
AD
76
77 if ($op == "-daemon") {
661135c7
AD
78 while (true) {
79 passthru(PHP_EXECUTABLE . " " . $argv[0] . " -daemon-loop");
80 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
81 sleep(DAEMON_SLEEP_INTERVAL);
fecd57c8 82 }
81596c66 83 }
9e21a571 84
661135c7
AD
85 if ($op == "-daemon-loop") {
86 if (!make_stampfile('update_daemon.stamp')) {
87 die("error: unable to create stampfile\n");
88 }
9e21a571 89
dbaa4e4a 90 // Call to the feed batch update function
661135c7 91 // or regenerate feedbrowser cache
9e21a571 92
661135c7
AD
93 if (rand(0,100) > 30) {
94 update_daemon_common($link);
9e21a571 95 } else {
661135c7 96 $count = update_feedbrowser_cache($link);
e3b42c5a
AD
97 _debug("Feedbrowser updated, $count feeds processed.");
98
99 purge_orphans($link, true);
dbaa4e4a 100
e3b42c5a
AD
101 $rc = cleanup_tags($link, 14, 50000);
102
103 _debug("Cleaned $rc cached tags.");
9e21a571 104 }
ef59e6e8 105
fecd57c8 106 }
fecd57c8 107
868650e4
AD
108 if ($op == "-cleanup-tags") {
109 $rc = cleanup_tags($link, 14, 50000);
110 print "$rc tags deleted.\n";
111 }
112
661135c7 113 db_close($link);
fecd57c8 114
661135c7
AD
115 unlink(LOCK_DIRECTORY . "/$lock_filename");
116?>