3 set_include_path(get_include_path() . PATH_SEPARATOR
.
4 dirname(__FILE__
) . "/include");
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";
16 require_once "update_self.php";
18 if (!defined('PHP_EXECUTABLE'))
19 define('PHP_EXECUTABLE', '/usr/bin/php');
23 if (count($argv) == 0 && !defined('STDIN')) {
26 <title
>Tiny Tiny RSS data update script
.</title
>
27 <meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8">
28 <link rel
="stylesheet" type
="text/css" href
="utility.css">
32 <div
class="floatingLogo"><img src
="images/logo_wide.png"></div
>
33 <h1
><?php
echo __("Tiny Tiny RSS data update script.") ?
></h1
>
35 <?php
print_error("Please run this script from the command line. Use option \"-help\" to display command help if this error is displayed erroneously."); ?
>
42 if (count($argv) == 1 ||
in_array("-help", $op) ) {
43 print "Tiny Tiny RSS data update script.\n\n";
45 print " -feeds - update feeds\n";
46 print " -feedbrowser - update feedbrowser\n";
47 print " -daemon - start single-process update daemon\n";
48 print " -cleanup-tags - perform tags table maintenance\n";
49 print " -get-feeds - receive popular feeds from linked instances\n";
50 print " -import USER FILE - import articles from XML\n";
51 print " -update-self - update tt-rss installation to latest version\n";
52 print " -quiet - don't show messages\n";
53 print " -indexes - recreate missing schema indexes\n";
54 print " -convert-filters - convert type1 filters to type2\n";
55 print " -force-update - force update of all feeds\n";
56 print " -help - show this help\n";
60 define('QUIET', in_array("-quiet", $op));
62 if (!in_array("-daemon", $op)) {
63 $lock_filename = "update.lock";
65 $lock_filename = "update_daemon.lock";
68 $lock_handle = make_lockfile($lock_filename);
71 // Try to lock a file in order to avoid concurrent update.
73 die("error: Can't create lockfile ($lock_filename). ".
74 "Maybe another update process is already running.\n");
77 // Create a database connection.
78 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
80 init_connection($link);
82 if (in_array("-feeds", $op)) {
83 // Update all feeds needing a update.
84 update_daemon_common($link);
87 $count = update_feedbrowser_cache($link);
88 _debug("Feedbrowser updated, $count feeds processed.");
90 // Purge orphans and cleanup tags
91 purge_orphans($link, true);
93 $rc = cleanup_tags($link, 14, 50000);
94 _debug("Cleaned $rc cached tags.");
96 get_linked_feeds($link);
99 if (in_array("-feedbrowser", $op)) {
100 $count = update_feedbrowser_cache($link);
101 print "Finished, $count feeds processed.\n";
104 if (in_array("-daemon", $op)) {
105 $op = array_diff($op, array("-daemon"));
107 passthru(PHP_EXECUTABLE
. " " . implode(' ', $op) . " -daemon-loop");
108 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
109 sleep(DAEMON_SLEEP_INTERVAL
);
113 if (in_array("-daemon-loop", $op)) {
114 if (!make_stampfile('update_daemon.stamp')) {
115 die("error: unable to create stampfile\n");
118 // Call to the feed batch update function
119 // or regenerate feedbrowser cache
121 if (rand(0,100) > 30) {
122 update_daemon_common($link);
124 $count = update_feedbrowser_cache($link);
125 _debug("Feedbrowser updated, $count feeds processed.");
127 purge_orphans($link, true);
129 $rc = cleanup_tags($link, 14, 50000);
131 _debug("Cleaned $rc cached tags.");
133 get_linked_feeds($link);
138 if (in_array("-cleanup-tags", $op)) {
139 $rc = cleanup_tags($link, 14, 50000);
140 _debug("$rc tags deleted.\n");
143 if (in_array("-get-feeds", $op)) {
144 get_linked_feeds($link);
147 if (in_array("-import",$op)) {
148 $username = $argv[count($argv) - 2];
149 $filename = $argv[count($argv) - 1];
152 print "error: please specify username.\n";
156 if (!is_file($filename)) {
157 print "error: input filename ($filename) doesn't exist.\n";
161 _debug("importing $filename for user $username...\n");
163 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
165 if (db_num_rows($result) == 0) {
166 print "error: could not find user $username.\n";
170 $owner_uid = db_fetch_result($result, 0, "id");
172 perform_data_import($link, $filename, $owner_uid);
176 if (in_array("-indexes", $op)) {
177 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
178 _debug("Type 'yes' to continue.");
180 if (read_stdin() != 'yes')
183 _debug("clearing existing indexes...");
185 if (DB_TYPE
== "pgsql") {
186 $result = db_query($link, "SELECT relname FROM
187 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
188 AND relname NOT LIKE '%_pkey'
191 $result = db_query($link, "SELECT index_name,table_name FROM
192 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
195 while ($line = db_fetch_assoc($result)) {
196 if (DB_TYPE
== "pgsql") {
197 $statement = "DROP INDEX " . $line["relname"];
200 $statement = "ALTER TABLE ".
201 $line['table_name']." DROP INDEX ".$line['index_name'];
204 db_query($link, $statement, false);
207 _debug("reading indexes from schema for: " . DB_TYPE
);
209 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
211 while ($line = fgets($fp)) {
214 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
215 $index = $matches[1];
216 $table = $matches[2];
218 $statement = "CREATE INDEX $index ON $table";
221 db_query($link, $statement);
226 _debug("unable to open schema file.");
231 if (in_array("-update-self", $op)) {
232 _debug("Warning: self-updating is experimental. Use at your own risk.");
233 _debug("Please backup your tt-rss directory before continuing. Your database will not be modified.");
234 _debug("Type 'yes' to continue.");
236 if (read_stdin() != 'yes')
239 update_self($link, in_array("-force", $op));
242 if (in_array("-convert-filters", $op)) {
243 _debug("WARNING: this will remove all existing type2 filters.");
244 _debug("Type 'yes' to continue.");
246 if (read_stdin() != 'yes')
249 _debug("converting filters...");
251 db_query($link, "DELETE FROM ttrss_filters2");
253 $result = db_query($link, "SELECT * FROM ttrss_filters ORDER BY id");
255 while ($line = db_fetch_assoc($result)) {
256 $owner_uid = $line["owner_uid"];
258 // date filters are removed
259 if ($line["filter_type"] != 5) {
262 if (sql_bool_to_bool($line["cat_filter"])) {
263 $feed_id = "CAT:" . (int)$line["cat_id"];
265 $feed_id = (int)$line["feed_id"];
268 $filter["enabled"] = $line["enabled"] ?
"on" : "off";
269 $filter["rule"] = array(
271 "reg_exp" => $line["reg_exp"],
272 "feed_id" => $feed_id,
273 "filter_type" => $line["filter_type"])));
275 $filter["action"] = array(
277 "action_id" => $line["action_id"],
278 "action_param_label" => $line["action_param"],
279 "action_param" => $line["action_param"])));
281 // Oh god it's full of hacks
284 $_SESSION["uid"] = $owner_uid;
286 $filters = new Pref_Filters($link, $_REQUEST);
293 if (in_array("-force-update", $op)) {
294 _debug("marking all feeds as needing update...");
296 db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01',
297 last_updated = '1970-01-01'");
302 if ($lock_handle != false) {
303 fclose($lock_handle);
306 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
307 unlink(LOCK_DIRECTORY
. "/$lock_filename");