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";
17 require_once "errorhandler.php";
19 if (!defined('PHP_EXECUTABLE'))
20 define('PHP_EXECUTABLE', '/usr/bin/php');
22 // Create a database connection.
23 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
27 $longopts = array("feeds",
42 foreach ($pluginhost->get_commands() as $command => $data) {
43 array_push($longopts, $command . $data["suffix"]);
46 $options = getopt("", $longopts);
48 if (count($options) == 0 && !defined('STDIN')) {
51 <title
>Tiny Tiny RSS data update script
.</title
>
52 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
53 <link rel
="stylesheet" type
="text/css" href
="utility.css">
57 <div
class="floatingLogo"><img src
="images/logo_small.png"></div
>
58 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
60 <?php
print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?
>
67 if (count($options) == 0 ||
isset($options["help"]) ) {
68 print "Tiny Tiny RSS data update script.\n\n";
70 print " --feeds - update feeds\n";
71 print " --feedbrowser - update feedbrowser\n";
72 print " --daemon - start single-process update daemon\n";
73 print " --task N - create lockfile using this task id\n";
74 print " --cleanup-tags - perform tags table maintenance\n";
75 print " --quiet - don't output messages to stdout\n";
76 print " --log FILE - log messages to FILE\n";
77 print " --indexes - recreate missing schema indexes\n";
78 print " --update-schema - update database schema\n";
79 print " --convert-filters - convert type1 filters to type2\n";
80 print " --force-update - force update of all feeds\n";
81 print " --list-plugins - list all available plugins\n";
82 print " --help - show this help\n";
83 print "Plugin options:\n";
85 foreach ($pluginhost->get_commands() as $command => $data) {
86 $args = $data['arghelp'];
87 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
93 if (!isset($options['update-schema'])) {
94 $schema_version = get_schema_version($link);
96 if ($schema_version != SCHEMA_VERSION
) {
97 die("Schema version is wrong, please upgrade the database.\n");
101 define('QUIET', isset($options['quiet']));
103 if (isset($options["log"])) {
104 _debug("Logging to " . $options["log"]);
105 define('LOGFILE', $options["log"]);
108 if (!isset($options["daemon"])) {
109 $lock_filename = "update.lock";
111 $lock_filename = "update_daemon.lock";
114 if (isset($options["task"])) {
115 _debug("Using task id " . $options["task"]);
116 $lock_filename = $lock_filename . "-task_" . $options["task"];
119 $lock_handle = make_lockfile($lock_filename);
122 // Try to lock a file in order to avoid concurrent update.
124 die("error: Can't create lockfile ($lock_filename). ".
125 "Maybe another update process is already running.\n");
128 if (isset($options["force-update"])) {
129 _debug("marking all feeds as needing update...");
131 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
132 last_updated = '1970-01-01'");
135 if (isset($options["feeds"])) {
136 // Update all feeds needing a update.
137 update_daemon_common($link);
139 // Update feedbrowser
140 $count = update_feedbrowser_cache($link);
141 _debug("Feedbrowser updated, $count feeds processed.");
143 // Purge orphans and cleanup tags
144 purge_orphans($link, true);
146 $rc = cleanup_tags($link, 14, 50000);
147 _debug("Cleaned $rc cached tags.");
150 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
153 if (isset($options["feedbrowser"])) {
154 $count = update_feedbrowser_cache($link);
155 print "Finished, $count feeds processed.\n";
158 if (isset($options["daemon"])) {
160 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
162 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet");
163 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
164 sleep(DAEMON_SLEEP_INTERVAL
);
168 if (isset($options["daemon-loop"])) {
169 if (!make_stampfile('update_daemon.stamp')) {
170 _debug("warning: unable to create stampfile\n");
173 // Call to the feed batch update function
174 // or regenerate feedbrowser cache
176 if (rand(0,100) > 30) {
177 update_daemon_common($link);
179 $count = update_feedbrowser_cache($link);
180 _debug("Feedbrowser updated, $count feeds processed.");
182 purge_orphans($link, true);
184 $rc = cleanup_tags($link, 14, 50000);
186 _debug("Cleaned $rc cached tags.");
189 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
194 if (isset($options["cleanup-tags"])) {
195 $rc = cleanup_tags($link, 14, 50000);
196 _debug("$rc tags deleted.\n");
199 if (isset($options["indexes"])) {
200 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
201 _debug("Type 'yes' to continue.");
203 if (read_stdin() != 'yes')
206 _debug("clearing existing indexes...");
208 if (DB_TYPE
== "pgsql") {
209 $result = db_query($link, "SELECT relname FROM
210 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
211 AND relname NOT LIKE '%_pkey'
214 $result = db_query($link, "SELECT index_name,table_name FROM
215 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
218 while ($line = db_fetch_assoc($result)) {
219 if (DB_TYPE
== "pgsql") {
220 $statement = "DROP INDEX " . $line["relname"];
223 $statement = "ALTER TABLE ".
224 $line['table_name']." DROP INDEX ".$line['index_name'];
227 db_query($link, $statement, false);
230 _debug("reading indexes from schema for: " . DB_TYPE
);
232 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
234 while ($line = fgets($fp)) {
237 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
238 $index = $matches[1];
239 $table = $matches[2];
241 $statement = "CREATE INDEX $index ON $table";
244 db_query($link, $statement);
249 _debug("unable to open schema file.");
254 if (isset($options["convert-filters"])) {
255 _debug("WARNING: this will remove all existing type2 filters.");
256 _debug("Type 'yes' to continue.");
258 if (read_stdin() != 'yes')
261 _debug("converting filters...");
263 db_query($link, "DELETE FROM ttrss_filters2");
265 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
267 while ($line = db_fetch_assoc($result)) {
268 $owner_uid = $line["owner_uid"];
270 // date filters are removed
271 if ($line["filter_type"] != 5) {
274 if (sql_bool_to_bool($line["cat_filter"])) {
275 $feed_id = "CAT:" . (int)$line["cat_id"];
277 $feed_id = (int)$line["feed_id"];
280 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
281 $filter["rule"] = array(
283 "reg_exp" => $line["reg_exp"],
284 "feed_id" => $feed_id,
285 "filter_type" => $line["filter_type"])));
287 $filter["action"] = array(
289 "action_id" => $line["action_id"],
290 "action_param_label" => $line["action_param"],
291 "action_param" => $line["action_param"])));
293 // Oh god it's full of hacks
296 $_SESSION["uid"] = $owner_uid;
298 $filters = new Pref_Filters($link, $_REQUEST);
305 if (isset($options["update-schema"])) {
306 _debug("checking for updates (" . DB_TYPE
. ")...");
308 $updater = new DbUpdater($link, DB_TYPE
, SCHEMA_VERSION
);
310 if ($updater->isUpdateRequired()) {
311 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
312 _debug("WARNING: please backup your database before continuing.");
313 _debug("Type 'yes' to continue.");
315 if (read_stdin() != 'yes')
318 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
319 _debug("performing update up to version $i...");
321 $result = $updater->performUpdateTo($i);
323 _debug($result ?
"OK!" : "FAILED!");
325 if (!$result) return;
329 _debug("update not required.");
334 if (isset($options["list-plugins"])) {
335 $tmppluginhost = new PluginHost($link);
336 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
);
337 $enabled = array_map("trim", explode(",", PLUGINS
));
339 echo "List of all available plugins:\n";
341 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
342 $about = $plugin->about();
344 $status = $about[3] ?
"system" : "user";
346 if (in_array($name, $enabled)) $name .= "*";
348 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
349 $name, $status, $about[0], $about[2], $about[1]);
352 echo "Plugins marked by * are currently enabled for all users.\n";
356 $pluginhost->run_commands($options);
360 if ($lock_handle != false) {
361 fclose($lock_handle);
364 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
365 unlink(LOCK_DIRECTORY
. "/$lock_filename");