| version 1.14, 2017/09/18 07:16:58 |
version 1.15, 2017/09/19 02:20:11 |
|
|
| * 2017-09-12 - implemented new SK-NIC-EPP/CentralNic export |
* 2017-09-12 - implemented new SK-NIC-EPP/CentralNic export |
| */ |
*/ |
| |
|
| /* $Platon: scripts/php/sk-nic/SK-NIC.php,v 1.13 2017/09/16 09:36:22 nepto Exp $ */ |
/* $Platon: scripts/php/sk-nic/SK-NIC.php,v 1.14 2017/09/18 07:16:58 nepto Exp $ */ |
| |
|
| require_once 'DB.php'; |
require_once 'DB.php'; |
| |
|
|
|
| $this->_db->disconnect(); |
$this->_db->disconnect(); |
| } // }}} |
} // }}} |
| |
|
| |
function setContext($context) /* {{{ */ |
| |
{ |
| |
switch ($context) { |
| |
case 'domain': |
| |
$this->context = 'domain'; |
| |
$this->primary_key = 'domain'; |
| |
$this->table_current = 'sknic_domains'; |
| |
$this->table_import = 'sknic_domains_import'; |
| |
$this->table_history = 'sknic_domains_history'; |
| |
break; |
| |
case 'registrar': |
| |
$this->context = 'registrar'; |
| |
$this->primary_key = 'id_reg'; |
| |
$this->table_current = 'sknic_registrars'; |
| |
$this->table_import = 'sknic_registrars_import'; |
| |
$this->table_history = 'sknic_registrars_history'; |
| |
break; |
| |
default: |
| |
die(sprintf("%s() ERROR: unknown context: %s\n", __FUNCTION__, $context)); |
| |
} |
| |
} /* }}} */ |
| |
|
| |
function setContextFromFilename($filename) /* {{{ */ |
| |
{ |
| |
$basefile = basename($filename); |
| |
if (preg_match('/^domain/', $basefile)) { |
| |
return $this->setContext('domain'); |
| |
} elseif (preg_match('/^registrar/', $basefile)) { |
| |
return $this->setContext('registrar'); |
| |
} |
| |
|
| |
die(sprintf("%s() ERROR: unable to detect context from file: %s\n", __FUNCTION__, $basefile)); |
| |
} /* }}} */ |
| |
|
| function _tableColumns($table_name) /* {{{ */ |
function _tableColumns($table_name) /* {{{ */ |
| { |
{ |
| $table_info = $this->_db->tableInfo($table_name); |
$table_info = $this->_db->tableInfo($table_name); |
|
|
| |
|
| function _getMaxChangedDate() /* {{{ */ |
function _getMaxChangedDate() /* {{{ */ |
| { |
{ |
| $query = 'SELECT MAX(dt_changed) FROM sknic_domains'; |
$query = 'SELECT MAX(dt_changed) FROM '.$this->table_current; |
| $date = $this->_db->getOne($query); |
$date = $this->_db->getOne($query); |
| DB::isError($date) && $this->error($date); |
DB::isError($date) && $this->error($date); |
| return $date; |
return $date; |
|
|
| } |
} |
| $this->_cleanImport(); |
$this->_cleanImport(); |
| $this->_writeImportWithLoadData($filename, $changed_date); |
$this->_writeImportWithLoadData($filename, $changed_date); |
| $this->_importedNameserversReorder(); |
|
| return true; |
return true; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function checkImport() /* {{{ */ |
function checkImport() /* {{{ */ |
| { |
{ |
| $query = 'SELECT COUNT(*) FROM sknic_domains_import'; |
$query = 'SELECT COUNT(*) FROM '.$this->table_import; |
| $count = $this->_db->getOne($query); |
$count = $this->_db->getOne($query); |
| DB::isError($count) && $this->error($count); |
DB::isError($count) && $this->error($count); |
| if ($count < 5000) { |
$min_count = ($this->context == 'domains') ? 100000 : 1000; |
| die("checkImport(): too less imported records: $count (must be min 5000)\n"); |
$max_count = ($this->context == 'domains') ? 900000 : 9000; |
| } |
if ($count < $min_count) { |
| if ($count > 600000) { |
die(sprintf("%s() ERROR: too less imported records: %d (must be min %d)\n", |
| die("checkImport(): too much imported records: $count (must be max 600000)\n"); |
__FUNCTION__, $count, $min_count)); |
| |
} |
| |
if ($count > $max_count) { |
| |
die(sprintf("%s() ERROR: too much imported records: %d (must be max 600000)\n", |
| |
__FUNCTION__, $count, $max_count)); |
| } |
} |
| return true; |
return true; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function _cleanImport() /* {{{ */ |
function _cleanImport() /* {{{ */ |
| { |
{ |
| $result = $this->_db->query('TRUNCATE TABLE sknic_domains_import'); |
$result = $this->_db->query('TRUNCATE TABLE '.$this->table_import); |
| DB::isError($result) && $this->error($result); |
DB::isError($result) && $this->error($result); |
| } /* }}} */ |
} /* }}} */ |
| |
|
|
|
| |
|
| function _writeImportWithLoadData($filename, $changed_date) /* {{{ */ |
function _writeImportWithLoadData($filename, $changed_date) /* {{{ */ |
| { |
{ |
| $query = sprintf('load data local infile %s' |
if ($this->context == 'domains') { |
| .' into table sknic_domains_import' |
$query = sprintf('LOAD DATA LOCAL INFILE %s' |
| .' fields terminated by ";"' |
.' INTO TABLE %s' |
| .' (domain,id_reg,id_owner,status,ns1,ns2,ns3,ns4,@dt_valid)' |
.' CHARACTER SET UTF8' |
| .' SET dt_valid = STR_TO_DATE(@dt_valid, %s), dt_changed = %s', |
.' FIELDS TERMINATED BY ";"' |
| $this->_db->quote($filename), |
.' (domain,id_reg,id_owner,status,ns1,ns2,ns3,ns4,@dt_valid)' |
| $this->_db->quote($this->input_dt_format_sql), |
.' SET dt_valid = STR_TO_DATE(@dt_valid, %s), dt_changed = %s', |
| $this->_db->quote($changed_date)); |
$this->_db->quote($filename), |
| $result = $this->_db->query($query); |
$this->table_import, |
| DB::isError($result) && $this->error($result); |
$this->_db->quote($this->input_dt_format_sql), |
| $query = 'DELETE FROM sknic_domains_import WHERE domain LIKE "-- %"'; |
$this->_db->quote($changed_date)); |
| |
$result = $this->_db->query($query); |
| |
DB::isError($result) && $this->error($result); |
| |
} elseif ($this->context == 'registrar') { |
| |
$this->_db->query('SET NAMES UTF8'); |
| |
$query = sprintf('LOAD DATA LOCAL INFILE %s' |
| |
.' INTO TABLE %s' |
| |
.' CHARACTER SET UTF8' |
| |
.' FIELDS TERMINATED BY ";"' |
| |
.' (id_reg,name,street,city,phone,email)' |
| |
.' SET dt_changed = %s', |
| |
$this->_db->quote($filename), |
| |
$this->table_import, |
| |
$this->_db->quote($changed_date)); |
| |
$result = $this->_db->query($query); |
| |
DB::isError($result) && $this->error($result); |
| |
} else { |
| |
die(sprintf("%s() ERROR: unknown context: %s\n", __FUNCTION__, $context)); |
| |
} |
| |
|
| |
$query = "DELETE FROM $this->table_import WHERE $this->primary_key LIKE '-- %'"; |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
DB::isError($result) && $this->error($result); |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function _writeDomainHistory($rec, $deleted = 0) /* {{{ */ |
function _writeHistory($rec, $deleted = 0) /* {{{ */ |
| { |
{ |
| static $dt_deleted = null; |
static $dt_deleted = null; |
| if ($dt_deleted == null) { |
if ($dt_deleted == null) { |
| $dt_deleted = $this->_db->getOne('select distinct dt_changed from sknic_domains_import'); |
$dt_deleted = $this->_db->getOne("SELECT DISTINCT dt_changed from $this->table_import"); |
| if (strlen($dt_deleted) <= 0) { |
if (strlen($dt_deleted) <= 0) { |
| $dt_deleted = date('Y-m-d'); |
$dt_deleted = date('Y-m-d'); |
| } |
} |
| } |
} |
| $history_cols = $this->_tableColumns('sknic_domains_history'); |
$history_cols = $this->_tableColumns($this->table_history); |
| $cols = ''; |
$cols = ''; |
| $vals = ''; |
$vals = ''; |
| foreach ($history_cols as $col) { |
foreach ($history_cols as $col) { |
|
|
| $vals .= ','; |
$vals .= ','; |
| } |
} |
| } |
} |
| $query = sprintf('INSERT INTO sknic_domains_history' |
$query = sprintf('INSERT INTO %s (%s dt_deleted) VALUES (%s %s)', |
| .' (%s dt_deleted) VALUES (%s %s)', |
$this->table_history, |
| $cols, $vals, |
$cols, $vals, |
| $deleted ? $this->_db->quote($dt_deleted) : 'NULL'); |
$deleted ? $this->_db->quote($dt_deleted) : 'NULL'); |
| $this->_db->expectError(DB_ERROR_ALREADY_EXISTS); |
$this->_db->expectError(DB_ERROR_ALREADY_EXISTS); |
|
|
| $this->_db->popExpect(); |
$this->_db->popExpect(); |
| if (DB::isError($result)) { |
if (DB::isError($result)) { |
| if ($result->getCode() == DB_ERROR_ALREADY_EXISTS) { |
if ($result->getCode() == DB_ERROR_ALREADY_EXISTS) { |
| echo 'WARNING: sknic_domains_history entry already exists',"\n"; |
printf("%s WARNING: %s entry already exists\n", |
| |
__FUNCTION__, $this->table_history); |
| } else { |
} else { |
| $this->error($result); |
$this->error($result); |
| } |
} |
| } |
} |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function _updateDomain($rec, $changed_cols) /* {{{ */ |
function _updateRecord($rec, $changed_cols) /* {{{ */ |
| { |
{ |
| $update_str = ''; |
$update_str = ''; |
| foreach ($changed_cols AS $col) { |
foreach ($changed_cols AS $col) { |
| $update_str .= "$col = ".$this->_db->quote($rec["i_$col"]).','; |
$update_str .= "$col = ".$this->_db->quote($rec["i_$col"]).','; |
| } |
} |
| $update_str = substr($update_str, 0, strlen($update_str) - 1); |
$update_str = substr($update_str, 0, strlen($update_str) - 1); |
| $query = sprintf('UPDATE sknic_domains SET %s WHERE domain = %s', |
$query = sprintf('UPDATE %s SET %s WHERE %s = %s', |
| |
$this->table_current, |
| $update_str, |
$update_str, |
| $this->_db->quote($rec['c_domain'])); |
$this->primary_key, |
| |
$this->_db->quote($rec["c_$this->primary_key"])); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
DB::isError($result) && $this->error($result); |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function _insertDomain($rec) /* {{{ */ |
function _insertRecord($rec) /* {{{ */ |
| { |
{ |
| $columns = $this->_tableColumns('sknic_domains'); |
$columns = $this->_tableColumns($this->table_current); |
| $cols = ''; |
$cols = ''; |
| $vals = ''; |
$vals = ''; |
| foreach ($columns as $col) { |
foreach ($columns as $col) { |
|
|
| } |
} |
| $cols = substr($cols, 0, strlen($cols) - 1); |
$cols = substr($cols, 0, strlen($cols) - 1); |
| $vals = substr($vals, 0, strlen($vals) - 1); |
$vals = substr($vals, 0, strlen($vals) - 1); |
| $query = sprintf('INSERT INTO sknic_domains' |
$query = sprintf('INSERT INTO %s (%s) VALUES (%s)', |
| .' (%s) VALUES (%s)', |
$this->table_current, $cols, $vals); |
| $cols, $vals); |
|
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
DB::isError($result) && $this->error($result); |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function doComparison() /* {{{ */ |
function doComparison() /* {{{ */ |
| { |
{ |
| $changed = 0; |
$changed = 0; |
| $import_cols = $this->_tableColumns('sknic_domains_import'); |
$import_cols = $this->_tableColumns($this->table_import); |
| $domains_cols = $this->_tableColumns('sknic_domains'); |
$current_cols = $this->_tableColumns($this->table_current); |
| |
|
| $query = 'SELECT 1'; |
$query = 'SELECT 1'; |
| foreach ($import_cols as $col) { |
foreach ($import_cols as $col) { |
| $query .= ",i.$col AS i_$col"; // 'i' means "import" |
$query .= ",i.$col AS i_$col"; // 'i' means "import" |
| } |
} |
| foreach ($domains_cols as $col) { |
foreach ($current_cols as $col) { |
| $query .= ",c.$col AS c_$col"; // 'c' means "current" |
$query .= ",c.$col AS c_$col"; // 'c' means "current" |
| } |
} |
| $query .= ' FROM sknic_domains_import i %s sknic_domains c'; |
$query .= " FROM $this->table_import AS i %s $this->table_current AS c"; |
| $query .= ' ON i.domain = c.domain'; |
$query .= " ON i.$this->primary_key = c.$this->primary_key"; |
| |
|
| foreach (array('LEFT JOIN', 'RIGHT JOIN') as $subquery) { |
foreach (array('LEFT JOIN', 'RIGHT JOIN') as $subquery) { |
| $subqryres = $this->_db->query(sprintf($query, $subquery)); |
$subqryres = $this->_db->query(sprintf($query, $subquery)); |
| while ($subqryres->fetchInto($rec)) { |
while ($subqryres->fetchInto($rec)) { |
| if (@strlen($rec['c_domain']) <= 0) { // new domain |
if (@strlen($rec["c_$this->primary_key"]) <= 0) { // new domain |
| //echo 'new domain: ',$rec['i_domain'],"\n"; |
//echo 'new domain: ',$rec["i_$pkey"],"\n"; |
| $changed++; |
$changed++; |
| $this->_insertDomain($rec); |
$this->_insertRecord($rec); |
| continue; |
continue; |
| } |
} |
| if (@strlen($rec['i_domain']) <= 0) { // deleted domain |
if (@strlen($rec["i_$this->primary_key"]) <= 0) { // deleted domain |
| //echo 'deleted domain: ',$rec['c_domain'],"\n"; |
//echo 'deleted domain: ',$rec["c_$pkey"],"\n"; |
| $changed++; |
$changed++; |
| $this->_writeDomainHistory($rec, 1); |
$this->_writeHistory($rec, 1); |
| $query = sprintf('DELETE FROM sknic_domains WHERE domain = %s', |
$query = sprintf('DELETE FROM %s WHERE %s = %s', |
| $this->_db->quote($rec['c_domain'])); |
$this->table_current, |
| |
$this->primary_key, |
| |
$this->_db->quote($rec["c_$this->primary_key"])); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
DB::isError($result) && $this->error($result); |
| continue; |
continue; |
|
|
| //echo 'changed fields in domain ',$rec['i_domain'],':',"\n\t"; |
//echo 'changed fields in domain ',$rec['i_domain'],':',"\n\t"; |
| //echo join("\n\t",$changed_cols),"\n"; |
//echo join("\n\t",$changed_cols),"\n"; |
| $changed++; |
$changed++; |
| $this->_writeDomainHistory($rec); |
$this->_writeHistory($rec); |
| $this->_updateDomain($rec, $changed_cols); |
$this->_updateRecord($rec, $changed_cols); |
| } |
} |
| } |
} |
| $subqryres->free(); |
$subqryres->free(); |