db-updater.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
  3. get_include_path());
  4. require_once "functions.php";
  5. require_once "sessions.php";
  6. require_once "sanity_check.php";
  7. require_once "config.php";
  8. require_once "db.php";
  9. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  10. if (!init_connection($link)) return;
  11. login_sequence($link);
  12. $owner_uid = $_SESSION["uid"];
  13. if (!SINGLE_USER_MODE && $_SESSION["access_level"] < 10) {
  14. $_SESSION["login_error_msg"] = __("Your access level is insufficient to run this script.");
  15. render_login_form($link);
  16. exit;
  17. }
  18. ?>
  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. <body>
  26. <script type='text/javascript'>
  27. function confirmOP() {
  28. return confirm(__("Update the database?"));
  29. }
  30. </script>
  31. <div class="floatingLogo"><img src="images/logo_small.png"></div>
  32. <h1><?php echo __("Database Updater") ?></h1>
  33. <div class="content">
  34. <?php
  35. function getline($fp, $delim) {
  36. $result = "";
  37. while(!feof($fp)) {
  38. $tmp = fgetc($fp);
  39. if($tmp == $delim) {
  40. return $result;
  41. }
  42. $result .= $tmp;
  43. }
  44. return $result;
  45. }
  46. $op = $_POST["op"];
  47. $result = db_query($link, "SELECT schema_version FROM ttrss_version");
  48. $version = db_fetch_result($result, 0, "schema_version");
  49. $update_files = glob("schema/versions/".DB_TYPE."/*sql");
  50. $update_versions = array();
  51. foreach ($update_files as $f) {
  52. $m = array();
  53. preg_match_all("/schema\/versions\/".DB_TYPE."\/(\d*)\.sql/", $f, $m,
  54. PREG_PATTERN_ORDER);
  55. if ($m[1][0]) {
  56. $update_versions[$m[1][0]] = $f;
  57. }
  58. }
  59. ksort($update_versions, SORT_NUMERIC);
  60. $latest_version = max(array_keys($update_versions));
  61. if ($version == $latest_version) {
  62. if ($version != SCHEMA_VERSION) {
  63. print_error(__("Could not update database"));
  64. print "<p>" .
  65. __("Could not find necessary schema file, need version:") .
  66. " " . SCHEMA_VERSION . __(", found: ") . $latest_version . "</p>";
  67. } else {
  68. print_notice(__("Tiny Tiny RSS database is up to date."));
  69. print "<form method=\"GET\" action=\"index.php\">
  70. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  71. </form>";
  72. }
  73. } else if ($version <= $latest_version && !$op) {
  74. print_warning(__("Please backup your database before proceeding."));
  75. print "<p>" . T_sprintf("Your Tiny Tiny RSS database needs update to the latest version (<b>%d</b> to <b>%d</b>).", $version, $latest_version) . "</p>";
  76. /* print "<p>Available incremental updates:";
  77. foreach (array_keys($update_versions) as $v) {
  78. if ($v > $version) {
  79. print " <a href='$update_versions[$v]'>$v</a>";
  80. }
  81. } */
  82. print "</p>";
  83. print "<form method='POST'>
  84. <input type='hidden' name='op' value='do'>
  85. <input type='submit' onclick='return confirmOP()' value='".__("Perform updates")."'>
  86. </form>";
  87. } else if ($op == "do") {
  88. print "<p>".__("Performing updates...")."</p>";
  89. $num_updates = 0;
  90. foreach (array_keys($update_versions) as $v) {
  91. if ($v == $version + 1) {
  92. print "<p>".T_sprintf("Updating to version %d...", $v)."</p>";
  93. db_query($link, "BEGIN");
  94. $fp = fopen($update_versions[$v], "r");
  95. if ($fp) {
  96. while (!feof($fp)) {
  97. $query = trim(getline($fp, ";"));
  98. if ($query != "") {
  99. print "<p class='query'>$query</p>";
  100. db_query($link, $query);
  101. }
  102. }
  103. }
  104. fclose($fp);
  105. db_query($link, "COMMIT");
  106. print "<p>".__("Checking version... ");
  107. $result = db_query($link, "SELECT schema_version FROM ttrss_version");
  108. $version = db_fetch_result($result, 0, "schema_version");
  109. if ($version == $v) {
  110. print __("OK!");
  111. } else {
  112. print "<b>".__("ERROR!")."</b>";
  113. return;
  114. }
  115. $num_updates++;
  116. }
  117. }
  118. print "<p>".sprintf(ngettext("Finished. Performed <b>%d</b> update up to schema version <b>%d</b>.",
  119. "Finished. Performed <b>%d</b> updates up to schema version <b>%d</b>.", $num_updates), $num_updates, $version)."</p>";
  120. print "<form method=\"GET\" action=\"backend.php\">
  121. <input type=\"hidden\" name=\"op\" value=\"logout\">
  122. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  123. </form>";
  124. } else if ($version >= $latest_version) {
  125. print_error(__("Your database schema is from a newer version of Tiny Tiny RSS."));
  126. print "<p>" . T_sprintf("Found schema version: <b>%d</b>, required: <b>%d</b>.", $version, $latest_version) . "</p>";
  127. print "<p>" . __("Schema upgrade impossible. Please update Tiny Tiny RSS files to the newer version and continue.") . "</p>";
  128. print "<form method=\"GET\" action=\"backend.php\">
  129. <input type=\"hidden\" name=\"op\" value=\"logout\">
  130. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  131. </form>";
  132. }
  133. ?>
  134. </div>
  135. </body>
  136. </html>