]> git.wh0rd.org - tt-rss.git/blob - update.php
add support for registering update.php commands; move rest of the self-updating stuff...
[tt-rss.git] / update.php
1 #!/usr/bin/env php
2 <?php
3 set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
4 get_include_path());
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 // Create a database connection.
21 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
22
23 init_connection($link);
24
25
26 $op = $argv;
27
28 if (count($argv) == 0 && !defined('STDIN')) {
29 ?> <html>
30 <head>
31 <title>Tiny Tiny RSS data update script.</title>
32 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
33 <link rel="stylesheet" type="text/css" href="utility.css">
34 </head>
35
36 <body>
37 <div class="floatingLogo"><img src="images/logo_wide.png"></div>
38 <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
39
40 <?php print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?>
41
42 </body></html>
43 <?php
44 exit;
45 }
46
47 if (count($argv) == 1 || in_array("-help", $op) ) {
48 print "Tiny Tiny RSS data update script.\n\n";
49 print "Options:\n";
50 print " -feeds - update feeds\n";
51 print " -feedbrowser - update feedbrowser\n";
52 print " -daemon - start single-process update daemon\n";
53 print " -cleanup-tags - perform tags table maintenance\n";
54 print " -get-feeds - receive popular feeds from linked instances\n";
55 print " -import USER FILE - import articles from XML\n";
56 print " -quiet - don't show messages\n";
57 print " -indexes - recreate missing schema indexes\n";
58 print " -convert-filters - convert type1 filters to type2\n";
59 print " -force-update - force update of all feeds\n";
60 print " -help - show this help\n";
61 print "Plugin options:\n";
62
63 foreach ($pluginhost->get_commands() as $command => $data) {
64 printf(" %-19s - %s\n", "$command", $data["description"]);
65 }
66
67 return;
68 }
69
70 define('QUIET', in_array("-quiet", $op));
71
72 if (!in_array("-daemon", $op)) {
73 $lock_filename = "update.lock";
74 } else {
75 $lock_filename = "update_daemon.lock";
76 }
77
78 $lock_handle = make_lockfile($lock_filename);
79 $must_exit = false;
80
81 // Try to lock a file in order to avoid concurrent update.
82 if (!$lock_handle) {
83 die("error: Can't create lockfile ($lock_filename). ".
84 "Maybe another update process is already running.\n");
85 }
86
87 if (in_array("-feeds", $op)) {
88 // Update all feeds needing a update.
89 update_daemon_common($link);
90
91 // Update feedbrowser
92 $count = update_feedbrowser_cache($link);
93 _debug("Feedbrowser updated, $count feeds processed.");
94
95 // Purge orphans and cleanup tags
96 purge_orphans($link, true);
97
98 $rc = cleanup_tags($link, 14, 50000);
99 _debug("Cleaned $rc cached tags.");
100
101 get_linked_feeds($link);
102 }
103
104 if (in_array("-feedbrowser", $op)) {
105 $count = update_feedbrowser_cache($link);
106 print "Finished, $count feeds processed.\n";
107 }
108
109 if (in_array("-daemon", $op)) {
110 $op = array_diff($op, array("-daemon"));
111 while (true) {
112 passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop");
113 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
114 sleep(DAEMON_SLEEP_INTERVAL);
115 }
116 }
117
118 if (in_array("-daemon-loop", $op)) {
119 if (!make_stampfile('update_daemon.stamp')) {
120 die("error: unable to create stampfile\n");
121 }
122
123 // Call to the feed batch update function
124 // or regenerate feedbrowser cache
125
126 if (rand(0,100) > 30) {
127 update_daemon_common($link);
128 } else {
129 $count = update_feedbrowser_cache($link);
130 _debug("Feedbrowser updated, $count feeds processed.");
131
132 purge_orphans($link, true);
133
134 $rc = cleanup_tags($link, 14, 50000);
135
136 _debug("Cleaned $rc cached tags.");
137
138 get_linked_feeds($link);
139 }
140
141 }
142
143 if (in_array("-cleanup-tags", $op)) {
144 $rc = cleanup_tags($link, 14, 50000);
145 _debug("$rc tags deleted.\n");
146 }
147
148 if (in_array("-get-feeds", $op)) {
149 get_linked_feeds($link);
150 }
151
152 if (in_array("-import",$op)) {
153 $username = $argv[count($argv) - 2];
154 $filename = $argv[count($argv) - 1];
155
156 if (!$username) {
157 print "error: please specify username.\n";
158 return;
159 }
160
161 if (!is_file($filename)) {
162 print "error: input filename ($filename) doesn't exist.\n";
163 return;
164 }
165
166 _debug("importing $filename for user $username...\n");
167
168 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
169
170 if (db_num_rows($result) == 0) {
171 print "error: could not find user $username.\n";
172 return;
173 }
174
175 $owner_uid = db_fetch_result($result, 0, "id");
176
177 perform_data_import($link, $filename, $owner_uid);
178
179 }
180
181 if (in_array("-indexes", $op)) {
182 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
183 _debug("Type 'yes' to continue.");
184
185 if (read_stdin() != 'yes')
186 exit;
187
188 _debug("clearing existing indexes...");
189
190 if (DB_TYPE == "pgsql") {
191 $result = db_query($link, "SELECT relname FROM
192 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
193 AND relname NOT LIKE '%_pkey'
194 AND relkind = 'i'");
195 } else {
196 $result = db_query($link, "SELECT index_name,table_name FROM
197 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
198 }
199
200 while ($line = db_fetch_assoc($result)) {
201 if (DB_TYPE == "pgsql") {
202 $statement = "DROP INDEX " . $line["relname"];
203 _debug($statement);
204 } else {
205 $statement = "ALTER TABLE ".
206 $line['table_name']." DROP INDEX ".$line['index_name'];
207 _debug($statement);
208 }
209 db_query($link, $statement, false);
210 }
211
212 _debug("reading indexes from schema for: " . DB_TYPE);
213
214 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
215 if ($fp) {
216 while ($line = fgets($fp)) {
217 $matches = array();
218
219 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
220 $index = $matches[1];
221 $table = $matches[2];
222
223 $statement = "CREATE INDEX $index ON $table";
224
225 _debug($statement);
226 db_query($link, $statement);
227 }
228 }
229 fclose($fp);
230 } else {
231 _debug("unable to open schema file.");
232 }
233 _debug("all done.");
234 }
235
236 if (in_array("-convert-filters", $op)) {
237 _debug("WARNING: this will remove all existing type2 filters.");
238 _debug("Type 'yes' to continue.");
239
240 if (read_stdin() != 'yes')
241 exit;
242
243 _debug("converting filters...");
244
245 db_query($link, "DELETE FROM ttrss_filters2");
246
247 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
248
249 while ($line = db_fetch_assoc($result)) {
250 $owner_uid = $line["owner_uid"];
251
252 // date filters are removed
253 if ($line["filter_type"] != 5) {
254 $filter = array();
255
256 if (sql_bool_to_bool($line["cat_filter"])) {
257 $feed_id = "CAT:" . (int)$line["cat_id"];
258 } else {
259 $feed_id = (int)$line["feed_id"];
260 }
261
262 $filter["enabled"] = $line["enabled"] ? "on" : "off";
263 $filter["rule"] = array(
264 json_encode(array(
265 "reg_exp" => $line["reg_exp"],
266 "feed_id" => $feed_id,
267 "filter_type" => $line["filter_type"])));
268
269 $filter["action"] = array(
270 json_encode(array(
271 "action_id" => $line["action_id"],
272 "action_param_label" => $line["action_param"],
273 "action_param" => $line["action_param"])));
274
275 // Oh god it's full of hacks
276
277 $_REQUEST = $filter;
278 $_SESSION["uid"] = $owner_uid;
279
280 $filters = new Pref_Filters($link, $_REQUEST);
281 $filters->add();
282 }
283 }
284
285 }
286
287 if (in_array("-force-update", $op)) {
288 _debug("marking all feeds as needing update...");
289
290 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
291 last_updated = '1970-01-01'");
292 }
293
294 $pluginhost->run_commands($op);
295
296 db_close($link);
297
298 if ($lock_handle != false) {
299 fclose($lock_handle);
300 }
301
302 if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
303 unlink(LOCK_DIRECTORY . "/$lock_filename");
304 ?>