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');
22 $longopts = array("feeds",
43 foreach (PluginHost
::getInstance()->get_commands() as $command => $data) {
44 array_push($longopts, $command . $data["suffix"]);
47 $options = getopt("", $longopts);
49 if (!is_array($options)) {
50 die("error: getopt() failed. ".
51 "Most probably you are using PHP CGI to run this script ".
52 "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ".
53 "additional information.\n");
56 if (count($options) == 0 && !defined('STDIN')) {
59 <title
>Tiny Tiny RSS data update script
.</title
>
60 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
61 <link rel
="stylesheet" type
="text/css" href
="css/utility.css">
65 <div
class="floatingLogo"><img src
="images/logo_small.png"></div
>
66 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
68 <?php
print_error("Please run this script from the command line. Use option \"--help\" to display command help if this error is displayed erroneously."); ?
>
75 if (count($options) == 0 ||
isset($options["help"]) ) {
76 print "Tiny Tiny RSS data update script.\n\n";
78 print " --feeds - update feeds\n";
79 print " --feedbrowser - update feedbrowser\n";
80 print " --daemon - start single-process update daemon\n";
81 print " --task N - create lockfile using this task id\n";
82 print " --cleanup-tags - perform tags table maintenance\n";
83 print " --quiet - don't output messages to stdout\n";
84 print " --log FILE - log messages to FILE\n";
85 print " --indexes - recreate missing schema indexes\n";
86 print " --update-schema - update database schema\n";
87 print " --gen-search-idx - generate basic PostgreSQL fulltext search index\n";
88 print " --convert-filters - convert type1 filters to type2\n";
89 print " --force-update - force update of all feeds\n";
90 print " --list-plugins - list all available plugins\n";
91 print " --debug-feed N - perform debug update of feed N\n";
92 print " --force-refetch - debug update: force refetch feed data\n";
93 print " --force-rehash - debug update: force rehash articles\n";
94 print " --decrypt-feeds - decrypt feed passwords\n";
95 print " --help - show this help\n";
96 print "Plugin options:\n";
98 foreach (PluginHost
::getInstance()->get_commands() as $command => $data) {
99 $args = $data['arghelp'];
100 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
106 if (!isset($options['daemon'])) {
107 require_once "errorhandler.php";
110 if (!isset($options['update-schema'])) {
111 $schema_version = get_schema_version();
113 if ($schema_version != SCHEMA_VERSION
) {
114 die("Schema version is wrong, please upgrade the database.\n");
118 define('QUIET', isset($options['quiet']));
120 if (isset($options["log"])) {
121 _debug("Logging to " . $options["log"]);
122 define('LOGFILE', $options["log"]);
125 if (!isset($options["daemon"])) {
126 $lock_filename = "update.lock";
128 $lock_filename = "update_daemon.lock";
131 if (isset($options["task"])) {
132 _debug("Using task id " . $options["task"]);
133 $lock_filename = $lock_filename . "-task_" . $options["task"];
136 if (isset($options["pidlock"])) {
137 $my_pid = $options["pidlock"];
138 $lock_filename = "update_daemon-$my_pid.lock";
142 _debug("Lock: $lock_filename");
144 $lock_handle = make_lockfile($lock_filename);
147 if (isset($options["task"]) && isset($options["pidlock"])) {
148 $waits = $options["task"] * 5;
149 _debug("Waiting before update ($waits)");
153 // Try to lock a file in order to avoid concurrent update.
155 die("error: Can't create lockfile ($lock_filename). ".
156 "Maybe another update process is already running.\n");
159 if (isset($options["force-update"])) {
160 _debug("marking all feeds as needing update...");
162 db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
163 last_updated = '1970-01-01'");
166 if (isset($options["feeds"])) {
167 RSSUtils
::update_daemon_common();
168 RSSUtils
::housekeeping_common(true);
170 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
173 if (isset($options["feedbrowser"])) {
174 $count = RSSUtils
::update_feedbrowser_cache();
175 print "Finished, $count feeds processed.\n";
178 if (isset($options["daemon"])) {
180 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
181 $log = isset($options['log']) ?
'--log '.$options['log'] : '';
183 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet $log");
184 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
185 sleep(DAEMON_SLEEP_INTERVAL
);
189 if (isset($options["daemon-loop"])) {
190 if (!make_stampfile('update_daemon.stamp')) {
191 _debug("warning: unable to create stampfile\n");
194 RSSUtils
::update_daemon_common(isset($options["pidlock"]) ?
50 : DAEMON_FEED_LIMIT
);
196 if (!isset($options["pidlock"]) ||
$options["task"] == 0)
197 RSSUtils
::housekeeping_common(true);
199 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
202 if (isset($options["cleanup-tags"])) {
203 $rc = cleanup_tags( 14, 50000);
204 _debug("$rc tags deleted.\n");
207 if (isset($options["indexes"])) {
208 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
209 _debug("Type 'yes' to continue.");
211 if (read_stdin() != 'yes')
214 _debug("clearing existing indexes...");
216 if (DB_TYPE
== "pgsql") {
217 $result = db_query( "SELECT relname FROM
218 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
219 AND relname NOT LIKE '%_pkey'
222 $result = db_query( "SELECT index_name,table_name FROM
223 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
226 while ($line = db_fetch_assoc($result)) {
227 if (DB_TYPE
== "pgsql") {
228 $statement = "DROP INDEX " . $line["relname"];
231 $statement = "ALTER TABLE ".
232 $line['table_name']." DROP INDEX ".$line['index_name'];
235 db_query( $statement, false);
238 _debug("reading indexes from schema for: " . DB_TYPE
);
240 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
242 while ($line = fgets($fp)) {
245 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
246 $index = $matches[1];
247 $table = $matches[2];
249 $statement = "CREATE INDEX $index ON $table";
252 db_query( $statement);
257 _debug("unable to open schema file.");
262 if (isset($options["convert-filters"])) {
263 _debug("WARNING: this will remove all existing type2 filters.");
264 _debug("Type 'yes' to continue.");
266 if (read_stdin() != 'yes')
269 _debug("converting filters...");
271 db_query( "DELETE FROM ttrss_filters2");
273 $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id");
275 while ($line = db_fetch_assoc($result)) {
276 $owner_uid = $line["owner_uid"];
278 // date filters are removed
279 if ($line["filter_type"] != 5) {
282 if (sql_bool_to_bool($line["cat_filter"])) {
283 $feed_id = "CAT:" . (int)$line["cat_id"];
285 $feed_id = (int)$line["feed_id"];
288 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
289 $filter["rule"] = array(
291 "reg_exp" => $line["reg_exp"],
292 "feed_id" => $feed_id,
293 "filter_type" => $line["filter_type"])));
295 $filter["action"] = array(
297 "action_id" => $line["action_id"],
298 "action_param_label" => $line["action_param"],
299 "action_param" => $line["action_param"])));
301 // Oh god it's full of hacks
304 $_SESSION["uid"] = $owner_uid;
306 $filters = new Pref_Filters($_REQUEST);
313 if (isset($options["update-schema"])) {
314 _debug("checking for updates (" . DB_TYPE
. ")...");
316 $updater = new DbUpdater(Db
::get(), DB_TYPE
, SCHEMA_VERSION
);
318 if ($updater->isUpdateRequired()) {
319 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
320 _debug("WARNING: please backup your database before continuing.");
321 _debug("Type 'yes' to continue.");
323 if (read_stdin() != 'yes')
326 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
327 _debug("performing update up to version $i...");
329 $result = $updater->performUpdateTo($i, false);
331 _debug($result ?
"OK!" : "FAILED!");
333 if (!$result) return;
337 _debug("update not required.");
342 if (isset($options["gen-search-idx"])) {
343 echo "Generating search index (stemming set to English)...\n";
345 $result = db_query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
346 $count = db_fetch_result($result, 0, "count");
348 print "Articles to process: $count.\n";
354 $result = db_query("SELECT id, title, content FROM ttrss_entries WHERE tsvector_combined IS NULL ORDER BY id LIMIT $limit");
356 while ($line = db_fetch_assoc($result)) {
357 $tsvector_combined = db_escape_string(mb_substr($line['title'] . ' ' . strip_tags(str_replace('<', ' <', $line['content'])),
360 db_query("UPDATE ttrss_entries SET tsvector_combined = to_tsvector('english', '$tsvector_combined') WHERE id = " . $line["id"]);
363 $processed +
= db_num_rows($result);
364 print "Processed $processed articles...\n";
366 if (db_num_rows($result) != $limit) {
373 if (isset($options["list-plugins"])) {
374 $tmppluginhost = new PluginHost();
375 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
, false);
376 $enabled = array_map("trim", explode(",", PLUGINS
));
378 echo "List of all available plugins:\n";
380 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
381 $about = $plugin->about();
383 $status = $about[3] ?
"system" : "user";
385 if (in_array($name, $enabled)) $name .= "*";
387 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
388 $name, $status, $about[0], $about[2], $about[1]);
391 echo "Plugins marked by * are currently enabled for all users.\n";
395 if (isset($options["debug-feed"])) {
396 $feed = $options["debug-feed"];
398 if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
399 if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
401 $_REQUEST['xdebug'] = 1;
403 $rc = RSSUtils
::update_rss_feed($feed) != false ?
0 : 1;
408 if (isset($options["decrypt-feeds"])) {
409 $result = db_query("SELECT id, auth_pass FROM ttrss_feeds WHERE auth_pass_encrypted = true");
411 if (!function_exists("mcrypt_decrypt")) {
412 _debug("mcrypt functions not available.");
416 require_once "crypt.php";
422 while ($line = db_fetch_assoc($result)) {
423 _debug("processing feed id " . $line["id"]);
425 $auth_pass = db_escape_string(decrypt_string($line["auth_pass"]));
427 db_query("UPDATE ttrss_feeds SET auth_pass_encrypted = false, auth_pass = '$auth_pass'
428 WHERE id = " . $line["id"]);
435 _debug("$total feeds processed.");
438 PluginHost
::getInstance()->run_commands($options);
440 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
441 if (strtoupper(substr(PHP_OS
, 0, 3)) == 'WIN')
442 fclose($lock_handle);
443 unlink(LOCK_DIRECTORY
. "/$lock_filename");