3 set_include_path(dirname(__FILE__
) ."/include" . PATH_SEPARATOR
.
6 define('DISABLE_SESSIONS', true);
8 chdir(dirname(__FILE__
));
10 require_once "functions.php";
11 require_once "rssfuncs.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');
20 // Create a database connection.
21 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
23 init_connection($link);
25 $longopts = array("feeds",
39 foreach ($pluginhost->get_commands() as $command => $data) {
40 array_push($longopts, $command . $data["suffix"]);
43 $options = getopt("", $longopts);
45 if (count($options) == 0 && !defined('STDIN')) {
48 <title
>Tiny Tiny RSS data update script
.</title
>
49 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
50 <link rel
="stylesheet" type
="text/css" href
="utility.css">
54 <div
class="floatingLogo"><img src
="images/logo_small.png"></div
>
55 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
57 <?php
print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?
>
64 if (count($options) == 0 ||
isset($options["help"]) ) {
65 print "Tiny Tiny RSS data update script.\n\n";
67 print " --feeds - update feeds\n";
68 print " --feedbrowser - update feedbrowser\n";
69 print " --daemon - start single-process update daemon\n";
70 print " --task N - create lockfile using this task id\n";
71 print " --cleanup-tags - perform tags table maintenance\n";
72 print " --quiet - don't output messages to stdout\n";
73 print " --log FILE - log messages to FILE\n";
74 print " --indexes - recreate missing schema indexes\n";
75 print " --convert-filters - convert type1 filters to type2\n";
76 print " --force-update - force update of all feeds\n";
77 print " --list-plugins - list all available plugins\n";
78 print " --help - show this help\n";
79 print "Plugin options:\n";
81 foreach ($pluginhost->get_commands() as $command => $data) {
82 $args = $data['arghelp'];
83 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
89 define('QUIET', isset($options['quiet']));
91 if (isset($options["log"])) {
92 _debug("Logging to " . $options["log"]);
93 define('LOGFILE', $options["log"]);
96 if (!isset($options["daemon"])) {
97 $lock_filename = "update.lock";
99 $lock_filename = "update_daemon.lock";
102 if (isset($options["task"])) {
103 _debug("Using task id " . $options["task"]);
104 $lock_filename = $lock_filename . "-task_" . $options["task"];
107 $lock_handle = make_lockfile($lock_filename);
110 // Try to lock a file in order to avoid concurrent update.
112 die("error: Can't create lockfile ($lock_filename). ".
113 "Maybe another update process is already running.\n");
116 if (isset($options["force-update"])) {
117 _debug("marking all feeds as needing update...");
119 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
120 last_updated = '1970-01-01'");
123 if (isset($options["feeds"])) {
124 // Update all feeds needing a update.
125 update_daemon_common($link);
127 // Update feedbrowser
128 $count = update_feedbrowser_cache($link);
129 _debug("Feedbrowser updated, $count feeds processed.");
131 // Purge orphans and cleanup tags
132 purge_orphans($link, true);
134 $rc = cleanup_tags($link, 14, 50000);
135 _debug("Cleaned $rc cached tags.");
138 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
141 if (isset($options["feedbrowser"])) {
142 $count = update_feedbrowser_cache($link);
143 print "Finished, $count feeds processed.\n";
146 if (isset($options["daemon"])) {
148 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
150 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet");
151 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
152 sleep(DAEMON_SLEEP_INTERVAL
);
156 if (isset($options["daemon-loop"])) {
157 if (!make_stampfile('update_daemon.stamp')) {
158 _debug("warning: unable to create stampfile\n");
161 // Call to the feed batch update function
162 // or regenerate feedbrowser cache
164 if (rand(0,100) > 30) {
165 update_daemon_common($link);
167 $count = update_feedbrowser_cache($link);
168 _debug("Feedbrowser updated, $count feeds processed.");
170 purge_orphans($link, true);
172 $rc = cleanup_tags($link, 14, 50000);
174 _debug("Cleaned $rc cached tags.");
177 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
182 if (isset($options["cleanup-tags"])) {
183 $rc = cleanup_tags($link, 14, 50000);
184 _debug("$rc tags deleted.\n");
187 if (isset($options["indexes"])) {
188 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
189 _debug("Type 'yes' to continue.");
191 if (read_stdin() != 'yes')
194 _debug("clearing existing indexes...");
196 if (DB_TYPE
== "pgsql") {
197 $result = db_query($link, "SELECT relname FROM
198 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
199 AND relname NOT LIKE '%_pkey'
202 $result = db_query($link, "SELECT index_name,table_name FROM
203 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
206 while ($line = db_fetch_assoc($result)) {
207 if (DB_TYPE
== "pgsql") {
208 $statement = "DROP INDEX " . $line["relname"];
211 $statement = "ALTER TABLE ".
212 $line['table_name']." DROP INDEX ".$line['index_name'];
215 db_query($link, $statement, false);
218 _debug("reading indexes from schema for: " . DB_TYPE
);
220 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
222 while ($line = fgets($fp)) {
225 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
226 $index = $matches[1];
227 $table = $matches[2];
229 $statement = "CREATE INDEX $index ON $table";
232 db_query($link, $statement);
237 _debug("unable to open schema file.");
242 if (isset($options["convert-filters"])) {
243 _debug("WARNING: this will remove all existing type2 filters.");
244 _debug("Type 'yes' to continue.");
246 if (read_stdin() != 'yes')
249 _debug("converting filters...");
251 db_query($link, "DELETE FROM ttrss_filters2");
253 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
255 while ($line = db_fetch_assoc($result)) {
256 $owner_uid = $line["owner_uid"];
258 // date filters are removed
259 if ($line["filter_type"] != 5) {
262 if (sql_bool_to_bool($line["cat_filter"])) {
263 $feed_id = "CAT:" . (int)$line["cat_id"];
265 $feed_id = (int)$line["feed_id"];
268 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
269 $filter["rule"] = array(
271 "reg_exp" => $line["reg_exp"],
272 "feed_id" => $feed_id,
273 "filter_type" => $line["filter_type"])));
275 $filter["action"] = array(
277 "action_id" => $line["action_id"],
278 "action_param_label" => $line["action_param"],
279 "action_param" => $line["action_param"])));
281 // Oh god it's full of hacks
284 $_SESSION["uid"] = $owner_uid;
286 $filters = new Pref_Filters($link, $_REQUEST);
293 if (isset($options["list-plugins"])) {
294 $tmppluginhost = new PluginHost($link);
295 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
);
296 $enabled = array_map("trim", explode(",", PLUGINS
));
298 echo "List of all available plugins:\n";
300 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
301 $about = $plugin->about();
303 $status = $about[3] ?
"system" : "user";
305 if (in_array($name, $enabled)) $name .= "*";
307 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
308 $name, $status, $about[0], $about[2], $about[1]);
311 echo "Plugins marked by * are currently enabled for all users.\n";
315 $pluginhost->run_commands($options);
319 if ($lock_handle != false) {
320 fclose($lock_handle);
323 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
324 unlink(LOCK_DIRECTORY
. "/$lock_filename");