]> git.wh0rd.org - tt-rss.git/blame - update.php
filter dialog: remove placeholder
[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";
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
a77a4733
AD
20 $pdo = Db::pdo();
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",
df659891 37 "gen-search-idx",
764555ff 38 "list-plugins",
368bd7ea
AD
39 "debug-feed:",
40 "force-refetch",
41 "force-rehash",
17a8e61d 42 "decrypt-feeds",
764555ff
AD
43 "help");
44
1ffe3391 45 foreach (PluginHost::getInstance()->get_commands() as $command => $data) {
4cf0f9a9 46 array_push($longopts, $command . $data["suffix"]);
764555ff
AD
47 }
48
49 $options = getopt("", $longopts);
3de78afd 50
6f61ba46
AD
51 if (!is_array($options)) {
52 die("error: getopt() failed. ".
53 "Most probably you are using PHP CGI to run this script ".
54 "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ".
55 "additional information.\n");
56 }
57
764555ff 58 if (count($options) == 0 && !defined('STDIN')) {
366f06f7
AD
59 ?> <html>
60 <head>
3de78afd 61 <title>Tiny Tiny RSS data update script.</title>
366f06f7 62 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
366f06f7
AD
63 </head>
64
65 <body>
884d1650 66 <div class="floatingLogo"><img src="images/logo_small.png"></div>
3de78afd 67 <h1><?php echo __("Tiny Tiny RSS data update script.") ?></h1>
366f06f7 68
e6d66fe5 69 <?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
70
71 </body></html>
72 <?php
73 exit;
74 }
75
764555ff 76 if (count($options) == 0 || isset($options["help"]) ) {
661135c7
AD
77 print "Tiny Tiny RSS data update script.\n\n";
78 print "Options:\n";
764555ff
AD
79 print " --feeds - update feeds\n";
80 print " --feedbrowser - update feedbrowser\n";
81 print " --daemon - start single-process update daemon\n";
82 print " --task N - create lockfile using this task id\n";
83 print " --cleanup-tags - perform tags table maintenance\n";
dc24b520 84 print " --quiet - don't output messages to stdout\n";
2191eb7a 85 print " --log FILE - log messages to FILE\n";
764555ff 86 print " --indexes - recreate missing schema indexes\n";
b4c47f7e 87 print " --update-schema - update database schema\n";
df659891 88 print " --gen-search-idx - generate basic PostgreSQL fulltext search index\n";
764555ff
AD
89 print " --convert-filters - convert type1 filters to type2\n";
90 print " --force-update - force update of all feeds\n";
91 print " --list-plugins - list all available plugins\n";
368bd7ea
AD
92 print " --debug-feed N - perform debug update of feed N\n";
93 print " --force-refetch - debug update: force refetch feed data\n";
94 print " --force-rehash - debug update: force rehash articles\n";
17a8e61d 95 print " --decrypt-feeds - decrypt feed passwords\n";
764555ff 96 print " --help - show this help\n";
73f28fe9
AD
97 print "Plugin options:\n";
98
1ffe3391 99 foreach (PluginHost::getInstance()->get_commands() as $command => $data) {
4cf0f9a9
AD
100 $args = $data['arghelp'];
101 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
73f28fe9
AD
102 }
103
661135c7 104 return;
81596c66 105 }
87b9fb65 106
0186be6a
AD
107 if (!isset($options['daemon'])) {
108 require_once "errorhandler.php";
109 }
110
857efe49 111 if (!isset($options['update-schema'])) {
6322ac79 112 $schema_version = get_schema_version();
857efe49
AD
113
114 if ($schema_version != SCHEMA_VERSION) {
115 die("Schema version is wrong, please upgrade the database.\n");
116 }
117 }
118
dc24b520
AD
119 define('QUIET', isset($options['quiet']));
120
2191eb7a
AD
121 if (isset($options["log"])) {
122 _debug("Logging to " . $options["log"]);
123 define('LOGFILE', $options["log"]);
124 }
125
764555ff 126 if (!isset($options["daemon"])) {
661135c7
AD
127 $lock_filename = "update.lock";
128 } else {
129 $lock_filename = "update_daemon.lock";
130 }
fecd57c8 131
764555ff
AD
132 if (isset($options["task"])) {
133 _debug("Using task id " . $options["task"]);
134 $lock_filename = $lock_filename . "-task_" . $options["task"];
135 }
136
7440a7fe
AD
137 if (isset($options["pidlock"])) {
138 $my_pid = $options["pidlock"];
139 $lock_filename = "update_daemon-$my_pid.lock";
140
141 }
142
143 _debug("Lock: $lock_filename");
144
661135c7
AD
145 $lock_handle = make_lockfile($lock_filename);
146 $must_exit = false;
fecd57c8 147
8a386529 148 if (isset($options["task"]) && isset($options["pidlock"])) {
7440a7fe
AD
149 $waits = $options["task"] * 5;
150 _debug("Waiting before update ($waits)");
151 sleep($waits);
152 }
153
661135c7
AD
154 // Try to lock a file in order to avoid concurrent update.
155 if (!$lock_handle) {
156 die("error: Can't create lockfile ($lock_filename). ".
157 "Maybe another update process is already running.\n");
158 }
fecd57c8 159
764555ff
AD
160 if (isset($options["force-update"])) {
161 _debug("marking all feeds as needing update...");
162
a77a4733
AD
163 $pdo->query( "UPDATE ttrss_feeds SET
164 last_update_started = '1970-01-01', last_updated = '1970-01-01'");
764555ff
AD
165 }
166
167 if (isset($options["feeds"])) {
e6c886bf
AD
168 RSSUtils::update_daemon_common();
169 RSSUtils::housekeeping_common(true);
f32eb194 170
1ffe3391 171 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op);
661135c7 172 }
fecd57c8 173
764555ff 174 if (isset($options["feedbrowser"])) {
e6c886bf 175 $count = RSSUtils::update_feedbrowser_cache();
661135c7 176 print "Finished, $count feeds processed.\n";
fecd57c8 177 }
661135c7 178
764555ff 179 if (isset($options["daemon"])) {
661135c7 180 while (true) {
0ae9f746 181 $quiet = (isset($options["quiet"])) ? "--quiet" : "";
0f0bbe73 182 $log = isset($options['log']) ? '--log '.$options['log'] : '';
0ae9f746 183
0f0bbe73 184 passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet $log");
4fd07908
AD
185
186 // let's enforce a minimum spawn interval as to not forkbomb the host
187 $spawn_interval = max(60, DAEMON_SLEEP_INTERVAL);
188
189 _debug("Sleeping for $spawn_interval seconds...");
190 sleep($spawn_interval);
fecd57c8 191 }
81596c66 192 }
9e21a571 193
764555ff 194 if (isset($options["daemon-loop"])) {
661135c7 195 if (!make_stampfile('update_daemon.stamp')) {
e81610d9 196 _debug("warning: unable to create stampfile\n");
661135c7 197 }
9e21a571 198
e6c886bf 199 RSSUtils::update_daemon_common(isset($options["pidlock"]) ? 50 : DAEMON_FEED_LIMIT);
830f6f08
AD
200
201 if (!isset($options["pidlock"]) || $options["task"] == 0)
e6c886bf 202 RSSUtils::housekeeping_common(true);
dbaa4e4a 203
fce451a4 204 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op);
fecd57c8 205 }
fecd57c8 206
764555ff 207 if (isset($options["cleanup-tags"])) {
6322ac79 208 $rc = cleanup_tags( 14, 50000);
5439d333 209 _debug("$rc tags deleted.\n");
868650e4
AD
210 }
211
764555ff 212 if (isset($options["indexes"])) {
871f0a7a
AD
213 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
214 _debug("Type 'yes' to continue.");
215
216 if (read_stdin() != 'yes')
217 exit;
218
219 _debug("clearing existing indexes...");
220
221 if (DB_TYPE == "pgsql") {
a77a4733 222 $sth = $pdo->query( "SELECT relname FROM
871f0a7a
AD
223 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
224 AND relname NOT LIKE '%_pkey'
225 AND relkind = 'i'");
226 } else {
a77a4733 227 $sth = $pdo->query( "SELECT index_name,table_name FROM
871f0a7a
AD
228 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
229 }
230
a77a4733 231 while ($line = $sth->fetch()) {
871f0a7a
AD
232 if (DB_TYPE == "pgsql") {
233 $statement = "DROP INDEX " . $line["relname"];
234 _debug($statement);
235 } else {
236 $statement = "ALTER TABLE ".
237 $line['table_name']." DROP INDEX ".$line['index_name'];
238 _debug($statement);
239 }
a77a4733 240 $pdo->query($statement);
871f0a7a
AD
241 }
242
243 _debug("reading indexes from schema for: " . DB_TYPE);
244
245 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
246 if ($fp) {
247 while ($line = fgets($fp)) {
248 $matches = array();
249
250 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
251 $index = $matches[1];
252 $table = $matches[2];
253
254 $statement = "CREATE INDEX $index ON $table";
255
256 _debug($statement);
a77a4733 257 $pdo->query($statement);
871f0a7a
AD
258 }
259 }
260 fclose($fp);
261 } else {
262 _debug("unable to open schema file.");
263 }
264 _debug("all done.");
265 }
266
764555ff 267 if (isset($options["convert-filters"])) {
6aff7845
AD
268 _debug("WARNING: this will remove all existing type2 filters.");
269 _debug("Type 'yes' to continue.");
270
271 if (read_stdin() != 'yes')
272 exit;
273
274 _debug("converting filters...");
275
a77a4733 276 $pdo->query("DELETE FROM ttrss_filters2");
6aff7845 277
a77a4733 278 $res = $pdo->query("SELECT * FROM ttrss_filters ORDER BY id");
6aff7845 279
a77a4733 280 while ($line = $res->fetch()) {
6aff7845
AD
281 $owner_uid = $line["owner_uid"];
282
37f78940
AD
283 // date filters are removed
284 if ($line["filter_type"] != 5) {
285 $filter = array();
286
287 if (sql_bool_to_bool($line["cat_filter"])) {
288 $feed_id = "CAT:" . (int)$line["cat_id"];
289 } else {
290 $feed_id = (int)$line["feed_id"];
291 }
6aff7845 292
37f78940
AD
293 $filter["enabled"] = $line["enabled"] ? "on" : "off";
294 $filter["rule"] = array(
295 json_encode(array(
296 "reg_exp" => $line["reg_exp"],
297 "feed_id" => $feed_id,
298 "filter_type" => $line["filter_type"])));
6aff7845 299
37f78940
AD
300 $filter["action"] = array(
301 json_encode(array(
302 "action_id" => $line["action_id"],
303 "action_param_label" => $line["action_param"],
304 "action_param" => $line["action_param"])));
6aff7845 305
37f78940 306 // Oh god it's full of hacks
5451903c 307
37f78940
AD
308 $_REQUEST = $filter;
309 $_SESSION["uid"] = $owner_uid;
6aff7845 310
1f294435 311 $filters = new Pref_Filters($_REQUEST);
37f78940
AD
312 $filters->add();
313 }
6aff7845
AD
314 }
315
316 }
317
b4c47f7e
AD
318 if (isset($options["update-schema"])) {
319 _debug("checking for updates (" . DB_TYPE . ")...");
320
1d92297a 321 $updater = new DbUpdater(Db::pdo(), DB_TYPE, SCHEMA_VERSION);
b4c47f7e
AD
322
323 if ($updater->isUpdateRequired()) {
324 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION);
325 _debug("WARNING: please backup your database before continuing.");
326 _debug("Type 'yes' to continue.");
327
328 if (read_stdin() != 'yes')
329 exit;
330
331 for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
332 _debug("performing update up to version $i...");
333
977cea14 334 $result = $updater->performUpdateTo($i, false);
b4c47f7e
AD
335
336 _debug($result ? "OK!" : "FAILED!");
337
338 if (!$result) return;
339
340 }
341 } else {
342 _debug("update not required.");
343 }
344
345 }
346
df659891
AD
347 if (isset($options["gen-search-idx"])) {
348 echo "Generating search index (stemming set to English)...\n";
e854442e 349
a77a4733
AD
350 $res = $pdo->query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
351 $row = $res->fetch();
352 $count = $row['count'];
e854442e 353
73ef6f04 354 print "Articles to process: $count.\n";
e854442e 355
73ef6f04
AD
356 $limit = 500;
357 $processed = 0;
e854442e 358
a77a4733
AD
359 $sth = $pdo->prepare("SELECT id, title, content FROM ttrss_entries WHERE
360 tsvector_combined IS NULL ORDER BY id LIMIT ?");
361 $sth->execute([$limit]);
362
363 $usth = $pdo->prepare("UPDATE ttrss_entries
364 SET tsvector_combined = to_tsvector('english', ?) WHERE id = ?");
365
e854442e 366 while (true) {
e854442e 367
a77a4733 368 while ($line = $sth->fetch()) {
49a888ec
AD
369 $tsvector_combined = mb_substr($line['title'] . ' ' .
370 preg_replace('/[<\?\:]/', ' ', strip_tags($line['content'])),
a77a4733 371 0, 1000000);
e854442e 372
a77a4733
AD
373 $usth->execute([$tsvector_combined, $line['id']]);
374
375 $processed++;
ecbb3292 376 }
e854442e 377
ecbb3292 378 print "Processed $processed articles...\n";
73ef6f04 379
a77a4733 380 if ($processed < $limit) {
e854442e
AD
381 echo "All done.\n";
382 break;
383 }
e854442e 384 }
e854442e
AD
385 }
386
764555ff 387 if (isset($options["list-plugins"])) {
6f7798b6 388 $tmppluginhost = new PluginHost();
583f163f 389 $tmppluginhost->load_all($tmppluginhost::KIND_ALL, false);
20b86c79
AD
390 $enabled = array_map("trim", explode(",", PLUGINS));
391
392 echo "List of all available plugins:\n";
393
7a866114 394 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
d2a421e3 395 $about = $plugin->about();
7a866114 396
20b86c79
AD
397 $status = $about[3] ? "system" : "user";
398
399 if (in_array($name, $enabled)) $name .= "*";
400
401 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
402 $name, $status, $about[0], $about[2], $about[1]);
7a866114 403 }
20b86c79
AD
404
405 echo "Plugins marked by * are currently enabled for all users.\n";
406
7a866114
AD
407 }
408
368bd7ea
AD
409 if (isset($options["debug-feed"])) {
410 $feed = $options["debug-feed"];
411
412 if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
413 if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
414
415 $_REQUEST['xdebug'] = 1;
416
e6c886bf 417 $rc = RSSUtils::update_rss_feed($feed) != false ? 0 : 1;
3c111597
AD
418
419 exit($rc);
368bd7ea
AD
420 }
421
17a8e61d 422 if (isset($options["decrypt-feeds"])) {
17a8e61d
AD
423
424 if (!function_exists("mcrypt_decrypt")) {
425 _debug("mcrypt functions not available.");
426 return;
427 }
428
a77a4733
AD
429 $res = $pdo->query("SELECT id, auth_pass FROM ttrss_feeds WHERE auth_pass_encrypted = true");
430
17a8e61d
AD
431 require_once "crypt.php";
432
433 $total = 0;
434
a77a4733
AD
435 $pdo->beginTransaction();
436
437 $usth = $pdo->prepare("UPDATE ttrss_feeds SET auth_pass_encrypted = false, auth_pass = ?
438 WHERE id = ?");
17a8e61d 439
a77a4733 440 while ($line = $res->fetch()) {
17a8e61d
AD
441 _debug("processing feed id " . $line["id"]);
442
a77a4733 443 $auth_pass = decrypt_string($line["auth_pass"]);
17a8e61d 444
a77a4733 445 $usth->execute([$auth_pass, $line['id']]);
17a8e61d
AD
446
447 ++$total;
448 }
449
a77a4733 450 $pdo->commit();
17a8e61d
AD
451
452 _debug("$total feeds processed.");
453 }
454
1ffe3391 455 PluginHost::getInstance()->run_commands($options);
73f28fe9 456
9973b13e 457 if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
458 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
459 fclose($lock_handle);
0f906745 460 unlink(LOCK_DIRECTORY . "/$lock_filename");
9bfda43e 461?>