makeTree.php 4.1 KB
<?php
 
 /*
 *  make local data tree from list of missions 
 */ 
 
   error_reporting(E_ERROR);
	
	if (getenv("AMDA_SPASE_INTERFACE") === false) 
		require_once("./config.php");
	else
		require_once(getenv("AMDA_SPASE_INTERFACE")."/config.php");

	if (getenv("DATAPATH") !== false && file_exists(getenv("DATAPATH")."rank.json")) 
		$cmpArr = json_decode(file_get_contents(getenv("DATAPATH")."rank.json"), true);
	else 
		$cmpArr = null;
                               
	$ddMission = new DomDocument("1.0");
	
	$localDom  = new DomDocument("1.0");
	$localDom->formatOutput = true;

	$BASE = $localDom->createElement('dataRoot'); 
	$BASE->setAttribute('xml:id', 'DATAROOT');
	$Amda = $localDom->createElement('dataCenter');
	$Amda->setAttribute('name', 'AMDA');
	$Amda->setAttribute('desc', 'AMDA_Internal_Data_Base');          
	$Amda->setAttribute('xml:id', 'myLocalData-treeRootNode');

	foreach ( glob(AMDATREEDIR.'DD_*.xml') as $xml ) 
	{              													
		$ddMission->load($xml);
		$missions = $ddMission->getElementsByTagName("mission");
		foreach ($missions as $mission) 
		{
			if ($mission->getAttribute('xml:id') == '') continue;
			$nodeNew = $localDom->importNode($mission,true);
                     
			if (is_array($cmpArr)) 
			{
				if ($mission->getAttribute('xml:id') == 'Ephemerides') { // Planets and Moons
                    $datasets = $nodeNew->getElementsByTagName('dataset');
                    foreach ($datasets as $ds ) {
                        $target = $ds->getAttribute('target');
                        $index = $cmpArr[$target];
                        if ($index) { 
                            if ( $ds->parentNode->tagName == 'datasetGroup' ) {
                                 if (!$ds->parentNode->hasAttribute('rank')) {
                                    $ds->parentNode->setAttribute('rank',$index);
                                 }
                            } else {
                                $ds->setAttribute('rank',$index);
                            }
                        }
                    }
				}
				else if ($mission->getAttribute('xml:id') == 'PlanetsProperties') { 
                    $instruments = $nodeNew->getElementsByTagName('instrument');
                    foreach ($instruments as $ins ) {
                        $target = $ins->getAttribute('name');
                        $index = $cmpArr[$target];
                        if ($index)  
                            $ins->setAttribute('rank',$index);
                    }
				}
 
                $targetMis = $mission->getAttribute('target');
                $index = $cmpArr[$targetMis];
                if ($index)  
                        $nodeNew->setAttribute('rank',$index);
                else $nodeNew->setAttribute('rank', 999);
                
                $observatories = $nodeNew->getElementsByTagName('observatory');
                if ($observatories->length > 0)
                {
                    foreach ($observatories as $obs)
                    {
                        if ($obs->hasAttribute('target'))
                        {
                            $target = $obs->getAttribute('target');
                            $index = $cmpArr[$target];
                            if ($index  &&  $target != $targetMis)
                                    $obs->setAttribute('rank',$index);											
                        }
                    }
                }
			}
			//TODO set propre restriction 
			$Amda->appendChild($nodeNew); 
		} 
	}                  
                                    
	if ($Amda->hasChildNodes()) $BASE->appendChild($Amda);             
	$localDom->appendChild($BASE);

	// check ID duplication                        
	$allIds = array();
	/** @var DOMElement $element */
	foreach($localDom->getElementsByTagName('*') as $element)
	{
		$id = $element->getAttribute('xml:id');
		$allIds[] = $id;
		if (!$id) {
			echo 'No id in node ' . $element->tagName . PHP_EOL;
		}
	}
	$arr = array_count_values($allIds);

	foreach ($arr as $key => $value) {
		if ($value > 1) print_r("id '$key' is duplicated $value times." . PHP_EOL);
	}
          
	$localDom->save('LocalParams.xml');  
?>