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",
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 " --update-schema - update database schema\n";
76 print " --convert-filters - convert type1 filters to type2\n";
77 print " --force-update - force update of all feeds\n";
78 print " --list-plugins - list all available plugins\n";
79 print " --help - show this help\n";
80 print "Plugin options:\n";
82 foreach ($pluginhost->get_commands() as $command => $data) {
83 $args = $data['arghelp'];
84 printf(" --%-19s - %s\n", "$command $args", $data["description"]);
90 if (!isset($options['daemon'])) {
91 require_once "errorhandler.php";
94 if (!isset($options['update-schema'])) {
95 $schema_version = get_schema_version();
97 if ($schema_version != SCHEMA_VERSION
) {
98 die("Schema version is wrong, please upgrade the database.\n");
102 define('QUIET', isset($options['quiet']));
104 if (isset($options["log"])) {
105 _debug("Logging to " . $options["log"]);
106 define('LOGFILE', $options["log"]);
109 if (!isset($options["daemon"])) {
110 $lock_filename = "update.lock";
112 $lock_filename = "update_daemon.lock";
115 if (isset($options["task"])) {
116 _debug("Using task id " . $options["task"]);
117 $lock_filename = $lock_filename . "-task_" . $options["task"];
120 if (isset($options["pidlock"])) {
121 $my_pid = $options["pidlock"];
122 $lock_filename = "update_daemon-$my_pid.lock";
126 _debug("Lock: $lock_filename");
128 $lock_handle = make_lockfile($lock_filename);
131 if (isset($options["task"])) {
132 $waits = $options["task"] * 5;
133 _debug("Waiting before update ($waits)");
137 // Try to lock a file in order to avoid concurrent update.
139 die("error: Can't create lockfile ($lock_filename). ".
140 "Maybe another update process is already running.\n");
143 if (isset($options["force-update"])) {
144 _debug("marking all feeds as needing update...");
146 db_query( "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
147 last_updated = '1970-01-01'");
150 if (isset($options["feeds"])) {
151 // Update all feeds needing a update.
152 update_daemon_common();
154 // Update feedbrowser
155 $count = update_feedbrowser_cache();
156 _debug("Feedbrowser updated, $count feeds processed.");
158 // Purge orphans and cleanup tags
159 purge_orphans( true);
161 $rc = cleanup_tags( 14, 50000);
162 _debug("Cleaned $rc cached tags.");
165 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
168 if (isset($options["feedbrowser"])) {
169 $count = update_feedbrowser_cache();
170 print "Finished, $count feeds processed.\n";
173 if (isset($options["daemon"])) {
175 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
177 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet");
178 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
179 sleep(DAEMON_SLEEP_INTERVAL
);
183 if (isset($options["daemon-loop"])) {
184 if (!make_stampfile('update_daemon.stamp')) {
185 _debug("warning: unable to create stampfile\n");
188 // Call to the feed batch update function
189 // or regenerate feedbrowser cache
191 if (rand(0,100) > 30) {
192 update_daemon_common();
194 $count = update_feedbrowser_cache();
195 _debug("Feedbrowser updated, $count feeds processed.");
197 purge_orphans( true);
199 $rc = cleanup_tags( 14, 50000);
201 _debug("Cleaned $rc cached tags.");
204 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
209 if (isset($options["cleanup-tags"])) {
210 $rc = cleanup_tags( 14, 50000);
211 _debug("$rc tags deleted.\n");
214 if (isset($options["indexes"])) {
215 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
216 _debug("Type 'yes' to continue.");
218 if (read_stdin() != 'yes')
221 _debug("clearing existing indexes...");
223 if (DB_TYPE
== "pgsql") {
224 $result = db_query( "SELECT relname FROM
225 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
226 AND relname NOT LIKE '%_pkey'
229 $result = db_query( "SELECT index_name,table_name FROM
230 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
233 while ($line = db_fetch_assoc($result)) {
234 if (DB_TYPE
== "pgsql") {
235 $statement = "DROP INDEX " . $line["relname"];
238 $statement = "ALTER TABLE ".
239 $line['table_name']." DROP INDEX ".$line['index_name'];
242 db_query( $statement, false);
245 _debug("reading indexes from schema for: " . DB_TYPE
);
247 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
249 while ($line = fgets($fp)) {
252 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
253 $index = $matches[1];
254 $table = $matches[2];
256 $statement = "CREATE INDEX $index ON $table";
259 db_query( $statement);
264 _debug("unable to open schema file.");
269 if (isset($options["convert-filters"])) {
270 _debug("WARNING: this will remove all existing type2 filters.");
271 _debug("Type 'yes' to continue.");
273 if (read_stdin() != 'yes')
276 _debug("converting filters...");
278 db_query( "DELETE FROM ttrss_filters2");
280 $result = db_query( "SELECT * FROM ttrss_filters ORDER BY id");
282 while ($line = db_fetch_assoc($result)) {
283 $owner_uid = $line["owner_uid"];
285 // date filters are removed
286 if ($line["filter_type"] != 5) {
289 if (sql_bool_to_bool($line["cat_filter"])) {
290 $feed_id = "CAT:" . (int)$line["cat_id"];
292 $feed_id = (int)$line["feed_id"];
295 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
296 $filter["rule"] = array(
298 "reg_exp" => $line["reg_exp"],
299 "feed_id" => $feed_id,
300 "filter_type" => $line["filter_type"])));
302 $filter["action"] = array(
304 "action_id" => $line["action_id"],
305 "action_param_label" => $line["action_param"],
306 "action_param" => $line["action_param"])));
308 // Oh god it's full of hacks
311 $_SESSION["uid"] = $owner_uid;
313 $filters = new Pref_Filters( $_REQUEST);
320 if (isset($options["update-schema"])) {
321 _debug("checking for updates (" . DB_TYPE
. ")...");
323 $updater = new DbUpdater( DB_TYPE
, SCHEMA_VERSION
);
325 if ($updater->isUpdateRequired()) {
326 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
327 _debug("WARNING: please backup your database before continuing.");
328 _debug("Type 'yes' to continue.");
330 if (read_stdin() != 'yes')
333 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
334 _debug("performing update up to version $i...");
336 $result = $updater->performUpdateTo($i);
338 _debug($result ?
"OK!" : "FAILED!");
340 if (!$result) return;
344 _debug("update not required.");
349 if (isset($options["list-plugins"])) {
350 $tmppluginhost = new PluginHost(Db
::get());
351 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
);
352 $enabled = array_map("trim", explode(",", PLUGINS
));
354 echo "List of all available plugins:\n";
356 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
357 $about = $plugin->about();
359 $status = $about[3] ?
"system" : "user";
361 if (in_array($name, $enabled)) $name .= "*";
363 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
364 $name, $status, $about[0], $about[2], $about[1]);
367 echo "Plugins marked by * are currently enabled for all users.\n";
371 $pluginhost->run_commands($options);
373 if ($lock_handle != false) {
374 fclose($lock_handle);
377 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
378 unlink(LOCK_DIRECTORY
. "/$lock_filename");