<?php
header('Content-type: text/html');

ini_set("memory_limit","1024M"); /*Increase memory limits ... try to avoid recursion*/
ini_set('max_execution_time', 90);

$includeWLPs = $_GET['includeWLPs'];

if ($includeWLPs){
	$RDA2BFxslt = "RDA2BF2_mapping_v5_MapsWorkLevelPs_displayRDA.xsl";
} else {
	$RDA2BFxslt = "RDA2BF2_mapping_v5_displayRDA.xsl";
}

//$path = '../my_public_html/';

//$RDA2BFxslt = "RDA2BF2_mapping_v08.xsl";
//$RDA2BFxslt = "RDA2BF2_mapping_v1_displayRDA.xsl";
//$RDA2BFxslt = "RDA2BF2_mapping_v4_MapsWorkLevelPs_displayRDA.xsl";
//$rda_xml = "gold_rda_derivations_dbis_20190324_Instances.rdf";
//$rda_xml = "gold_rda_derivations_dbis_20190402_Instances_OnlyFamilies.rdf";
$rda_xml = "gold_rda_derivations_dbis_20190419_Instances_OnlyFamilies.rdf";

$rda_xml_stream = file_get_contents($rda_xml);
if (!$rda_xml_stream) {
	print "Ontology not loaded!!!";
	return ;
}
 
//$map_rda2bibframe = makeHtmlVisible(transform_record_from_stream($rda_xml_stream, $RDA2BFxslt));
$map_rda2bibframe = transform_record_from_stream($rda_xml_stream, $RDA2BFxslt);

print $map_rda2bibframe;


function makeHtmlVisible($xmlString){
        $res = $xmlString;

      $res = preg_replace("'</?html[^>]*>'", '', $res);
//        $res = preg_replace("'<'", '&lt;', $res);
//        $res = preg_replace("'>'", '&gt;', $res);
//        $res = preg_replace("'&gt;'", '&gt;<br />', $res);

        return $res;
}


function makeXmlVisible($xmlString){
        $res = $xmlString;

      //$res = preg_replace("'</?html[^>]*>'", '', $res);
        $res = preg_replace("'<'", '&lt;', $res);
        $res = preg_replace("'>'", '&gt;', $res);
        $res = preg_replace("'&gt;'", '&gt;<br />', $res);

        return $res;
}


function transform_record_from_stream($xmlString, $xslfile) {
        $xsl = new DOMDocument;
        $xsl->load($xslfile);

        $processor = new XSLTProcessor();
        $processor->importStyleSheet($xsl);

        $xml = new DOMDocument;
        $xml->loadXML($xmlString);

        $res = $processor->transformToXML($xml);

        return $res;
}

function display_record_from_stream($xmlString, $xslfile) {
        $xsl = new DOMDocument;
        $xsl->load($xslfile);

        $processor = new XSLTProcessor();
        $processor->importStyleSheet($xsl);

        $xml = new DOMDocument;
        $xml->loadXML($xmlString);

        $res = $processor->transformToXML($xml);

        return makeXmlVisible($res);
        //print "<p>".makeXmlVisible($res)."</p>";
}


function transform_record($xmlfile, $xslfile) {
        $xsl = new DOMDocument;
        $xsl->load($xslfile);

        $processor = new XSLTProcessor();
        $processor->importStyleSheet($xsl);

        $xml = new DOMDocument;
        $xml->load($xmlfile);

        $res = $processor->transformToXML($xml);

        return $res;
}


function display_record($xmlfile, $xslfile) {
        $xsl = new DOMDocument;
        $xsl->load($xslfile);

        $processor = new XSLTProcessor();
        $processor->importStyleSheet($xsl);

        $xml = new DOMDocument;
        $xml->load($xmlfile);

        $res = $processor->transformToXML($xml);

        print "<h1>--- FRBR Keys</h1><br/><p>".makeXmlVisible($res)."</p>";
}


?>
