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