]> git.wh0rd.org - tt-rss.git/blob - update.php
set required error_reporting just in case
[tt-rss.git] / update.php
1 <?php
2 error_reporting(E_ERROR | E_WARNING | E_PARSE);
3
4 require_once "sessions.php";
5
6 require_once "sanity_check.php";
7 require_once "functions.php";
8 require_once "config.php";
9 require_once "db.php";
10
11 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
12
13 if (DB_TYPE == "pgsql") {
14 pg_query($link, "set client_encoding = 'utf-8'");
15 pg_set_client_encoding("UNICODE");
16 }
17
18 login_sequence($link);
19
20 $owner_uid = $_SESSION["uid"];
21
22 if ($_SESSION["access_level"] < 10) {
23 print "<p>Error: your access level is insufficient to run this script.</p>";
24 exit;
25 }
26
27 define('SCHEMA_VERSION', 13);
28
29 ?>
30
31 <html>
32 <head>
33 <title>Database Updater</title>
34 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
35 <link rel="stylesheet" type="text/css" href="utility.css">
36 </head>
37
38 <body>
39
40 <script type='text/javascript'>
41 function confirmOP() {
42 return confirm("Update the database?");
43 }
44 </script>
45
46 <div class="floatingLogo"><img src="images/ttrss_logo.png"></div>
47
48 <h1>Database Updater</h1>
49
50 <?php
51 function getline($fp, $delim) {
52 $result = "";
53 while(!feof($fp)) {
54 $tmp = fgetc($fp);
55
56 if($tmp == $delim) {
57 return $result;
58 }
59 $result .= $tmp;
60 }
61 return $result;
62 }
63
64 $op = $_POST["op"];
65
66 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
67 $version = db_fetch_result($result, 0, "schema_version");
68
69 $update_files = glob("schema/versions/".DB_TYPE."/*sql");
70 $update_versions = array();
71
72 foreach ($update_files as $f) {
73 $m = array();
74 preg_match_all("/schema\/versions\/".DB_TYPE."\/(\d*)\.sql/", $f, $m,
75 PREG_PATTERN_ORDER);
76
77 if ($m[1][0]) {
78 $update_versions[$m[1][0]] = $f;
79 }
80 }
81
82 ksort($update_versions, SORT_NUMERIC);
83
84 $latest_version = max(array_keys($update_versions));
85
86 if ($version == $latest_version) {
87 print "<p>Tiny Tiny RSS database is up to date (version $version).</p>";
88 print "<form method=\"GET\" action=\"tt-rss.php\">
89 <input type=\"submit\" value=\"Return to Tiny Tiny RSS\">
90 </form>";
91
92 return;
93 }
94
95 if (!$op) {
96 print "<p class='warning'><b>Warning:</b> Please backup your database before proceeding.</p>";
97
98 print "<p>Your Tiny Tiny RSS database needs update to the latest
99 version ($version &mdash;&gt; $latest_version).</p>";
100
101 /* print "<p>Available incremental updates:";
102
103 foreach (array_keys($update_versions) as $v) {
104 if ($v > $version) {
105 print " <a href='$update_versions[$v]'>$v</a>";
106 }
107 } */
108
109 print "</p>";
110
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>";
115
116 } else if ($op == "do") {
117
118 print "<p>Performing updates (from version $version)...</p>";
119
120 $num_updates = 0;
121
122 foreach (array_keys($update_versions) as $v) {
123 if ($v == $version + 1) {
124 print "<p>Updating to version $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'><b>QUERY:</b> $query</p>";
131 db_query($link, $query);
132 }
133 }
134 }
135 fclose($fp);
136
137 print "<p>Checking version... ";
138
139 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
140 $version = db_fetch_result($result, 0, "schema_version");
141
142 if ($version == $v) {
143 print "OK! ($version)";
144 } else {
145 print "<b>ERROR!</b>";
146 return;
147 }
148
149 $num_updates++;
150 }
151 }
152
153 print "<p>Finished. Performed $num_updates updates up to schema
154 version $version.</p>";
155
156 print "<form method=\"GET\" action=\"tt-rss.php\">
157 <input type=\"submit\" value=\"Return to Tiny Tiny RSS\">
158 </form>";
159
160 }
161
162 ?>
163
164 </body>
165 </html>
166