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";
16 require_once "errorhandler.php";
18 if (!defined('PHP_EXECUTABLE'))
19 define('PHP_EXECUTABLE', '/usr/bin/php');
21 // Create a database connection.
22 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
24 init_connection($link);
26 $longopts = array("feeds",
41 foreach ($pluginhost->get_commands() as $command => $data) {
42 array_push($longopts, $command . $data["suffix"]);
45 $options = getopt("", $longopts);
47 if (count($options) == 0 && !defined('STDIN')) {
50 <title
>Tiny Tiny RSS data update script
.</title
>
51 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
52 <link rel
="stylesheet" type
="text/css" href
="utility.css">
56 <div
class="floatingLogo"><img src
="images/logo_small.png"></div
>
57 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
59 <?php
print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?
>
66 if (count($options) == 0 ||
isset($options["help"]) ) {
67 print "Tiny Tiny RSS data update script.\n\n";
69 print " --feeds - update feeds\n";
70 print " --feedbrowser - update feedbrowser\n";
71 print " --daemon - start single-process update daemon\n";
72 print " --task N - create lockfile using this task id\n";
73 print " --cleanup-tags - perform tags table maintenance\n";
74 print " --quiet - don't output messages to stdout\n";
75 print " --log FILE - log messages to FILE\n";
76 print " --indexes - recreate missing schema indexes\n";
77 print " --update-schema - update database schema\n";
78 print " --convert-filters - convert type1 filters to type2\n";
79 print " --force-update - force update of all feeds\n";
80 print " --list-plugins - list all available plugins\n";
81 print " --help - show this help\n";
82 print "Plugin options:\n";
84 foreach ($pluginhost->get_commands() as $command => $data) {
85 $args = $data['arghelp'];
86 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
92 if (!isset($options['update-schema'])) {
93 $schema_version = get_schema_version($link);
95 if ($schema_version != SCHEMA_VERSION
) {
96 die("Schema version is wrong, please upgrade the database.\n");
100 define('QUIET', isset($options['quiet']));
102 if (isset($options["log"])) {
103 _debug("Logging to " . $options["log"]);
104 define('LOGFILE', $options["log"]);
107 if (!isset($options["daemon"])) {
108 $lock_filename = "update.lock";
110 $lock_filename = "update_daemon.lock";
113 if (isset($options["task"])) {
114 _debug("Using task id " . $options["task"]);
115 $lock_filename = $lock_filename . "-task_" . $options["task"];
118 $lock_handle = make_lockfile($lock_filename);
121 // Try to lock a file in order to avoid concurrent update.
123 die("error: Can't create lockfile ($lock_filename). ".
124 "Maybe another update process is already running.\n");
127 if (isset($options["force-update"])) {
128 _debug("marking all feeds as needing update...");
130 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
131 last_updated = '1970-01-01'");
134 if (isset($options["feeds"])) {
135 // Update all feeds needing a update.
136 update_daemon_common($link);
138 // Update feedbrowser
139 $count = update_feedbrowser_cache($link);
140 _debug("Feedbrowser updated, $count feeds processed.");
142 // Purge orphans and cleanup tags
143 purge_orphans($link, true);
145 $rc = cleanup_tags($link, 14, 50000);
146 _debug("Cleaned $rc cached tags.");
149 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
152 if (isset($options["feedbrowser"])) {
153 $count = update_feedbrowser_cache($link);
154 print "Finished, $count feeds processed.\n";
157 if (isset($options["daemon"])) {
159 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
161 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet");
162 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
163 sleep(DAEMON_SLEEP_INTERVAL
);
167 if (isset($options["daemon-loop"])) {
168 if (!make_stampfile('update_daemon.stamp')) {
169 _debug("warning: unable to create stampfile\n");
172 // Call to the feed batch update function
173 // or regenerate feedbrowser cache
175 if (rand(0,100) > 30) {
176 update_daemon_common($link);
178 $count = update_feedbrowser_cache($link);
179 _debug("Feedbrowser updated, $count feeds processed.");
181 purge_orphans($link, true);
183 $rc = cleanup_tags($link, 14, 50000);
185 _debug("Cleaned $rc cached tags.");
188 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
193 if (isset($options["cleanup-tags"])) {
194 $rc = cleanup_tags($link, 14, 50000);
195 _debug("$rc tags deleted.\n");
198 if (isset($options["indexes"])) {
199 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
200 _debug("Type 'yes' to continue.");
202 if (read_stdin() != 'yes')
205 _debug("clearing existing indexes...");
207 if (DB_TYPE
== "pgsql") {
208 $result = db_query($link, "SELECT relname FROM
209 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
210 AND relname NOT LIKE '%_pkey'
213 $result = db_query($link, "SELECT index_name,table_name FROM
214 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
217 while ($line = db_fetch_assoc($result)) {
218 if (DB_TYPE
== "pgsql") {
219 $statement = "DROP INDEX " . $line["relname"];
222 $statement = "ALTER TABLE ".
223 $line['table_name']." DROP INDEX ".$line['index_name'];
226 db_query($link, $statement, false);
229 _debug("reading indexes from schema for: " . DB_TYPE
);
231 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
233 while ($line = fgets($fp)) {
236 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
237 $index = $matches[1];
238 $table = $matches[2];
240 $statement = "CREATE INDEX $index ON $table";
243 db_query($link, $statement);
248 _debug("unable to open schema file.");
253 if (isset($options["convert-filters"])) {
254 _debug("WARNING: this will remove all existing type2 filters.");
255 _debug("Type 'yes' to continue.");
257 if (read_stdin() != 'yes')
260 _debug("converting filters...");
262 db_query($link, "DELETE FROM ttrss_filters2");
264 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
266 while ($line = db_fetch_assoc($result)) {
267 $owner_uid = $line["owner_uid"];
269 // date filters are removed
270 if ($line["filter_type"] != 5) {
273 if (sql_bool_to_bool($line["cat_filter"])) {
274 $feed_id = "CAT:" . (int)$line["cat_id"];
276 $feed_id = (int)$line["feed_id"];
279 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
280 $filter["rule"] = array(
282 "reg_exp" => $line["reg_exp"],
283 "feed_id" => $feed_id,
284 "filter_type" => $line["filter_type"])));
286 $filter["action"] = array(
288 "action_id" => $line["action_id"],
289 "action_param_label" => $line["action_param"],
290 "action_param" => $line["action_param"])));
292 // Oh god it's full of hacks
295 $_SESSION["uid"] = $owner_uid;
297 $filters = new Pref_Filters($link, $_REQUEST);
304 if (isset($options["update-schema"])) {
305 _debug("checking for updates (" . DB_TYPE
. ")...");
307 $updater = new DbUpdater($link, DB_TYPE
, SCHEMA_VERSION
);
309 if ($updater->isUpdateRequired()) {
310 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
311 _debug("WARNING: please backup your database before continuing.");
312 _debug("Type 'yes' to continue.");
314 if (read_stdin() != 'yes')
317 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
318 _debug("performing update up to version $i...");
320 $result = $updater->performUpdateTo($i);
322 _debug($result ?
"OK!" : "FAILED!");
324 if (!$result) return;
328 _debug("update not required.");
333 if (isset($options["list-plugins"])) {
334 $tmppluginhost = new PluginHost($link);
335 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
);
336 $enabled = array_map("trim", explode(",", PLUGINS
));
338 echo "List of all available plugins:\n";
340 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
341 $about = $plugin->about();
343 $status = $about[3] ?
"system" : "user";
345 if (in_array($name, $enabled)) $name .= "*";
347 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
348 $name, $status, $about[0], $about[2], $about[1]);
351 echo "Plugins marked by * are currently enabled for all users.\n";
355 $pluginhost->run_commands($options);
359 if ($lock_handle != false) {
360 fclose($lock_handle);
363 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
364 unlink(LOCK_DIRECTORY
. "/$lock_filename");