db-updater.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  3. require_once "sessions.php";
  4. require_once "sanity_check.php";
  5. require_once "functions.php";
  6. require_once "config.php";
  7. require_once "db.php";
  8. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  9. init_connection($link);
  10. login_sequence($link);
  11. $owner_uid = $_SESSION["uid"];
  12. if (!SINGLE_USER_MODE && $_SESSION["access_level"] < 10) {
  13. $_SESSION["login_error_msg"] = __("Your access level is insufficient to run this script.");
  14. render_login_form($link);
  15. exit;
  16. }
  17. ?>
  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. <body>
  25. <script type='text/javascript'>
  26. function confirmOP() {
  27. return confirm(__("Update the database?"));
  28. }
  29. </script>
  30. <div class="floatingLogo"><img src="images/ttrss_logo.png"></div>
  31. <h1><?php echo __("Database Updater") ?></h1>
  32. <?php
  33. function getline($fp, $delim) {
  34. $result = "";
  35. while(!feof($fp)) {
  36. $tmp = fgetc($fp);
  37. if($tmp == $delim) {
  38. return $result;
  39. }
  40. $result .= $tmp;
  41. }
  42. return $result;
  43. }
  44. $op = $_POST["op"];
  45. $result = db_query($link, "SELECT schema_version FROM ttrss_version");
  46. $version = db_fetch_result($result, 0, "schema_version");
  47. $update_files = glob("schema/versions/".DB_TYPE."/*sql");
  48. $update_versions = array();
  49. foreach ($update_files as $f) {
  50. $m = array();
  51. preg_match_all("/schema\/versions\/".DB_TYPE."\/(\d*)\.sql/", $f, $m,
  52. PREG_PATTERN_ORDER);
  53. if ($m[1][0]) {
  54. $update_versions[$m[1][0]] = $f;
  55. }
  56. }
  57. ksort($update_versions, SORT_NUMERIC);
  58. $latest_version = max(array_keys($update_versions));
  59. if ($version == $latest_version) {
  60. if ($version != SCHEMA_VERSION) {
  61. print_error(__("Could not update database"));
  62. print "<p>" .
  63. __("Could not find necessary schema file, need version:") .
  64. " " . SCHEMA_VERSION . __(", found: ") . $latest_version . "</p>";
  65. } else {
  66. print "<p>".__("Tiny Tiny RSS database is up to date.")."</p>";
  67. print "<form method=\"GET\" action=\"tt-rss.php\">
  68. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  69. </form>";
  70. }
  71. return;
  72. }
  73. if (!$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. $fp = fopen($update_versions[$v], "r");
  94. if ($fp) {
  95. while (!feof($fp)) {
  96. $query = trim(getline($fp, ";"));
  97. if ($query != "") {
  98. print "<p class='query'>$query</p>";
  99. db_query($link, $query);
  100. }
  101. }
  102. }
  103. fclose($fp);
  104. print "<p>".__("Checking version... ");
  105. $result = db_query($link, "SELECT schema_version FROM ttrss_version");
  106. $version = db_fetch_result($result, 0, "schema_version");
  107. if ($version == $v) {
  108. print __("OK!");
  109. } else {
  110. print "<b>".__("ERROR!")."</b>";
  111. return;
  112. }
  113. $num_updates++;
  114. }
  115. }
  116. print "<p>".T_sprintf("Finished. Performed <b>%d</b> update(s) up to schema
  117. version <b>%d</b>.", $num_updates, $version)."</p>";
  118. print "<form method=\"GET\" action=\"logout.php\">
  119. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  120. </form>";
  121. }
  122. ?>
  123. </body>
  124. </html>