]>
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('PHP_EXECUTABLE'))
18 define('PHP_EXECUTABLE', '/usr/bin/php');
22 if (count($argv) == 1 ||
in_array("-help", $op) ) {
23 print "Tiny Tiny RSS data update script.\n\n";
25 print " -feeds - update feeds\n";
26 print " -feedbrowser - update feedbrowser\n";
27 print " -daemon - start single-process update daemon\n";
28 print " -cleanup-tags - perform tags table maintenance\n";
29 print " -get-feeds - receive popular feeds from linked instances\n";
30 print " -import USER FILE - import articles from XML\n";
31 print " -update-self - update tt-rss installation to latest version\n";
32 print " -quiet - don't show messages\n";
33 print " -indexes - recreate missing schema indexes\n";
34 print " -help - show this help\n";
38 define('QUIET', in_array("-quiet", $op));
40 if (!in_array("-daemon", $op)) {
41 $lock_filename = "update.lock";
43 $lock_filename = "update_daemon.lock";
46 $lock_handle = make_lockfile($lock_filename);
49 // Try to lock a file in order to avoid concurrent update.
51 die("error: Can't create lockfile ($lock_filename). ".
52 "Maybe another update process is already running.\n");
55 // Create a database connection.
56 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
58 init_connection($link);
60 if (in_array("-feeds", $op)) {
61 // Update all feeds needing a update.
62 update_daemon_common($link);
65 $count = update_feedbrowser_cache($link);
66 _debug("Feedbrowser updated, $count feeds processed.");
68 // Purge orphans and cleanup tags
69 purge_orphans($link, true);
71 $rc = cleanup_tags($link, 14, 50000);
72 _debug("Cleaned $rc cached tags.");
74 get_linked_feeds($link);
77 if (in_array("-feedbrowser", $op)) {
78 $count = update_feedbrowser_cache($link);
79 print "Finished, $count feeds processed.\n";
82 if (in_array("-daemon", $op)) {
83 $op = array_diff($op, array("-daemon"));
85 passthru(PHP_EXECUTABLE
. " " . implode(' ', $op) . " -daemon-loop");
86 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL
. " seconds...");
87 sleep(DAEMON_SLEEP_INTERVAL
);
91 if (in_array("-daemon-loop", $op)) {
92 if (!make_stampfile('update_daemon.stamp')) {
93 die("error: unable to create stampfile\n");
96 // Call to the feed batch update function
97 // or regenerate feedbrowser cache
99 if (rand(0,100) > 30) {
100 update_daemon_common($link);
102 $count = update_feedbrowser_cache($link);
103 _debug("Feedbrowser updated, $count feeds processed.");
105 purge_orphans($link, true);
107 $rc = cleanup_tags($link, 14, 50000);
109 _debug("Cleaned $rc cached tags.");
111 get_linked_feeds($link);
116 if (in_array("-cleanup-tags", $op)) {
117 $rc = cleanup_tags($link, 14, 50000);
118 _debug("$rc tags deleted.\n");
121 if (in_array("-get-feeds", $op)) {
122 get_linked_feeds($link);
125 if (in_array("-import",$op)) {
126 $username = $argv[count($argv) - 2];
127 $filename = $argv[count($argv) - 1];
130 print "error: please specify username.\n";
134 if (!is_file($filename)) {
135 print "error: input filename ($filename) doesn't exist.\n";
139 _debug("importing $filename for user $username...\n");
141 $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$username'");
143 if (db_num_rows($result) == 0) {
144 print "error: could not find user $username.\n";
148 $owner_uid = db_fetch_result($result, 0, "id");
150 perform_data_import($link, $filename, $owner_uid);
154 if (in_array("-indexes", $op)) {
155 _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!");
156 _debug("Type 'yes' to continue.");
158 if (read_stdin() != 'yes')
161 _debug("clearing existing indexes...");
163 if (DB_TYPE
== "pgsql") {
164 $result = db_query($link, "SELECT relname FROM
165 pg_catalog.pg_class WHERE relname LIKE 'ttrss_%'
166 AND relname NOT LIKE '%_pkey'
169 $result = db_query($link, "SELECT index_name,table_name FROM
170 information_schema.statistics WHERE index_name LIKE 'ttrss_%'");
173 while ($line = db_fetch_assoc($result)) {
174 if (DB_TYPE
== "pgsql") {
175 $statement = "DROP INDEX " . $line["relname"];
178 $statement = "ALTER TABLE ".
179 $line['table_name']." DROP INDEX ".$line['index_name'];
182 db_query($link, $statement, false);
185 _debug("reading indexes from schema for: " . DB_TYPE
);
187 $fp = fopen("schema/ttrss_schema_" . DB_TYPE
. ".sql", "r");
189 while ($line = fgets($fp)) {
192 if (preg_match("/^create index ([^ ]+) on ([^ ]+)$/i", $line, $matches)) {
193 $index = $matches[1];
194 $table = $matches[2];
196 $statement = "CREATE INDEX $index ON $table";
199 db_query($link, $statement);
204 _debug("unable to open schema file.");
209 if (in_array("-update-self", $op)) {
210 _debug("Warning: self-updating is experimental. Use at your own risk.");
211 _debug("Please backup your tt-rss directory before continuing. Your database will not be modified.");
212 _debug("Type 'yes' to continue.");
214 if (read_stdin() != 'yes')
217 $work_dir = dirname(__FILE__
);
218 $parent_dir = dirname($work_dir);
220 if (!is_writable($work_dir) && !is_writable("$parent_dir")) {
221 _debug("Both current and parent directories should be writable as current user.");
225 if (!is_writable(sys_get_temp_dir())) {
226 _debug("System temporary directory should be writable as current user.");
230 _debug("Checking for tar...");
233 system("tar --version >/dev/null", $system_rc);
235 if ($system_rc != 0) {
236 _debug("Could not run tar executable (RC=$system_rc).");
240 _debug("Checking for latest version...");
242 $version_info = json_decode(fetch_file_contents("http://tt-rss.org/version.php"),
245 if (!is_array($version_info)) {
246 _debug("Unable to fetch version information.");
250 $target_version = $version_info["version"];
251 $target_dir = "$parent_dir/tt-rss-$target_version";
253 _debug("Target version: $target_version");
255 if (version_compare(VERSION
, $target_version) != -1 && !in_array("-force", $op)) {
256 _debug("You are on latest version. Update not needed.");
259 if (file_exists($target_dir)) {
260 _debug("Target directory $target_dir already exists.");
264 _debug("Downloading checksums...");
265 $md5sum_data = fetch_file_contents("http://tt-rss.org/download/md5sum.txt");
268 _debug("Could not download checksums.");
272 $md5sum_data = explode("\n", $md5sum_data);
274 $tarball_url = "http://tt-rss.org/download/tt-rss-$target_version.tar.gz";
275 $data = fetch_file_contents($tarball_url);
278 _debug("Could not download distribution tarball ($tarball_url).");
282 _debug("Verifying tarball checksum...");
284 $target_md5sum = false;
286 foreach ($md5sum_data as $line) {
287 $pair = explode(" ", $line);
289 if ($pair[1] == "tt-rss-$target_version.tar.gz") {
290 $target_md5sum = $pair[0];
295 if (!$target_md5sum) {
296 _debug("Unable to locate checksum for target version.");
300 $test_md5sum = md5($data);
302 if ($test_md5sum != $target_md5sum) {
303 _debug("Downloaded checksum doesn't match (got $test_md5sum, expected $target_md5sum).");
307 $tmp_file = tempnam(sys_get_temp_dir(), 'tt-rss');
308 _debug("Saving download to $tmp_file");
310 if (!file_put_contents($tmp_file, $data)) {
311 _debug("Unable to save download.");
315 if (!chdir($parent_dir)) {
316 _debug("Unable to change into parent directory.");
320 $old_dir = tmpdirname($parent_dir, "tt-rss-old");
322 _debug("Renaming current directory to ".basename($old_dir));
323 if (!rename($work_dir, $old_dir)) {
324 _debug("Unable to rename current directory.");
328 _debug("Extracting tarball...");
329 system("tar zxf $tmp_file", $system_rc);
331 if ($system_rc != 0) {
332 _debug("Error while extracting tarball (RC=$system_rc).");
336 _debug("Renaming target directory...");
337 if (!rename($target_dir, $work_dir)) {
338 _debug("Unable to rename target directory.");
344 _debug("Copying config.php...");
345 if (!copy("$old_dir/config.php", "$work_dir/config.php")) {
346 _debug("Unable to copy config.php to $work_dir.");
350 _debug("Cleaning up...");
353 _debug("Fixing permissions...");
355 $directories = array(
357 CACHE_DIR
. "/htmlpurifier",
358 CACHE_DIR
. "/export",
359 CACHE_DIR
. "/images",
360 CACHE_DIR
. "/magpie",
361 CACHE_DIR
. "/simplepie",
365 foreach ($directories as $dir) {
370 _debug("Upgrade completed.");
371 _debug("Your old tt-rss directory is saved at $old_dir. ".
372 "Please migrate locally modified files (if any) and remove it.");
373 _debug("You might need to re-enter current directory in shell to see new files.");
378 if ($lock_handle != false) {
379 fclose($lock_handle);
382 if (file_exists(LOCK_DIRECTORY
. "/$lock_filename"))
383 unlink(LOCK_DIRECTORY
. "/$lock_filename");