Changeset 816 for trunk/Modules/Import/Import.php
- Timestamp:
- Feb 22, 2015, 11:20:50 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/Import/Import.php
r756 r816 7 7 class ModuleImport extends AppModule 8 8 { 9 10 11 12 13 14 15 16 17 $this->Dependencies = array(); 18 19 20 21 22 $this->System->RegisterPage('import', 'PageImport'); 23 9 function __construct($System) 10 { 11 parent::__construct($System); 12 $this->Name = 'Import'; 13 $this->Version = '1.0'; 14 $this->Creator = 'Chronos'; 15 $this->License = 'GNU/GPL'; 16 $this->Description = 'Support for import of data.'; 17 $this->Dependencies = array(); 18 } 19 20 function Start() 21 { 22 $this->System->RegisterPage('import', 'PageImport'); 23 } 24 24 } 25 25 26 26 class Import 27 27 { 28 var $Version; 28 var $Version; 29 29 var $Group; 30 30 var $NewItemCount; 31 31 var $System; 32 32 33 33 function __construct($System) 34 34 { 35 35 $this->System = &$System; 36 36 } 37 37 38 38 function SetVersion($Version) 39 39 { 40 40 global $System; 41 41 42 42 $DbResult = $System->Database->query('SELECT * FROM `ClientVersion` WHERE `Version` = "'.$Version.'"'); 43 $this->Version = $DbResult->fetch_assoc(); 44 } 45 43 $this->Version = $DbResult->fetch_assoc(); 44 } 45 46 46 function InsertItem($Value) 47 { 47 { 48 48 $insert = true; 49 49 $Columns = ''; … … 58 58 $Where = ' (`'.$this->Group['PrimaryKeyItem'].'` = "'.$Value[$this->Group['PrimaryKeyItem']].'") AND (`Language`=0) ' ; 59 59 //print_r($Value); 60 61 $DbResultMiddle = $this->System->Database->query('SELECT `VersionEnd`,`VersionStart`, `ID`, `Entry`, '.$Columns.' FROM `'.$this->Group['TablePrefix'].'` WHERE '.$Where.' AND `VersionStart` <= '.$this->Version['BuildNumber'].' AND `VersionEnd` >= '.$this->Version['BuildNumber'].' ORDER BY `VersionEnd` DESC LIMIT 1'); 60 61 $DbResultMiddle = $this->System->Database->query('SELECT `VersionEnd`,`VersionStart`, `ID`, `Entry`, '.$Columns.' FROM `'.$this->Group['TablePrefix'].'` WHERE '.$Where.' AND `VersionStart` <= '.$this->Version['BuildNumber'].' AND `VersionEnd` >= '.$this->Version['BuildNumber'].' ORDER BY `VersionEnd` DESC LIMIT 1'); 62 62 $DbResultBefore = $this->System->Database->query('SELECT `VersionEnd`,`VersionStart`, `ID`, `Entry`, '.$Columns.' FROM `'.$this->Group['TablePrefix'].'` WHERE '.$Where.' AND `VersionEnd` <= '.$this->Version['BuildNumber'].' ORDER BY `VersionEnd` DESC LIMIT 1'); 63 63 $DbResultAfter = $this->System->Database->query('SELECT `VersionEnd`,`VersionStart`, `ID`, `Entry`, '.$Columns.' FROM `'.$this->Group['TablePrefix'].'` WHERE '.$Where.' AND `VersionStart` >= '.$this->Version['BuildNumber'].' ORDER BY `VersionStart` LIMIT 1'); … … 69 69 $DbRowAfter = $DbResultAfter->fetch_assoc(); 70 70 $DbRowBefore = $DbResultBefore->fetch_assoc(); 71 72 if($this->HaveSameText($this->Group, $DbRowBefore, $Value) and ($DbResultBefore->num_rows > 0) ) 71 72 if($this->HaveSameText($this->Group, $DbRowBefore, $Value) and ($DbResultBefore->num_rows > 0) ) 73 73 { 74 74 $insert = false; 75 75 if ($this->Group['Id'] == 1) { 76 76 $set = ' , EndText = "'.$Value['EndText'].'" , ObjectiveText1 = "'.$Value['ObjectiveText1'].'"'.' , ObjectiveText2 = "'.$Value['ObjectiveText2'].'"'.' , ObjectiveText3 = "'.$Value['ObjectiveText3'].'"'.' , ObjectiveText4 = "'.$Value['ObjectiveText4'].'"'; 77 } else $set = ''; 77 } else $set = ''; 78 78 $this->System->Database->query('UPDATE `'.$this->Group['TablePrefix'].'` SET `VersionEnd` = "'.$this->Version['BuildNumber'].'" '.$set.' WHERE `ID`='.$DbRowBefore['ID']); 79 echo('b '); 80 81 } else 82 if($this->HaveSameText($this->Group, $DbRowAfter, $Value) and ($DbResultAfter->num_rows > 0)) 79 echo('b '); 80 81 } else 82 if($this->HaveSameText($this->Group, $DbRowAfter, $Value) and ($DbResultAfter->num_rows > 0)) 83 83 { 84 84 $insert = false; 85 85 if ($this->Group['Id'] == 1) { 86 86 $set = ' , EndText = "'.$Value['EndText'].'" , ObjectiveText1 = "'.$Value['ObjectiveText1'].'"'.' , ObjectiveText2 = "'.$Value['ObjectiveText2'].'"'.' , ObjectiveText3 = "'.$Value['ObjectiveText3'].'"'.' , ObjectiveText4 = "'.$Value['ObjectiveText4'].'"'; 87 } else $set = ''; 87 } else $set = ''; 88 88 $this->System->Database->query('UPDATE `'.$this->Group['TablePrefix'].'` SET `VersionStart` = "'.$this->Version['BuildNumber'].'" '.$set.' WHERE `ID`='.$DbRowAfter['ID']); 89 echo('a '); 90 91 } else 92 { 93 94 if (isset($DbRowAfter['VersionStart'])) { 89 echo('a '); 90 91 } else 92 { 93 94 if (isset($DbRowAfter['VersionStart'])) { 95 95 if ($DbRowAfter['VersionStart'] <= $this->Version['BuildNumber']) { 96 96 echo('Allready imported '.$DbRowBefore['Entry'].' '); … … 103 103 $inserted = false; 104 104 } 105 } 106 105 } 106 107 107 //if [DEPRECATED] do not import 108 foreach($this->Group['Items'] as $GroupItem) { 108 foreach($this->Group['Items'] as $GroupItem) { 109 109 if (false !== strpos($Value[$GroupItem['Column']],'[DEPRECATED')) { 110 110 echo('d '.$DbRowBefore['Entry'].' '); 111 111 $insert = false; 112 } 113 } 112 } 113 } 114 114 115 115 if ($insert) { 116 116 $insert = false; 117 117 foreach($this->Group['Items'] as $GroupItem) 118 { 118 { 119 119 if ($Value[$GroupItem['Column']] <> '') $insert = true; 120 120 } 121 121 } 122 122 123 123 if (isset($DbRowMiddle['Entry'])) $insert = false; 124 124 if (isset($DbRowAfter['Entry'])) $Value['Entry'] = $DbRowAfter['Entry']; 125 125 if (isset($DbRowBefore['Entry'])) $Value['Entry'] = $DbRowBefore['Entry']; 126 126 127 if ($insert) 128 { 129 127 if ($insert) 128 { 129 130 130 $Columns = '`Entry`, `Language`, `VersionStart`, `VersionEnd`'; 131 131 $Values = $Value['Entry'].', 0, '.$this->Version['BuildNumber'].', '.$this->Version['BuildNumber']; 132 foreach($this->Group['Items'] as $GroupItem) 133 { 132 foreach($this->Group['Items'] as $GroupItem) 133 { 134 134 $Columns .= ', `'.$GroupItem['Column'].'`'; 135 135 $Values .= ', "'.$Value[$GroupItem['Column']].'"'; 136 136 } 137 137 $this->System->Database->query('INSERT `'.$this->Group['TablePrefix'].'` ('.$Columns.') VALUES ('.$Values.')'); 138 138 139 139 echo ' 140 '.$Value['Entry'].' = '.$DbRowBefore['VersionStart'].'.'.$DbRowBefore['VersionEnd'].'< '.$this->Version['BuildNumber'].' <'.$DbRowAfter['VersionStart'].'.'.$DbRowAfter['VersionEnd'].'... '.$DbRowMiddle['VersionStart'].' '.$DbRowMiddle['VersionEnd'].' 140 '.$Value['Entry'].' = '.$DbRowBefore['VersionStart'].'.'.$DbRowBefore['VersionEnd'].'< '.$this->Version['BuildNumber'].' <'.$DbRowAfter['VersionStart'].'.'.$DbRowAfter['VersionEnd'].'... '.$DbRowMiddle['VersionStart'].' '.$DbRowMiddle['VersionEnd'].' 141 141 '; 142 143 if (false !== strpos($Values,'[DEPRECATED')) 142 143 if (false !== strpos($Values,'[DEPRECATED')) 144 144 echo $Values; 145 145 146 146 echo('# '); 147 147 $InsertId = $this->System->Database->insert_id; 148 148 $this->System->ModuleManager->Modules['Log']->WriteLog('Text <a href="form.php?group='.$this->Group['Id'].'&ID='.$InsertId.'">'.$InsertId.'</a> ('.$Value['Entry'].') ze skupiny '.$this->Group['Name'].' byl v nové verzi '.$this->Version['Version'].' změněn.', LOG_TYPE_IMPORT); 149 149 } 150 } 151 } else 150 } 151 } else 152 152 { 153 153 // Insert new text … … 162 162 $DbRow = $DbResult->fetch_row(); 163 163 $Value['Entry'] += $DbRow[0]; 164 } 164 } 165 165 } 166 166 $Columns = '`Entry`, `Language`, `VersionStart`, `VersionEnd`'; … … 168 168 $insert = false; 169 169 foreach($this->Group['Items'] as $GroupItem) 170 { 170 { 171 171 $Columns .= ', `'.$GroupItem['Column'].'`'; 172 172 $Values .= ', "'.$Value[$GroupItem['Column']].'"'; … … 182 182 } 183 183 } 184 184 185 185 function ImportLUA() 186 186 { 187 187 global $TranslationTree, $PatchVersion; 188 188 $Output = 'Načítání textů z LUA souboru...'; 189 189 190 190 if(($this->Group['LuaFileName'] != '') and ($this->Group['TablePrefix'] != '')) 191 191 { 192 192 193 $Output .= '<br />'.$this->Group['Name'].'<br />'; 193 $Output .= '<br />'.$this->Group['Name'].'<br />'; 194 194 // if($this->Group['LastVersion'] < $this->Version['BuildNumber'] + 1) 195 195 { 196 196 $File = new FileStream(); 197 197 198 198 $File->OpenFile(dirname(__FILE__).'/../../source/'.$this->Version['Version'].'/lua/'.$this->Group['LuaFileName'].'.lua'); 199 199 $this->NewItemCount = 0; … … 215 215 $Value['Text'] = str_replace('\n', "\n", $Value['Text']); 216 216 $Value['Text'] = addslashes(stripslashes($Value['Text'])); 217 $Line = trim(substr($Line, strpos($TempLine, '"') + 1)); // Skip closing quote and semicolon 218 } else 217 $Line = trim(substr($Line, strpos($TempLine, '"') + 1)); // Skip closing quote and semicolon 218 } else 219 219 { 220 220 // Nonstring value … … 224 224 $Value['Comment'] = addslashes(stripslashes(substr($Line, 3))); // Skip " --" 225 225 //print_r($Value); 226 226 227 227 $this->InsertItem($Value); 228 }; 228 }; 229 229 $Count++; 230 230 } … … 237 237 return ($Output); 238 238 } 239 239 240 240 function UpdateTranslated() 241 241 { 242 242 global $TranslationTree, $PatchVersion, $Config; 243 243 244 244 $Output = '<br /><br />Začínám se synchronizací VersionEnd u přeložených textů<br />'; 245 foreach($TranslationTree as $Group) 246 { 247 $Output .= '<br />'.$Group['Name'].' '; 248 $do = true; 249 while($do) 245 foreach($TranslationTree as $Group) 246 { 247 $Output .= '<br />'.$Group['Name'].' '; 248 $do = true; 249 while($do) 250 250 { 251 251 $DbResult = $this->System->Database->query('SELECT `gs_tran`.`ID`, '. 252 253 254 255 256 257 258 259 260 while($DbRow = $DbResult->fetch_assoc()) 261 { 252 '`gs_tran`.`VersionEnd` AS `VersionEnd_tran`, '. 253 '`gs_tran`.`VersionStart` AS `VersionStart_tran`, '. 254 '`gs_orig`.`VersionEnd` AS `VersionEnd_orig`, '. 255 '`gs_orig`.`VersionStart` AS `VersionStart_orig` FROM `'. 256 $Group['TablePrefix'].'` AS `gs_tran` JOIN `'.$Group['TablePrefix']. 257 '` AS `gs_orig` ON `gs_orig`.`ID` = `gs_tran`.`Take` WHERE '. 258 '`gs_tran`.`VersionEnd` <> `gs_orig`.`VersionEnd` OR `gs_tran`.`VersionStart` <> `gs_orig`.`VersionStart`'); 259 $do = ($DbResult->num_rows > 0); 260 while($DbRow = $DbResult->fetch_assoc()) 261 { 262 262 echo '`'; 263 263 $this->System->Database->query('UPDATE `'.$Group['TablePrefix'].'` SET `VersionEnd` = '.$DbRow['VersionEnd_orig'].', `VersionStart` = '.$DbRow['VersionStart_orig'].' WHERE `ID` = '.$DbRow['ID']); 264 $Output .= '. '; 265 } 266 } 264 $Output .= '. '; 265 } 266 } 267 267 $Output .= '<strong>Dokončeno.</strong>'; 268 268 } 269 269 return($Output); 270 270 } 271 271 272 272 function HaveSameText($Group, $DbRow2, $Value) 273 273 { 274 274 $result = true; 275 foreach($Group['Items'] as $GroupItem) 276 { 275 foreach($Group['Items'] as $GroupItem) 276 { 277 277 $old = $DbRow2[$GroupItem['Column']]; 278 278 $old = str_replace(chr(10), '', $old); … … 291 291 $old = strtolower($old); 292 292 $old = str_replace('$b', '', $old); 293 294 if ($this->Group['Id'] == 1) 293 294 if ($this->Group['Id'] == 1) 295 295 while ($part = substr($old, strpos($old, '<'), strpos($old, '>')-strpos($old, '<'))) 296 if ($part <> '') { 296 if ($part <> '') { 297 297 $old = str_replace($part.'>', '', $old); 298 298 } 299 299 300 300 if (($GroupItem['MangosColumn'] <> '') and ($Group['MangosDatabase'] == 'mangos')) 301 301 $new = $Value[$GroupItem['MangosColumn']]; 302 302 else $new = $Value[$GroupItem['Column']]; 303 303 304 304 $new = str_replace(chr(10), '', $new); 305 305 $new = str_replace(chr(13), '', $new); … … 318 318 $new = str_replace('$b', '', $new); 319 319 320 if ($this->Group['Id'] == 1) 320 if ($this->Group['Id'] == 1) 321 321 while ($part = substr($new, strpos($new, '<'), strpos($new, '>')-strpos($new, '<'))) 322 if ($part <> '') { 322 if ($part <> '') { 323 323 $new = str_replace($part.'>', '', $new); 324 324 } 325 325 326 326 if(($old == 'null') or ($old == 'NULL')) $old = ''; 327 327 if(($new == 'null') or ($new == 'NULL')) $new = ''; 328 329 if(($new <> '') and ($old <> $new) and ($GroupItem['Column'] <> 'Comment')) 328 329 if(($new <> '') and ($old <> $new) and ($GroupItem['Column'] <> 'Comment')) 330 330 { 331 331 // echo $old.'X'.$new; 332 if ( ($GroupItem['Column'] <> 'EndText') 333 and ($GroupItem['Column'] <> 'ObjectiveText1') 334 and ($GroupItem['Column'] <> 'ObjectiveText2') 335 and ($GroupItem['Column'] <> 'ObjectiveText3') 336 and ($GroupItem['Column'] <> 'ObjectiveText4') ) 332 if ( ($GroupItem['Column'] <> 'EndText') 333 and ($GroupItem['Column'] <> 'ObjectiveText1') 334 and ($GroupItem['Column'] <> 'ObjectiveText2') 335 and ($GroupItem['Column'] <> 'ObjectiveText3') 336 and ($GroupItem['Column'] <> 'ObjectiveText4') ) 337 337 $result = false; 338 338 } 339 339 } 340 340 341 341 return($result); 342 342 } 343 343 344 344 function ImportDBC() 345 345 { 346 346 global $System, $TranslationTree, $Config; 347 347 348 348 $Output = 'Načítání textů z DBC souboru...'; 349 349 if(($this->Group['DBCFileName'] != '') and ($this->Group['TablePrefix'] != '')) 350 350 { 351 $Output .= '<br />'.$this->Group['Name'].'<br />'; 351 $Output .= '<br />'.$this->Group['Name'].'<br />'; 352 352 353 353 // Load string column index list 354 $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']); 355 354 $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']); 355 356 356 $ColumnIndexes = array(); 357 357 $ColumnFormat = array(); … … 361 361 $ColumnIndexes[$DbRow['GroupItem']] = $DbRow['ColumnIndex']; 362 362 } 363 363 364 364 $DBCFile = new DBCFile(); 365 365 $DBCFile->OpenFile(dirname(__FILE__).'/../../source/'.$this->Version['Version'].'/dbc/'.$this->Group['DBCFileName'].'.dbc', $ColumnFormat); 366 366 $ItemCount = $DBCFile->GetRecordCount(); 367 367 $this->NewItemCount = 0; 368 $Count = 0; 369 368 $Count = 0; 369 370 370 for($I = 0; $I < $ItemCount; $I++) 371 371 { … … 373 373 if(array_key_exists($GroupItem['Id'], $ColumnIndexes)) 374 374 { 375 $Value[$GroupItem['Column']] = addslashes($DBCFile->GetString($I, $ColumnIndexes[$GroupItem['Id']])); 376 } 377 375 $Value[$GroupItem['Column']] = addslashes($DBCFile->GetString($I, $ColumnIndexes[$GroupItem['Id']])); 376 } 377 378 378 // Get multicolumn value 379 379 $Columns = explode(',', $this->Group['DBCIndex']); … … 381 381 foreach($Columns as $Column) 382 382 $ColumnValue .= '_'.$DBCFile->GetUint($I, $Column); 383 $ColumnValue = substr($ColumnValue, 1); 384 $Value[$this->Group['PrimaryKeyItem']] = $ColumnValue; 383 $ColumnValue = substr($ColumnValue, 1); 384 $Value[$this->Group['PrimaryKeyItem']] = $ColumnValue; 385 385 $this->InsertItem($Value); 386 386 $Count++; 387 } 387 } 388 388 $Output .= '<br />Celkem: '.$Count.' Nových: '.$this->NewItemCount.'<br />'; 389 389 $this->UpdateLastVersion(); … … 396 396 { 397 397 global $TranslationTree; 398 398 399 399 $this->Group = $TranslationTree[$GroupId]; 400 400 401 401 if($this->Group['SourceType'] == 'dbc') $Output = $this->ImportDBC(); 402 402 else if($this->Group['SourceType'] == 'sql') $Output = $this->ImportSQL(); … … 406 406 return($Output); 407 407 } 408 408 409 409 function ImportSQL() 410 410 { 411 411 global $TranslationTree, $PatchVersion; 412 412 413 413 $Output= ''; 414 414 $File = new FileStream(); … … 422 422 $Line = $File->ReadLine(); 423 423 // Struktura 424 if(strpos($Line, 'CREATE TABLE `'.$this->Group['MangosTable'].'`') !== false) 424 if(strpos($Line, 'CREATE TABLE `'.$this->Group['MangosTable'].'`') !== false) 425 425 { 426 426 $Line = ''; … … 428 428 $i = 0; 429 429 } 430 if((strpos($Line, ';') !== false) and ($folow_structure == true)) 430 if((strpos($Line, ';') !== false) and ($folow_structure == true)) 431 431 { 432 432 $folow_structure = false; … … 443 443 $i++; 444 444 } 445 445 446 446 //data 447 447 if((strpos($Line, 'INSERT INTO `'.$this->Group['MangosTable'].'`') !== false) and (isset($structure))) 448 { 448 { 449 449 while ((strpos($Line, ');') === false) or ($File->EOF())) 450 450 $Line = $Line.$File->ReadLine(); 451 451 $Line = str_replace("),\n(", '),(', $Line); 452 452 453 453 $Line = substr($Line, strpos($Line, '(') + 1); 454 454 $Line = substr($Line, 0, strpos($Line, ');')); 455 455 $LineParts = explode('),(', $Line); 456 456 457 457 unset($Line); 458 458 459 459 $value_buff = ''; 460 460 $Value = ''; 461 461 foreach($LineParts as $LinePart) 462 462 { 463 463 464 464 unset($Value, $value_buff); 465 465 foreach($structure as $i => $column) … … 469 469 $value_buff = substr($LinePart, 0, strpos($LinePart, ',')); 470 470 $LinePart = substr($LinePart, strlen($value_buff) + 1); 471 } else 471 } else 472 472 { 473 473 $LinePart = substr($LinePart, 1); 474 474 $value_buff = substr($LinePart, 0, strpos($LinePart, "'")); 475 while(substr($value_buff, strlen($value_buff) - 1, 1) == "\\") 475 while(substr($value_buff, strlen($value_buff) - 1, 1) == "\\") 476 476 { 477 477 $str = substr($LinePart, strlen($value_buff)); … … 479 479 $value_buff = $value_buff.$str2; 480 480 $str = substr($str, strlen($str2)); 481 } 481 } 482 482 $LinePart = substr($LinePart, strlen($value_buff) + 2); 483 } 483 } 484 484 if(($value_buff != 'null') and ($value_buff != 'NULL')) 485 485 { 486 486 $str = ''; 487 while(substr($value_buff, strlen($value_buff) - 1,1) == " ") 487 while(substr($value_buff, strlen($value_buff) - 1,1) == " ") 488 488 { 489 489 $value_buff = substr($value_buff, 0, strlen($value_buff) - 1); … … 491 491 } 492 492 $str2 = ''; 493 while(substr($value_buff, 0, 1) == ' ') 493 while(substr($value_buff, 0, 1) == ' ') 494 494 { 495 495 $value_buff = substr($value_buff, 1, strlen($value_buff) - 1); … … 497 497 } 498 498 $Value[$column] = $str2.trim($value_buff).$str; 499 } else 500 { 499 } else 500 { 501 501 $Value[$column] = ''; 502 502 } … … 513 513 } 514 514 } 515 515 516 516 // Get multicolumn value 517 517 $Columns = explode(',', $this->Group['MangosTableIndex']); … … 519 519 foreach($Columns as $Column) 520 520 $ColumnValue .= '_'.$Value[$Column]; 521 $ColumnValue = substr($ColumnValue, 1); 521 $ColumnValue = substr($ColumnValue, 1); 522 522 $Value[$this->Group['PrimaryKeyItem']] = $ColumnValue; 523 523 $this->InsertItem($Value); 524 524 $Count++; 525 525 } 526 } 526 } 527 527 } 528 528 $Output = '<br />Celkem: '.$Count.' Nových: '.$this->NewItemCount.'<br />'; … … 530 530 return($Output); 531 531 } 532 532 533 533 function UpdateLastVersion() { 534 534 $DbResult = $this->System->Database->query('SELECT * FROM `Group` WHERE `Id`='.$this->Group['Id']); 535 $Version = $DbResult->fetch_assoc(); 535 $Version = $DbResult->fetch_assoc(); 536 536 if ($Version['LastVersion'] < $this->Version['BuildNumber']) 537 537 $this->System->Database->query('UPDATE `Group` SET `LastVersion` = "'.$this->Version['BuildNumber'].'", `LastImport` = NOW() WHERE `Id`='.$this->Group['Id']);
Note:
See TracChangeset
for help on using the changeset viewer.