New file |
0,0 → 1,279 |
<?php |
/** |
* Exemple de lancement du script : /opt/lampp/bin/php cli.php ontologie -a analyser |
* |
*/ |
class Ontologie extends Script { |
|
private $lecteur = null; |
private $fichier = ''; |
private $lotsTermes = array(); |
private $lotsRelations = array(); |
private $baseIdGroupe = 10000; |
private $baseIdSousGroupe = 20000; |
private $types = array( |
'FREQUENCY_MODIFIERS' => 2, |
'QUALIFIERS' => 3, |
'RELATIVE_MODIFIERS' => 4, |
'RELATIVE_VALUES' => 5, |
'SPATIAL_MODIFIERS' => 6, |
'LOCATER_REGIONS' => 7, |
'TEMPORAL_MODIFIERS' => 8, |
'UNIT_TERMS' => 9, |
'QUANTITATIVE_PROPERTIES' => 10, |
'NEW_QUALITATIVE_PROPERTIES' => 11, |
'DISALLOWED_TERMS' => 20, |
'QUALITATIVE_STATES' => 13); |
|
public function executer() { |
try { |
$this->bdd = new Bdd(); |
$this->fichier = realpath(dirname(__FILE__)).'/../../../donnees/ontologie/v1.00_2003-02-18/Ontology.xml'; |
// Lancement de l'action demandée |
$cmd = $this->getParametre('a'); |
switch ($cmd) { |
case 'analyser' : |
$this->vider(); |
$this->lireFichierXml(); |
break; |
case 'vider' : |
$this->vider(); |
default : |
throw new Exception("Erreur : la commande '$cmd' n'existe pas!"); |
} |
} catch (Exception $e) { |
$this->traiterErreur($e->getMessage()); |
} |
} |
|
|
/** |
* Lit le fichier OSM et lance l'analyse des noeuds xml en fonction de leur type. |
*/ |
private function lireFichierXml() { |
$termes = array_keys($this->types); |
$this->lecteur = new XMLReader(); |
if ($this->ouvrirFichierXml()) { |
while ($this->lecteur->read()) { |
if ($this->lecteur->nodeType == XMLREADER::ELEMENT) { |
if (in_array($this->lecteur->localName, $termes)) { |
$noeud = $this->obtenirNoeudSimpleXml(); |
$type = $this->lecteur->localName; |
$this->traiterTermes($this->types[$type], $noeud->children()); |
} else if ($this->lecteur->localName == 'STATE_GROUPS') { |
$noeud = $this->obtenirNoeudSimpleXml(); |
$this->traiterGroupes($noeud->children()); |
} |
} |
} |
if (count($this->lotsTermes) > 0) { |
$this->insererLotDeTermes(); |
} |
if (count($this->lotsRelations) > 0) { |
$this->insererLotDeRelations(); |
} |
} |
} |
|
private function ouvrirFichierXml() { |
if ($this->lecteur->open($this->fichier) === false) { |
throw new Exception("Impossible d'ouvrir le fichier XML : $this->fichier"); |
} |
return true; |
} |
|
private function obtenirNoeudSimpleXml() { |
$doc = new DOMDocument; |
$element = $this->lecteur->expand(); |
$noeud = simplexml_import_dom($doc->importNode($element, true)); |
return $noeud; |
} |
|
private function traiterTermes($type, $termes) { |
foreach ($termes as $terme) { |
$id = (int) $terme->attributes()->GLOBALID; |
if (isset($lotsTermes[$id]) === false) { |
$valeur = array(); |
$valeur[] = (int) $id; |
$valeur[] = (int) $type; |
$valeur[] = (string) $terme->attributes()->term; |
$valeur[] = (string) $this->obtenirDefinition($terme); |
$valeur[] = (int) $this->obtenirPreference($terme); |
$valeur[] = (int) $this->obtenirAuteur($terme); |
$valeur[] = (int) $terme->attributes()->citationREF; |
$valeur[] = (int) $this->obtenirImage($terme); |
$this->lotsTermes[$id] = $valeur; |
} |
if (isset($terme->attributes()->parentID)) { |
$relation = array(); |
$relation[] = (int) $terme->attributes()->GLOBALID; |
$relation[] = (int) $terme->attributes()->parentID; |
$relation[] = 'A POUR PARENT'; |
$this->lotsRelations[] = $relation; |
} |
if (isset($terme->attributes()->stateOfNEWPROPERTYID)) { |
$relation = array(); |
$relation[] = (int) $terme->attributes()->GLOBALID; |
$relation[] = (int) $terme->attributes()->stateOfNEWPROPERTYID; |
$relation[] = 'A POUR NOUVELLE QUALITE'; |
$this->lotsRelations[] = $relation; |
} |
} |
} |
|
private function obtenirDefinition($terme) { |
$definition = null; |
if (isset($terme->DEFINITION)) { |
$definition = $terme->DEFINITION; |
} |
return $definition; |
} |
|
private function obtenirPreference($terme) { |
$preference = '1'; |
if (isset($terme->attributes()->PREFERRED_TERM)) { |
$valeur = (string) $terme->attributes()->PREFERRED_TERM; |
$preference = (trim($valeur) == 'Disallowed Term') ? '0' : '1'; |
} |
return $preference; |
} |
|
private function obtenirAuteur($terme) { |
$auteur = 0; |
if (isset($terme->attributes()->authorID)) { |
$auteur = $terme->attributes()->authorID; |
} elseif (isset($terme->attributes()->authorREF)) { |
$auteur = $terme->attributes()->authorREF; |
} |
return $auteur; |
} |
|
private function obtenirImage($terme) { |
$image = 0; |
if (isset($terme->attributes()->pictureREF)) { |
$image = $terme->attributes()->pictureREF; |
} |
return $image; |
} |
|
private function traiterGroupes($groupes) { |
$lotsTermes = $lotsRelations = array(); |
foreach ($groupes as $groupe) { |
$id = $this->baseIdGroupe + (int) $groupe->attributes()->GROUP_ID; |
if (isset($lotsTermes[$id]) === false) { |
$valeur = array(); |
$valeur[] = (int) $id; |
$valeur[] = 18; |
$valeur[] = (string) $groupe->attributes()->groupName; |
$valeur[] = ''; |
$valeur[] = 1; |
$valeur[] = 0; |
$valeur[] = 0; |
$valeur[] = 0; |
$this->lotsTermes[$id] = $valeur; |
} |
if (isset($groupe->STRUCTURES_LINKED_TO_GROUP)) { |
foreach ($groupe->STRUCTURES_LINKED_TO_GROUP->children() as $structure) { |
$relation = array(); |
$relation[] = (int) $structure->attributes()->GLOBALID; |
$relation[] = (int) $id; |
$relation[] = 'A POUR GROUPE'; |
$this->lotsRelations[] = $relation; |
} |
} |
if (isset($groupe->STATES_IN_GROUP)) { |
foreach ($groupe->STATES_IN_GROUP->children() as $etat) { |
$relation = array(); |
$relation[] = (int) $id; |
$relation[] = (int) $etat->attributes()->GLOBALID; |
$relation[] = 'A POUR ETAT'; |
$this->lotsRelations[] = $relation; |
} |
} |
if (isset($groupe->STATESUBGROUPS)) { |
$this->traiterSousGroupes($id, $groupe->STATESUBGROUPS->children()); |
} |
} |
} |
|
private function traiterSousGroupes($idGroupe, $sousGroupes) { |
$lotsTermes = $lotsRelations = array(); |
foreach ($sousGroupes as $sg) { |
$id = $this->baseIdSousGroupe + (int) $sg->attributes()->STATESUBGROUP_GLOBALID; |
if (isset($lotsTermes[$id]) === false) { |
$valeur = array(); |
$valeur[] = (int) $id; |
$valeur[] = 19; |
$valeur[] = (string) $sg->attributes()->subgGroupName; |
$valeur[] = ''; |
$valeur[] = 1; |
$valeur[] = 0; |
$valeur[] = 0; |
$valeur[] = 0; |
$this->lotsTermes[$id] = $valeur; |
|
$relation = array(); |
$relation[] = (int) $idGroupe; |
$relation[] = (int) $id; |
$relation[] = 'A POUR SOUS-GROUPE'; |
$this->lotsRelations[] = $relation; |
} |
if (isset($sg->STATES_IN_SUBGROUP)) { |
foreach ($sg->STATES_IN_SUBGROUP->children() as $etat) { |
$relation = array(); |
$relation[] = (int) $id; |
$relation[] = (int) $etat->attributes()->GLOBALID; |
$relation[] = 'A POUR ETAT'; |
$this->lotsRelations[] = $relation; |
} |
} |
} |
} |
|
private function insererLotDeTermes() { |
$champs = implode(',', array('id_terme', 'ce_type', 'terme', 'definition', 'preference', 'ce_auteur', 'ce_publication', 'ce_image')); |
$values = $this->creerValues($this->lotsTermes); |
$requete = "INSERT INTO ontologie_terme ($champs) VALUES $values"; |
$this->executerSql($requete); |
} |
|
private function insererLotDeRelations() { |
$champs = implode(',', array('id_terme_01', 'id_terme_02', 'relation')); |
$values = $this->creerValues($this->lotsRelations); |
$requete = "INSERT INTO ontologie_relation ($champs) VALUES $values"; |
$this->executerSql($requete); |
} |
|
private function creerValues($valeurs) { |
$values = array(); |
foreach ($valeurs as $valeur) { |
foreach ($valeur as $id => $val) { |
$valeur[$id] = $this->etreVide($val) ? 'NULL' : $this->proteger(trim($val)); |
} |
$values[] = '('.implode(',', $valeur).')'; |
} |
$values = implode(',', $values); |
return $values; |
} |
|
private function etreVide($val) { |
$vide = ($val === null || trim($val) === '') ? true : false; |
return $vide; |
} |
|
private function executerSql($requete) { |
$this->bdd->requeter($requete); |
} |
|
private function proteger($chaine) { |
return $this->bdd->proteger($chaine); |
} |
|
private function vider() { |
$requete = 'TRUNCATE TABLE ontologie_terme'; |
$this->executerSql($requete); |
$requete = 'TRUNCATE TABLE ontologie_relation'; |
$this->executerSql($requete); |
} |
} |
?> |