danielmarschall / oidplus

OIDplus 2.0 - An OpenSource online Registration Authority for OIDs and other Object Types
https://www.oidplus.com
Apache License 2.0
10 stars 6 forks source link

Replacing plugin manifest.xml with manifest.json #51

Closed danielmarschall closed 2 weeks ago

danielmarschall commented 2 weeks ago

In the next version of OIDplus, manifest.xml will be deprecated and replaced with a JSON file.

The WIP converting utility is as follows:

<?php

class XmlToJson {

    public static function ConvertManifest($file, $sid, $has_css, $has_js, $has_cssSetup, $has_jsSetup) {
#       echo "$file\n";
        $fileContents= file_get_contents($file);
        $simpleXml = simplexml_load_string($fileContents);

        $data = [
                "\$schema" => "urn:oid:1.3.6.1.4.1.37476.2.5.2.5.$sid.1",
                "manifest" => [
                        "type" => "".$simpleXml->type[0],
                        "info" => [
                                "name" => "".$simpleXml->info[0]->name[0],
                                "author" => "".$simpleXml->info[0]->author[0],
                                "license" => "".$simpleXml->info[0]->license[0],
                                "version" => "".$simpleXml->info[0]->version[0],
                                "descriptionHTML" => trim(str_replace(['\r','\n','\t'], '', "".$simpleXml->info[0]->descriptionHTML[0])),
                                "oid" => "".$simpleXml->info[0]->oid[0],
                        ],
                        "php" => [
                                "mainclass" => "".$simpleXml->php[0]->mainclass[0]
                        ]
                ]
        ];

        if ($has_cssSetup) {
                $data["manifest"]["cssSetup"] = array();
                if (count($simpleXml->cssSetup) > 0)
                foreach ($simpleXml->cssSetup->file as $cssSetup) {
                        $data["manifest"]["cssSetup"][] = "".$cssSetup;
                }
        }

        if ($has_jsSetup) {
                $data["manifest"]["jsSetup"] = array();
                if (count($simpleXml->jsSetup) > 0)
                foreach ($simpleXml->jsSetup->file as $jsSetup) {
                        $data["manifest"]["jsSetup"][] = "".$jsSetup;
                }
        }

        if ($has_css) {
                $data["manifest"]["css"] = array();
                if (count($simpleXml->css) > 0)
                foreach ($simpleXml->css->file as $css) {
                        $data["manifest"]["css"][] = "".$css;
                }
        }

        if ($has_js) {
                $data["manifest"]["js"] = array();
                if (count($simpleXml->js) > 0)
                foreach ($simpleXml->js->file as $js) {
                        $data["manifest"]["js"][] = "".$js;
                }
        }

        if ($sid == 3) {
                $data["manifest"]["language"] = [];
                $data["manifest"]["language"]["code"] = "".$simpleXml->language[0]->code[0];
                $data["manifest"]["language"]["flag"] = "".$simpleXml->language[0]->flag[0];
                $data["manifest"]["language"]["messages"] = "".$simpleXml->language[0]->messages[0];
        }

        $json = json_encode($data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);

        $json = preg_replace_callback(
            '/^(?: {4})+/m',
            function($m) {
                return str_repeat("\t", strlen($m[0]) / 4);
            },
            $json
        );

        return $json;
    }

}

$plugin_types = [
        "adminPages" => [2, true, true, false, false],
        "auth" => [8, false, false, false, false],
        "captcha" => [12, true, true, true, true],
        "database" => [6, false, false, true, true],
        "design" => [7, true, false, false, false],
        "language" => [3, false, false, false, false],
        "logger" => [9, false, false, false, false],
        "objectTypes" => [10, true, true, false, false], # due to interface gridGeneratorLinks (INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_6) this plugin type can also have CSS and JS
        "publicPages" => [2, true, true, false, false],
        "raPages" => [2, true, true, false, false],
        "sqlSlang" => [11, false, false, false, false]
];

foreach ($plugin_types as $subfolder => $attrib) {
        $files = glob("plugins/*/$subfolder/*/manifest.xml");
        if (count($files) == 0) echo "Attention: $subfolder nothing found\n";
        foreach ($files as $file) {
#               echo "$file\n";
                $json = XmlToJson::ConvertManifest($file, $attrib[0], $attrib[1], $attrib[2], $attrib[3], $attrib[4]);
                $file = str_replace(".xml", ".json", $file);
                file_put_contents($file, $json);
        }
        // TODO: also generate json schema for the plugin type
}

Example for a JSON manifest:

{
    "$schema": "urn:oid:1.3.6.1.4.1.37476.2.5.2.2.2.1",
    "manifest": {
        "type": "ViaThinkSoft\\OIDplus\\OIDplusPagePluginAdmin",
        "info": {
            "name": "System registration",
            "author": "ViaThinkSoft",
            "license": "Apache 2.0",
            "version": "",
            "descriptionHTML": "",
            "oid": "1.3.6.1.4.1.37476.2.5.2.4.3.120"
        },
        "php": {
            "mainclass": "ViaThinkSoft\\OIDplus\\OIDplusPageAdminRegistration"
        },
        "css": [],
        "js": [
            "OIDplusPageAdminRegistration.js"
        ]
    }
}

I will provide and update to the following files soon:

@wehowski Please update your plugins after the new version is released (should be this weekend). For backwards compatibility, you can keep manifest.xml and manifest.json if you wish.

danielmarschall commented 2 weeks ago

Released as 2.0.1.24 to the public.

The final version of the converter utility is located at dev/migrate_plugin_manifest.phps

The plugins bundled with OIDplus have been updated.