]> git.wh0rd.org - tt-rss.git/blame - db-updater.php
experimental split of public calls into public.php (refs #389)
[tt-rss.git] / db-updater.php
CommitLineData
661135c7 1<?php
fb074239 2 require_once "functions.php";
661135c7 3 require_once "sessions.php";
661135c7 4 require_once "sanity_check.php";
661135c7
AD
5 require_once "config.php";
6 require_once "db.php";
661135c7 7
133ab8c7
AD
8 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
9
10 init_connection($link);
661135c7 11 login_sequence($link);
133ab8c7 12
661135c7 13 $owner_uid = $_SESSION["uid"];
133ab8c7
AD
14
15 if (!SINGLE_USER_MODE && $_SESSION["access_level"] < 10) {
661135c7
AD
16 $_SESSION["login_error_msg"] = __("Your access level is insufficient to run this script.");
17 render_login_form($link);
18 exit;
19 }
20
21
22?>
23
24<html>
25<head>
26<title>Database Updater</title>
27<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
28<link rel="stylesheet" type="text/css" href="utility.css">
29</head>
30
31<body>
32
33<script type='text/javascript'>
34function confirmOP() {
35 return confirm(__("Update the database?"));
36}
37</script>
38
0ae2bb2a 39<div class="floatingLogo"><img src="images/logo_wide.png"></div>
661135c7
AD
40
41<h1><?php echo __("Database Updater") ?></h1>
42
43<?php
44 function getline($fp, $delim) {
45 $result = "";
46 while(!feof($fp)) {
47 $tmp = fgetc($fp);
133ab8c7 48
661135c7
AD
49 if($tmp == $delim) {
50 return $result;
51 }
52 $result .= $tmp;
53 }
54 return $result;
55 }
133ab8c7 56
661135c7 57 $op = $_POST["op"];
133ab8c7 58
661135c7
AD
59 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
60 $version = db_fetch_result($result, 0, "schema_version");
133ab8c7 61
661135c7
AD
62 $update_files = glob("schema/versions/".DB_TYPE."/*sql");
63 $update_versions = array();
133ab8c7 64
661135c7
AD
65 foreach ($update_files as $f) {
66 $m = array();
67 preg_match_all("/schema\/versions\/".DB_TYPE."\/(\d*)\.sql/", $f, $m,
68 PREG_PATTERN_ORDER);
133ab8c7 69
661135c7
AD
70 if ($m[1][0]) {
71 $update_versions[$m[1][0]] = $f;
72 }
73 }
133ab8c7 74
661135c7 75 ksort($update_versions, SORT_NUMERIC);
133ab8c7 76
661135c7
AD
77 $latest_version = max(array_keys($update_versions));
78
79 if ($version == $latest_version) {
80
81 if ($version != SCHEMA_VERSION) {
82 print_error(__("Could not update database"));
83
133ab8c7 84 print "<p>" .
661135c7
AD
85 __("Could not find necessary schema file, need version:") .
86 " " . SCHEMA_VERSION . __(", found: ") . $latest_version . "</p>";
87
88 } else {
6ef0c9c3 89 print_notice(__("Tiny Tiny RSS database is up to date."));
661135c7
AD
90 print "<form method=\"GET\" action=\"tt-rss.php\">
91 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
92 </form>";
93 }
94
0ae2bb2a 95 } else if ($version <= $latest_version && !$op) {
133ab8c7 96
661135c7 97 print_warning(__("Please backup your database before proceeding."));
133ab8c7 98
661135c7 99 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>";
133ab8c7 100
661135c7 101 /* print "<p>Available incremental updates:";
133ab8c7 102
661135c7
AD
103 foreach (array_keys($update_versions) as $v) {
104 if ($v > $version) {
105 print " <a href='$update_versions[$v]'>$v</a>";
106 }
107 } */
133ab8c7 108
661135c7 109 print "</p>";
133ab8c7 110
661135c7
AD
111 print "<form method='POST'>
112 <input type='hidden' name='op' value='do'>
113 <input type='submit' onclick='return confirmOP()' value='".__("Perform updates")."'>
114 </form>";
133ab8c7 115
661135c7 116 } else if ($op == "do") {
133ab8c7 117
661135c7 118 print "<p>".__("Performing updates...")."</p>";
133ab8c7 119
661135c7 120 $num_updates = 0;
133ab8c7 121
661135c7
AD
122 foreach (array_keys($update_versions) as $v) {
123 if ($v == $version + 1) {
124 print "<p>".T_sprintf("Updating to version %d...", $v)."</p>";
125 $fp = fopen($update_versions[$v], "r");
126 if ($fp) {
127 while (!feof($fp)) {
128 $query = trim(getline($fp, ";"));
129 if ($query != "") {
130 print "<p class='query'>$query</p>";
131 db_query($link, $query);
132 }
133 }
134 }
135 fclose($fp);
133ab8c7 136
661135c7 137 print "<p>".__("Checking version... ");
133ab8c7 138
661135c7
AD
139 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
140 $version = db_fetch_result($result, 0, "schema_version");
133ab8c7 141
661135c7
AD
142 if ($version == $v) {
143 print __("OK!");
144 } else {
145 print "<b>".__("ERROR!")."</b>";
146 return;
147 }
133ab8c7 148
661135c7
AD
149 $num_updates++;
150 }
151 }
133ab8c7 152
661135c7
AD
153 print "<p>".T_sprintf("Finished. Performed <b>%d</b> update(s) up to schema
154 version <b>%d</b>.", $num_updates, $version)."</p>";
133ab8c7 155
2376ad49
AD
156 print "<form method=\"GET\" action=\"backend.php\">
157 <input type=\"hidden\" name=\"op\" value=\"logout\">
661135c7
AD
158 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
159 </form>";
160
0ae2bb2a
AD
161 } else if ($version >= $latest_version) {
162
163 print_error(__("Your database schema is from a newer version of Tiny Tiny RSS."));
164
165 print "<p>" . T_sprintf("Found schema version: <b>%d</b>, required: <b>%d</b>.", $version, $latest_version) . "</p>";
166
167 print "<p>" . __("Schema upgrade impossible. Please update Tiny Tiny RSS files to the newer version and continue.") . "</p>";
168
169 print "<form method=\"GET\" action=\"backend.php\">
170 <input type=\"hidden\" name=\"op\" value=\"logout\">
171 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
172 </form>";
173
174
661135c7 175 }
133ab8c7 176
661135c7
AD
177?>
178
179</body>
180</html>
181