source: trunk/import/import.php@ 456

Last change on this file since 456 was 456, checked in by george, 15 years ago
File size: 14.4 KB
Line 
1<?php
2
3class Import
4{
5 var $Version;
6 var $Group;
7 var $NewItemCount;
8 var $System;
9
10 function __construct($System)
11 {
12 $this->System = &$System;
13 }
14
15 function SetVersion($Version)
16 {
17 global $System;
18
19 $DbResult = $System->Database->query('SELECT * FROM `ClientVersion` WHERE `Version` = "'.$Version.'"');
20 $this->Version = $DbResult->fetch_assoc();
21 }
22
23 function InsertItem($Value)
24 {
25 $Columns = '';
26 foreach($this->Group['Items'] as $GroupItem)
27 {
28 $Columns .= ', `'.$GroupItem['Column'].'` ';
29 }
30 $Columns = substr($Columns, 1);
31 $Where = ' (`'.$this->Group['PrimaryKeyItem'].'` = "'.$Value[$this->Group['PrimaryKeyItem']].'")';
32 //print_r($Value);
33
34 $DbResult2 = $this->System->Database->query('SELECT `VersionEnd`, `ID`, `Entry`, '.$Columns.' FROM `'.$this->Group['TablePrefix'].'` WHERE '.$Where.' AND (`Language`=0) ORDER BY `VersionStart` DESC LIMIT 1');
35 //echo('SELECT `VersionEnd`, `ID`, `Entry`, '.$Columns.' FROM `'.$this->Group['TablePrefix'].'` WHERE '.$Where.' AND (`Language`=0) ORDER BY `VersionStart` DESC LIMIT 1');
36 if(isset($DbResult2) and ($DbResult2->num_rows > 0))
37 {
38 // Update existed text
39 $DbRow2 = $DbResult2->fetch_assoc();
40 if($this->HaveSameText($this->Group, $DbRow2, $Value))
41 {
42 if($DbRow2['VersionEnd'] <> $this->Version['BuildNumber'])
43 {
44 $this->System->Database->query('UPDATE `'.$this->Group['TablePrefix'].'` SET `VersionEnd` = "'.$this->Version['BuildNumber'].'" WHERE `ID`='.$DbRow2['ID']);
45 echo(', ');
46 } else echo('. ');
47 } else
48 {
49 $Columns = '`Entry`, `Language`, `VersionStart`, `VersionEnd`';
50 $Values = $DbRow2['Entry'].', 0, '.$this->Version['BuildNumber'].', '.$this->Version['BuildNumber'];
51 foreach($this->Group['Items'] as $GroupItem)
52 {
53 $Columns .= ', `'.$GroupItem['Column'].'`';
54 $Values .= ', "'.$Value[$GroupItem['Column']].'"';
55 }
56 $this->System->Database->query('INSERT `'.$this->Group['TablePrefix'].'` ('.$Columns.') VALUES ('.$Values.')');
57 echo('# ');
58 $InsertId = $this->System->Database->insert_id;
59 WriteLog('Text <a href="form.php?group='.$this->Group['Id'].'&amp;ID='.$InsertId.'">'.$InsertId.'</a> ('.$DbRow2['Entry'].') ze skupiny '.$this->Group['Name'].' byl v nové verzi '.$this->Version['Version'].' změněn.', LOG_TYPE_IMPORT);
60 }
61 } else
62 {
63 // Insert new text
64 if(is_numeric($Value[$this->Group['PrimaryKeyItem']])) $Value['Entry'] = $Value[$this->Group['PrimaryKeyItem']];
65 else
66 {
67 // Get new unused Entry for tables without numeric id
68 $Value['Entry'] = 1;
69 $DbResult = $this->System->Database->query('SELECT MAX(`Entry`) FROM `'.$this->Group['TablePrefix'].'`');
70 if($DbResult->num_rows > 0)
71 {
72 $DbRow = $DbResult->fetch_row();
73 $Value['Entry'] += $DbRow[0];
74 }
75 }
76 $Columns = '`Entry`, `Language`, `VersionStart`, `VersionEnd`';
77 $Values = $Value['Entry'].', 0, '.$this->Version['BuildNumber'].', '.$this->Version['BuildNumber'];
78 foreach($this->Group['Items'] as $GroupItem)
79 {
80 $Columns .= ', `'.$GroupItem['Column'].'`';
81 $Values .= ', "'.$Value[$GroupItem['Column']].'"';
82 }
83 $this->System->Database->query('INSERT `'.$this->Group['TablePrefix'].'` ('.$Columns.') VALUES ('.$Values.')');
84 $InsertId = $this->System->Database->insert_id;
85 echo('+ ');
86 $this->NewItemCount++;
87 WriteLog('Text <a href="form.php?group='.$this->Group['Id'].'&amp;ID='.$InsertId.'">'.$InsertId.'</a> ('.$Value['Entry'].') ze skupiny '.$this->Group['Name'].' byl v nové verzi '.$this->Version['Version'].' přidán.', LOG_TYPE_IMPORT);
88 }
89 }
90
91 function ImportLUA()
92 {
93 global $TranslationTree, $PatchVersion;
94
95 echo('Načítání textů z LUA souboru...');
96 if(($this->Group['LuaFileName'] != '') and ($this->Group['TablePrefix'] != ''))
97 {
98 echo('<br />'.$this->Group['Name'].'<br />');
99 if($this->Group['LastVersion'] < $this->Version['BuildNumber'])
100 {
101 $File = new FileStream();
102 $File->OpenFile('../source/'.$this->Version['Version'].'/lua/'.$this->Group['LuaFileName'].'.lua');
103 $this->NewItemCount = 0;
104 $Count = 0;
105 while(!$File->EOF())
106 {
107 $Line = $File->ReadString();
108 if(strpos($Line, '=') !== false)
109 {
110 $LineParts = explode('=', $Line, 2);
111 $Value['ShortCut'] = trim($LineParts[0]);
112 $Line = trim($LineParts[1]);
113 if($Line[0] == '"')
114 {
115 // Quoted string value
116 $Line = substr($Line, 1); // Skip start qoute
117 $TempLine = str_replace('\"', ' ', $Line); // Temporary remove slashed quotes
118 $Value['Text'] = addslashes(stripslashes(substr($Line, 0, strpos($TempLine, '"'))));
119 $Line = trim(substr($Line, strpos($TempLine, '"') + 1)); // Skip closing quote and semicolon
120 } else
121 {
122 // Nonstring value
123 $Value['Text'] = substr($Line, 0, strpos($Line, ';'));
124 }
125 $Line = substr($Line, strpos($Line, ';') + 1);
126 $Value['Comment'] = addslashes(stripslashes(substr($Line, 3))); // Skip " --"
127 //print_r($Value);
128
129 $this->InsertItem($Value);
130 };
131 $Count++;
132 }
133 echo('<br />Celkem: '.$Count.' Nových: '.$this->NewItemCount.'<br />');
134 $this->System->Database->query('UPDATE `Group` SET `LastVersion` = "'.$this->Version['BuildNumber'].'", `LastImport` = NOW() WHERE `Id`='.$this->Group['Id']);
135 } else echo('Již importován pro verzi '.$this->Version['Version']);
136 } else echo('Není definováno jméno zdrojového souboru');
137 echo('<strong>Dokončeno.</strong>');
138 }
139
140 function UpdateTranslated()
141 {
142 global $TranslationTree, $PatchVersion, $Config;
143
144 echo('<br /><br />Začínám se synchronizací VersionEnd u přeložených textů<br />');
145 foreach($TranslationTree as $Group)
146 {
147 echo('<br />'.$Group['Name'].' ');
148 $DbResult = $this->System->Database->query('SELECT `gs_tran`.`ID`, `gs_tran`.`VersionEnd` AS `VersionEnd_tran`, `gs_orig`.`VersionEnd` AS `VersionEnd_orig` FROM `'.$Group['TablePrefix'].'` AS `gs_tran` JOIN `'.$Group['TablePrefix'].'` AS `gs_orig` ON `gs_orig`.`ID` = `gs_tran`.`Take` WHERE `gs_tran`.`VersionEnd` <> `gs_orig`.`VersionEnd`');
149 while($DbRow = $DbResult->fetch_assoc())
150 {
151 $this->System->Database->query('UPDATE `'.$Group['TablePrefix'].'` SET `VersionEnd` = '.$DbRow['VersionEnd_orig'].' WHERE `ID` = '.$DbRow['ID']);
152 echo('. ');
153 }
154 echo('<strong>Dokončeno.</strong>');
155 }
156 }
157
158 function HaveSameText($Group, $DbRow2, $Value)
159 {
160 $result = true;
161 foreach($Group['Items'] as $GroupItem)
162 {
163 $old = $DbRow2[$GroupItem['Column']];
164 $old = str_replace(chr(10), '', $old);
165 $old = str_replace(chr(13), '', $old);
166 $old = str_replace('\n', '', $old);
167 $old = str_replace('\r', '', $old);
168 $old = str_replace('\"', '"', $old);
169 $old = str_replace('\\\\', '\\', $old);
170 $old = str_replace('\32', '32', $old);
171 $old = str_replace('\124', '124', $old);
172 $old = str_replace("\'", "'", $old);
173 $old = str_replace("Â", "", $old);
174 $old = str_replace("�", "", $old);
175
176
177 if (($GroupItem['MangosColumn'] <> '') and ($Group['MangosDatabase'] == 'mangos'))
178 $new = $Value[$GroupItem['MangosColumn']];
179 else $new = $Value[$GroupItem['Column']];
180
181 $new = str_replace(chr(10), '', $new);
182 $new = str_replace(chr(13), '', $new);
183 $new = str_replace('\n', '', $new);
184 $new = str_replace('\r', '', $new);
185 $new = str_replace('\"', '"', $new);
186 $new = str_replace('\\\\', '\\', $new);
187 $new = str_replace('\32', '32', $new);
188 $new = str_replace('\124', '124', $new);
189 $new = str_replace("\'", "'", $new);
190 $new = str_replace("Â", "", $new);
191 $new = str_replace("�", "", $new);
192
193 if(($old == 'null') or ($old == 'NULL')) $old = '';
194 if(($new == 'null') or ($new == 'NULL')) $new = '';
195
196 if(($old <> $new) and ($GroupItem['Column'] <> 'Comment'))
197 {
198 // echo $old.'X'.$new;
199 $result = false;
200 }
201 }
202 return($result);
203 }
204
205 function ImportDBC()
206 {
207 global $System, $TranslationTree, $Config;
208
209 echo('Načítání textů z DBC souboru...');
210 if(($this->Group['DBCFileName'] != '') and ($this->Group['TablePrefix'] != ''))
211 {
212 echo('<br />'.$this->Group['Name'].'<br />');
213
214 // Load string column index list
215 $DbResult = $System->Database->query('SELECT * FROM `GroupItem` JOIN `GroupItemDBC` ON `GroupItem`.`Id` = `GroupItemDBC`.`GroupItem` AND `GroupItemDBC`.`ClientVersion` = '.$this->Version['Id'].' WHERE `GroupItem`.`Group` = '.$this->Group['Id']);
216 $ColumnIndexes = array();
217 $ColumnFormat = array();
218 while($DbRow = $DbResult->fetch_assoc())
219 {
220 $ColumnFormat[$DbRow['ColumnIndex']] = FORMAT_STRING;
221 $ColumnIndexes[$DbRow['GroupItem']] = $DbRow['ColumnIndex'];
222 }
223
224 $DBCFile = new DBCFile();
225 $DBCFile->OpenFile('../source/'.$this->Version['Version'].'/dbc/'.$this->Group['DBCFileName'].'.dbc', $ColumnFormat);
226 $ItemCount = $DBCFile->GetRecordCount();
227 $this->NewItemCount = 0;
228 $Count = 0;
229
230 for($I = 0; $I < $ItemCount; $I++)
231 {
232 foreach($this->Group['Items'] as $GroupItem)
233 {
234 $Value[$GroupItem['Column']] = addslashes($DBCFile->GetString($I, $ColumnIndexes[$GroupItem['Id']]));
235 }
236 $Value['Entry'] = $DBCFile->GetUint($I, 0);
237 $this->InsertItem($Value);
238 $Count++;
239 }
240 echo('<br />Celkem: '.$Count.' Nových: '.$this->NewItemCount.'<br />');
241 $System->Database->query('UPDATE `Group` SET `LastVersion` = "'.$this->Version['BuildNumber'].'", `LastImport` = NOW() WHERE `Id`='.$this->Group['Id']);
242 }
243 echo('<strong>Dokončeno.</strong>');
244 }
245
246 function ImportGroup($GroupId)
247 {
248 global $TranslationTree;
249
250 $this->Group = $TranslationTree[$GroupId];
251
252 if($this->Group['SourceType'] == 'dbc') $this->ImportDBC();
253 else if($this->Group['SourceType'] == 'sql') $this->ImportSQL();
254 else if($this->Group['SourceType'] == 'lua') $this->ImportLUA();
255 else echo('Neznámý typ zdroje pro import');
256 $this->UpdateTranslated();
257 }
258
259 function ImportSQL()
260 {
261 global $TranslationTree, $PatchVersion;
262
263 $File = new FileStream();
264 $File->OpenFile('../source/'.$this->Version['Version'].'/sql/'.$this->Group['MangosTable'].'.sql');
265 $this->NewItemCount = 0;
266 $Count = 0;
267 $folow_structure = false;
268 $i = 0;
269 while((!$File->EOF()))
270 {
271 $Line = $File->ReadString();
272 // Struktura
273 if(strpos($Line, 'CREATE TABLE `'.$this->Group['MangosTable'].'`') !== false)
274 {
275 $Line = '';
276 $folow_structure = true;
277 $i = 0;
278 }
279 if((strpos($Line, ';') !== false) and ($folow_structure == true))
280 {
281 $folow_structure = false;
282 // echo ('Struktura: <br />');
283 // print_r($structure);
284 // echo ('<br /><br />');
285 }
286 if(($folow_structure == true) and ($Line != ''))
287 {
288 $str = substr($Line, 0, strpos($Line, '`'));
289 $Line = substr($Line, strpos($Line, '`') + 1);
290 $Line = substr($Line, 0, strpos($Line, '`'));
291 if(strlen($str) < 3) $structure[$i] = $Line;
292 $i++;
293 }
294
295 //data
296 if((strpos($Line, 'INSERT INTO `'.$this->Group['MangosTable'].'`') !== false) and (isset($structure)))
297 {
298 $Line = substr($Line, strpos($Line, '(') + 1);
299 $Line = substr($Line, 0, strpos($Line, ');'));
300 $LineParts = explode('),(', $Line);
301 unset($Line);
302
303 foreach($LineParts as $LinePart)
304 {
305 unset($Value, $value_buff);
306 foreach($structure as $i => $column)
307 {
308 if (substr($LinePart, 0, 1) != "'")
309 {
310 $value_buff = substr($LinePart, 0, strpos($LinePart, ','));
311 $LinePart = substr($LinePart, strlen($value_buff) + 1);
312 } else
313 {
314 $LinePart = substr($LinePart, 1);
315 $value_buff = substr($LinePart, 0, strpos($LinePart, "'"));
316 while(substr($value_buff, strlen($value_buff) - 1, 1) == "\\")
317 {
318 $str = substr($LinePart, strlen($value_buff));
319 $str2 = substr($str, 0, strpos($str, "'", 1));
320 $value_buff = $value_buff.$str2;
321 $str = substr($str, strlen($str2));
322 }
323 $LinePart = substr($LinePart, strlen($value_buff) + 2);
324 }
325 if(($value_buff != 'null') and ($value_buff != 'NULL'))
326 {
327 $str = '';
328 while(substr($value_buff, strlen($value_buff) - 1,1) == " ")
329 {
330 $value_buff = substr($value_buff, 0, strlen($value_buff) - 1);
331 $str .= ' ';
332 }
333 $str2 = '';
334 while(substr($value_buff, 0, 1) == ' ')
335 {
336 $value_buff = substr($value_buff, 1, strlen($value_buff) - 1);
337 $str2 .= ' ';
338 }
339 $Value[$column] = $str2.trim($value_buff).$str;
340 } else
341 {
342 $Value[$column] = '';
343 }
344 // echo ($column.'-"'.$Value[$column].'"<br>');
345 }
346 foreach($this->Group['Items'] as $GroupItem)
347 {
348 if($GroupItem['MangosColumn'] != '')
349 $Value[$GroupItem['Column']] = $Value[$GroupItem['MangosColumn']];
350 }
351
352 // Get multicolumn value
353 $Columns = explode(',', $this->Group['MangosTableIndex']);
354 $ColumnValue = '';
355 foreach($Columns as $Column)
356 $ColumnValue .= '_'.$Value[$Column];
357 $ColumnValue = substr($ColumnValue, 1);
358 $Value[$this->Group['PrimaryKeyItem']] = $ColumnValue;
359 $this->InsertItem($Value);
360 $Count++;
361 }
362 }
363 }
364 echo('<br />Celkem: '.$Count.' Nových: '.$this->NewItemCount.'<br />');
365 $this->System->Database->query('UPDATE `Group` SET `LastVersion` = "'.$this->Version['BuildNumber'].'", `LastImport` = NOW() WHERE `Id`='.$this->Group['Id']);
366 }
367}
368
369?>
Note: See TracBrowser for help on using the repository browser.