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 "rssfuncs.php";
13 require_once "config.php";
14 require_once "sanity_check.php";
15 require_once "db.php";
16 require_once "db-prefs.php";
18 if (!defined('PHP_EXECUTABLE'))
19 define('PHP_EXECUTABLE', '/usr/bin/php');
23 $longopts = array("feeds",
44 foreach (PluginHost
::getInstance()->get_commands() as $command => $data) {
45 array_push($longopts, $command . $data["suffix"]);
48 $options = getopt("", $longopts);
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");
57 if (count($options) == 0 && !defined('STDIN')) {
60 <title
>Tiny Tiny RSS data update script
.</title
>
61 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
62 <link rel
="stylesheet" type
="text/css" href
="css/utility.css">
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 db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
164 last_updated = '1970-01-01'");
167 if (isset($options["feeds"])) {
168 update_daemon_common();
169 housekeeping_common(true);
171 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
174 if (isset($options["feedbrowser"])) {
175 $count = 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");
185 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
186 sleep(DAEMON_SLEEP_INTERVAL
);
190 if (isset($options["daemon-loop"])) {
191 if (!make_stampfile('update_daemon.stamp')) {
192 _debug("warning: unable to create stampfile\n");
195 update_daemon_common(isset($options["pidlock"]) ?
50 : DAEMON_FEED_LIMIT
);
197 if (!isset($options["pidlock"]) ||
$options["task"] == 0)
198 housekeeping_common(true);
200 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
203 if (isset($options["cleanup-tags"])) {
204 $rc = cleanup_tags( 14, 50000);
205 _debug("$rc tags deleted.\n");
208 if (isset($options["indexes"])) {
209 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
210 _debug("Type 'yes' to continue.");
212 if (read_stdin() != 'yes')
215 _debug("clearing existing indexes...");
217 if (DB_TYPE
== "pgsql") {
218 $result = db_query( "SELECT relname FROM
219 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
220 AND relname NOT LIKE '%_pkey'
223 $result = db_query( "SELECT index_name,table_name FROM
224 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
227 while ($line = db_fetch_assoc($result)) {
228 if (DB_TYPE
== "pgsql") {
229 $statement = "DROP INDEX " . $line["relname"];
232 $statement = "ALTER TABLE ".
233 $line['table_name']." DROP INDEX ".$line['index_name'];
236 db_query( $statement, false);
239 _debug("reading indexes from schema for: " . DB_TYPE
);
241 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
243 while ($line = fgets($fp)) {
246 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
247 $index = $matches[1];
248 $table = $matches[2];
250 $statement = "CREATE INDEX $index ON $table";
253 db_query( $statement);
258 _debug("unable to open schema file.");
263 if (isset($options["convert-filters"])) {
264 _debug("WARNING: this will remove all existing type2 filters.");
265 _debug("Type 'yes' to continue.");
267 if (read_stdin() != 'yes')
270 _debug("converting filters...");
272 db_query( "DELETE FROM ttrss_filters2");
274 $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id");
276 while ($line = db_fetch_assoc($result)) {
277 $owner_uid = $line["owner_uid"];
279 // date filters are removed
280 if ($line["filter_type"] != 5) {
283 if (sql_bool_to_bool($line["cat_filter"])) {
284 $feed_id = "CAT:" . (int)$line["cat_id"];
286 $feed_id = (int)$line["feed_id"];
289 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
290 $filter["rule"] = array(
292 "reg_exp" => $line["reg_exp"],
293 "feed_id" => $feed_id,
294 "filter_type" => $line["filter_type"])));
296 $filter["action"] = array(
298 "action_id" => $line["action_id"],
299 "action_param_label" => $line["action_param"],
300 "action_param" => $line["action_param"])));
302 // Oh god it's full of hacks
305 $_SESSION["uid"] = $owner_uid;
307 $filters = new Pref_Filters($_REQUEST);
314 if (isset($options["update-schema"])) {
315 _debug("checking for updates (" . DB_TYPE
. ")...");
317 $updater = new DbUpdater(Db
::get(), DB_TYPE
, SCHEMA_VERSION
);
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.");
324 if (read_stdin() != 'yes')
327 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
328 _debug("performing update up to version $i...");
330 $result = $updater->performUpdateTo($i, false);
332 _debug($result ?
"OK!" : "FAILED!");
334 if (!$result) return;
338 _debug("update not required.");
343 if (isset($options["gen-search-idx"])) {
344 echo "Generating search index (stemming set to English)...\n";
346 $result = db_query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
347 $count = db_fetch_result($result, 0, "count");
349 print "Articles to process: $count.\n";
355 $result = db_query("SELECT id, title, content FROM ttrss_entries WHERE tsvector_combined IS NULL ORDER BY id LIMIT $limit");
357 while ($line = db_fetch_assoc($result)) {
358 $tsvector_combined = db_escape_string(mb_substr($line['title'] . ' ' . strip_tags(str_replace('<', ' <', $line['content'])),
361 db_query("UPDATE ttrss_entries SET tsvector_combined = to_tsvector('english', '$tsvector_combined') WHERE id = " . $line["id"]);
364 $processed +
= db_num_rows($result);
365 print "Processed $processed articles...\n";
367 if (db_num_rows($result) != $limit) {
374 if (isset($options["list-plugins"])) {
375 $tmppluginhost = new PluginHost();
376 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
, false);
377 $enabled = array_map("trim", explode(",", PLUGINS
));
379 echo "List of all available plugins:\n";
381 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
382 $about = $plugin->about();
384 $status = $about[3] ?
"system" : "user";
386 if (in_array($name, $enabled)) $name .= "*";
388 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
389 $name, $status, $about[0], $about[2], $about[1]);
392 echo "Plugins marked by * are currently enabled for all users.\n";
396 if (isset($options["debug-feed"])) {
397 $feed = $options["debug-feed"];
399 if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
400 if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
402 $_REQUEST['xdebug'] = 1;
404 $rc = update_rss_feed($feed) != false ?
0 : 1;
409 if (isset($options["decrypt-feeds"])) {
410 $result = db_query("SELECT id, auth_pass FROM ttrss_feeds WHERE auth_pass_encrypted = true");
412 if (!function_exists("mcrypt_decrypt")) {
413 _debug("mcrypt functions not available.");
417 require_once "crypt.php";
423 while ($line = db_fetch_assoc($result)) {
424 _debug("processing feed id " . $line["id"]);
426 $auth_pass = db_escape_string(decrypt_string($line["auth_pass"]));
428 db_query("UPDATE ttrss_feeds SET auth_pass_encrypted = false, auth_pass = '$auth_pass'
429 WHERE id = " . $line["id"]);
436 _debug("$total feeds processed.");
439 PluginHost
::getInstance()->run_commands($options);
441 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
442 if (strtoupper(substr(PHP_OS
, 0, 3)) == 'WIN')
443 fclose($lock_handle);
444 unlink(LOCK_DIRECTORY
. "/$lock_filename");