makeTree.php 3.16 KB
<?php
 
 /*
 *  make local data tree from list of missions 
 */ 
 
   error_reporting(E_ERROR);
   define("AMDA_SPASE_INTERFACE", getenv("AMDA_SPASE_INTERFACE")); 
	
	if ( AMDA_SPASE_INTERFACE == "" ) 
		require_once("./config.php");
	else
		require_once(AMDA_SPASE_INTERFACE."/config.php");
		
	$AMDAINSTALLATION = getenv("AMDAINSTALLATION");
	
	define("DATAPATH", "$AMDAINSTALLATION/AMDA_IHM/generic_data/");

	if (file_exists(DATAPATH.'rank.json')) 
		$cmpArr = json_decode(file_get_contents(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)) 
			{
				$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 
//                 $available =  $nodeNew->hasAttribute('restriction');
//                 $nodeNew->setAttribute('available', !$available);
			$Amda->appendChild($nodeNew); 
		} 
	}                  
                                    
	if ($Amda->hasChildNodes()) $BASE->appendChild($Amda);             
	$localDom->appendChild($BASE);

	// check ID duplication                        
	$allIds = array();
	foreach($localDom->getElementsByTagName('*') as $element)
	{                                                                          
		$allIds[] = $element->getAttribute('xml:id');
	}
	$arr = array_count_values($allIds);

	print_r('DUPLICATED IDs : '.PHP_EOL);
	foreach ($arr as $key => $value) 
		if ($value > 1) print_r($key.'=>'.$value.PHP_EOL);
                    
	// set 'TBD' attribute - if the parameter is not checked yet
	if (is_dir(TBD)) 
	{
		$params = $localDom->getElementsByTagName('parameter');
		$nToCheck = 0;       
		foreach ($params as $param) {
			$id = $param->getAttribute("xml:id");
			if (file_exists(TBD."$id.xml")) {
					$param->setAttribute('tbd', true);	
					$nToCheck ++;
			}
		}
		echo "TOCHECK ".$nToCheck.PHP_EOL; 		
	}
             
	$localDom->save('LocalParams.xml');  
?>