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",
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 define('QUIET', isset($options['quiet']));
93 if (isset($options["log"])) {
94 _debug("Logging to " . $options["log"]);
95 define('LOGFILE', $options["log"]);
98 if (!isset($options["daemon"])) {
99 $lock_filename = "update.lock";
101 $lock_filename = "update_daemon.lock";
104 if (isset($options["task"])) {
105 _debug("Using task id " . $options["task"]);
106 $lock_filename = $lock_filename . "-task_" . $options["task"];
109 $lock_handle = make_lockfile($lock_filename);
112 // Try to lock a file in order to avoid concurrent update.
114 die("error: Can't create lockfile ($lock_filename). ".
115 "Maybe another update process is already running.\n");
118 if (isset($options["force-update"])) {
119 _debug("marking all feeds as needing update...");
121 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
122 last_updated = '1970-01-01'");
125 if (isset($options["feeds"])) {
126 // Update all feeds needing a update.
127 update_daemon_common($link);
129 // Update feedbrowser
130 $count = update_feedbrowser_cache($link);
131 _debug("Feedbrowser updated, $count feeds processed.");
133 // Purge orphans and cleanup tags
134 purge_orphans($link, true);
136 $rc = cleanup_tags($link, 14, 50000);
137 _debug("Cleaned $rc cached tags.");
140 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
143 if (isset($options["feedbrowser"])) {
144 $count = update_feedbrowser_cache($link);
145 print "Finished, $count feeds processed.\n";
148 if (isset($options["daemon"])) {
150 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
152 passthru(PHP_EXECUTABLE
. " " . $argv[0] ." --daemon-loop $quiet");
153 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
154 sleep(DAEMON_SLEEP_INTERVAL
);
158 if (isset($options["daemon-loop"])) {
159 if (!make_stampfile('update_daemon.stamp')) {
160 _debug("warning: unable to create stampfile\n");
163 // Call to the feed batch update function
164 // or regenerate feedbrowser cache
166 if (rand(0,100) > 30) {
167 update_daemon_common($link);
169 $count = update_feedbrowser_cache($link);
170 _debug("Feedbrowser updated, $count feeds processed.");
172 purge_orphans($link, true);
174 $rc = cleanup_tags($link, 14, 50000);
176 _debug("Cleaned $rc cached tags.");
179 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
184 if (isset($options["cleanup-tags"])) {
185 $rc = cleanup_tags($link, 14, 50000);
186 _debug("$rc tags deleted.\n");
189 if (isset($options["indexes"])) {
190 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
191 _debug("Type 'yes' to continue.");
193 if (read_stdin() != 'yes')
196 _debug("clearing existing indexes...");
198 if (DB_TYPE
== "pgsql") {
199 $result = db_query($link, "SELECT relname FROM
200 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
201 AND relname NOT LIKE '%_pkey'
204 $result = db_query($link, "SELECT index_name,table_name FROM
205 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
208 while ($line = db_fetch_assoc($result)) {
209 if (DB_TYPE
== "pgsql") {
210 $statement = "DROP INDEX " . $line["relname"];
213 $statement = "ALTER TABLE ".
214 $line['table_name']." DROP INDEX ".$line['index_name'];
217 db_query($link, $statement, false);
220 _debug("reading indexes from schema for: " . DB_TYPE
);
222 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
224 while ($line = fgets($fp)) {
227 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
228 $index = $matches[1];
229 $table = $matches[2];
231 $statement = "CREATE INDEX $index ON $table";
234 db_query($link, $statement);
239 _debug("unable to open schema file.");
244 if (isset($options["convert-filters"])) {
245 _debug("WARNING: this will remove all existing type2 filters.");
246 _debug("Type 'yes' to continue.");
248 if (read_stdin() != 'yes')
251 _debug("converting filters...");
253 db_query($link, "DELETE FROM ttrss_filters2");
255 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
257 while ($line = db_fetch_assoc($result)) {
258 $owner_uid = $line["owner_uid"];
260 // date filters are removed
261 if ($line["filter_type"] != 5) {
264 if (sql_bool_to_bool($line["cat_filter"])) {
265 $feed_id = "CAT:" . (int)$line["cat_id"];
267 $feed_id = (int)$line["feed_id"];
270 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
271 $filter["rule"] = array(
273 "reg_exp" => $line["reg_exp"],
274 "feed_id" => $feed_id,
275 "filter_type" => $line["filter_type"])));
277 $filter["action"] = array(
279 "action_id" => $line["action_id"],
280 "action_param_label" => $line["action_param"],
281 "action_param" => $line["action_param"])));
283 // Oh god it's full of hacks
286 $_SESSION["uid"] = $owner_uid;
288 $filters = new Pref_Filters($link, $_REQUEST);
295 if (isset($options["update-schema"])) {
296 _debug("checking for updates (" . DB_TYPE
. ")...");
298 $updater = new DbUpdater($link, DB_TYPE
, SCHEMA_VERSION
);
300 if ($updater->isUpdateRequired()) {
301 _debug("schema update required, version " . $updater->getSchemaVersion() . " to " . SCHEMA_VERSION
);
302 _debug("WARNING: please backup your database before continuing.");
303 _debug("Type 'yes' to continue.");
305 if (read_stdin() != 'yes')
308 for ($i = $updater->getSchemaVersion() +
1; $i <= SCHEMA_VERSION
; $i++
) {
309 _debug("performing update up to version $i...");
311 $result = $updater->performUpdateTo($i);
313 _debug($result ?
"OK!" : "FAILED!");
315 if (!$result) return;
319 _debug("update not required.");
324 if (isset($options["list-plugins"])) {
325 $tmppluginhost = new PluginHost($link);
326 $tmppluginhost->load_all($tmppluginhost::KIND_ALL
);
327 $enabled = array_map("trim", explode(",", PLUGINS
));
329 echo "List of all available plugins:\n";
331 foreach ($tmppluginhost->get_plugins() as $name => $plugin) {
332 $about = $plugin->about();
334 $status = $about[3] ?
"system" : "user";
336 if (in_array($name, $enabled)) $name .= "*";
338 printf("%-50s %-10s v%.2f (by %s)\n%s\n\n",
339 $name, $status, $about[0], $about[2], $about[1]);
342 echo "Plugins marked by * are currently enabled for all users.\n";
346 $pluginhost->run_commands($options);
350 if ($lock_handle != false) {
351 fclose($lock_handle);
354 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
355 unlink(LOCK_DIRECTORY
. "/$lock_filename");