]>
Commit | Line | Data |
---|---|---|
fecd57c8 AD |
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 | header("Location: login.php"); die; | |
22 | } | |
23 | ||
24 | define('SCHEMA_VERSION', 13); | |
25 | ||
26 | ?> | |
27 | ||
28 | <html> | |
29 | <head> | |
30 | <title>Database Updater</title> | |
31 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
32 | <link rel="stylesheet" type="text/css" href="update.css"> | |
33 | </head> | |
34 | ||
35 | <body> | |
36 | ||
37 | <h1>Database Updater</h1> | |
38 | ||
39 | <?php | |
40 | function getline($fp, $delim) { | |
41 | $result = ""; | |
42 | while(!feof($fp)) { | |
43 | $tmp = fgetc($fp); | |
44 | ||
45 | if($tmp == $delim) { | |
46 | return $result; | |
47 | } | |
48 | $result .= $tmp; | |
49 | } | |
50 | return $result; | |
51 | } | |
52 | ||
53 | $op = $_REQUEST["op"]; | |
54 | ||
55 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
56 | $version = db_fetch_result($result, 0, "schema_version"); | |
57 | ||
58 | $update_files = glob("schema/versions/".DB_TYPE."/*sql"); | |
59 | $update_versions = array(); | |
60 | ||
61 | foreach ($update_files as $f) { | |
62 | $m = array(); | |
63 | preg_match_all("/schema\/versions\/".DB_TYPE."\/(\d*)\.sql/", $f, $m, | |
64 | PREG_PATTERN_ORDER); | |
65 | ||
66 | if ($m[1][0]) { | |
67 | $update_versions[$m[1][0]] = $f; | |
68 | } | |
69 | } | |
70 | ||
71 | ksort($update_versions, SORT_NUMERIC); | |
72 | ||
73 | $latest_version = max(array_keys($update_versions)); | |
74 | ||
75 | if ($version == $latest_version) { | |
76 | print "Database schema is up to date (version $version)."; | |
77 | return; | |
78 | } | |
79 | ||
80 | if (!$op) { | |
81 | print "<p class='warning'><b>Warning:</b> Please backup your database before proceeding.</p>"; | |
82 | ||
83 | print "<p>Your database schema version is $version. Latest version is ". | |
84 | "$latest_version.</p>"; | |
85 | ||
86 | print "<p>Available incremental updates:"; | |
87 | ||
88 | foreach (array_keys($update_versions) as $v) { | |
89 | if ($v > $version) { | |
90 | print " <a href='$update_versions[$v]'>$v</a>"; | |
91 | } | |
92 | } | |
93 | ||
94 | print "</p>"; | |
95 | print "<a href='update.php?op=do'>Click here to perform updates.</a>"; | |
96 | ||
97 | } else if ($op == "do") { | |
98 | ||
99 | print "<p>Performing updates (version: $version)...</p>"; | |
100 | ||
101 | $num_updates = 0; | |
102 | ||
103 | foreach (array_keys($update_versions) as $v) { | |
104 | if ($v == $version + 1) { | |
105 | print "<p>Updating to version $v...</p>"; | |
106 | $fp = fopen($update_versions[$v], "r"); | |
107 | if ($fp) { | |
108 | while (!feof($fp)) { | |
109 | $query = trim(getline($fp, ";")); | |
110 | if ($query != "") { | |
111 | print "<p class='query'><b>QUERY:</b> $query</p>"; | |
112 | db_query($link, $query); | |
113 | } | |
114 | } | |
115 | } | |
116 | fclose($fp); | |
117 | ||
118 | print "<p>Checking version... "; | |
119 | ||
120 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
121 | $version = db_fetch_result($result, 0, "schema_version"); | |
122 | ||
123 | if ($version == $v) { | |
124 | print "OK!"; | |
125 | } else { | |
126 | print "<b>ERROR!</b>"; | |
127 | return; | |
128 | } | |
129 | ||
130 | $num_updates++; | |
131 | } | |
132 | } | |
133 | ||
134 | print "<p>Finished. Performed $num_updates updates up to schema | |
135 | version $version.</p>"; | |
136 | } | |
137 | ?> | |
138 | ||
139 | ||
140 | </body> | |
141 | </html> | |
142 |