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');
24 $longopts = array("feeds",
40 foreach ($pluginhost->get_commands() as $command => $data) {
41 array_push($longopts, $command . $data["suffix"]);
44 $options = getopt("", $longopts);
46 if (count($options) == 0 && !defined('STDIN')) {
49 <title
>Tiny Tiny RSS data update script
.</title
>
50 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
51 <link rel
="stylesheet" type
="text/css" href
="utility.css">
55 <div
class="floatingLogo"><img src
="images/logo_small.png"></div
>
56 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
58 <?php
print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?
>
65 if (count($options) == 0 ||
isset($options["help"]) ) {
66 print "Tiny Tiny RSS data update script.\n\n";
68 print " --feeds - update feeds\n";
69 print " --feedbrowser - update feedbrowser\n";
70 print " --daemon - start single-process update daemon\n";
71 print " --task N - create lockfile using this task id\n";
72 print " --cleanup-tags - perform tags table maintenance\n";
73 print " --quiet - don't output messages to stdout\n";
74 print " --log FILE - log messages to FILE\n";
75 print " --indexes - recreate missing schema indexes\n";
76 print " --update-schema - update database schema\n";
77 print " --convert-filters - convert type1 filters to type2\n";
78 print " --force-update - force update of all feeds\n";
79 print " --list-plugins - list all available plugins\n";
80 print " --help - show this help\n";
81 print "Plugin options:\n";
83 foreach ($pluginhost->get_commands() as $command => $data) {
84 $args = $data['arghelp'];
85 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
91 if (!isset($options['update-schema'])) {
92 $schema_version = get_schema_version();
94 if ($schema_version != SCHEMA_VERSION
) {
95 die("Schema version is wrong, please upgrade the database.\n");
99 define('QUIET', isset($options['quiet']));
101 if (isset($options["log"])) {
102 _debug("Logging to " . $options["log"]);
103 define('LOGFILE', $options["log"]);
106 if (!isset($options["daemon"])) {
107 $lock_filename = "update.lock";
109 $lock_filename = "update_daemon.lock";
112 if (isset($options["task"])) {
113 _debug("Using task id " . $options["task"]);
114 $lock_filename = $lock_filename . "-task_" . $options["task"];
117 if (isset($options["pidlock"])) {
118 $my_pid = $options["pidlock"];
119 $lock_filename = "update_daemon-$my_pid.lock";
123 _debug("Lock: $lock_filename");
125 $lock_handle = make_lockfile($lock_filename);
128 if (isset($options["task"])) {
129 $waits = $options["task"] * 5;
130 _debug("Waiting before update ($waits)");
134 // Try to lock a file in order to avoid concurrent update.
136 die("error: Can't create lockfile ($lock_filename). ".
137 "Maybe another update process is already running.\n");
140 if (isset($options["force-update"])) {
141 _debug("marking all feeds as needing update...");
143 db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
144 last_updated = '1970-01-01'");
147 if (isset($options["feeds"])) {
148 // Update all feeds needing a update.
149 update_daemon_common();
151 // Update feedbrowser
152 $count = update_feedbrowser_cache();
153 _debug("Feedbrowser updated, $count feeds processed.");
155 // Purge orphans and cleanup tags
156 purge_orphans( true);
158 $rc = cleanup_tags( 14, 50000);
159 _debug("Cleaned $rc cached tags.");
162 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
165 if (isset($options["feedbrowser"])) {
166 $count = update_feedbrowser_cache();
167 print "Finished, $count feeds processed.\n";
170 if (isset($options["daemon"])) {
172 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
174 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet");
175 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
176 sleep(DAEMON_SLEEP_INTERVAL
);
180 if (isset($options["daemon-loop"])) {
181 if (!make_stampfile('update_daemon.stamp')) {
182 _debug("warning: unable to create stampfile\n");
185 // Call to the feed batch update function
186 // or regenerate feedbrowser cache
188 if (rand(0,100) > 30) {
189 update_daemon_common();
191 $count = update_feedbrowser_cache();
192 _debug("Feedbrowser updated, $count feeds processed.");
194 purge_orphans( true);
196 $rc = cleanup_tags( 14, 50000);
198 _debug("Cleaned $rc cached tags.");
201 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
206 if (isset($options["cleanup-tags"])) {
207 $rc = cleanup_tags( 14, 50000);
208 _debug("$rc tags deleted.\n");
211 if (isset($options["indexes"])) {
212 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
213 _debug("Type 'yes' to continue.");
215 if (read_stdin() != 'yes')
218 _debug("clearing existing indexes...");
220 if (DB_TYPE
== "pgsql") {
221 $result = db_query( "SELECT relname FROM
222 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
223 AND relname NOT LIKE '%_pkey'
226 $result = db_query( "SELECT index_name,table_name FROM
227 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
230 while ($line = db_fetch_assoc($result)) {
231 if (DB_TYPE
== "pgsql") {
232 $statement = "DROP INDEX " . $line["relname"];
235 $statement = "ALTER TABLE ".
236 $line['table_name']." DROP INDEX ".$line['index_name'];
239 db_query( $statement, false);
242 _debug("reading indexes from schema for: " . DB_TYPE
);
244 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
246 while ($line = fgets($fp)) {
249 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
250 $index = $matches[1];
251 $table = $matches[2];
253 $statement = "CREATE INDEX $index ON $table";
256 db_query( $statement);
261 _debug("unable to open schema file.");
266 if (isset($options["convert-filters"])) {
267 _debug("WARNING: this will remove all existing type2 filters.");
268 _debug("Type 'yes' to continue.");
270 if (read_stdin() != 'yes')
273 _debug("converting filters...");
275 db_query( "DELETE FROM ttrss_filters2");
277 $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id");
279 while ($line = db_fetch_assoc($result)) {
280 $owner_uid = $line["owner_uid"];
282 // date filters are removed
283 if ($line["filter_type"] != 5) {
286 if (sql_bool_to_bool($line["cat_filter"])) {
287 $feed_id = "CAT:" . (int)$line["cat_id"];
289 $feed_id = (int)$line["feed_id"];
292 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
293 $filter["rule"] = array(
295 "reg_exp" => $line["reg_exp"],
296 "feed_id" => $feed_id,
297 "filter_type" => $line["filter_type"])));
299 $filter["action"] = array(
301 "action_id" => $line["action_id"],
302 "action_param_label" => $line["action_param"],
303 "action_param" => $line["action_param"])));
305 // Oh god it's full of hacks
308 $_SESSION["uid"] = $owner_uid;
310 $filters = new Pref_Filters( $_REQUEST);
317 if (isset($options["update-schema"])) {
318 _debug("checking for updates (" . DB_TYPE
. ")...");
320 $updater = new DbUpdater( DB_TYPE
, SCHEMA_VERSION
);
322 if ($updater->isUpdateRequired()) {
323 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
324 _debug("WARNING: please backup your database before continuing.");
325 _debug("Type 'yes' to continue.");
327 if (read_stdin() != 'yes')
330 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
331 _debug("performing update up to version $i...");
333 $result = $updater->performUpdateTo($i);
335 _debug($result ?
"OK!" : "FAILED!");
337 if (!$result) return;
341 _debug("update not required.");
346 if (isset($options["list-plugins"])) {
347 $tmppluginhost = new PluginHost();
348 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
);
349 $enabled = array_map("trim", explode(",", PLUGINS
));
351 echo "List of all available plugins:\n";
353 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
354 $about = $plugin->about();
356 $status = $about[3] ?
"system" : "user";
358 if (in_array($name, $enabled)) $name .= "*";
360 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
361 $name, $status, $about[0], $about[2], $about[1]);
364 echo "Plugins marked by * are currently enabled for all users.\n";
368 $pluginhost->run_commands($options);
370 if ($lock_handle != false) {
371 fclose($lock_handle);
374 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
375 unlink(LOCK_DIRECTORY
. "/$lock_filename");