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