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 " --indexes - recreate missing schema indexes\n";
87 print " --update-schema - update database schema\n";
88 print " --gen-search-idx - generate basic PostgreSQL fulltext search index\n";
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";
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";
95 print " --decrypt-feeds - decrypt feed passwords\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 define('QUIET', isset($options['quiet']));
121 if (isset($options["log"])) {
122 _debug("Logging to " . $options["log"]);
123 define('LOGFILE', $options["log"]);
126 if (!isset($options["daemon"])) {
127 $lock_filename = "update.lock";
129 $lock_filename = "update_daemon.lock";
132 if (isset($options["task"])) {
133 _debug("Using task id " . $options["task"]);
134 $lock_filename = $lock_filename . "-task_" . $options["task"];
137 if (isset($options["pidlock"])) {
138 $my_pid = $options["pidlock"];
139 $lock_filename = "update_daemon-$my_pid.lock";
143 _debug("Lock: $lock_filename");
145 $lock_handle = make_lockfile($lock_filename);
148 if (isset($options["task"]) && isset($options["pidlock"])) {
149 $waits = $options["task"] * 5;
150 _debug("Waiting before update ($waits)");
154 // Try to lock a file in order to avoid concurrent update.
156 die("error: Can't create lockfile ($lock_filename). ".
157 "Maybe another update process is already running.\n");
160 if (isset($options["force-update"])) {
161 _debug("marking all feeds as needing update...");
163 $pdo->query( "UPDATE ttrss_feeds SET
164 last_update_started = '1970-01-01', last_updated = '1970-01-01'");
167 if (isset($options["feeds"])) {
168 RSSUtils
::update_daemon_common();
169 RSSUtils
::housekeeping_common(true);
171 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
174 if (isset($options["feedbrowser"])) {
175 $count = RSSUtils
::update_feedbrowser_cache();
176 print "Finished, $count feeds processed.\n";
179 if (isset($options["daemon"])) {
181 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
182 $log = isset($options['log']) ?
'--log '.$options['log'] : '';
184 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet $log");
186 // let's enforce a minimum spawn interval as to not forkbomb the host
187 $spawn_interval = max(60, DAEMON_SLEEP_INTERVAL
);
189 _debug("Sleeping for $spawn_interval seconds...");
190 sleep($spawn_interval);
194 if (isset($options["daemon-loop"])) {
195 if (!make_stampfile('update_daemon.stamp')) {
196 _debug("warning: unable to create stampfile\n");
199 RSSUtils
::update_daemon_common(isset($options["pidlock"]) ?
50 : DAEMON_FEED_LIMIT
);
201 if (!isset($options["pidlock"]) ||
$options["task"] == 0)
202 RSSUtils
::housekeeping_common(true);
204 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
207 if (isset($options["cleanup-tags"])) {
208 $rc = cleanup_tags( 14, 50000);
209 _debug("$rc tags deleted.\n");
212 if (isset($options["indexes"])) {
213 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
214 _debug("Type 'yes' to continue.");
216 if (read_stdin() != 'yes')
219 _debug("clearing existing indexes...");
221 if (DB_TYPE
== "pgsql") {
222 $sth = $pdo->query( "SELECT relname FROM
223 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
224 AND relname NOT LIKE '%_pkey'
227 $sth = $pdo->query( "SELECT index_name,table_name FROM
228 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
231 while ($line = $sth->fetch()) {
232 if (DB_TYPE
== "pgsql") {
233 $statement = "DROP INDEX " . $line["relname"];
236 $statement = "ALTER TABLE ".
237 $line['table_name']." DROP INDEX ".$line['index_name'];
240 $pdo->query($statement);
243 _debug("reading indexes from schema for: " . DB_TYPE
);
245 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
247 while ($line = fgets($fp)) {
250 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
251 $index = $matches[1];
252 $table = $matches[2];
254 $statement = "CREATE INDEX $index ON $table";
257 $pdo->query($statement);
262 _debug("unable to open schema file.");
267 if (isset($options["convert-filters"])) {
268 _debug("WARNING: this will remove all existing type2 filters.");
269 _debug("Type 'yes' to continue.");
271 if (read_stdin() != 'yes')
274 _debug("converting filters...");
276 $pdo->query("DELETE FROM ttrss_filters2");
278 $res = $pdo->query("SELECT * FROM ttrss_filters ORDER BY id");
280 while ($line = $res->fetch()) {
281 $owner_uid = $line["owner_uid"];
283 // date filters are removed
284 if ($line["filter_type"] != 5) {
287 if (sql_bool_to_bool($line["cat_filter"])) {
288 $feed_id = "CAT:" . (int)$line["cat_id"];
290 $feed_id = (int)$line["feed_id"];
293 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
294 $filter["rule"] = array(
296 "reg_exp" => $line["reg_exp"],
297 "feed_id" => $feed_id,
298 "filter_type" => $line["filter_type"])));
300 $filter["action"] = array(
302 "action_id" => $line["action_id"],
303 "action_param_label" => $line["action_param"],
304 "action_param" => $line["action_param"])));
306 // Oh god it's full of hacks
309 $_SESSION["uid"] = $owner_uid;
311 $filters = new Pref_Filters($_REQUEST);
318 if (isset($options["update-schema"])) {
319 _debug("checking for updates (" . DB_TYPE
. ")...");
321 $updater = new DbUpdater(Db
::pdo(), DB_TYPE
, SCHEMA_VERSION
);
323 if ($updater->isUpdateRequired()) {
324 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
325 _debug("WARNING: please backup your database before continuing.");
326 _debug("Type 'yes' to continue.");
328 if (read_stdin() != 'yes')
331 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
332 _debug("performing update up to version $i...");
334 $result = $updater->performUpdateTo($i, false);
336 _debug($result ?
"OK!" : "FAILED!");
338 if (!$result) return;
342 _debug("update not required.");
347 if (isset($options["gen-search-idx"])) {
348 echo "Generating search index (stemming set to English)...\n";
350 $res = $pdo->query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
351 $row = $res->fetch();
352 $count = $row['count'];
354 print "Articles to process: $count.\n";
359 $sth = $pdo->prepare("SELECT id, title, content FROM ttrss_entries WHERE
360 tsvector_combined IS NULL ORDER BY id LIMIT ?");
361 $sth->execute([$limit]);
363 $usth = $pdo->prepare("UPDATE ttrss_entries
364 SET tsvector_combined = to_tsvector('english', ?) WHERE id = ?");
368 while ($line = $sth->fetch()) {
369 $tsvector_combined = mb_substr($line['title'] . ' ' .
370 preg_replace('/[<\?\:]/', ' ', strip_tags($line['content'])),
373 $usth->execute([$tsvector_combined, $line['id']]);
378 print "Processed $processed articles...\n";
380 if ($processed < $limit) {
387 if (isset($options["list-plugins"])) {
388 $tmppluginhost = new PluginHost();
389 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
, false);
390 $enabled = array_map("trim", explode(",", PLUGINS
));
392 echo "List of all available plugins:\n";
394 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
395 $about = $plugin->about();
397 $status = $about[3] ?
"system" : "user";
399 if (in_array($name, $enabled)) $name .= "*";
401 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
402 $name, $status, $about[0], $about[2], $about[1]);
405 echo "Plugins marked by * are currently enabled for all users.\n";
409 if (isset($options["debug-feed"])) {
410 $feed = $options["debug-feed"];
412 if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
413 if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
415 $_REQUEST['xdebug'] = 1;
417 $rc = RSSUtils
::update_rss_feed($feed) != false ?
0 : 1;
422 if (isset($options["decrypt-feeds"])) {
424 if (!function_exists("mcrypt_decrypt")) {
425 _debug("mcrypt functions not available.");
429 $res = $pdo->query("SELECT id, auth_pass FROM ttrss_feeds WHERE auth_pass_encrypted = true");
431 require_once "crypt.php";
435 $pdo->beginTransaction();
437 $usth = $pdo->prepare("UPDATE ttrss_feeds SET auth_pass_encrypted = false, auth_pass = ?
440 while ($line = $res->fetch()) {
441 _debug("processing feed id " . $line["id"]);
443 $auth_pass = decrypt_string($line["auth_pass"]);
445 $usth->execute([$auth_pass, $line['id']]);
452 _debug("$total feeds processed.");
455 PluginHost
::getInstance()->run_commands($options);
457 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
458 if (strtoupper(substr(PHP_OS
, 0, 3)) == 'WIN')
459 fclose($lock_handle);
460 unlink(LOCK_DIRECTORY
. "/$lock_filename");