]> git.wh0rd.org - tt-rss.git/blame - update.php
update.php: display error message when running from the browser
[tt-rss.git] / update.php
CommitLineData
ece78711 1#!/usr/bin/env php
fecd57c8 2<?php
55f34b81 3 set_include_path(get_include_path() . PATH_SEPARATOR .
f03a795d 4 dirname(__FILE__) . "/include");
107d0cf3 5
661135c7
AD
6 define('DISABLE_SESSIONS', true);
7
9b27cec8
AD
8 chdir(dirname(__FILE__));
9
fb074239 10 require_once "functions.php";
2c08214a 11 require_once "rssfuncs.php";
81596c66 12 require_once "sanity_check.php";
81596c66
AD
13 require_once "config.php";
14 require_once "db.php";
661135c7 15 require_once "db-prefs.php";
661135c7 16
366f06f7
AD
17 if (!defined('STDIN')) {
18 ?> <html>
19 <head>
20 <title>Database Updater</title>
21 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22 <link rel="stylesheet" type="text/css" href="utility.css">
23 </head>
24
25 <body>
26 <div class="floatingLogo"><img src="images/logo_wide.png"></div>
27 <h1><?php echo __("Update") ?></h1>
28
29 <?php print_error("Please run this script from the command line."); ?>
30
31 </body></html>
32 <?php
33 exit;
34 }
35
86268d8b
AD
36 if (!defined('PHP_EXECUTABLE'))
37 define('PHP_EXECUTABLE', '/usr/bin/php');
38
5439d333 39 $op = $argv;
70dcff6b 40
5439d333 41 if (count($argv) == 1 || in_array("-help", $op) ) {
661135c7
AD
42 print "Tiny Tiny RSS data update script.\n\n";
43 print "Options:\n";
55f34b81
AD
44 print " -feeds - update feeds\n";
45 print " -feedbrowser - update feedbrowser\n";
46 print " -daemon - start single-process update daemon\n";
47 print " -cleanup-tags - perform tags table maintenance\n";
48 print " -get-feeds - receive popular feeds from linked instances\n";
49 print " -import USER FILE - import articles from XML\n";
e3449aa1 50 print " -update-self - update tt-rss installation to latest version\n";
5439d333 51 print " -quiet - don't show messages\n";
9a57512c 52 print " -indexes - recreate missing schema indexes\n";
55f34b81 53 print " -help - show this help\n";
661135c7 54 return;
81596c66 55 }
87b9fb65 56
5439d333
RW
57 define('QUIET', in_array("-quiet", $op));
58
59 if (!in_array("-daemon", $op)) {
661135c7
AD
60 $lock_filename = "update.lock";
61 } else {
62 $lock_filename = "update_daemon.lock";
63 }
fecd57c8 64
661135c7
AD
65 $lock_handle = make_lockfile($lock_filename);
66 $must_exit = false;
fecd57c8 67
661135c7
AD
68 // Try to lock a file in order to avoid concurrent update.
69 if (!$lock_handle) {
70 die("error: Can't create lockfile ($lock_filename). ".
71 "Maybe another update process is already running.\n");
72 }
fecd57c8 73
661135c7 74 // Create a database connection.
dbaa4e4a 75 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
fecd57c8 76
661135c7 77 init_connection($link);
ef59e6e8 78
5439d333 79 if (in_array("-feeds", $op)) {
661135c7
AD
80 // Update all feeds needing a update.
81 update_daemon_common($link);
84e9a8c7
AD
82
83 // Update feedbrowser
84 $count = update_feedbrowser_cache($link);
85 _debug("Feedbrowser updated, $count feeds processed.");
86
87 // Purge orphans and cleanup tags
88 purge_orphans($link, true);
89
90 $rc = cleanup_tags($link, 14, 50000);
91 _debug("Cleaned $rc cached tags.");
f32eb194
AD
92
93 get_linked_feeds($link);
661135c7 94 }
fecd57c8 95
5439d333 96 if (in_array("-feedbrowser", $op)) {
661135c7
AD
97 $count = update_feedbrowser_cache($link);
98 print "Finished, $count feeds processed.\n";
fecd57c8 99 }
661135c7 100
5439d333
RW
101 if (in_array("-daemon", $op)) {
102 $op = array_diff($op, array("-daemon"));
661135c7 103 while (true) {
5439d333 104 passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop");
661135c7
AD
105 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
106 sleep(DAEMON_SLEEP_INTERVAL);
fecd57c8 107 }
81596c66 108 }
9e21a571 109
5439d333 110 if (in_array("-daemon-loop", $op)) {
661135c7
AD
111 if (!make_stampfile('update_daemon.stamp')) {
112 die("error: unable to create stampfile\n");
113 }
9e21a571 114
dbaa4e4a 115 // Call to the feed batch update function
661135c7 116 // or regenerate feedbrowser cache
9e21a571 117
661135c7
AD
118 if (rand(0,100) > 30) {
119 update_daemon_common($link);
9e21a571 120 } else {
661135c7 121 $count = update_feedbrowser_cache($link);
e3b42c5a
AD
122 _debug("Feedbrowser updated, $count feeds processed.");
123
124 purge_orphans($link, true);
dbaa4e4a 125
e3b42c5a
AD
126 $rc = cleanup_tags($link, 14, 50000);
127
128 _debug("Cleaned $rc cached tags.");
f32eb194
AD
129
130 get_linked_feeds($link);
9e21a571 131 }
ef59e6e8 132
fecd57c8 133 }
fecd57c8 134
5439d333 135 if (in_array("-cleanup-tags", $op)) {
868650e4 136 $rc = cleanup_tags($link, 14, 50000);
5439d333 137 _debug("$rc tags deleted.\n");
868650e4
AD
138 }
139
5439d333 140 if (in_array("-get-feeds", $op)) {
ae5f7bb1
AD
141 get_linked_feeds($link);
142 }
143
5439d333
RW
144 if (in_array("-import",$op)) {
145 $username = $argv[count($argv) - 2];
146 $filename = $argv[count($argv) - 1];
55f34b81
AD
147
148 if (!$username) {
149 print "error: please specify username.\n";
150 return;
151 }
152
153 if (!is_file($filename)) {
154 print "error: input filename ($filename) doesn't exist.\n";
155 return;
156 }
157
5439d333 158 _debug("importing $filename for user $username...\n");
55f34b81
AD
159
160 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
161
162 if (db_num_rows($result) == 0) {
163 print "error: could not find user $username.\n";
164 return;
165 }
166
167 $owner_uid = db_fetch_result($result, 0, "id");
168
169 perform_data_import($link, $filename, $owner_uid);
170
171 }
172
871f0a7a
AD
173 if (in_array("-indexes", $op)) {
174 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
175 _debug("Type 'yes' to continue.");
176
177 if (read_stdin() != 'yes')
178 exit;
179
180 _debug("clearing existing indexes...");
181
182 if (DB_TYPE == "pgsql") {
183 $result = db_query($link, "SELECT relname FROM
184 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
185 AND relname NOT LIKE '%_pkey'
186 AND relkind = 'i'");
187 } else {
188 $result = db_query($link, "SELECT index_name,table_name FROM
189 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
190 }
191
192 while ($line = db_fetch_assoc($result)) {
193 if (DB_TYPE == "pgsql") {
194 $statement = "DROP INDEX " . $line["relname"];
195 _debug($statement);
196 } else {
197 $statement = "ALTER TABLE ".
198 $line['table_name']." DROP INDEX ".$line['index_name'];
199 _debug($statement);
200 }
201 db_query($link, $statement, false);
202 }
203
204 _debug("reading indexes from schema for: " . DB_TYPE);
205
206 $fp = fopen("schema/ttrss_schema_" . DB_TYPE . ".sql", "r");
207 if ($fp) {
208 while ($line = fgets($fp)) {
209 $matches = array();
210
211 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
212 $index = $matches[1];
213 $table = $matches[2];
214
215 $statement = "CREATE INDEX $index ON $table";
216
217 _debug($statement);
218 db_query($link, $statement);
219 }
220 }
221 fclose($fp);
222 } else {
223 _debug("unable to open schema file.");
224 }
225 _debug("all done.");
226 }
227
e3449aa1
AD
228 if (in_array("-update-self", $op)) {
229 _debug("Warning: self-updating is experimental. Use at your own risk.");
230 _debug("Please backup your tt-rss directory before continuing. Your database will not be modified.");
231 _debug("Type 'yes' to continue.");
232
233 if (read_stdin() != 'yes')
234 exit;
235
236 $work_dir = dirname(__FILE__);
237 $parent_dir = dirname($work_dir);
238
239 if (!is_writable($work_dir) && !is_writable("$parent_dir")) {
240 _debug("Both current and parent directories should be writable as current user.");
241 exit;
242 }
243
244 if (!is_writable(sys_get_temp_dir())) {
245 _debug("System temporary directory should be writable as current user.");
246 exit;
247 }
248
249 _debug("Checking for tar...");
250
251 $system_rc = 0;
252 system("tar --version >/dev/null", $system_rc);
253
254 if ($system_rc != 0) {
255 _debug("Could not run tar executable (RC=$system_rc).");
256 exit;
257 }
258
259 _debug("Checking for latest version...");
260
261 $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"),
262 true);
263
264 if (!is_array($version_info)) {
265 _debug("Unable to fetch version information.");
266 exit;
267 }
268
269 $target_version = $version_info["version"];
270 $target_dir = "$parent_dir/tt-rss-$target_version";
271
272 _debug("Target version: $target_version");
273
274 if (version_compare(VERSION, $target_version) != -1 && !in_array("-force", $op)) {
275 _debug("You are on latest version. Update not needed.");
276 exit;
277 }
278 if (file_exists($target_dir)) {
279 _debug("Target directory $target_dir already exists.");
280 exit;
281 }
282
283 _debug("Downloading checksums...");
284 $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt");
285
286 if (!$md5sum_data) {
287 _debug("Could not download checksums.");
288 exit;
289 }
290
291 $md5sum_data = explode("\n", $md5sum_data);
292
293 $tarball_url = "http://tt-rss.org/download/tt-rss-$target_version.tar.gz";
294 $data = fetch_file_contents($tarball_url);
295
296 if (!$data) {
297 _debug("Could not download distribution tarball ($tarball_url).");
298 exit;
299 }
300
301 _debug("Verifying tarball checksum...");
302
303 $target_md5sum = false;
304
305 foreach ($md5sum_data as $line) {
306 $pair = explode(" ", $line);
307
308 if ($pair[1] == "tt-rss-$target_version.tar.gz") {
309 $target_md5sum = $pair[0];
310 break;
311 }
312 }
313
314 if (!$target_md5sum) {
315 _debug("Unable to locate checksum for target version.");
316 exit;
317 }
318
319 $test_md5sum = md5($data);
320
321 if ($test_md5sum != $target_md5sum) {
322 _debug("Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum).");
323 exit;
324 }
325
326 $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss');
327 _debug("Saving download to $tmp_file");
328
329 if (!file_put_contents($tmp_file, $data)) {
330 _debug("Unable to save download.");
331 exit;
332 }
333
334 if (!chdir($parent_dir)) {
335 _debug("Unable to change into parent directory.");
336 exit;
337 }
338
339 $old_dir = tmpdirname($parent_dir, "tt-rss-old");
340
341 _debug("Renaming current directory to ".basename($old_dir));
342 if (!rename($work_dir, $old_dir)) {
343 _debug("Unable to rename current directory.");
344 exit;
345 }
346
347 _debug("Extracting tarball...");
348 system("tar zxf $tmp_file", $system_rc);
349
350 if ($system_rc != 0) {
351 _debug("Error while extracting tarball (RC=$system_rc).");
352 exit;
353 }
354
355 _debug("Renaming target directory...");
356 if (!rename($target_dir, $work_dir)) {
357 _debug("Unable to rename target directory.");
358 exit;
359 }
360
361 chdir($work_dir);
362
363 _debug("Copying config.php...");
364 if (!copy("$old_dir/config.php", "$work_dir/config.php")) {
365 _debug("Unable to copy config.php to $work_dir.");
366 exit;
367 }
368
369 _debug("Cleaning up...");
370 unlink($tmp_file);
371
372 _debug("Fixing permissions...");
373
374 $directories = array(
375 CACHE_DIR,
376 CACHE_DIR . "/htmlpurifier",
377 CACHE_DIR . "/export",
378 CACHE_DIR . "/images",
379 CACHE_DIR . "/magpie",
380 CACHE_DIR . "/simplepie",
381 ICONS_DIR,
382 LOCK_DIRECTORY);
383
384 foreach ($directories as $dir) {
385 _debug("-> $dir");
386 chmod($dir, 0777);
387 }
388
389 _debug("Upgrade completed.");
390 _debug("Your old tt-rss directory is saved at $old_dir. ".
391 "Please migrate locally modified files (if any) and remove it.");
392 _debug("You might need to re-enter current directory in shell to see new files.");
393 }
394
661135c7 395 db_close($link);
107d0cf3 396