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