]> git.wh0rd.org - tt-rss.git/blame - update.php
move counter stuff to a separate 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";
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 37 "list-plugins",
368bd7ea
AD
38 "debug-feed:",
39 "force-refetch",
40 "force-rehash",
17a8e61d 41 "decrypt-feeds",
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">
5bbc4bb4 62 <link rel="stylesheet" type="text/css" href="css/utility.css">
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
6322ac79 163 db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
764555ff
AD
164 last_updated = '1970-01-01'");
165 }
166
167 if (isset($options["feeds"])) {
6322ac79 168 update_daemon_common();
e2cf81e2 169 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"])) {
6322ac79 175 $count = 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");
661135c7
AD
185 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
186 sleep(DAEMON_SLEEP_INTERVAL);
fecd57c8 187 }
81596c66 188 }
9e21a571 189
764555ff 190 if (isset($options["daemon-loop"])) {
661135c7 191 if (!make_stampfile('update_daemon.stamp')) {
e81610d9 192 _debug("warning: unable to create stampfile\n");
661135c7 193 }
9e21a571 194
8cabc200 195 update_daemon_common(isset($options["pidlock"]) ? 50 : DAEMON_FEED_LIMIT);
830f6f08
AD
196
197 if (!isset($options["pidlock"]) || $options["task"] == 0)
198 housekeeping_common(true);
dbaa4e4a 199
fce451a4 200 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op);
fecd57c8 201 }
fecd57c8 202
764555ff 203 if (isset($options["cleanup-tags"])) {
6322ac79 204 $rc = cleanup_tags( 14, 50000);
5439d333 205 _debug("$rc tags deleted.\n");
868650e4
AD
206 }
207
764555ff 208 if (isset($options["indexes"])) {
871f0a7a
AD
209 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
210 _debug("Type 'yes' to continue.");
211
212 if (read_stdin() != 'yes')
213 exit;
214
215 _debug("clearing existing indexes...");
216
217 if (DB_TYPE == "pgsql") {
6322ac79 218 $result = db_query( "SELECT relname FROM
871f0a7a
AD
219 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
220 AND relname NOT LIKE '%_pkey'
221 AND relkind = 'i'");
222 } else {
6322ac79 223 $result = db_query( "SELECT index_name,table_name FROM
871f0a7a
AD
224 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
225 }
226
227 while ($line = db_fetch_assoc($result)) {
228 if (DB_TYPE == "pgsql") {
229 $statement = "DROP INDEX " . $line["relname"];
230 _debug($statement);
231 } else {
232 $statement = "ALTER TABLE ".
233 $line['table_name']." DROP INDEX ".$line['index_name'];
234 _debug($statement);
235 }
6322ac79 236 db_query( $statement, false);
871f0a7a
AD
237 }
238
239 _debug("reading indexes from schema for: " . DB_TYPE);
240
241 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
242 if ($fp) {
243 while ($line = fgets($fp)) {
244 $matches = array();
245
246 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
247 $index = $matches[1];
248 $table = $matches[2];
249
250 $statement = "CREATE INDEX $index ON $table";
251
252 _debug($statement);
6322ac79 253 db_query( $statement);
871f0a7a
AD
254 }
255 }
256 fclose($fp);
257 } else {
258 _debug("unable to open schema file.");
259 }
260 _debug("all done.");
261 }
262
764555ff 263 if (isset($options["convert-filters"])) {
6aff7845
AD
264 _debug("WARNING: this will remove all existing type2 filters.");
265 _debug("Type 'yes' to continue.");
266
267 if (read_stdin() != 'yes')
268 exit;
269
270 _debug("converting filters...");
271
6322ac79 272 db_query( "DELETE FROM ttrss_filters2");
6aff7845 273
6322ac79 274 $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id");
6aff7845
AD
275
276 while ($line = db_fetch_assoc($result)) {
277 $owner_uid = $line["owner_uid"];
278
37f78940
AD
279 // date filters are removed
280 if ($line["filter_type"] != 5) {
281 $filter = array();
282
283 if (sql_bool_to_bool($line["cat_filter"])) {
284 $feed_id = "CAT:" . (int)$line["cat_id"];
285 } else {
286 $feed_id = (int)$line["feed_id"];
287 }
6aff7845 288
37f78940
AD
289 $filter["enabled"] = $line["enabled"] ? "on" : "off";
290 $filter["rule"] = array(
291 json_encode(array(
292 "reg_exp" => $line["reg_exp"],
293 "feed_id" => $feed_id,
294 "filter_type" => $line["filter_type"])));
6aff7845 295
37f78940
AD
296 $filter["action"] = array(
297 json_encode(array(
298 "action_id" => $line["action_id"],
299 "action_param_label" => $line["action_param"],
300 "action_param" => $line["action_param"])));
6aff7845 301
37f78940 302 // Oh god it's full of hacks
5451903c 303
37f78940
AD
304 $_REQUEST = $filter;
305 $_SESSION["uid"] = $owner_uid;
6aff7845 306
1f294435 307 $filters = new Pref_Filters($_REQUEST);
37f78940
AD
308 $filters->add();
309 }
6aff7845
AD
310 }
311
312 }
313
b4c47f7e
AD
314 if (isset($options["update-schema"])) {
315 _debug("checking for updates (" . DB_TYPE . ")...");
316
0630a100 317 $updater = new DbUpdater(Db::get(), DB_TYPE, SCHEMA_VERSION);
b4c47f7e
AD
318
319 if ($updater->isUpdateRequired()) {
320 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION);
321 _debug("WARNING: please backup your database before continuing.");
322 _debug("Type 'yes' to continue.");
323
324 if (read_stdin() != 'yes')
325 exit;
326
327 for ($i = $updater->getSchemaVersion() + 1; $i <= SCHEMA_VERSION; $i++) {
328 _debug("performing update up to version $i...");
329
977cea14 330 $result = $updater->performUpdateTo($i, false);
b4c47f7e
AD
331
332 _debug($result ? "OK!" : "FAILED!");
333
334 if (!$result) return;
335
336 }
337 } else {
338 _debug("update not required.");
339 }
340
341 }
342
df659891
AD
343 if (isset($options["gen-search-idx"])) {
344 echo "Generating search index (stemming set to English)...\n";
e854442e 345
410c0ce6 346 $result = db_query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
e854442e
AD
347 $count = db_fetch_result($result, 0, "count");
348
73ef6f04 349 print "Articles to process: $count.\n";
e854442e 350
73ef6f04
AD
351 $limit = 500;
352 $processed = 0;
e854442e
AD
353
354 while (true) {
73ef6f04 355 $result = db_query("SELECT id, title, content FROM ttrss_entries WHERE tsvector_combined IS NULL ORDER BY id LIMIT $limit");
e854442e 356
ecbb3292 357 while ($line = db_fetch_assoc($result)) {
978989bb 358 $tsvector_combined = db_escape_string(mb_substr($line['title'] . ' ' . strip_tags(str_replace('<', ' <', $line['content'])),
ecbb3292 359 0, 1000000));
e854442e 360
ecbb3292
AD
361 db_query("UPDATE ttrss_entries SET tsvector_combined = to_tsvector('english', '$tsvector_combined') WHERE id = " . $line["id"]);
362 }
e854442e 363
ecbb3292
AD
364 $processed += db_num_rows($result);
365 print "Processed $processed articles...\n";
73ef6f04 366
ecbb3292 367 if (db_num_rows($result) != $limit) {
e854442e
AD
368 echo "All done.\n";
369 break;
370 }
e854442e 371 }
e854442e
AD
372 }
373
764555ff 374 if (isset($options["list-plugins"])) {
6f7798b6 375 $tmppluginhost = new PluginHost();
583f163f 376 $tmppluginhost->load_all($tmppluginhost::KIND_ALL, false);
20b86c79
AD
377 $enabled = array_map("trim", explode(",", PLUGINS));
378
379 echo "List of all available plugins:\n";
380
7a866114 381 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
d2a421e3 382 $about = $plugin->about();
7a866114 383
20b86c79
AD
384 $status = $about[3] ? "system" : "user";
385
386 if (in_array($name, $enabled)) $name .= "*";
387
388 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
389 $name, $status, $about[0], $about[2], $about[1]);
7a866114 390 }
20b86c79
AD
391
392 echo "Plugins marked by * are currently enabled for all users.\n";
393
7a866114
AD
394 }
395
368bd7ea
AD
396 if (isset($options["debug-feed"])) {
397 $feed = $options["debug-feed"];
398
399 if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
400 if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
401
402 $_REQUEST['xdebug'] = 1;
403
7b55001e 404 $rc = update_rss_feed($feed) != false ? 0 : 1;
3c111597
AD
405
406 exit($rc);
368bd7ea
AD
407 }
408
17a8e61d
AD
409 if (isset($options["decrypt-feeds"])) {
410 $result = db_query("SELECT id, auth_pass FROM ttrss_feeds WHERE auth_pass_encrypted = true");
411
412 if (!function_exists("mcrypt_decrypt")) {
413 _debug("mcrypt functions not available.");
414 return;
415 }
416
417 require_once "crypt.php";
418
419 $total = 0;
420
421 db_query("BEGIN");
422
423 while ($line = db_fetch_assoc($result)) {
424 _debug("processing feed id " . $line["id"]);
425
426 $auth_pass = db_escape_string(decrypt_string($line["auth_pass"]));
427
e6d66fe5 428 db_query("UPDATE ttrss_feeds SET auth_pass_encrypted = false, auth_pass = '$auth_pass'
17a8e61d
AD
429 WHERE id = " . $line["id"]);
430
431 ++$total;
432 }
433
434 db_query("COMMIT");
435
436 _debug("$total feeds processed.");
437 }
438
1ffe3391 439 PluginHost::getInstance()->run_commands($options);
73f28fe9 440
9973b13e 441 if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
442 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
443 fclose($lock_handle);
0f906745 444 unlink(LOCK_DIRECTORY . "/$lock_filename");
9bfda43e 445?>