]> git.wh0rd.org - tt-rss.git/blob - update.php
update sanity_config.php
[tt-rss.git] / update.php
1 #!/usr/bin/env php
2 <?php
3 set_include_path(get_include_path() . PATH_SEPARATOR .
4 dirname(__FILE__) . "/include");
5
6 define('DISABLE_SESSIONS', true);
7
8 chdir(dirname(__FILE__));
9
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";
17
18 if (!defined('STDIN')) {
19 ?> <html>
20 <head>
21 <title>Database Updater</title>
22 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
23 <link rel="stylesheet" type="text/css" href="utility.css">
24 </head>
25
26 <body>
27 <div class="floatingLogo"><img src="images/logo_wide.png"></div>
28 <h1><?php echo __("Update") ?></h1>
29
30 <?php print_error("Please run this script from the command line."); ?>
31
32 </body></html>
33 <?php
34 exit;
35 }
36
37 if (!defined('PHP_EXECUTABLE'))
38 define('PHP_EXECUTABLE', '/usr/bin/php');
39
40 $op = $argv;
41
42 if (count($argv) == 1 || in_array("-help", $op) ) {
43 print "Tiny Tiny RSS data update script.\n\n";
44 print "Options:\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 " -help - show this help\n";
55 return;
56 }
57
58 define('QUIET', in_array("-quiet", $op));
59
60 if (!in_array("-daemon", $op)) {
61 $lock_filename = "update.lock";
62 } else {
63 $lock_filename = "update_daemon.lock";
64 }
65
66 $lock_handle = make_lockfile($lock_filename);
67 $must_exit = false;
68
69 // Try to lock a file in order to avoid concurrent update.
70 if (!$lock_handle) {
71 die("error: Can't create lockfile ($lock_filename). ".
72 "Maybe another update process is already running.\n");
73 }
74
75 // Create a database connection.
76 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
77
78 init_connection($link);
79
80 if (in_array("-feeds", $op)) {
81 // Update all feeds needing a update.
82 update_daemon_common($link);
83
84 // Update feedbrowser
85 $count = update_feedbrowser_cache($link);
86 _debug("Feedbrowser updated, $count feeds processed.");
87
88 // Purge orphans and cleanup tags
89 purge_orphans($link, true);
90
91 $rc = cleanup_tags($link, 14, 50000);
92 _debug("Cleaned $rc cached tags.");
93
94 get_linked_feeds($link);
95 }
96
97 if (in_array("-feedbrowser", $op)) {
98 $count = update_feedbrowser_cache($link);
99 print "Finished, $count feeds processed.\n";
100 }
101
102 if (in_array("-daemon", $op)) {
103 $op = array_diff($op, array("-daemon"));
104 while (true) {
105 passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop");
106 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
107 sleep(DAEMON_SLEEP_INTERVAL);
108 }
109 }
110
111 if (in_array("-daemon-loop", $op)) {
112 if (!make_stampfile('update_daemon.stamp')) {
113 die("error: unable to create stampfile\n");
114 }
115
116 // Call to the feed batch update function
117 // or regenerate feedbrowser cache
118
119 if (rand(0,100) > 30) {
120 update_daemon_common($link);
121 } else {
122 $count = update_feedbrowser_cache($link);
123 _debug("Feedbrowser updated, $count feeds processed.");
124
125 purge_orphans($link, true);
126
127 $rc = cleanup_tags($link, 14, 50000);
128
129 _debug("Cleaned $rc cached tags.");
130
131 get_linked_feeds($link);
132 }
133
134 }
135
136 if (in_array("-cleanup-tags", $op)) {
137 $rc = cleanup_tags($link, 14, 50000);
138 _debug("$rc tags deleted.\n");
139 }
140
141 if (in_array("-get-feeds", $op)) {
142 get_linked_feeds($link);
143 }
144
145 if (in_array("-import",$op)) {
146 $username = $argv[count($argv) - 2];
147 $filename = $argv[count($argv) - 1];
148
149 if (!$username) {
150 print "error: please specify username.\n";
151 return;
152 }
153
154 if (!is_file($filename)) {
155 print "error: input filename ($filename) doesn't exist.\n";
156 return;
157 }
158
159 _debug("importing $filename for user $username...\n");
160
161 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
162
163 if (db_num_rows($result) == 0) {
164 print "error: could not find user $username.\n";
165 return;
166 }
167
168 $owner_uid = db_fetch_result($result, 0, "id");
169
170 perform_data_import($link, $filename, $owner_uid);
171
172 }
173
174 if (in_array("-indexes", $op)) {
175 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
176 _debug("Type 'yes' to continue.");
177
178 if (read_stdin() != 'yes')
179 exit;
180
181 _debug("clearing existing indexes...");
182
183 if (DB_TYPE == "pgsql") {
184 $result = db_query($link, "SELECT relname FROM
185 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
186 AND relname NOT LIKE '%_pkey'
187 AND relkind = 'i'");
188 } else {
189 $result = db_query($link, "SELECT index_name,table_name FROM
190 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
191 }
192
193 while ($line = db_fetch_assoc($result)) {
194 if (DB_TYPE == "pgsql") {
195 $statement = "DROP INDEX " . $line["relname"];
196 _debug($statement);
197 } else {
198 $statement = "ALTER TABLE ".
199 $line['table_name']." DROP INDEX ".$line['index_name'];
200 _debug($statement);
201 }
202 db_query($link, $statement, false);
203 }
204
205 _debug("reading indexes from schema for: " . DB_TYPE);
206
207 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
208 if ($fp) {
209 while ($line = fgets($fp)) {
210 $matches = array();
211
212 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
213 $index = $matches[1];
214 $table = $matches[2];
215
216 $statement = "CREATE INDEX $index ON $table";
217
218 _debug($statement);
219 db_query($link, $statement);
220 }
221 }
222 fclose($fp);
223 } else {
224 _debug("unable to open schema file.");
225 }
226 _debug("all done.");
227 }
228
229 if (in_array("-update-self", $op)) {
230 _debug("Warning: self-updating is experimental. Use at your own risk.");
231 _debug("Please backup your tt-rss directory before continuing. Your database will not be modified.");
232 _debug("Type 'yes' to continue.");
233
234 if (read_stdin() != 'yes')
235 exit;
236
237 update_self($link, in_array("-force", $op));
238 }
239
240 db_close($link);
241
242 if ($lock_handle != false) {
243 fclose($lock_handle);
244 }
245
246 if (file_exists(LOCK_DIRECTORY . "/$lock_filename"))
247 unlink(LOCK_DIRECTORY . "/$lock_filename");
248 ?>