update.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. require_once "sessions.php";
  3. require_once "sanity_check.php";
  4. require_once "functions.php";
  5. require_once "config.php";
  6. require_once "db.php";
  7. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  8. if (DB_TYPE == "pgsql") {
  9. pg_query($link, "set client_encoding = 'utf-8'");
  10. pg_set_client_encoding("UNICODE");
  11. }
  12. login_sequence($link);
  13. $owner_uid = $_SESSION["uid"];
  14. if ($_SESSION["access_level"] < 10) {
  15. header("Location: login.php"); die;
  16. }
  17. define('SCHEMA_VERSION', 13);
  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="update.css">
  24. </head>
  25. <body>
  26. <script type='text/javascript'>
  27. function confirmOP() {
  28. return confirm("Update the database?");
  29. }
  30. </script>
  31. <h1>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. print "<p>Tiny Tiny RSS database is up to date (version $version).</p>";
  61. print "<p><a href='tt-rss.php'>Return to Tiny Tiny RSS</a></p>";
  62. return;
  63. }
  64. if (!$op) {
  65. print "<p class='warning'><b>Warning:</b> Please backup your database before proceeding.</p>";
  66. print "<p>Your Tiny Tiny RSS database needs update to the latest
  67. version ($version &mdash;&gt; $latest_version).</p>";
  68. /* print "<p>Available incremental updates:";
  69. foreach (array_keys($update_versions) as $v) {
  70. if ($v > $version) {
  71. print " <a href='$update_versions[$v]'>$v</a>";
  72. }
  73. } */
  74. print "</p>";
  75. print "<form method='POST'>
  76. <input type='hidden' name='op' value='do'>
  77. <input type='submit' onclick='return confirmOP()' value='Perform updates'>
  78. </form>";
  79. } else if ($op == "do") {
  80. print "<p>Performing updates (from version $version)...</p>";
  81. $num_updates = 0;
  82. foreach (array_keys($update_versions) as $v) {
  83. if ($v == $version + 1) {
  84. print "<p>Updating to version $v...</p>";
  85. $fp = fopen($update_versions[$v], "r");
  86. if ($fp) {
  87. while (!feof($fp)) {
  88. $query = trim(getline($fp, ";"));
  89. if ($query != "") {
  90. print "<p class='query'><b>QUERY:</b> $query</p>";
  91. db_query($link, $query);
  92. }
  93. }
  94. }
  95. fclose($fp);
  96. print "<p>Checking version... ";
  97. $result = db_query($link, "SELECT schema_version FROM ttrss_version");
  98. $version = db_fetch_result($result, 0, "schema_version");
  99. if ($version == $v) {
  100. print "OK! ($version)";
  101. } else {
  102. print "<b>ERROR!</b>";
  103. return;
  104. }
  105. $num_updates++;
  106. }
  107. }
  108. print "<p>Finished. Performed $num_updates updates up to schema
  109. version $version.</p>";
  110. print "<p><a href='tt-rss.php'>Return to Tiny Tiny RSS</a></p>";
  111. }
  112. ?>
  113. </body>
  114. </html>