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