hedera-web/rest/edi/update.php

113 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2018-05-23 10:14:20 +00:00
class Update extends Vn\Lib\Method {
function run($db) {
$db->selectDb('edi');
//$db->options(MYSQLI_OPT_LOCAL_INFILE, TRUE);
$tmpDir = '/tmp/floricode';
2018-05-23 10:14:20 +00:00
// Establish the FTP connection
2018-05-23 10:14:20 +00:00
$ftpConf = $db->getRow('SELECT host, user, password FROM ftp_config');
2016-08-31 11:53:46 +00:00
echo "Openning FTP connection to {$ftpConf['host']}\n";
2018-05-23 10:14:20 +00:00
$ftpConn = ftp_connect($ftpConf['host']);
if (!$ftpConn)
2018-05-23 10:14:20 +00:00
throw new Exception('Can not connect to '. $ftpConf['host']);
2018-05-23 10:14:20 +00:00
if (!ftp_login($ftpConn, $ftpConf['user'], $ftpConf['password']))
throw new Exception('Can not login to '. $ftpConf['user'] .'@'. $ftpConf['host']);
2018-05-23 10:14:20 +00:00
// Gets the list with the tables to update
2018-05-23 10:14:20 +00:00
set_time_limit(0);
2018-05-23 10:14:20 +00:00
$res = $db->query(
'SELECT file_name, to_table, file, updated FROM file_config');
$dwFiles = [];
2018-05-23 10:14:20 +00:00
if (!file_exists($tmpDir))
mkdir($tmpDir);
2018-05-23 10:14:20 +00:00
while ($row = $res->fetch_assoc())
try {
$file = $row['file'];
$table = $row['to_table'];
$baseName = $row['file_name'];
2018-05-23 10:14:20 +00:00
if ($row['updated']) {
$updated = DateTime::createFromFormat('Y-m-d', $row['updated']);
$updated->setTime(0, 0, 0, 0);
2018-05-23 11:09:55 +00:00
} else
$updated = NULL;
$remoteFile = "codes/$file.ZIP";
$zipFile = "$tmpDir/$file.zip";
$ucDir = "$tmpDir/$file";
2018-05-23 10:14:20 +00:00
// Downloads and decompress the file with the data
2018-05-23 10:14:20 +00:00
if (!isset($dwFiles[$file])) {
$dwFiles[$file] = TRUE;
2016-08-31 11:53:46 +00:00
echo "Downloading $remoteFile\n";
2018-05-23 10:14:20 +00:00
if (!ftp_get($ftpConn, $zipFile, $remoteFile, FTP_BINARY))
throw new Exception("Error downloading $remoteFile to $zipFile");
$zip = new ZipArchive;
2018-05-23 10:14:20 +00:00
if ($zip->open($zipFile) !== TRUE)
throw new Exception("Can not open $zipFile");
2018-05-23 10:14:20 +00:00
@mkdir($ucDir, 0774, TRUE);
2018-05-23 10:14:20 +00:00
if (!$zip->extractTo($ucDir))
throw new Exception("Can not uncompress file $zipFile");
$zip->close();
2018-05-23 10:14:20 +00:00
unlink($zipFile);
}
foreach (glob("$ucDir/$baseName*.txt") as $fileName)
break;
if (!$fileName)
2018-05-23 10:14:20 +00:00
throw new Exception("Import file for table $table does not exist");
2018-05-23 10:14:20 +00:00
// If data is updated, omits the table
2018-05-23 10:14:20 +00:00
$lastUpdated = substr($fileName, -10, 6);
$lastUpdated = DateTime::createFromFormat('dmy', $lastUpdated);
$lastUpdated->setTime(0, 0, 0, 0);
2018-05-23 10:14:20 +00:00
if (isset($updated) && $lastUpdated <= $updated) {
echo "Table $table is updated, omitted\n";
continue;
2018-05-23 10:14:20 +00:00
}
2018-05-23 10:14:20 +00:00
// Updates the table
2016-08-31 11:53:46 +00:00
echo "Dumping data to table $table\n";
2018-05-23 10:14:20 +00:00
$db->query("START TRANSACTION");
$db->query("DELETE FROM {$db->quote($table)}");
$db->queryFromFile(__DIR__."/sql/$table", ['file' => $fileName]);
$db->query("UPDATE file_config SET updated = # WHERE file_name = #",
[$lastUpdated, $baseName]
);
2018-05-23 10:14:20 +00:00
$db->query("COMMIT");
2018-05-23 11:09:55 +00:00
} catch (Exception $e) {
2018-05-23 10:14:20 +00:00
$db->query('ROLLBACK');
error_log($e->getMessage());
}
2018-05-23 10:14:20 +00:00
shell_exec("rm -R $tmpDir");
ftp_close($ftpConn);
2016-08-31 11:53:46 +00:00
echo "Update completed\n";
}
}