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