3 set_include_path(dirname(__FILE__
) ."/include" . PATH_SEPARATOR
.
6 define('DISABLE_SESSIONS', true);
8 chdir(dirname(__FILE__
));
10 require_once "autoload.php";
11 require_once "functions.php";
12 require_once "config.php";
13 require_once "sanity_check.php";
14 require_once "db.php";
15 require_once "db-prefs.php";
17 if (!defined('PHP_EXECUTABLE'))
18 define('PHP_EXECUTABLE', '/usr/bin/php');
24 $longopts = array("feeds",
45 foreach (PluginHost
::getInstance()->get_commands() as $command => $data) {
46 array_push($longopts, $command . $data["suffix"]);
49 $options = getopt("", $longopts);
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");
58 if (count($options) == 0 && !defined('STDIN')) {
61 <title
>Tiny Tiny RSS data update script
.</title
>
62 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
66 <div
class="floatingLogo"><img src
="images/logo_small.png"></div
>
67 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
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."); ?
>
76 if (count($options) == 0 ||
isset($options["help"]) ) {
77 print "Tiny Tiny RSS data update script.\n\n";
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";
84 print " --quiet - don't output messages to stdout\n";
85 print " --log FILE - log messages to FILE\n";
86 print " --log-level N - log verbosity level\n";
87 print " --indexes - recreate missing schema indexes\n";
88 print " --update-schema - update database schema\n";
89 print " --gen-search-idx - generate basic PostgreSQL fulltext search index\n";
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";
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";
96 print " --help - show this help\n";
97 print "Plugin options:\n";
99 foreach (PluginHost
::getInstance()->get_commands() as $command => $data) {
100 $args = $data['arghelp'];
101 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
107 if (!isset($options['daemon'])) {
108 require_once "errorhandler.php";
111 if (!isset($options['update-schema'])) {
112 $schema_version = get_schema_version();
114 if ($schema_version != SCHEMA_VERSION
) {
115 die("Schema version is wrong, please upgrade the database.\n");
119 Debug
::set_enabled(true);
120 Debug
::set_quiet(isset($options['quiet']));
122 if (isset($options["log-level"])) {
123 Debug
::set_loglevel((int)$options["log-level"]);
126 if (isset($options["log"])) {
127 Debug
::set_logfile($options["log"]);
128 Debug
::log("Logging to " . $options["log"]);
131 if (!isset($options["daemon"])) {
132 $lock_filename = "update.lock";
134 $lock_filename = "update_daemon.lock";
137 if (isset($options["task"])) {
138 Debug
::log("Using task id " . $options["task"]);
139 $lock_filename = $lock_filename . "-task_" . $options["task"];
142 if (isset($options["pidlock"])) {
143 $my_pid = $options["pidlock"];
144 $lock_filename = "update_daemon-$my_pid.lock";
148 Debug
::log("Lock: $lock_filename");
150 $lock_handle = make_lockfile($lock_filename);
153 if (isset($options["task"]) && isset($options["pidlock"])) {
154 $waits = $options["task"] * 5;
155 Debug
::log("Waiting before update ($waits)");
159 // Try to lock a file in order to avoid concurrent update.
161 die("error: Can't create lockfile ($lock_filename). ".
162 "Maybe another update process is already running.\n");
165 if (isset($options["force-update"])) {
166 Debug
::log("marking all feeds as needing update...");
168 $pdo->query( "UPDATE ttrss_feeds SET
169 last_update_started = '1970-01-01', last_updated = '1970-01-01'");
172 if (isset($options["feeds"])) {
173 RSSUtils
::update_daemon_common();
174 RSSUtils
::housekeeping_common(true);
176 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
179 if (isset($options["feedbrowser"])) {
180 $count = RSSUtils
::update_feedbrowser_cache();
181 print "Finished, $count feeds processed.\n";
184 if (isset($options["daemon"])) {
186 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
187 $log = isset($options['log']) ?
'--log '.$options['log'] : '';
188 $log_level = isset($options['log-level']) ?
'--log-level '.$options['log-level'] : '';
190 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet $log $log_level");
192 // let's enforce a minimum spawn interval as to not forkbomb the host
193 $spawn_interval = max(60, DAEMON_SLEEP_INTERVAL
);
195 Debug
::log("Sleeping for $spawn_interval seconds...");
196 sleep($spawn_interval);
200 if (isset($options["daemon-loop"])) {
201 if (!make_stampfile('update_daemon.stamp')) {
202 Debug
::log("warning: unable to create stampfile\n");
205 RSSUtils
::update_daemon_common(isset($options["pidlock"]) ?
50 : DAEMON_FEED_LIMIT
);
207 if (!isset($options["pidlock"]) ||
$options["task"] == 0)
208 RSSUtils
::housekeeping_common(true);
210 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
213 if (isset($options["cleanup-tags"])) {
214 $rc = cleanup_tags( 14, 50000);
215 Debug
::log("$rc tags deleted.\n");
218 if (isset($options["indexes"])) {
219 Debug
::log("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
220 Debug
::log("Type 'yes' to continue.");
222 if (read_stdin() != 'yes')
225 Debug
::log("clearing existing indexes...");
227 if (DB_TYPE
== "pgsql") {
228 $sth = $pdo->query( "SELECT relname FROM
229 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
230 AND relname NOT LIKE '%_pkey'
233 $sth = $pdo->query( "SELECT index_name,table_name FROM
234 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
237 while ($line = $sth->fetch()) {
238 if (DB_TYPE
== "pgsql") {
239 $statement = "DROP INDEX " . $line["relname"];
240 Debug
::log($statement);
242 $statement = "ALTER TABLE ".
243 $line['table_name']." DROP INDEX ".$line['index_name'];
244 Debug
::log($statement);
246 $pdo->query($statement);
249 Debug
::log("reading indexes from schema for: " . DB_TYPE
);
251 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
253 while ($line = fgets($fp)) {
256 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
257 $index = $matches[1];
258 $table = $matches[2];
260 $statement = "CREATE INDEX $index ON $table";
262 Debug
::log($statement);
263 $pdo->query($statement);
268 Debug
::log("unable to open schema file.");
270 Debug
::log("all done.");
273 if (isset($options["convert-filters"])) {
274 Debug
::log("WARNING: this will remove all existing type2 filters.");
275 Debug
::log("Type 'yes' to continue.");
277 if (read_stdin() != 'yes')
280 Debug
::log("converting filters...");
282 $pdo->query("DELETE FROM ttrss_filters2");
284 $res = $pdo->query("SELECT * FROM ttrss_filters ORDER BY id");
286 while ($line = $res->fetch()) {
287 $owner_uid = $line["owner_uid"];
289 // date filters are removed
290 if ($line["filter_type"] != 5) {
293 if (sql_bool_to_bool($line["cat_filter"])) {
294 $feed_id = "CAT:" . (int)$line["cat_id"];
296 $feed_id = (int)$line["feed_id"];
299 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
300 $filter["rule"] = array(
302 "reg_exp" => $line["reg_exp"],
303 "feed_id" => $feed_id,
304 "filter_type" => $line["filter_type"])));
306 $filter["action"] = array(
308 "action_id" => $line["action_id"],
309 "action_param_label" => $line["action_param"],
310 "action_param" => $line["action_param"])));
312 // Oh god it's full of hacks
315 $_SESSION["uid"] = $owner_uid;
317 $filters = new Pref_Filters($_REQUEST);
324 if (isset($options["update-schema"])) {
325 Debug
::log("checking for updates (" . DB_TYPE
. ")...");
327 $updater = new DbUpdater(Db
::pdo(), DB_TYPE
, SCHEMA_VERSION
);
329 if ($updater->isUpdateRequired()) {
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.");
334 if (read_stdin() != 'yes')
337 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
338 Debug
::log("performing update up to version $i...");
340 $result = $updater->performUpdateTo($i, false);
342 Debug
::log($result ?
"OK!" : "FAILED!");
344 if (!$result) return;
348 Debug
::log("update not required.");
353 if (isset($options["gen-search-idx"])) {
354 echo "Generating search index (stemming set to English)...\n";
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'];
360 print "Articles to process: $count.\n";
365 $sth = $pdo->prepare("SELECT id, title, content FROM ttrss_entries WHERE
366 tsvector_combined IS NULL ORDER BY id LIMIT ?");
367 $sth->execute([$limit]);
369 $usth = $pdo->prepare("UPDATE ttrss_entries
370 SET tsvector_combined = to_tsvector('english', ?) WHERE id = ?");
374 while ($line = $sth->fetch()) {
375 $tsvector_combined = mb_substr(strip_tags($line["title"] . " " . $line["content"]), 0, 1000000);
377 $usth->execute([$tsvector_combined, $line['id']]);
382 print "Processed $processed articles...\n";
384 if ($processed < $limit) {
391 if (isset($options["list-plugins"])) {
392 $tmppluginhost = new PluginHost();
393 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
, false);
394 $enabled = array_map("trim", explode(",", PLUGINS
));
396 echo "List of all available plugins:\n";
398 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
399 $about = $plugin->about();
401 $status = $about[3] ?
"system" : "user";
403 if (in_array($name, $enabled)) $name .= "*";
405 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
406 $name, $status, $about[0], $about[2], $about[1]);
409 echo "Plugins marked by * are currently enabled for all users.\n";
413 if (isset($options["debug-feed"])) {
414 $feed = $options["debug-feed"];
416 if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
417 if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
419 Debug
::set_loglevel(Debug
::$LOG_EXTENDED);
421 $rc = RSSUtils
::update_rss_feed($feed) != false ?
0 : 1;
426 PluginHost
::getInstance()->run_commands($options);
428 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
429 if (strtoupper(substr(PHP_OS
, 0, 3)) == 'WIN')
430 fclose($lock_handle);
431 unlink(LOCK_DIRECTORY
. "/$lock_filename");