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