]> git.wh0rd.org - tt-rss.git/blame - update.php
another take on inclusion of errorhandler in CLI scripts
[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
fb074239 10 require_once "functions.php";
2c08214a 11 require_once "rssfuncs.php";
81596c66 12 require_once "config.php";
f02713bb 13 require_once "sanity_check.php";
81596c66 14 require_once "db.php";
661135c7 15 require_once "db-prefs.php";
661135c7 16
3de78afd
AD
17 if (!defined('PHP_EXECUTABLE'))
18 define('PHP_EXECUTABLE', '/usr/bin/php');
19
73f28fe9
AD
20 // Create a database connection.
21 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
22
23 init_connection($link);
24
764555ff
AD
25 $longopts = array("feeds",
26 "feedbrowser",
27 "daemon",
28 "daemon-loop",
29 "task:",
30 "cleanup-tags",
31 "quiet",
2191eb7a 32 "log:",
764555ff 33 "indexes",
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
0186be6a
AD
91 if (!isset($options['daemon'])) {
92 require_once "errorhandler.php";
93 }
94
857efe49
AD
95 if (!isset($options['update-schema'])) {
96 $schema_version = get_schema_version($link);
97
98 if ($schema_version != SCHEMA_VERSION) {
99 die("Schema version is wrong, please upgrade the database.\n");
100 }
101 }
102
dc24b520
AD
103 define('QUIET', isset($options['quiet']));
104
2191eb7a
AD
105 if (isset($options["log"])) {
106 _debug("Logging to " . $options["log"]);
107 define('LOGFILE', $options["log"]);
108 }
109
764555ff 110 if (!isset($options["daemon"])) {
661135c7
AD
111 $lock_filename = "update.lock";
112 } else {
113 $lock_filename = "update_daemon.lock";
114 }
fecd57c8 115
764555ff
AD
116 if (isset($options["task"])) {
117 _debug("Using task id " . $options["task"]);
118 $lock_filename = $lock_filename . "-task_" . $options["task"];
119 }
120
661135c7
AD
121 $lock_handle = make_lockfile($lock_filename);
122 $must_exit = false;
fecd57c8 123
661135c7
AD
124 // Try to lock a file in order to avoid concurrent update.
125 if (!$lock_handle) {
126 die("error: Can't create lockfile ($lock_filename). ".
127 "Maybe another update process is already running.\n");
128 }
fecd57c8 129
764555ff
AD
130 if (isset($options["force-update"])) {
131 _debug("marking all feeds as needing update...");
132
133 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
134 last_updated = '1970-01-01'");
135 }
136
137 if (isset($options["feeds"])) {
661135c7
AD
138 // Update all feeds needing a update.
139 update_daemon_common($link);
84e9a8c7
AD
140
141 // Update feedbrowser
142 $count = update_feedbrowser_cache($link);
143 _debug("Feedbrowser updated, $count feeds processed.");
144
145 // Purge orphans and cleanup tags
146 purge_orphans($link, true);
147
148 $rc = cleanup_tags($link, 14, 50000);
149 _debug("Cleaned $rc cached tags.");
f32eb194 150
41b82aa4
AD
151 global $pluginhost;
152 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
661135c7 153 }
fecd57c8 154
764555ff 155 if (isset($options["feedbrowser"])) {
661135c7
AD
156 $count = update_feedbrowser_cache($link);
157 print "Finished, $count feeds processed.\n";
fecd57c8 158 }
661135c7 159
764555ff 160 if (isset($options["daemon"])) {
661135c7 161 while (true) {
0ae9f746
AD
162 $quiet = (isset($options["quiet"])) ? "--quiet" : "";
163
164 passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet");
661135c7
AD
165 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
166 sleep(DAEMON_SLEEP_INTERVAL);
fecd57c8 167 }
81596c66 168 }
9e21a571 169
764555ff 170 if (isset($options["daemon-loop"])) {
661135c7 171 if (!make_stampfile('update_daemon.stamp')) {
e81610d9 172 _debug("warning: unable to create stampfile\n");
661135c7 173 }
9e21a571 174
dbaa4e4a 175 // Call to the feed batch update function
661135c7 176 // or regenerate feedbrowser cache
9e21a571 177
661135c7
AD
178 if (rand(0,100) > 30) {
179 update_daemon_common($link);
9e21a571 180 } else {
661135c7 181 $count = update_feedbrowser_cache($link);
e3b42c5a
AD
182 _debug("Feedbrowser updated, $count feeds processed.");
183
184 purge_orphans($link, true);
dbaa4e4a 185
e3b42c5a
AD
186 $rc = cleanup_tags($link, 14, 50000);
187
188 _debug("Cleaned $rc cached tags.");
f32eb194 189
41b82aa4
AD
190 global $pluginhost;
191 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
9e21a571 192 }
ef59e6e8 193
fecd57c8 194 }
fecd57c8 195
764555ff 196 if (isset($options["cleanup-tags"])) {
868650e4 197 $rc = cleanup_tags($link, 14, 50000);
5439d333 198 _debug("$rc tags deleted.\n");
868650e4
AD
199 }
200
764555ff 201 if (isset($options["indexes"])) {
871f0a7a
AD
202 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
203 _debug("Type 'yes' to continue.");
204
205 if (read_stdin() != 'yes')
206 exit;
207
208 _debug("clearing existing indexes...");
209
210 if (DB_TYPE == "pgsql") {
211 $result = db_query($link, "SELECT relname FROM
212 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
213 AND relname NOT LIKE '%_pkey'
214 AND relkind = 'i'");
215 } else {
216 $result = db_query($link, "SELECT index_name,table_name FROM
217 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
218 }
219
220 while ($line = db_fetch_assoc($result)) {
221 if (DB_TYPE == "pgsql") {
222 $statement = "DROP INDEX " . $line["relname"];
223 _debug($statement);
224 } else {
225 $statement = "ALTER TABLE ".
226 $line['table_name']." DROP INDEX ".$line['index_name'];
227 _debug($statement);
228 }
229 db_query($link, $statement, false);
230 }
231
232 _debug("reading indexes from schema for: " . DB_TYPE);
233
234 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
235 if ($fp) {
236 while ($line = fgets($fp)) {
237 $matches = array();
238
239 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
240 $index = $matches[1];
241 $table = $matches[2];
242
243 $statement = "CREATE INDEX $index ON $table";
244
245 _debug($statement);
246 db_query($link, $statement);
247 }
248 }
249 fclose($fp);
250 } else {
251 _debug("unable to open schema file.");
252 }
253 _debug("all done.");
254 }
255
764555ff 256 if (isset($options["convert-filters"])) {
6aff7845
AD
257 _debug("WARNING: this will remove all existing type2 filters.");
258 _debug("Type 'yes' to continue.");
259
260 if (read_stdin() != 'yes')
261 exit;
262
263 _debug("converting filters...");
264
265 db_query($link, "DELETE FROM ttrss_filters2");
266
267 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
268
269 while ($line = db_fetch_assoc($result)) {
270 $owner_uid = $line["owner_uid"];
271
37f78940
AD
272 // date filters are removed
273 if ($line["filter_type"] != 5) {
274 $filter = array();
275
276 if (sql_bool_to_bool($line["cat_filter"])) {
277 $feed_id = "CAT:" . (int)$line["cat_id"];
278 } else {
279 $feed_id = (int)$line["feed_id"];
280 }
6aff7845 281
37f78940
AD
282 $filter["enabled"] = $line["enabled"] ? "on" : "off";
283 $filter["rule"] = array(
284 json_encode(array(
285 "reg_exp" => $line["reg_exp"],
286 "feed_id" => $feed_id,
287 "filter_type" => $line["filter_type"])));
6aff7845 288
37f78940
AD
289 $filter["action"] = array(
290 json_encode(array(
291 "action_id" => $line["action_id"],
292 "action_param_label" => $line["action_param"],
293 "action_param" => $line["action_param"])));
6aff7845 294
37f78940 295 // Oh god it's full of hacks
5451903c 296
37f78940
AD
297 $_REQUEST = $filter;
298 $_SESSION["uid"] = $owner_uid;
6aff7845 299
37f78940
AD
300 $filters = new Pref_Filters($link, $_REQUEST);
301 $filters->add();
302 }
6aff7845
AD
303 }
304
305 }
306
b4c47f7e
AD
307 if (isset($options["update-schema"])) {
308 _debug("checking for updates (" . DB_TYPE . ")...");
309
310 $updater = new DbUpdater($link, DB_TYPE, SCHEMA_VERSION);
311
312 if ($updater->isUpdateRequired()) {
313 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION);
314 _debug("WARNING: please backup your database before continuing.");
315 _debug("Type 'yes' to continue.");
316
317 if (read_stdin() != 'yes')
318 exit;
319
320 for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
321 _debug("performing update up to version $i...");
322
323 $result = $updater->performUpdateTo($i);
324
325 _debug($result ? "OK!" : "FAILED!");
326
327 if (!$result) return;
328
329 }
330 } else {
331 _debug("update not required.");
332 }
333
334 }
335
764555ff 336 if (isset($options["list-plugins"])) {
7a866114 337 $tmppluginhost = new PluginHost($link);
d2a421e3 338 $tmppluginhost->load_all($tmppluginhost::KIND_ALL);
20b86c79
AD
339 $enabled = array_map("trim", explode(",", PLUGINS));
340
341 echo "List of all available plugins:\n";
342
7a866114 343 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
d2a421e3 344 $about = $plugin->about();
7a866114 345
20b86c79
AD
346 $status = $about[3] ? "system" : "user";
347
348 if (in_array($name, $enabled)) $name .= "*";
349
350 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
351 $name, $status, $about[0], $about[2], $about[1]);
7a866114 352 }
20b86c79
AD
353
354 echo "Plugins marked by * are currently enabled for all users.\n";
355
7a866114
AD
356 }
357
764555ff 358 $pluginhost->run_commands($options);
73f28fe9 359
661135c7 360 db_close($link);
107d0cf3 361