Add summary to update.php

This commit is contained in:
Sebastian Meyer 2021-08-14 09:28:45 +02:00
parent edf2e4ae06
commit 3d4c65dba7
1 changed files with 11 additions and 1 deletions

View File

@ -80,7 +80,7 @@ if (!is_dir($dataDir) || !is_writable($dataDir)) {
} }
// Alright, let's start! // Alright, let's start!
echo "Updating $dataDir from $sourceDir\n"; echo "Updating $dataDir from $sourceDir\n";
$todo = array (); $todo = [];
$error = false; $error = false;
$oldFiles = glob($dataDir.'*.xml'); $oldFiles = glob($dataDir.'*.xml');
foreach ($oldFiles as $oldFile) { foreach ($oldFiles as $oldFile) {
@ -90,6 +90,12 @@ $newFiles = glob($sourceDir.'*.xml');
foreach ($newFiles as $newFile) { foreach ($newFiles as $newFile) {
$todo[pathinfo($newFile, PATHINFO_FILENAME)] = 'update'; $todo[pathinfo($newFile, PATHINFO_FILENAME)] = 'update';
} }
$files = [
'added' => 0,
'updated' => 0,
'deleted' => 0,
'total' => count($newFiles)
];
foreach ($todo as $identifier => $task) { foreach ($todo as $identifier => $task) {
echo " Checking record $identifier ... "; echo " Checking record $identifier ... ";
if ($task === 'update') { if ($task === 'update') {
@ -97,6 +103,7 @@ foreach ($todo as $identifier => $task) {
// Add file // Add file
if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) { if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) {
echo format('added', 'green')."\n"; echo format('added', 'green')."\n";
$files['added']++;
} else { } else {
echo format('addition failed', 'red')."\n"; echo format('addition failed', 'red')."\n";
$error = true; $error = true;
@ -105,6 +112,7 @@ foreach ($todo as $identifier => $task) {
// Replace file // Replace file
if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) { if (copy($sourceDir.$identifier.'.xml', $dataDir.$identifier.'.xml')) {
echo format('updated', 'green')."\n"; echo format('updated', 'green')."\n";
$files['updated']++;
} else { } else {
echo format('update failed', 'red')."\n"; echo format('update failed', 'red')."\n";
$error = true; $error = true;
@ -117,6 +125,7 @@ foreach ($todo as $identifier => $task) {
// Truncate file // Truncate file
if (fclose(fopen($dataDir.$identifier.'.xml', 'w'))) { if (fclose(fopen($dataDir.$identifier.'.xml', 'w'))) {
echo format('deleted', 'green')."\n"; echo format('deleted', 'green')."\n";
$files['deleted']++;
} else { } else {
echo format('deletion failed', 'red')."\n"; echo format('deletion failed', 'red')."\n";
$error = true; $error = true;
@ -126,6 +135,7 @@ foreach ($todo as $identifier => $task) {
} }
} }
} }
echo 'Summary: '.$files['added'].' added, '.$files['updated'].' updated, '.$files['deleted'].' deleted. Total records served: '.$files['total']."\n";
if ($error) { if ($error) {
echo "Update completed, but errors occurred. Please check the logs!\n"; echo "Update completed, but errors occurred. Please check the logs!\n";
} else { } else {