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",
40 foreach (PluginHost
::getInstance()->get_commands() as $command => $data) {
41 array_push($longopts, $command . $data["suffix"]);
44 $options = getopt("", $longopts);
46 if (!is_array($options)) {
47 die("error: getopt() failed. ".
48 "Most probably you are using PHP CGI to run this script ".
49 "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ".
50 "additional information.\n");
53 if (count($options) == 0 && !defined('STDIN')) {
56 <title
>Tiny Tiny RSS data update script
.</title
>
57 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
58 <link rel
="stylesheet" type
="text/css" href
="css/utility.css">
62 <div
class="floatingLogo"><img src
="images/logo_small.png"></div
>
63 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
65 <?php
print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?
>
72 if (count($options) == 0 ||
isset($options["help"]) ) {
73 print "Tiny Tiny RSS data update script.\n\n";
75 print " --feeds - update feeds\n";
76 print " --feedbrowser - update feedbrowser\n";
77 print " --daemon - start single-process update daemon\n";
78 print " --task N - create lockfile using this task id\n";
79 print " --cleanup-tags - perform tags table maintenance\n";
80 print " --quiet - don't output messages to stdout\n";
81 print " --log FILE - log messages to FILE\n";
82 print " --indexes - recreate missing schema indexes\n";
83 print " --update-schema - update database schema\n";
84 print " --gen-search-idx - generate basic PostgreSQL fulltext search index\n";
85 print " --convert-filters - convert type1 filters to type2\n";
86 print " --force-update - force update of all feeds\n";
87 print " --list-plugins - list all available plugins\n";
88 print " --help - show this help\n";
89 print "Plugin options:\n";
91 foreach (PluginHost
::getInstance()->get_commands() as $command => $data) {
92 $args = $data['arghelp'];
93 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
99 if (!isset($options['daemon'])) {
100 require_once "errorhandler.php";
103 if (!isset($options['update-schema'])) {
104 $schema_version = get_schema_version();
106 if ($schema_version != SCHEMA_VERSION
) {
107 die("Schema version is wrong, please upgrade the database.\n");
111 define('QUIET', isset($options['quiet']));
113 if (isset($options["log"])) {
114 _debug("Logging to " . $options["log"]);
115 define('LOGFILE', $options["log"]);
118 if (!isset($options["daemon"])) {
119 $lock_filename = "update.lock";
121 $lock_filename = "update_daemon.lock";
124 if (isset($options["task"])) {
125 _debug("Using task id " . $options["task"]);
126 $lock_filename = $lock_filename . "-task_" . $options["task"];
129 if (isset($options["pidlock"])) {
130 $my_pid = $options["pidlock"];
131 $lock_filename = "update_daemon-$my_pid.lock";
135 _debug("Lock: $lock_filename");
137 $lock_handle = make_lockfile($lock_filename);
140 if (isset($options["task"]) && isset($options["pidlock"])) {
141 $waits = $options["task"] * 5;
142 _debug("Waiting before update ($waits)");
146 // Try to lock a file in order to avoid concurrent update.
148 die("error: Can't create lockfile ($lock_filename). ".
149 "Maybe another update process is already running.\n");
152 if (isset($options["force-update"])) {
153 _debug("marking all feeds as needing update...");
155 db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
156 last_updated = '1970-01-01'");
159 if (isset($options["feeds"])) {
160 update_daemon_common();
161 housekeeping_common(true);
163 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
166 if (isset($options["feedbrowser"])) {
167 $count = update_feedbrowser_cache();
168 print "Finished, $count feeds processed.\n";
171 if (isset($options["daemon"])) {
173 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
174 $log = isset($options['log']) ?
'--log '.$options['log'] : '';
176 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet $log");
177 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
178 sleep(DAEMON_SLEEP_INTERVAL
);
182 if (isset($options["daemon-loop"])) {
183 if (!make_stampfile('update_daemon.stamp')) {
184 _debug("warning: unable to create stampfile\n");
187 update_daemon_common(isset($options["pidlock"]) ?
50 : DAEMON_FEED_LIMIT
);
189 if (!isset($options["pidlock"]) ||
$options["task"] == 0)
190 housekeeping_common(true);
192 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
195 if (isset($options["cleanup-tags"])) {
196 $rc = cleanup_tags( 14, 50000);
197 _debug("$rc tags deleted.\n");
200 if (isset($options["indexes"])) {
201 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
202 _debug("Type 'yes' to continue.");
204 if (read_stdin() != 'yes')
207 _debug("clearing existing indexes...");
209 if (DB_TYPE
== "pgsql") {
210 $result = db_query( "SELECT relname FROM
211 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
212 AND relname NOT LIKE '%_pkey'
215 $result = db_query( "SELECT index_name,table_name FROM
216 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
219 while ($line = db_fetch_assoc($result)) {
220 if (DB_TYPE
== "pgsql") {
221 $statement = "DROP INDEX " . $line["relname"];
224 $statement = "ALTER TABLE ".
225 $line['table_name']." DROP INDEX ".$line['index_name'];
228 db_query( $statement, false);
231 _debug("reading indexes from schema for: " . DB_TYPE
);
233 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
235 while ($line = fgets($fp)) {
238 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
239 $index = $matches[1];
240 $table = $matches[2];
242 $statement = "CREATE INDEX $index ON $table";
245 db_query( $statement);
250 _debug("unable to open schema file.");
255 if (isset($options["convert-filters"])) {
256 _debug("WARNING: this will remove all existing type2 filters.");
257 _debug("Type 'yes' to continue.");
259 if (read_stdin() != 'yes')
262 _debug("converting filters...");
264 db_query( "DELETE FROM ttrss_filters2");
266 $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id");
268 while ($line = db_fetch_assoc($result)) {
269 $owner_uid = $line["owner_uid"];
271 // date filters are removed
272 if ($line["filter_type"] != 5) {
275 if (sql_bool_to_bool($line["cat_filter"])) {
276 $feed_id = "CAT:" . (int)$line["cat_id"];
278 $feed_id = (int)$line["feed_id"];
281 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
282 $filter["rule"] = array(
284 "reg_exp" => $line["reg_exp"],
285 "feed_id" => $feed_id,
286 "filter_type" => $line["filter_type"])));
288 $filter["action"] = array(
290 "action_id" => $line["action_id"],
291 "action_param_label" => $line["action_param"],
292 "action_param" => $line["action_param"])));
294 // Oh god it's full of hacks
297 $_SESSION["uid"] = $owner_uid;
299 $filters = new Pref_Filters($_REQUEST);
306 if (isset($options["update-schema"])) {
307 _debug("checking for updates (" . DB_TYPE
. ")...");
309 $updater = new DbUpdater(Db
::get(), DB_TYPE
, SCHEMA_VERSION
);
311 if ($updater->isUpdateRequired()) {
312 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
313 _debug("WARNING: please backup your database before continuing.");
314 _debug("Type 'yes' to continue.");
316 if (read_stdin() != 'yes')
319 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
320 _debug("performing update up to version $i...");
322 $result = $updater->performUpdateTo($i);
324 _debug($result ?
"OK!" : "FAILED!");
326 if (!$result) return;
330 _debug("update not required.");
335 if (isset($options["gen-search-idx"])) {
336 echo "Generating search index (stemming set to English)...\n";
338 $result = db_query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
339 $count = db_fetch_result($result, 0, "count");
341 print "Articles to process: $count.\n";
347 $result = db_query("SELECT id, title, content FROM ttrss_entries WHERE tsvector_combined IS NULL ORDER BY id LIMIT $limit");
349 while ($line = db_fetch_assoc($result)) {
350 $tsvector_combined = db_escape_string(mb_substr($line['title'] . ' ' . strip_tags($line['content']),
353 db_query("UPDATE ttrss_entries SET tsvector_combined = to_tsvector('english', '$tsvector_combined') WHERE id = " . $line["id"]);
356 $processed +
= db_num_rows($result);
357 print "Processed $processed articles...\n";
359 if (db_num_rows($result) != $limit) {
366 if (isset($options["list-plugins"])) {
367 $tmppluginhost = new PluginHost();
368 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
);
369 $enabled = array_map("trim", explode(",", PLUGINS
));
371 echo "List of all available plugins:\n";
373 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
374 $about = $plugin->about();
376 $status = $about[3] ?
"system" : "user";
378 if (in_array($name, $enabled)) $name .= "*";
380 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
381 $name, $status, $about[0], $about[2], $about[1]);
384 echo "Plugins marked by * are currently enabled for all users.\n";
388 PluginHost
::getInstance()->run_commands($options);
390 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
391 unlink(LOCK_DIRECTORY
. "/$lock_filename");