2016-08-26 12:43:45 +00:00
|
|
|
<?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);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
$tmpDir = '/tmp/floricode';
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
// Establish the FTP connection
|
2016-08-26 12:43:45 +00:00
|
|
|
|
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']);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
if (!$ftpConn)
|
2018-05-23 10:14:20 +00:00
|
|
|
throw new Exception('Can not connect to '. $ftpConf['host']);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
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']);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
// Gets the list with the tables to update
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
set_time_limit(0);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
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');
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
$dwFiles = [];
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!file_exists($tmpDir))
|
|
|
|
mkdir($tmpDir);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
while ($row = $res->fetch_assoc())
|
2016-08-26 12:43:45 +00:00
|
|
|
try {
|
|
|
|
$file = $row['file'];
|
2019-05-24 13:30:48 +00:00
|
|
|
$table = $row['toTable'];
|
|
|
|
$baseName = $row['fileName'];
|
2016-08-26 12:43:45 +00:00
|
|
|
|
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
|
2016-08-26 12:43:45 +00:00
|
|
|
$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
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!isset($dwFiles[$file])) {
|
2016-08-26 12:43:45 +00:00
|
|
|
$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");
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
$zip = new ZipArchive;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($zip->open($zipFile) !== TRUE)
|
|
|
|
throw new Exception("Can not open $zipFile");
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
@mkdir($ucDir, 0774, TRUE);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!$zip->extractTo($ucDir))
|
|
|
|
throw new Exception("Can not uncompress file $zipFile");
|
2016-08-26 12:43:45 +00:00
|
|
|
|
|
|
|
$zip->close();
|
2018-05-23 10:14:20 +00:00
|
|
|
unlink($zipFile);
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 11:08:17 +00:00
|
|
|
foreach (glob("$ucDir/$baseName*.txt") as $fileName)
|
2016-08-26 12:43:45 +00:00
|
|
|
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");
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
// If data is updated, omits the table
|
2016-08-26 12:43:45 +00:00
|
|
|
|
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);
|
2016-08-26 12:43:45 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (isset($updated) && $lastUpdated <= $updated) {
|
|
|
|
echo "Table $table is updated, omitted\n";
|
2016-08-26 12:43:45 +00:00
|
|
|
continue;
|
2018-05-23 10:14:20 +00:00
|
|
|
}
|
2016-08-26 12:43:45 +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 = #",
|
2016-08-26 12:43:45 +00:00
|
|
|
[$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());
|
2020-01-08 10:52:29 +00:00
|
|
|
$db->query('ROLLBACK');
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
|
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";
|
2016-08-26 12:43:45 +00:00
|
|
|
}
|
|
|
|
}
|