Untitled

mail@pastecode.io avatar
unknown
php
21 days ago
7.8 kB
1
Indexable
Never
<?php

function loadXMLFromFileOrUrl($path) {
    libxml_use_internal_errors(true);
    if (filter_var($path, FILTER_VALIDATE_URL)) {
        $xml = simplexml_load_file($path);
    } else {
        $xml = simplexml_load_string(file_get_contents($path));
    }
    if (!$xml) {
        echo "Failed to load XML: ", implode(", ", libxml_get_errors());
        libxml_clear_errors();
        return false;
    }
    return $xml;
}

function compareXMLFiles($xml1, $xml2, $rootElement, $idElement, $ignoreElements, $productLimit, $checkValueDifferences, $hideProductIdRowsWithoutDifferences) {
    $ignoreElements = array_map('trim', explode(',', $ignoreElements));
    $products1 = $xml1->xpath("//$rootElement");
    $products2 = $xml2->xpath("//$rootElement");

    $productIds1 = array_map(function ($product) use ($idElement) {
        return (string)$product->$idElement;
    }, $products1);

    $productIds2 = array_map(function ($product) use ($idElement) {
        return (string)$product->$idElement;
    }, $products2);

    $commonIds = array_intersect($productIds1, $productIds2);
    $uniqueIds1 = array_diff($productIds1, $productIds2);
    $uniqueIds2 = array_diff($productIds2, $productIds1);

    echo "<table border='1'>";
    echo "<tr><th>Product ID</th><th>Details</th></tr>";

    $counter = 0;

    foreach ($commonIds as $id) {
        $product1 = current(array_filter($products1, function($product) use ($idElement, $id) {
            return (string)$product->$idElement === $id;
        }));
        $product2 = current(array_filter($products2, function($product) use ($idElement, $id) {
            return (string)$product->$idElement === $id;
        }));

        $differences = [];
        $keys1 = array_keys((array)$product1->children());
        $keys2 = array_keys((array)$product2->children());
        // echo '<pre>'; print_r($keys1); echo '</pre>';
        // echo '<pre>'; print_r($keys2); echo '</pre>';

        $allKeys = array_unique(array_merge($keys1, $keys2));

        if ($checkValueDifferences) {
            foreach ($allKeys as $key) {
                if (in_array($key, $ignoreElements)) {
                    continue;
                }
                $value1 = isset($product1->$key) ? (string)$product1->$key : '<span style="background-color: red; color: white;">N/A</span>';
                $value2 = isset($product2->$key) ? (string)$product2->$key : '<span style="background-color: red; color: white;">N/A</span>';
                // echo '<pre>'; print_r($value1); echo '</pre>';
                // echo '<pre>'; print_r($value2); echo '</pre>';
                if ($value1 !== $value2) {
                    $differences[] = "<span style='background-color: deeppink; color: white; padding: 2px;'>$key</span> differs: <br>xml1: $value1 <br>xml2: $value2";
                }
            }
        }

        if (!empty($differences)) {
            echo "<tr><td style='padding-left: 5px;'>" . htmlspecialchars($id) . "</td><td>" . implode("<hr>", $differences) . "</td></tr>";
        } elseif (!$hideProductIdRowsWithoutDifferences) {
            echo "<tr><td style='padding-left: 5px;'>" . htmlspecialchars($id) . "</td><td>No differences in values</td></tr>";
        }

        $counter++;
        if (!is_null($productLimit) && $counter >= $productLimit) {
            break;
        }
    }

    // Display unique product IDs
    foreach ($uniqueIds1 as $id) {
        echo "<tr><td>" . htmlspecialchars($id) . "</td><td style='background-color: red; color: white;'>Only in XML 1</td></tr>";
    }

    foreach ($uniqueIds2 as $id) {
        echo "<tr><td>" . htmlspecialchars($id) . "</td><td style='background-color: red; color: white;'>Only in XML 2</td></tr>";
    }

    echo "</table>";
}

// Handling form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['path1'], $_POST['path2'], $_POST['rootElement'], $_POST['idElement'])) {
    echo '<pre>$_POST:'; print_r($_POST); echo '</pre>';
    $ignoreElements                       =  isset($_POST['ignoreElements'])                                 ? $_POST['ignoreElements']                            : '';
    $productLimit                         = (isset($_POST['productLimit']) && $_POST['productLimit'] > 0)    ? $_POST['productLimit']                              : null;
    $checkValueDifferences                =  isset($_POST['checkValueDifferences'])                          ? (bool)$_POST['checkValueDifferences']               : false;
    $hideProductIdRowsWithoutDifferences  =  isset($_POST['hideProductIdRowsWithoutDifferences'])            ? (bool)$_POST['hideProductIdRowsWithoutDifferences'] : false;

    $xml1 = loadXMLFromFileOrUrl($_POST['path1']);
    $xml2 = loadXMLFromFileOrUrl($_POST['path2']);
    $rootElement = $_POST['rootElement'];
    $idElement = $_POST['idElement'];

    if ($xml1 && $xml2) {
        compareXMLFiles($xml1, $xml2, $rootElement, $idElement, $ignoreElements, $productLimit, $checkValueDifferences, $hideProductIdRowsWithoutDifferences);
    } else {
        echo "Error loading one or both XML files.";
    }
} else {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XML Comparison Tool</title>
<style>
    body {
        font-family: Arial, sans-serif;
        background-color: #f4f4f4;
        margin: 40px;
        color: #333;
    }
    form {
        background-color: #fff;
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    input[type="text"],
    input[type="number"] {
        width: 100%;
        padding: 8px;
        margin: 8px 0;
        box-sizing: border-box;
        border: 2px solid #ccc;
        border-radius: 4px;
    }
    input[type="submit"] {
        background-color: #4CAF50;
        color: white;
        padding: 14px 20px;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        font-size: 16px;
    }
    input[type="submit"]:hover {
        background-color: #45a049;
    }
    label {
        margin-top: 20px;
        display: block;
        font-weight: bold;
    }
    .checkbox-group {
        margin: 8px 0;
    }
</style>
</head>
<body>
<form method="post">
    <label for="path1">XML File 1 Path/URL:</label>
    <input type="text" id="path1" name="path1" required>

    <label for="path2">XML File 2 Path/URL:</label>
    <input type="text" id="path2" name="path2" required>

    <label for="rootElement">Root Element Name:</label>
    <input type="text" id="rootElement" name="rootElement" required>

    <label for="idElement">ID Element Name:</label>
    <input type="text" id="idElement" name="idElement" required>

    <label for="ignoreElements">Elements to Ignore (comma-separated):</label>
    <input type="text" id="ignoreElements" name="ignoreElements">

    <label for="productLimit">Max number of products to process:</label>
    <input type="number" id="productLimit" name="productLimit">

    <div class="checkbox-group">
        <label for="checkValueDifferences">Show Value Differences
            <input type="checkbox" id="checkValueDifferences" name="checkValueDifferences" checked>
        </label>
    </div>

    <div class="checkbox-group">
        <label for="hideProductIdRowsWithoutDifferences">Hide Product ID rows without differences
            <input type="checkbox" id="hideProductIdRowsWithoutDifferences" name="hideProductIdRowsWithoutDifferences">
        </label>
    </div>
    <br>

    <input type="submit" value="Compare">

</form>
</body>
</html>

<?php
}
?>