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",
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 " --help - show this help\n";
95 print "Plugin options:\n";
97 foreach (PluginHost
::getInstance()->get_commands() as $command => $data) {
98 $args = $data['arghelp'];
99 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
105 if (!isset($options['daemon'])) {
106 require_once "errorhandler.php";
109 if (!isset($options['update-schema'])) {
110 $schema_version = get_schema_version();
112 if ($schema_version != SCHEMA_VERSION
) {
113 die("Schema version is wrong, please upgrade the database.\n");
117 define('QUIET', isset($options['quiet']));
119 if (isset($options["log"])) {
120 _debug("Logging to " . $options["log"]);
121 define('LOGFILE', $options["log"]);
124 if (!isset($options["daemon"])) {
125 $lock_filename = "update.lock";
127 $lock_filename = "update_daemon.lock";
130 if (isset($options["task"])) {
131 _debug("Using task id " . $options["task"]);
132 $lock_filename = $lock_filename . "-task_" . $options["task"];
135 if (isset($options["pidlock"])) {
136 $my_pid = $options["pidlock"];
137 $lock_filename = "update_daemon-$my_pid.lock";
141 _debug("Lock: $lock_filename");
143 $lock_handle = make_lockfile($lock_filename);
146 if (isset($options["task"]) && isset($options["pidlock"])) {
147 $waits = $options["task"] * 5;
148 _debug("Waiting before update ($waits)");
152 // Try to lock a file in order to avoid concurrent update.
154 die("error: Can't create lockfile ($lock_filename). ".
155 "Maybe another update process is already running.\n");
158 if (isset($options["force-update"])) {
159 _debug("marking all feeds as needing update...");
161 db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
162 last_updated = '1970-01-01'");
165 if (isset($options["feeds"])) {
166 update_daemon_common();
167 housekeeping_common(true);
169 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
172 if (isset($options["feedbrowser"])) {
173 $count = update_feedbrowser_cache();
174 print "Finished, $count feeds processed.\n";
177 if (isset($options["daemon"])) {
179 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
180 $log = isset($options['log']) ?
'--log '.$options['log'] : '';
182 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet $log");
183 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
184 sleep(DAEMON_SLEEP_INTERVAL
);
188 if (isset($options["daemon-loop"])) {
189 if (!make_stampfile('update_daemon.stamp')) {
190 _debug("warning: unable to create stampfile\n");
193 update_daemon_common(isset($options["pidlock"]) ?
50 : DAEMON_FEED_LIMIT
);
195 if (!isset($options["pidlock"]) ||
$options["task"] == 0)
196 housekeeping_common(true);
198 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_UPDATE_TASK
, "hook_update_task", $op);
201 if (isset($options["cleanup-tags"])) {
202 $rc = cleanup_tags( 14, 50000);
203 _debug("$rc tags deleted.\n");
206 if (isset($options["indexes"])) {
207 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
208 _debug("Type 'yes' to continue.");
210 if (read_stdin() != 'yes')
213 _debug("clearing existing indexes...");
215 if (DB_TYPE
== "pgsql") {
216 $result = db_query( "SELECT relname FROM
217 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
218 AND relname NOT LIKE '%_pkey'
221 $result = db_query( "SELECT index_name,table_name FROM
222 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
225 while ($line = db_fetch_assoc($result)) {
226 if (DB_TYPE
== "pgsql") {
227 $statement = "DROP INDEX " . $line["relname"];
230 $statement = "ALTER TABLE ".
231 $line['table_name']." DROP INDEX ".$line['index_name'];
234 db_query( $statement, false);
237 _debug("reading indexes from schema for: " . DB_TYPE
);
239 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
241 while ($line = fgets($fp)) {
244 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
245 $index = $matches[1];
246 $table = $matches[2];
248 $statement = "CREATE INDEX $index ON $table";
251 db_query( $statement);
256 _debug("unable to open schema file.");
261 if (isset($options["convert-filters"])) {
262 _debug("WARNING: this will remove all existing type2 filters.");
263 _debug("Type 'yes' to continue.");
265 if (read_stdin() != 'yes')
268 _debug("converting filters...");
270 db_query( "DELETE FROM ttrss_filters2");
272 $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id");
274 while ($line = db_fetch_assoc($result)) {
275 $owner_uid = $line["owner_uid"];
277 // date filters are removed
278 if ($line["filter_type"] != 5) {
281 if (sql_bool_to_bool($line["cat_filter"])) {
282 $feed_id = "CAT:" . (int)$line["cat_id"];
284 $feed_id = (int)$line["feed_id"];
287 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
288 $filter["rule"] = array(
290 "reg_exp" => $line["reg_exp"],
291 "feed_id" => $feed_id,
292 "filter_type" => $line["filter_type"])));
294 $filter["action"] = array(
296 "action_id" => $line["action_id"],
297 "action_param_label" => $line["action_param"],
298 "action_param" => $line["action_param"])));
300 // Oh god it's full of hacks
303 $_SESSION["uid"] = $owner_uid;
305 $filters = new Pref_Filters($_REQUEST);
312 if (isset($options["update-schema"])) {
313 _debug("checking for updates (" . DB_TYPE
. ")...");
315 $updater = new DbUpdater(Db
::get(), DB_TYPE
, SCHEMA_VERSION
);
317 if ($updater->isUpdateRequired()) {
318 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
319 _debug("WARNING: please backup your database before continuing.");
320 _debug("Type 'yes' to continue.");
322 if (read_stdin() != 'yes')
325 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
326 _debug("performing update up to version $i...");
328 $result = $updater->performUpdateTo($i);
330 _debug($result ?
"OK!" : "FAILED!");
332 if (!$result) return;
336 _debug("update not required.");
341 if (isset($options["gen-search-idx"])) {
342 echo "Generating search index (stemming set to English)...\n";
344 $result = db_query("SELECT COUNT(id) AS count FROM ttrss_entries WHERE tsvector_combined IS NULL");
345 $count = db_fetch_result($result, 0, "count");
347 print "Articles to process: $count.\n";
353 $result = db_query("SELECT id, title, content FROM ttrss_entries WHERE tsvector_combined IS NULL ORDER BY id LIMIT $limit");
355 while ($line = db_fetch_assoc($result)) {
356 $tsvector_combined = db_escape_string(mb_substr($line['title'] . ' ' . strip_tags(str_replace('<', ' <', $line['content'])),
359 db_query("UPDATE ttrss_entries SET tsvector_combined = to_tsvector('english', '$tsvector_combined') WHERE id = " . $line["id"]);
362 $processed +
= db_num_rows($result);
363 print "Processed $processed articles...\n";
365 if (db_num_rows($result) != $limit) {
372 if (isset($options["list-plugins"])) {
373 $tmppluginhost = new PluginHost();
374 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
, false);
375 $enabled = array_map("trim", explode(",", PLUGINS
));
377 echo "List of all available plugins:\n";
379 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
380 $about = $plugin->about();
382 $status = $about[3] ?
"system" : "user";
384 if (in_array($name, $enabled)) $name .= "*";
386 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
387 $name, $status, $about[0], $about[2], $about[1]);
390 echo "Plugins marked by * are currently enabled for all users.\n";
394 if (isset($options["debug-feed"])) {
395 $feed = $options["debug-feed"];
397 if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true;
398 if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true;
400 $_REQUEST['xdebug'] = 1;
402 update_rss_feed($feed);
405 PluginHost
::getInstance()->run_commands($options);
407 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
408 unlink(LOCK_DIRECTORY
. "/$lock_filename");