source: trunk/import/import.php@ 388

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