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">
63 <link rel
="stylesheet" type
="text/css" href
="css/utility.css">
67 <div
class="floatingLogo"><img src
="images/logo_small.png"></div
>
68 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
70 <?php
print_error("Please run this script from the command line. Use option \"--help\" to display command help if this error is displayed erroneously."); ?
>
77 if (count($options) == 0 ||
isset($options["help"]) ) {
78 print "Tiny Tiny RSS data update script.\n\n";
80 print " --feeds - update feeds\n";
81 print " --feedbrowser - update feedbrowser\n";
82 print " --daemon - start single-process update daemon\n";
83 print " --task N - create lockfile using this task id\n";
84 print " --cleanup-tags - perform tags table maintenance\n";
85 print " --quiet - don't output messages to stdout\n";
86 print " --log FILE - log messages to FILE\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 " --decrypt-feeds - decrypt feed passwords\n";
97 print " --help - show this help\n";
98 print "Plugin options:\n";
100 foreach (PluginHost
::getInstance()->get_commands() as $command => $data) {
101 $args = $data['arghelp'];
102 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
108 if (!isset($options['daemon'])) {
109 require_once "errorhandler.php";
112 if (!isset($options['update-schema'])) {
113 $schema_version = get_schema_version();
115 if ($schema_version != SCHEMA_VERSION
) {
116 die("Schema version is wrong, please upgrade the database.\n");
120 define('QUIET', isset($options['quiet']));
122 if (isset($options["log"])) {
123 _debug("Logging to " . $options["log"]);
124 define('LOGFILE', $options["log"]);
127 if (!isset($options["daemon"])) {
128 $lock_filename = "update.lock";
130 $lock_filename = "update_daemon.lock";
133 if (isset($options["task"])) {
134 _debug("Using task id " . $options["task"]);
135 $lock_filename = $lock_filename . "-task_" . $options["task"];
138 if (isset($options["pidlock"])) {
139 $my_pid = $options["pidlock"];
140 $lock_filename = "update_daemon-$my_pid.lock";
144 _debug("Lock: $lock_filename");
146 $lock_handle = make_lockfile($lock_filename);
149 if (isset($options["task"]) && isset($options["pidlock"])) {
150 $waits = $options["task"] * 5;
151 _debug("Waiting before update ($waits)");
155 // Try to lock a file in order to avoid concurrent update.
157 die("error: Can't create lockfile ($lock_filename). ".
158 "Maybe another update process is already running.\n");
161 if (isset($options["force-update"])) {
162 _debug("marking all feeds as needing update...");
164 $pdo->query( "UPDATE ttrss_feeds SET
165 last_update_started = '1970-01-01', last_updated = '1970-01-01'");
168 if (isset($options["feeds"])) {
169 RSSUtils
::update_daemon_common();
170 RSSUtils
::housekeeping_common(true);
172 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
175 if (isset($options["feedbrowser"])) {
176 $count = RSSUtils
::update_feedbrowser_cache();
177 print "Finished, $count feeds processed.\n";
180 if (isset($options["daemon"])) {
182 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
183 $log = isset($options['log']) ?
'--log '.$options['log'] : '';
185 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet $log");
187 // let's enforce a minimum spawn interval as to not forkbomb the host
188 $spawn_interval = max(60, DAEMON_SLEEP_INTERVAL
);
190 _debug("Sleeping for $spawn_interval seconds...");
191 sleep($spawn_interval);
195 if (isset($options["daemon-loop"])) {
196 if (!make_stampfile('update_daemon.stamp')) {
197 _debug("warning: unable to create stampfile\n");
200 RSSUtils
::update_daemon_common(isset($options["pidlock"]) ?
50 : DAEMON_FEED_LIMIT
);
202 if (!isset($options["pidlock"]) ||
$options["task"] == 0)
203 RSSUtils
::housekeeping_common(true);
205 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
208 if (isset($options["cleanup-tags"])) {
209 $rc = cleanup_tags( 14, 50000);
210 _debug("$rc tags deleted.\n");
213 if (isset($options["indexes"])) {
214 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
215 _debug("Type 'yes' to continue.");
217 if (read_stdin() != 'yes')
220 _debug("clearing existing indexes...");
222 if (DB_TYPE
== "pgsql") {
223 $sth = $pdo->query( "SELECT relname FROM
224 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
225 AND relname NOT LIKE '%_pkey'
228 $sth = $pdo->query( "SELECT index_name,table_name FROM
229 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
232 while ($line = $sth->fetch()) {
233 if (DB_TYPE
== "pgsql") {
234 $statement = "DROP INDEX " . $line["relname"];
237 $statement = "ALTER TABLE ".
238 $line['table_name']." DROP INDEX ".$line['index_name'];
241 $pdo->query($statement);
244 _debug("reading indexes from schema for: " . DB_TYPE
);
246 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
248 while ($line = fgets($fp)) {
251 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
252 $index = $matches[1];
253 $table = $matches[2];
255 $statement = "CREATE INDEX $index ON $table";
258 $pdo->query($statement);
263 _debug("unable to open schema file.");
268 if (isset($options["convert-filters"])) {
269 _debug("WARNING: this will remove all existing type2 filters.");
270 _debug("Type 'yes' to continue.");
272 if (read_stdin() != 'yes')
275 _debug("converting filters...");
277 $pdo->query("DELETE FROM ttrss_filters2");
279 $res = $pdo->query("SELECT * FROM ttrss_filters ORDER BY id");
281 while ($line = $res->fetch()) {
282 $owner_uid = $line["owner_uid"];
284 // date filters are removed
285 if ($line["filter_type"] != 5) {
288 if (sql_bool_to_bool($line["cat_filter"])) {
289 $feed_id = "CAT:" . (int)$line["cat_id"];
291 $feed_id = (int)$line["feed_id"];
294 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
295 $filter["rule"] = array(
297 "reg_exp" => $line["reg_exp"],
298 "feed_id" => $feed_id,
299 "filter_type" => $line["filter_type"])));
301 $filter["action"] = array(
303 "action_id" => $line["action_id"],
304 "action_param_label" => $line["action_param"],
305 "action_param" => $line["action_param"])));
307 // Oh god it's full of hacks
310 $_SESSION["uid"] = $owner_uid;
312 $filters = new Pref_Filters($_REQUEST);
319 if (isset($options["update-schema"])) {
320 _debug("checking for updates (" . DB_TYPE
. ")...");
322 $updater = new DbUpdater(Db
::pdo(), DB_TYPE
, SCHEMA_VERSION
);
324 if ($updater->isUpdateRequired()) {
325 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
326 _debug("WARNING: please backup your database before continuing.");
327 _debug("Type 'yes' to continue.");
329 if (read_stdin() != 'yes')
332 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
333 _debug("performing update up to version $i...");
335 $result = $updater->performUpdateTo($i, false);
337 _debug($result ?
"OK!" : "FAILED!");
339 if (!$result) return;
343 _debug("update not required.");
348 if (isset($options["gen-search-idx"])) {
349 echo "Generating search index (stemming set to English)...\n";
351 $res = $pdo->query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
352 $row = $res->fetch();
353 $count = $row['count'];
355 print "Articles to process: $count.\n";
360 $sth = $pdo->prepare("SELECT id, title, content FROM ttrss_entries WHERE
361 tsvector_combined IS NULL ORDER BY id LIMIT ?");
362 $sth->execute([$limit]);
364 $usth = $pdo->prepare("UPDATE ttrss_entries
365 SET tsvector_combined = to_tsvector('english', ?) WHERE id = ?");
369 while ($line = $sth->fetch()) {
370 $tsvector_combined = mb_substr($line['title'] . ' ' .
371 preg_replace('/[<\?\:]/', ' ', strip_tags($line['content'])),
374 $usth->execute([$tsvector_combined, $line['id']]);
379 print "Processed $processed articles...\n";
381 if ($processed < $limit) {
388 if (isset($options["list-plugins"])) {
389 $tmppluginhost = new PluginHost();
390 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
, false);
391 $enabled = array_map("trim", explode(",", PLUGINS
));
393 echo "List of all available plugins:\n";
395 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
396 $about = $plugin->about();
398 $status = $about[3] ?
"system" : "user";
400 if (in_array($name, $enabled)) $name .= "*";
402 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
403 $name, $status, $about[0], $about[2], $about[1]);
406 echo "Plugins marked by * are currently enabled for all users.\n";
410 if (isset($options["debug-feed"])) {
411 $feed = $options["debug-feed"];
413 if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
414 if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
416 $_REQUEST['xdebug'] = 1;
418 $rc = RSSUtils
::update_rss_feed($feed) != false ?
0 : 1;
423 if (isset($options["decrypt-feeds"])) {
425 if (!function_exists("mcrypt_decrypt")) {
426 _debug("mcrypt functions not available.");
430 $res = $pdo->query("SELECT id, auth_pass FROM ttrss_feeds WHERE auth_pass_encrypted = true");
432 require_once "crypt.php";
436 $pdo->beginTransaction();
438 $usth = $pdo->prepare("UPDATE ttrss_feeds SET auth_pass_encrypted = false, auth_pass = ?
441 while ($line = $res->fetch()) {
442 _debug("processing feed id " . $line["id"]);
444 $auth_pass = decrypt_string($line["auth_pass"]);
446 $usth->execute([$auth_pass, $line['id']]);
453 _debug("$total feeds processed.");
456 PluginHost
::getInstance()->run_commands($options);
458 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
459 if (strtoupper(substr(PHP_OS
, 0, 3)) == 'WIN')
460 fclose($lock_handle);
461 unlink(LOCK_DIRECTORY
. "/$lock_filename");