1 | <?php |
---|
2 | $stoppage = true; |
---|
3 | include('../common/php_compat.php'); |
---|
4 | |
---|
5 | $struct = $opt = $data = array(); |
---|
6 | $dir = opendir('./sql'); |
---|
7 | while ($file = readdir($dir)) |
---|
8 | { |
---|
9 | if (strpos($file, 'sql_') !== false) |
---|
10 | { |
---|
11 | $table = str_replace(array('sql_tblstrct_', 'sql_tbldata_opt_', 'sql_tbldata_', '.sql'), '', $file); |
---|
12 | $table = 'kb3_'.preg_replace('/(_p\d{1,4})/', '', $table); |
---|
13 | if (strpos($file, 'tblstrct')) |
---|
14 | { |
---|
15 | $structc++; |
---|
16 | $struct[$table][] = $file; |
---|
17 | } |
---|
18 | elseif (strpos($file, '_opt_')) |
---|
19 | { |
---|
20 | $dcnt++; |
---|
21 | $optcnt++; |
---|
22 | $opt[$table][] = $file; |
---|
23 | asort($opt[$table]); |
---|
24 | } |
---|
25 | else |
---|
26 | { |
---|
27 | $dcnt++; |
---|
28 | $datacnt++; |
---|
29 | $data[$table][] = $file; |
---|
30 | asort($data[$table]); |
---|
31 | } |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | $db = mysql_pconnect($_SESSION['sql']['host'], $_SESSION['sql']['user'], $_SESSION['sql']['pass']); |
---|
36 | mysql_select_db($_SESSION['sql']['db']); |
---|
37 | $result = mysql_query('show tables'); |
---|
38 | while ($row = mysql_fetch_row($result)) |
---|
39 | { |
---|
40 | $table = $row[0]; |
---|
41 | unset($struct[$table]); |
---|
42 | } |
---|
43 | |
---|
44 | if ($_REQUEST['sub'] == 'struct') |
---|
45 | { |
---|
46 | foreach ($struct as $table => $files) |
---|
47 | { |
---|
48 | foreach ($files as $file) |
---|
49 | { |
---|
50 | echo 'Creating table '.$table.' from file '.$file.'...'; |
---|
51 | $query = file_get_contents('./sql/'.$file); |
---|
52 | $id = mysql_query($query); |
---|
53 | if ($id) |
---|
54 | { |
---|
55 | echo 'done<br/>'; |
---|
56 | } |
---|
57 | else |
---|
58 | { |
---|
59 | echo 'error: '.mysql_error().'<br/>'; |
---|
60 | } |
---|
61 | } |
---|
62 | unset($struct[$table]); |
---|
63 | } |
---|
64 | } |
---|
65 | if ($_REQUEST['do'] == 'reset') |
---|
66 | { |
---|
67 | unset($_SESSION['sqlinsert']); |
---|
68 | unset($_SESSION['doopt']); |
---|
69 | } |
---|
70 | |
---|
71 | if ($_REQUEST['sub'] == 'data') |
---|
72 | { |
---|
73 | if (!isset($_SESSION['sqlinsert'])) |
---|
74 | { |
---|
75 | $_SESSION['sqlinsert'] = 1; |
---|
76 | if (isset($_POST['opt'])) |
---|
77 | { |
---|
78 | $_SESSION['useopt'] = array(); |
---|
79 | foreach ($_POST['opt'] as $table => $value) |
---|
80 | { |
---|
81 | $_SESSION['useopt'][] = $table; |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | $i = 0; |
---|
87 | $did = false; |
---|
88 | $errors = false; |
---|
89 | if (!isset($_SESSION['doopt'])) |
---|
90 | { |
---|
91 | foreach ($data as $table => $files) |
---|
92 | { |
---|
93 | foreach ($files as $file) |
---|
94 | { |
---|
95 | $i++; |
---|
96 | if ($_SESSION['sqlinsert'] > $i) |
---|
97 | { |
---|
98 | continue; |
---|
99 | } |
---|
100 | echo 'Inserting data ('.$i.'/'.$datacnt.') into '.$table.' from file '.$file.'...<br/>'; |
---|
101 | |
---|
102 | $error = ''; |
---|
103 | $querys = file('./sql/'.$file); |
---|
104 | $lines = count($querys); |
---|
105 | $errors = 0; |
---|
106 | foreach ($querys as $query) |
---|
107 | { |
---|
108 | if (trim($query)) |
---|
109 | { |
---|
110 | $query = trim($query); |
---|
111 | if (substr($query, -1, 1) == ';') |
---|
112 | { |
---|
113 | $query = substr($query, 0, -1); |
---|
114 | } |
---|
115 | $query_count++; |
---|
116 | $id = mysql_query($query); |
---|
117 | if (!$id) |
---|
118 | { |
---|
119 | $error .= 'error: '.mysql_error().'<br/>'; |
---|
120 | $errors++; |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | echo 'File '.$file.' had '.$lines.' lines with '.$query_count.' querys. '.$errors.' Querys failed.<br/>'; |
---|
125 | if (!$error) |
---|
126 | { |
---|
127 | echo 'done<br/>'; |
---|
128 | echo '<meta http-equiv="refresh" content="1; URL=?step=4&sub=data" />'; |
---|
129 | echo 'Automatic reload in 1s for next chunk. <a href="?step=4&sub=data">Manual Link</a><br/>'; |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | echo $error; |
---|
134 | echo '<meta http-equiv="refresh" content="20; URL=?step=4&sub=data" />'; |
---|
135 | echo 'Automatic reload in 20s for next chunk because of the error occured. <a href="?step=4&sub=data">Manual Link</a><br/>'; |
---|
136 | } |
---|
137 | $_SESSION['sqlinsert']++; |
---|
138 | |
---|
139 | $did = true; |
---|
140 | break 2; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | if (isset($_SESSION['useopt']) && !$did) |
---|
146 | { |
---|
147 | $i = 0; |
---|
148 | if (!isset($_SESSION['doopt'])) |
---|
149 | { |
---|
150 | $_SESSION['doopt'] = true; |
---|
151 | $_SESSION['sqlinsert'] = 1; |
---|
152 | } |
---|
153 | foreach ($opt as $table => $files) |
---|
154 | { |
---|
155 | if (!in_array($table, $_SESSION['useopt'])) |
---|
156 | { |
---|
157 | continue; |
---|
158 | } |
---|
159 | foreach ($files as $file) |
---|
160 | { |
---|
161 | $optsel++; |
---|
162 | } |
---|
163 | } |
---|
164 | foreach ($opt as $table => $files) |
---|
165 | { |
---|
166 | if (!in_array($table, $_SESSION['useopt'])) |
---|
167 | { |
---|
168 | continue; |
---|
169 | } |
---|
170 | foreach ($files as $file) |
---|
171 | { |
---|
172 | $i++; |
---|
173 | if ($_SESSION['sqlinsert'] > $i) |
---|
174 | { |
---|
175 | continue; |
---|
176 | } |
---|
177 | echo 'Inserting optional data ('.$i.'/'.$optsel.') into '.$table.' from file '.$file.'...'; |
---|
178 | $querys = file('./sql/'.$file); |
---|
179 | foreach ($querys as $query) |
---|
180 | { |
---|
181 | $query = trim($query); |
---|
182 | if ($query) |
---|
183 | { |
---|
184 | if (substr($query, -1, 1) == ';') |
---|
185 | { |
---|
186 | $query = substr($query, 0, -1); |
---|
187 | } |
---|
188 | $id = mysql_query($query); |
---|
189 | } |
---|
190 | } |
---|
191 | if ($id) |
---|
192 | { |
---|
193 | echo 'done<br/>'; |
---|
194 | } |
---|
195 | else |
---|
196 | { |
---|
197 | echo 'error: '.mysql_error().'<br/>'; |
---|
198 | } |
---|
199 | $_SESSION['sqlinsert']++; |
---|
200 | echo '<meta http-equiv="refresh" content="1; URL=?step=4&sub=data" />'; |
---|
201 | echo 'Automatic reload in 1s for next chunk. <a href="?step=4&sub=data">Manual Reload</a><br/>'; |
---|
202 | $did = true; |
---|
203 | break 2; |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | if (!$did) |
---|
208 | { |
---|
209 | $stoppage = false; |
---|
210 | echo 'All tables imported. Checking tables for correct data...<br/>'; |
---|
211 | $check = file('./sql/tbl_check.txt'); |
---|
212 | foreach ($check as $line) |
---|
213 | { |
---|
214 | $tmp = explode(chr(9), $line); |
---|
215 | $table = trim($tmp[0]); |
---|
216 | $count = trim($tmp[1]); |
---|
217 | echo 'Checking table '.$table.': '; |
---|
218 | $result = mysql_query('SELECT count(*) as cnt FROM '.$table); |
---|
219 | $test = mysql_fetch_array($result); |
---|
220 | if ($test['cnt'] != $count) |
---|
221 | { |
---|
222 | echo $test['cnt'].'/'.$count.' - FAILED'; |
---|
223 | $stoppage = true; |
---|
224 | } |
---|
225 | else |
---|
226 | { |
---|
227 | echo $test['cnt'].'/'.$count.' - PASSED'; |
---|
228 | } |
---|
229 | echo '<br/>'; |
---|
230 | } |
---|
231 | if ($stoppage) |
---|
232 | { |
---|
233 | echo 'There has been an error with one of the tables, please <a href="?step=4&do=reset">Reset</a> and try again.<br/>'; |
---|
234 | } |
---|
235 | else |
---|
236 | { |
---|
237 | echo '<br/>All tables passed.<br/>'; |
---|
238 | echo 'You can now create or search your corporation/alliance: <a href="?step=5">Next Step</a><br/>'; |
---|
239 | } |
---|
240 | } |
---|
241 | echo '<br/>Use <a href="?step=4&sub=datasel&do=reset">Reset</a> to step back to the sql-opt select.<br/>'; |
---|
242 | } |
---|
243 | ?> |
---|
244 | <div class="block-header2">MySQL Data Import</div> |
---|
245 | Found <?php echo $structc; ?> table structures and <?php echo $dcnt; ?> data files for <?php echo count($opt)+count($data); ?> tables.<br/> |
---|
246 | <?php |
---|
247 | |
---|
248 | $structadd = 0; |
---|
249 | foreach ($struct as $table => $file) |
---|
250 | { |
---|
251 | echo 'Table struct has to be added: '.$table.'<br/>'; |
---|
252 | $structadd++; |
---|
253 | } |
---|
254 | if (!$structadd && $_REQUEST['sub'] != 'datasel' && $_REQUEST['sub'] != 'data') |
---|
255 | { |
---|
256 | echo 'All table structures seem to be in the database.<br/>'; |
---|
257 | echo 'I will now check some table structures in case you are upgrading from a previous version... '; |
---|
258 | include('install_step4_tblchk.php'); |
---|
259 | echo 'done<br/>Please continue with <a href="?step=4&sub=datasel">Importing Data</a><br/>'; |
---|
260 | |
---|
261 | echo '<br/><br/>In case you aborted the install and you got already data in those table you can bypass the import now by with <a href="?step=5">this link</a><br/>'; |
---|
262 | echo 'To be sure i will check some table data for you now:<br/>'; |
---|
263 | $check = file('./sql/tbl_check.txt'); |
---|
264 | foreach ($check as $line) |
---|
265 | { |
---|
266 | $tmp = explode(chr(9), $line); |
---|
267 | $table = trim($tmp[0]); |
---|
268 | $count = trim($tmp[1]); |
---|
269 | echo 'Checking table '.$table.': '; |
---|
270 | $result = mysql_query('SELECT count(*) as cnt FROM '.$table); |
---|
271 | $test = mysql_fetch_array($result); |
---|
272 | $failed = 0; |
---|
273 | if ($test['cnt'] != $count) |
---|
274 | { |
---|
275 | echo $test['cnt'].'/'.$count.' - FAILED'; |
---|
276 | $failed++; |
---|
277 | } |
---|
278 | else |
---|
279 | { |
---|
280 | echo $test['cnt'].'/'.$count.' - PASSED'; |
---|
281 | } |
---|
282 | echo '<br/>'; |
---|
283 | } |
---|
284 | echo 'Checking table kb3_items: '; |
---|
285 | $result = mysql_query('SELECT count(*) as cnt FROM kb3_items'); |
---|
286 | $test = mysql_fetch_array($result); |
---|
287 | $failed = 0; |
---|
288 | if ($test['cnt'] <= 5000) |
---|
289 | { |
---|
290 | echo $test['cnt'].'<5000 - FAILED'; |
---|
291 | $failed++; |
---|
292 | } |
---|
293 | else |
---|
294 | { |
---|
295 | echo $test['cnt'].'>5000 - PASSED'; |
---|
296 | } |
---|
297 | echo '<br/>'; |
---|
298 | if (!$failed) |
---|
299 | { |
---|
300 | echo 'All important table data seems to be there, you are safe to bypass the import.<br/>'; |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | echo 'There was an error in one of the important tables, please run the import.<br/>'; |
---|
305 | } |
---|
306 | } |
---|
307 | elseif ($structadd) |
---|
308 | { |
---|
309 | echo 'Some table structures have to be added, please continue with <a href="?step=4&sub=struct">Creating Tables</a><br/>'; |
---|
310 | } |
---|
311 | |
---|
312 | if ($_REQUEST['sub'] == 'datasel') |
---|
313 | { |
---|
314 | ?> |
---|
315 | <p>Please select optional SQL data to be inserted into the database:<br/></p> |
---|
316 | <form id="options" name="options" method="post" action="?step=4"> |
---|
317 | <input type="hidden" name="step" value="4"> |
---|
318 | <input type="hidden" name="sub" value="data"> |
---|
319 | <table class="kb-subtable"> |
---|
320 | <?php |
---|
321 | foreach ($opt as $table => $files) |
---|
322 | { |
---|
323 | ?> |
---|
324 | <tr><td width="120"><b><?php echo $table; ?></b></td><td><input type="checkbox" name="opt[<?php echo $table; ?>]"></td></tr> |
---|
325 | <?php |
---|
326 | } |
---|
327 | ?> |
---|
328 | <tr><td width="120"></td><td><input type=submit name=submit value="Ok"></td></tr> |
---|
329 | </table> |
---|
330 | <?php |
---|
331 | } |
---|
332 | ?> |
---|
333 | <?php if ($stoppage) |
---|
334 | { |
---|
335 | return; |
---|
336 | }?> |
---|
337 | <p><a href="?step=<?php echo ($_SESSION['state']+1); ?>">Next Step</a></p> |
---|