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');
2019-05-24 13:30:48 +00:00
$db->getHandler()->options(MYSQLI_OPT_LOCAL_INFILE, TRUE);
$tmpDir = '/tmp/floricode';
2018-05-23 10:14:20 +00:00
// Establish the FTP connection
2019-05-24 13:30:48 +00:00
$ftpConf = $db->getRow('SELECT host, user, password FROM ftpConfig');
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(
2019-05-24 13:30:48 +00:00
'SELECT fileName, toTable, file, updated FROM fileConfig');
$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'];
2019-05-24 13:30:48 +00:00
$table = $row['toTable'];
$baseName = $row['fileName'];
2018-05-23 10:14:20 +00:00
if ($row['updated']) {
$updated = DateTime::createFromFormat('Y-m-d', $row['updated']);
2020-06-30 14:52:42 +00:00
$updated->setTime(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;
2020-06-30 14:47:44 +00:00
if (empty($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);
2020-06-30 14:47:44 +00:00
$lastUpdated->setTime(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]);
2019-05-24 13:30:48 +00:00
$db->query("UPDATE fileConfig SET updated = # WHERE fileName = #",
[$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
error_log($e->getMessage());
$db->query('ROLLBACK');
}
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";
}
}