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