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