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