help3/xhpeditor/api/check_xhp.php |   61 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

New commits:
commit b347baf1eaefc0c5538ec6a4ea6b4a5ba2f8a895
Author:     Juan José González <juanjose...@libreoffice.org>
AuthorDate: Sun Jan 14 11:12:01 2024 -0600
Commit:     Olivier Hallot <olivier.hal...@libreoffice.org>
CommitDate: Tue Jan 16 06:24:34 2024 +0100

    Add check xhp async endpoint.
    
    I am adding the endpoint first before the client using it, to prevent what 
happend with the render endpoint returning 404.
    
    Change-Id: I118b4adacf026ef85781f9e8d91d9cfdd00719f5
    Reviewed-on: https://gerrit.libreoffice.org/c/dev-tools/+/162050
    Reviewed-by: Olivier Hallot <olivier.hal...@libreoffice.org>
    Tested-by: Olivier Hallot <olivier.hal...@libreoffice.org>

diff --git a/help3/xhpeditor/api/check_xhp.php 
b/help3/xhpeditor/api/check_xhp.php
new file mode 100644
index 00000000..7c0ca865
--- /dev/null
+++ b/help3/xhpeditor/api/check_xhp.php
@@ -0,0 +1,61 @@
+<?php
+require_once './helpers.php';
+
+try {
+    $method = get_method();
+    $data = get_data();
+
+    if (!isset($data['xhpdoc'])) {
+        respond(['message' => 'XHP is required'], 400);
+    }
+
+    $xhp = $data['xhpdoc'];
+
+    $all_errors = [
+        'xhp_errors' => [],
+        'duplicated_ids' => [],
+    ];
+
+    libxml_use_internal_errors(true);
+    libxml_clear_errors();
+
+    $root = 'helpdocument';
+    $old = new DOMDocument;
+
+    if (!$old->loadXML($xhp)) {
+        $all_errors['xhp_errors'] = libxml_get_errors();
+        libxml_clear_errors();
+    } else {
+        $creator = new DOMImplementation;
+        $doctype = $creator->createDocumentType($root, null, '../xmlhelp.dtd');
+        $new = $creator->createDocument(null, null, $doctype);
+        $new->encoding = "utf-8";
+
+        $oldNode = $old->getElementsByTagName($root)->item(0);
+        $newNode = $new->importNode($oldNode, true);
+        $new->appendChild($newNode);
+        libxml_clear_errors();
+
+        if(!$new->validate()) {
+            $all_errors['xhp_errors'] = libxml_get_errors();
+            libxml_clear_errors();
+        }
+
+        $tags_id_uniq = 
array('paragraph','note','warning','tip','h1','h2','h3','h4','h5','h6');
+        $xmlarray = simplexml_load_string($xhp);
+        $i = 0;
+        foreach($tags_id_uniq as $tag_uniq) {
+            foreach ($xmlarray->xpath("//$tag_uniq") as $tag){
+                $idarray[$i] = $tag['id'];
+                ++$i;
+            }
+        }
+        $dupped_array =  array_values(array_unique(array_diff_key($idarray, 
array_unique($idarray))));
+        $all_errors['duplicated_ids'] = $dupped_array;
+    }
+
+    respond(['errors' => $all_errors]);
+}
+catch (Exception $e) {
+    respond(['message' => 'Unexpected error'], 500);
+}

Reply via email to