]> git.wh0rd.org - tt-rss.git/blob - update.php
292b5c1be12a9c2b52961fce00b3ca80d8d4948b
[tt-rss.git] / update.php
1 #!/usr/bin/env php
2 <?php
3 set_include_path(get_include_path() . PATH_SEPARATOR .
4 dirname(__FILE__) . "/include");
5
6 define('DISABLE_SESSIONS', true);
7
8 chdir(dirname(__FILE__));
9
10 require_once "functions.php";
11 require_once "rssfuncs.php";
12 require_once "sanity_check.php";
13 require_once "config.php";
14 require_once "db.php";
15 require_once "db-prefs.php";
16
17 if (!defined('PHP_EXECUTABLE'))
18 define('PHP_EXECUTABLE', '/usr/bin/php');
19
20 $op = $argv;
21
22 if (count($argv) == 1 || in_array("-help", $op) ) {
23 print "Tiny Tiny RSS data update script.\n\n";
24 print "Options:\n";
25 print " -feeds - update feeds\n";
26 print " -feedbrowser - update feedbrowser\n";
27 print " -daemon - start single-process update daemon\n";
28 print " -cleanup-tags - perform tags table maintenance\n";
29 print " -get-feeds - receive popular feeds from linked instances\n";
30 print " -import USER FILE - import articles from XML\n";
31 print " -quiet - don't show messages\n";
32 print " -indexes - recreate missing schema indexes\n";
33 print " -help - show this help\n";
34 return;
35 }
36
37 define('QUIET', in_array("-quiet", $op));
38
39 if (!in_array("-daemon", $op)) {
40 $lock_filename = "update.lock";
41 } else {
42 $lock_filename = "update_daemon.lock";
43 }
44
45 $lock_handle = make_lockfile($lock_filename);
46 $must_exit = false;
47
48 // Try to lock a file in order to avoid concurrent update.
49 if (!$lock_handle) {
50 die("error: Can't create lockfile ($lock_filename). ".
51 "Maybe another update process is already running.\n");
52 }
53
54 // Create a database connection.
55 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
56
57 init_connection($link);
58
59 if (in_array("-feeds", $op)) {
60 // Update all feeds needing a update.
61 update_daemon_common($link);
62
63 // Update feedbrowser
64 $count = update_feedbrowser_cache($link);
65 _debug("Feedbrowser updated, $count feeds processed.");
66
67 // Purge orphans and cleanup tags
68 purge_orphans($link, true);
69
70 $rc = cleanup_tags($link, 14, 50000);
71 _debug("Cleaned $rc cached tags.");
72
73 get_linked_feeds($link);
74 }
75
76 if (in_array("-feedbrowser", $op)) {
77 $count = update_feedbrowser_cache($link);
78 print "Finished, $count feeds processed.\n";
79 }
80
81 if (in_array("-daemon", $op)) {
82 $op = array_diff($op, array("-daemon"));
83 while (true) {
84 passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop");
85 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
86 sleep(DAEMON_SLEEP_INTERVAL);
87 }
88 }
89
90 if (in_array("-daemon-loop", $op)) {
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 (in_array("-cleanup-tags", $op)) {
116 $rc = cleanup_tags($link, 14, 50000);
117 _debug("$rc tags deleted.\n");
118 }
119
120 if (in_array("-get-feeds", $op)) {
121 get_linked_feeds($link);
122 }
123
124 if (in_array("-import",$op)) {
125 $username = $argv[count($argv) - 2];
126 $filename = $argv[count($argv) - 1];
127
128 if (!$username) {
129 print "error: please specify username.\n";
130 return;
131 }
132
133 if (!is_file($filename)) {
134 print "error: input filename ($filename) doesn't exist.\n";
135 return;
136 }
137
138 _debug("importing $filename for user $username...\n");
139
140 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
141
142 if (db_num_rows($result) == 0) {
143 print "error: could not find user $username.\n";
144 return;
145 }
146
147 $owner_uid = db_fetch_result($result, 0, "id");
148
149 perform_data_import($link, $filename, $owner_uid);
150
151 }
152
153 if (in_array("-indexes", $op)) {
154 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
155 _debug("Type 'yes' to continue.");
156
157 if (read_stdin() != 'yes')
158 exit;
159
160 _debug("clearing existing indexes...");
161
162 if (DB_TYPE == "pgsql") {
163 $result = db_query($link, "SELECT relname FROM
164 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
165 AND relname NOT LIKE '%_pkey'
166 AND relkind = 'i'");
167 } else {
168 $result = db_query($link, "SELECT index_name,table_name FROM
169 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
170 }
171
172 while ($line = db_fetch_assoc($result)) {
173 if (DB_TYPE == "pgsql") {
174 $statement = "DROP INDEX " . $line["relname"];
175 _debug($statement);
176 } else {
177 $statement = "ALTER TABLE ".
178 $line['table_name']." DROP INDEX ".$line['index_name'];
179 _debug($statement);
180 }
181 db_query($link, $statement, false);
182 }
183
184 _debug("reading indexes from schema for: " . DB_TYPE);
185
186 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
187 if ($fp) {
188 while ($line = fgets($fp)) {
189 $matches = array();
190
191 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
192 $index = $matches[1];
193 $table = $matches[2];
194
195 $statement = "CREATE INDEX $index ON $table";
196
197 _debug($statement);
198 db_query($link, $statement);
199 }
200 }
201 fclose($fp);
202 } else {
203 _debug("unable to open schema file.");
204 }
205 _debug("all done.");
206 }
207
208 db_close($link);
209
210 if ($lock_handle != false) {
211 fclose($lock_handle);
212 }
213
214 unlink(LOCK_DIRECTORY . "/$lock_filename");
215 ?>