]> git.wh0rd.org - tt-rss.git/blame - update.php
daemon2: use passthrough to daemon-loop
[tt-rss.git] / update.php
CommitLineData
ece78711 1#!/usr/bin/env php
fecd57c8 2<?php
88e8fb3a
AD
3 set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
4 get_include_path());
107d0cf3 5
661135c7
AD
6 define('DISABLE_SESSIONS', true);
7
9b27cec8
AD
8 chdir(dirname(__FILE__));
9
404e2e36 10 require_once "autoload.php";
fb074239 11 require_once "functions.php";
2c08214a 12 require_once "rssfuncs.php";
81596c66 13 require_once "config.php";
f02713bb 14 require_once "sanity_check.php";
81596c66 15 require_once "db.php";
661135c7 16 require_once "db-prefs.php";
2a649c44 17 require_once "errorhandler.php";
661135c7 18
3de78afd
AD
19 if (!defined('PHP_EXECUTABLE'))
20 define('PHP_EXECUTABLE', '/usr/bin/php');
21
6322ac79 22 init_plugins();
73f28fe9 23
764555ff
AD
24 $longopts = array("feeds",
25 "feedbrowser",
26 "daemon",
27 "daemon-loop",
28 "task:",
29 "cleanup-tags",
30 "quiet",
2191eb7a 31 "log:",
764555ff 32 "indexes",
7440a7fe 33 "pidlock:",
b4c47f7e 34 "update-schema",
764555ff
AD
35 "convert-filters",
36 "force-update",
37 "list-plugins",
38 "help");
39
40 foreach ($pluginhost->get_commands() as $command => $data) {
4cf0f9a9 41 array_push($longopts, $command . $data["suffix"]);
764555ff
AD
42 }
43
44 $options = getopt("", $longopts);
3de78afd 45
764555ff 46 if (count($options) == 0 && !defined('STDIN')) {
366f06f7
AD
47 ?> <html>
48 <head>
3de78afd 49 <title>Tiny Tiny RSS data update script.</title>
366f06f7
AD
50 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
51 <link rel="stylesheet" type="text/css" href="utility.css">
52 </head>
53
54 <body>
884d1650 55 <div class="floatingLogo"><img src="images/logo_small.png"></div>
3de78afd 56 <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
366f06f7 57
3de78afd 58 <?php print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?>
366f06f7
AD
59
60 </body></html>
61 <?php
62 exit;
63 }
64
764555ff 65 if (count($options) == 0 || isset($options["help"]) ) {
661135c7
AD
66 print "Tiny Tiny RSS data update script.\n\n";
67 print "Options:\n";
764555ff
AD
68 print " --feeds - update feeds\n";
69 print " --feedbrowser - update feedbrowser\n";
70 print " --daemon - start single-process update daemon\n";
71 print " --task N - create lockfile using this task id\n";
72 print " --cleanup-tags - perform tags table maintenance\n";
dc24b520 73 print " --quiet - don't output messages to stdout\n";
2191eb7a 74 print " --log FILE - log messages to FILE\n";
764555ff 75 print " --indexes - recreate missing schema indexes\n";
b4c47f7e 76 print " --update-schema - update database schema\n";
764555ff
AD
77 print " --convert-filters - convert type1 filters to type2\n";
78 print " --force-update - force update of all feeds\n";
79 print " --list-plugins - list all available plugins\n";
80 print " --help - show this help\n";
73f28fe9
AD
81 print "Plugin options:\n";
82
83 foreach ($pluginhost->get_commands() as $command => $data) {
4cf0f9a9
AD
84 $args = $data['arghelp'];
85 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
73f28fe9
AD
86 }
87
661135c7 88 return;
81596c66 89 }
87b9fb65 90
857efe49 91 if (!isset($options['update-schema'])) {
6322ac79 92 $schema_version = get_schema_version();
857efe49
AD
93
94 if ($schema_version != SCHEMA_VERSION) {
95 die("Schema version is wrong, please upgrade the database.\n");
96 }
97 }
98
dc24b520
AD
99 define('QUIET', isset($options['quiet']));
100
2191eb7a
AD
101 if (isset($options["log"])) {
102 _debug("Logging to " . $options["log"]);
103 define('LOGFILE', $options["log"]);
104 }
105
764555ff 106 if (!isset($options["daemon"])) {
661135c7
AD
107 $lock_filename = "update.lock";
108 } else {
109 $lock_filename = "update_daemon.lock";
110 }
fecd57c8 111
764555ff
AD
112 if (isset($options["task"])) {
113 _debug("Using task id " . $options["task"]);
114 $lock_filename = $lock_filename . "-task_" . $options["task"];
115 }
116
7440a7fe
AD
117 if (isset($options["pidlock"])) {
118 $my_pid = $options["pidlock"];
119 $lock_filename = "update_daemon-$my_pid.lock";
120
121 }
122
123 _debug("Lock: $lock_filename");
124
661135c7
AD
125 $lock_handle = make_lockfile($lock_filename);
126 $must_exit = false;
fecd57c8 127
7440a7fe
AD
128 if (isset($options["task"])) {
129 $waits = $options["task"] * 5;
130 _debug("Waiting before update ($waits)");
131 sleep($waits);
132 }
133
661135c7
AD
134 // Try to lock a file in order to avoid concurrent update.
135 if (!$lock_handle) {
136 die("error: Can't create lockfile ($lock_filename). ".
137 "Maybe another update process is already running.\n");
138 }
fecd57c8 139
764555ff
AD
140 if (isset($options["force-update"])) {
141 _debug("marking all feeds as needing update...");
142
6322ac79 143 db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
764555ff
AD
144 last_updated = '1970-01-01'");
145 }
146
147 if (isset($options["feeds"])) {
661135c7 148 // Update all feeds needing a update.
6322ac79 149 update_daemon_common();
84e9a8c7
AD
150
151 // Update feedbrowser
6322ac79 152 $count = update_feedbrowser_cache();
84e9a8c7
AD
153 _debug("Feedbrowser updated, $count feeds processed.");
154
155 // Purge orphans and cleanup tags
6322ac79 156 purge_orphans( true);
84e9a8c7 157
6322ac79 158 $rc = cleanup_tags( 14, 50000);
84e9a8c7 159 _debug("Cleaned $rc cached tags.");
f32eb194 160
41b82aa4
AD
161 global $pluginhost;
162 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
661135c7 163 }
fecd57c8 164
764555ff 165 if (isset($options["feedbrowser"])) {
6322ac79 166 $count = update_feedbrowser_cache();
661135c7 167 print "Finished, $count feeds processed.\n";
fecd57c8 168 }
661135c7 169
764555ff 170 if (isset($options["daemon"])) {
661135c7 171 while (true) {
0ae9f746
AD
172 $quiet = (isset($options["quiet"])) ? "--quiet" : "";
173
174 passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet");
661135c7
AD
175 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
176 sleep(DAEMON_SLEEP_INTERVAL);
fecd57c8 177 }
81596c66 178 }
9e21a571 179
764555ff 180 if (isset($options["daemon-loop"])) {
661135c7 181 if (!make_stampfile('update_daemon.stamp')) {
e81610d9 182 _debug("warning: unable to create stampfile\n");
661135c7 183 }
9e21a571 184
dbaa4e4a 185 // Call to the feed batch update function
661135c7 186 // or regenerate feedbrowser cache
9e21a571 187
661135c7 188 if (rand(0,100) > 30) {
6322ac79 189 update_daemon_common();
9e21a571 190 } else {
6322ac79 191 $count = update_feedbrowser_cache();
e3b42c5a
AD
192 _debug("Feedbrowser updated, $count feeds processed.");
193
6322ac79 194 purge_orphans( true);
dbaa4e4a 195
6322ac79 196 $rc = cleanup_tags( 14, 50000);
e3b42c5a
AD
197
198 _debug("Cleaned $rc cached tags.");
f32eb194 199
41b82aa4
AD
200 global $pluginhost;
201 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
9e21a571 202 }
ef59e6e8 203
fecd57c8 204 }
fecd57c8 205
764555ff 206 if (isset($options["cleanup-tags"])) {
6322ac79 207 $rc = cleanup_tags( 14, 50000);
5439d333 208 _debug("$rc tags deleted.\n");
868650e4
AD
209 }
210
764555ff 211 if (isset($options["indexes"])) {
871f0a7a
AD
212 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
213 _debug("Type 'yes' to continue.");
214
215 if (read_stdin() != 'yes')
216 exit;
217
218 _debug("clearing existing indexes...");
219
220 if (DB_TYPE == "pgsql") {
6322ac79 221 $result = db_query( "SELECT relname FROM
871f0a7a
AD
222 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
223 AND relname NOT LIKE '%_pkey'
224 AND relkind = 'i'");
225 } else {
6322ac79 226 $result = db_query( "SELECT index_name,table_name FROM
871f0a7a
AD
227 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
228 }
229
230 while ($line = db_fetch_assoc($result)) {
231 if (DB_TYPE == "pgsql") {
232 $statement = "DROP INDEX " . $line["relname"];
233 _debug($statement);
234 } else {
235 $statement = "ALTER TABLE ".
236 $line['table_name']." DROP INDEX ".$line['index_name'];
237 _debug($statement);
238 }
6322ac79 239 db_query( $statement, false);
871f0a7a
AD
240 }
241
242 _debug("reading indexes from schema for: " . DB_TYPE);
243
244 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
245 if ($fp) {
246 while ($line = fgets($fp)) {
247 $matches = array();
248
249 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
250 $index = $matches[1];
251 $table = $matches[2];
252
253 $statement = "CREATE INDEX $index ON $table";
254
255 _debug($statement);
6322ac79 256 db_query( $statement);
871f0a7a
AD
257 }
258 }
259 fclose($fp);
260 } else {
261 _debug("unable to open schema file.");
262 }
263 _debug("all done.");
264 }
265
764555ff 266 if (isset($options["convert-filters"])) {
6aff7845
AD
267 _debug("WARNING: this will remove all existing type2 filters.");
268 _debug("Type 'yes' to continue.");
269
270 if (read_stdin() != 'yes')
271 exit;
272
273 _debug("converting filters...");
274
6322ac79 275 db_query( "DELETE FROM ttrss_filters2");
6aff7845 276
6322ac79 277 $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id");
6aff7845
AD
278
279 while ($line = db_fetch_assoc($result)) {
280 $owner_uid = $line["owner_uid"];
281
37f78940
AD
282 // date filters are removed
283 if ($line["filter_type"] != 5) {
284 $filter = array();
285
286 if (sql_bool_to_bool($line["cat_filter"])) {
287 $feed_id = "CAT:" . (int)$line["cat_id"];
288 } else {
289 $feed_id = (int)$line["feed_id"];
290 }
6aff7845 291
37f78940
AD
292 $filter["enabled"] = $line["enabled"] ? "on" : "off";
293 $filter["rule"] = array(
294 json_encode(array(
295 "reg_exp" => $line["reg_exp"],
296 "feed_id" => $feed_id,
297 "filter_type" => $line["filter_type"])));
6aff7845 298
37f78940
AD
299 $filter["action"] = array(
300 json_encode(array(
301 "action_id" => $line["action_id"],
302 "action_param_label" => $line["action_param"],
303 "action_param" => $line["action_param"])));
6aff7845 304
37f78940 305 // Oh god it's full of hacks
5451903c 306
37f78940
AD
307 $_REQUEST = $filter;
308 $_SESSION["uid"] = $owner_uid;
6aff7845 309
6322ac79 310 $filters = new Pref_Filters( $_REQUEST);
37f78940
AD
311 $filters->add();
312 }
6aff7845
AD
313 }
314
315 }
316
b4c47f7e
AD
317 if (isset($options["update-schema"])) {
318 _debug("checking for updates (" . DB_TYPE . ")...");
319
6322ac79 320 $updater = new DbUpdater( DB_TYPE, SCHEMA_VERSION);
b4c47f7e
AD
321
322 if ($updater->isUpdateRequired()) {
323 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION);
324 _debug("WARNING: please backup your database before continuing.");
325 _debug("Type 'yes' to continue.");
326
327 if (read_stdin() != 'yes')
328 exit;
329
330 for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
331 _debug("performing update up to version $i...");
332
333 $result = $updater->performUpdateTo($i);
334
335 _debug($result ? "OK!" : "FAILED!");
336
337 if (!$result) return;
338
339 }
340 } else {
341 _debug("update not required.");
342 }
343
344 }
345
764555ff 346 if (isset($options["list-plugins"])) {
6322ac79 347 $tmppluginhost = new PluginHost();
d2a421e3 348 $tmppluginhost->load_all($tmppluginhost::KIND_ALL);
20b86c79
AD
349 $enabled = array_map("trim", explode(",", PLUGINS));
350
351 echo "List of all available plugins:\n";
352
7a866114 353 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
d2a421e3 354 $about = $plugin->about();
7a866114 355
20b86c79
AD
356 $status = $about[3] ? "system" : "user";
357
358 if (in_array($name, $enabled)) $name .= "*";
359
360 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
361 $name, $status, $about[0], $about[2], $about[1]);
7a866114 362 }
20b86c79
AD
363
364 echo "Plugins marked by * are currently enabled for all users.\n";
365
7a866114
AD
366 }
367
764555ff 368 $pluginhost->run_commands($options);
73f28fe9 369