]>
git.wh0rd.org - tt-rss.git/blob - update.php
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";
17 if (!defined('STDIN')) {
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">
26 <div
class="floatingLogo"><img src
="images/logo_wide.png"></div
>
27 <h1
><?php
echo __("Update") ?
></h1
>
29 <?php
print_error("Please run this script from the command line."); ?
>
36 if (!defined('PHP_EXECUTABLE'))
37 define('PHP_EXECUTABLE', '/usr/bin/php');
41 if (count($argv) == 1 ||
in_array("-help", $op) ) {
42 print "Tiny Tiny RSS data update script.\n\n";
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";
50 print " -update-self - update tt-rss installation to latest version\n";
51 print " -quiet - don't show messages\n";
52 print " -indexes - recreate missing schema indexes\n";
53 print " -help - show this help\n";
57 define('QUIET', in_array("-quiet", $op));
59 if (!in_array("-daemon", $op)) {
60 $lock_filename = "update.lock";
62 $lock_filename = "update_daemon.lock";
65 $lock_handle = make_lockfile($lock_filename);
68 // Try to lock a file in order to avoid concurrent update.
70 die("error: Can't create lockfile ($lock_filename). ".
71 "Maybe another update process is already running.\n");
74 // Create a database connection.
75 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
77 init_connection($link);
79 if (in_array("-feeds", $op)) {
80 // Update all feeds needing a update.
81 update_daemon_common($link);
84 $count = update_feedbrowser_cache($link);
85 _debug("Feedbrowser updated, $count feeds processed.");
87 // Purge orphans and cleanup tags
88 purge_orphans($link, true);
90 $rc = cleanup_tags($link, 14, 50000);
91 _debug("Cleaned $rc cached tags.");
93 get_linked_feeds($link);
96 if (in_array("-feedbrowser", $op)) {
97 $count = update_feedbrowser_cache($link);
98 print "Finished, $count feeds processed.\n";
101 if (in_array("-daemon", $op)) {
102 $op = array_diff($op, array("-daemon"));
104 passthru(PHP_EXECUTABLE
. " " . implode(' ', $op) . " -daemon-loop");
105 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
106 sleep(DAEMON_SLEEP_INTERVAL
);
110 if (in_array("-daemon-loop", $op)) {
111 if (!make_stampfile('update_daemon.stamp')) {
112 die("error: unable to create stampfile\n");
115 // Call to the feed batch update function
116 // or regenerate feedbrowser cache
118 if (rand(0,100) > 30) {
119 update_daemon_common($link);
121 $count = update_feedbrowser_cache($link);
122 _debug("Feedbrowser updated, $count feeds processed.");
124 purge_orphans($link, true);
126 $rc = cleanup_tags($link, 14, 50000);
128 _debug("Cleaned $rc cached tags.");
130 get_linked_feeds($link);
135 if (in_array("-cleanup-tags", $op)) {
136 $rc = cleanup_tags($link, 14, 50000);
137 _debug("$rc tags deleted.\n");
140 if (in_array("-get-feeds", $op)) {
141 get_linked_feeds($link);
144 if (in_array("-import",$op)) {
145 $username = $argv[count($argv) - 2];
146 $filename = $argv[count($argv) - 1];
149 print "error: please specify username.\n";
153 if (!is_file($filename)) {
154 print "error: input filename ($filename) doesn't exist.\n";
158 _debug("importing $filename for user $username...\n");
160 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
162 if (db_num_rows($result) == 0) {
163 print "error: could not find user $username.\n";
167 $owner_uid = db_fetch_result($result, 0, "id");
169 perform_data_import($link, $filename, $owner_uid);
173 if (in_array("-indexes", $op)) {
174 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
175 _debug("Type 'yes' to continue.");
177 if (read_stdin() != 'yes')
180 _debug("clearing existing indexes...");
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'
188 $result = db_query($link, "SELECT index_name,table_name FROM
189 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
192 while ($line = db_fetch_assoc($result)) {
193 if (DB_TYPE
== "pgsql") {
194 $statement = "DROP INDEX " . $line["relname"];
197 $statement = "ALTER TABLE ".
198 $line['table_name']." DROP INDEX ".$line['index_name'];
201 db_query($link, $statement, false);
204 _debug("reading indexes from schema for: " . DB_TYPE
);
206 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
208 while ($line = fgets($fp)) {
211 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
212 $index = $matches[1];
213 $table = $matches[2];
215 $statement = "CREATE INDEX $index ON $table";
218 db_query($link, $statement);
223 _debug("unable to open schema file.");
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.");
233 if (read_stdin() != 'yes')
236 $work_dir = dirname(__FILE__
);
237 $parent_dir = dirname($work_dir);
239 if (!is_writable($work_dir) && !is_writable("$parent_dir")) {
240 _debug("Both current and parent directories should be writable as current user.");
244 if (!is_writable(sys_get_temp_dir())) {
245 _debug("System temporary directory should be writable as current user.");
249 _debug("Checking for tar...");
252 system("tar --version >/dev/null", $system_rc);
254 if ($system_rc != 0) {
255 _debug("Could not run tar executable (RC=$system_rc).");
259 _debug("Checking for latest version...");
261 $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"),
264 if (!is_array($version_info)) {
265 _debug("Unable to fetch version information.");
269 $target_version = $version_info["version"];
270 $target_dir = "$parent_dir/tt-rss-$target_version";
272 _debug("Target version: $target_version");
274 if (version_compare(VERSION
, $target_version) != -1 && !in_array("-force", $op)) {
275 _debug("You are on the latest version. Update not needed.");
278 if (file_exists($target_dir)) {
279 _debug("Target directory $target_dir already exists.");
283 _debug("Downloading checksums...");
284 $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt");
287 _debug("Could not download checksums.");
291 $md5sum_data = explode("\n", $md5sum_data);
293 $tarball_url = "http://tt-rss.org/download/tt-rss-$target_version.tar.gz";
294 $data = fetch_file_contents($tarball_url);
297 _debug("Could not download distribution tarball ($tarball_url).");
301 _debug("Verifying tarball checksum...");
303 $target_md5sum = false;
305 foreach ($md5sum_data as $line) {
306 $pair = explode(" ", $line);
308 if ($pair[1] == "tt-rss-$target_version.tar.gz") {
309 $target_md5sum = $pair[0];
314 if (!$target_md5sum) {
315 _debug("Unable to locate checksum for target version.");
319 $test_md5sum = md5($data);
321 if ($test_md5sum != $target_md5sum) {
322 _debug("Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum).");
326 $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss');
327 _debug("Saving download to $tmp_file");
329 if (!file_put_contents($tmp_file, $data)) {
330 _debug("Unable to save download.");
334 if (!chdir($parent_dir)) {
335 _debug("Unable to change into parent directory.");
339 $old_dir = tmpdirname($parent_dir, "tt-rss-old");
341 _debug("Renaming tt-rss directory to ".basename($old_dir));
342 if (!rename($work_dir, $old_dir)) {
343 _debug("Unable to rename tt-rss directory.");
347 _debug("Extracting tarball...");
348 system("tar zxf $tmp_file", $system_rc);
350 if ($system_rc != 0) {
351 _debug("Error while extracting tarball (RC=$system_rc).");
355 _debug("Renaming target directory...");
356 if (!rename($target_dir, $work_dir)) {
357 _debug("Unable to rename target directory.");
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.");
369 _debug("Cleaning up...");
372 _debug("Fixing permissions...");
374 $directories = array(
376 CACHE_DIR
. "/htmlpurifier",
377 CACHE_DIR
. "/export",
378 CACHE_DIR
. "/images",
379 CACHE_DIR
. "/magpie",
380 CACHE_DIR
. "/simplepie",
384 foreach ($directories as $dir) {
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.");
397 if ($lock_handle != false) {
398 fclose($lock_handle);
401 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
402 unlink(LOCK_DIRECTORY
. "/$lock_filename");