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 "sanity_check.php";
13 require_once "config.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);
28 if (count($argv) == 0 && !defined('STDIN')) {
31 <title
>Tiny Tiny RSS data update script
.</title
>
32 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
33 <link rel
="stylesheet" type
="text/css" href
="utility.css">
37 <div
class="floatingLogo"><img src
="images/logo_wide.png"></div
>
38 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
40 <?php
print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?
>
47 if (count($argv) == 1 ||
in_array("-help", $op) ) {
48 print "Tiny Tiny RSS data update script.\n\n";
50 print " -feeds - update feeds\n";
51 print " -feedbrowser - update feedbrowser\n";
52 print " -daemon - start single-process update daemon\n";
53 print " -cleanup-tags - perform tags table maintenance\n";
54 print " -import USER FILE - import articles from XML\n";
55 print " -quiet - don't show messages\n";
56 print " -indexes - recreate missing schema indexes\n";
57 print " -convert-filters - convert type1 filters to type2\n";
58 print " -force-update - force update of all feeds\n";
59 print " -help - show this help\n";
60 print "Plugin options:\n";
62 foreach ($pluginhost->get_commands() as $command => $data) {
63 printf(" %-19s - %s\n", "$command", $data["description"]);
69 define('QUIET', in_array("-quiet", $op));
71 if (!in_array("-daemon", $op)) {
72 $lock_filename = "update.lock";
74 $lock_filename = "update_daemon.lock";
77 $lock_handle = make_lockfile($lock_filename);
80 // Try to lock a file in order to avoid concurrent update.
82 die("error: Can't create lockfile ($lock_filename). ".
83 "Maybe another update process is already running.\n");
86 if (in_array("-feeds", $op)) {
87 // Update all feeds needing a update.
88 update_daemon_common($link);
91 $count = update_feedbrowser_cache($link);
92 _debug("Feedbrowser updated, $count feeds processed.");
94 // Purge orphans and cleanup tags
95 purge_orphans($link, true);
97 $rc = cleanup_tags($link, 14, 50000);
98 _debug("Cleaned $rc cached tags.");
101 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
104 if (in_array("-feedbrowser", $op)) {
105 $count = update_feedbrowser_cache($link);
106 print "Finished, $count feeds processed.\n";
109 if (in_array("-daemon", $op)) {
110 $op = array_diff($op, array("-daemon"));
112 passthru(PHP_EXECUTABLE
. " " . implode(' ', $op) . " -daemon-loop");
113 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
114 sleep(DAEMON_SLEEP_INTERVAL
);
118 if (in_array("-daemon-loop", $op)) {
119 if (!make_stampfile('update_daemon.stamp')) {
120 die("error: unable to create stampfile\n");
123 // Call to the feed batch update function
124 // or regenerate feedbrowser cache
126 if (rand(0,100) > 30) {
127 update_daemon_common($link);
129 $count = update_feedbrowser_cache($link);
130 _debug("Feedbrowser updated, $count feeds processed.");
132 purge_orphans($link, true);
134 $rc = cleanup_tags($link, 14, 50000);
136 _debug("Cleaned $rc cached tags.");
139 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
144 if (in_array("-cleanup-tags", $op)) {
145 $rc = cleanup_tags($link, 14, 50000);
146 _debug("$rc tags deleted.\n");
149 if (in_array("-import",$op)) {
150 $username = $argv[count($argv) - 2];
151 $filename = $argv[count($argv) - 1];
154 print "error: please specify username.\n";
158 if (!is_file($filename)) {
159 print "error: input filename ($filename) doesn't exist.\n";
163 _debug("importing $filename for user $username...\n");
165 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
167 if (db_num_rows($result) == 0) {
168 print "error: could not find user $username.\n";
172 $owner_uid = db_fetch_result($result, 0, "id");
174 perform_data_import($link, $filename, $owner_uid);
178 if (in_array("-indexes", $op)) {
179 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
180 _debug("Type 'yes' to continue.");
182 if (read_stdin() != 'yes')
185 _debug("clearing existing indexes...");
187 if (DB_TYPE
== "pgsql") {
188 $result = db_query($link, "SELECT relname FROM
189 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
190 AND relname NOT LIKE '%_pkey'
193 $result = db_query($link, "SELECT index_name,table_name FROM
194 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
197 while ($line = db_fetch_assoc($result)) {
198 if (DB_TYPE
== "pgsql") {
199 $statement = "DROP INDEX " . $line["relname"];
202 $statement = "ALTER TABLE ".
203 $line['table_name']." DROP INDEX ".$line['index_name'];
206 db_query($link, $statement, false);
209 _debug("reading indexes from schema for: " . DB_TYPE
);
211 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
213 while ($line = fgets($fp)) {
216 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
217 $index = $matches[1];
218 $table = $matches[2];
220 $statement = "CREATE INDEX $index ON $table";
223 db_query($link, $statement);
228 _debug("unable to open schema file.");
233 if (in_array("-convert-filters", $op)) {
234 _debug("WARNING: this will remove all existing type2 filters.");
235 _debug("Type 'yes' to continue.");
237 if (read_stdin() != 'yes')
240 _debug("converting filters...");
242 db_query($link, "DELETE FROM ttrss_filters2");
244 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
246 while ($line = db_fetch_assoc($result)) {
247 $owner_uid = $line["owner_uid"];
249 // date filters are removed
250 if ($line["filter_type"] != 5) {
253 if (sql_bool_to_bool($line["cat_filter"])) {
254 $feed_id = "CAT:" . (int)$line["cat_id"];
256 $feed_id = (int)$line["feed_id"];
259 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
260 $filter["rule"] = array(
262 "reg_exp" => $line["reg_exp"],
263 "feed_id" => $feed_id,
264 "filter_type" => $line["filter_type"])));
266 $filter["action"] = array(
268 "action_id" => $line["action_id"],
269 "action_param_label" => $line["action_param"],
270 "action_param" => $line["action_param"])));
272 // Oh god it's full of hacks
275 $_SESSION["uid"] = $owner_uid;
277 $filters = new Pref_Filters($link, $_REQUEST);
284 if (in_array("-force-update", $op)) {
285 _debug("marking all feeds as needing update...");
287 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
288 last_updated = '1970-01-01'");
291 $pluginhost->run_commands($op);
295 if ($lock_handle != false) {
296 fclose($lock_handle);
299 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
300 unlink(LOCK_DIRECTORY
. "/$lock_filename");