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