Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 226 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
225 jpm 1
<?php
2
/**
3
 * Exemple de lancement du script : /opt/lampp/bin/php cli.php ontologie -a analyser
4
 *
5
 */
6
class Ontologie extends Script {
7
 
8
	private $lecteur = null;
9
	private $fichier = '';
10
	private $lotsTermes = array();
11
	private $lotsRelations = array();
12
	private $baseIdGroupe = 10000;
13
	private $baseIdSousGroupe = 20000;
14
	private $types = array(
15
		'FREQUENCY_MODIFIERS' => 2,
16
		'QUALIFIERS' => 3,
17
		'RELATIVE_MODIFIERS' => 4,
18
		'RELATIVE_VALUES' => 5,
19
		'SPATIAL_MODIFIERS' => 6,
20
		'LOCATER_REGIONS' => 7,
21
		'TEMPORAL_MODIFIERS' => 8,
22
		'UNIT_TERMS' => 9,
23
		'QUANTITATIVE_PROPERTIES' => 10,
24
		'NEW_QUALITATIVE_PROPERTIES' => 11,
25
		'DISALLOWED_TERMS' => 20,
26
		'QUALITATIVE_STATES' => 13);
27
 
28
	public function executer() {
29
		try {
30
			$this->bdd = new Bdd();
31
			$this->fichier = realpath(dirname(__FILE__)).'/../../../donnees/ontologie/v1.00_2003-02-18/Ontology.xml';
32
			// Lancement de l'action demandée
33
			$cmd = $this->getParametre('a');
34
		    switch ($cmd) {
35
		    	case 'analyser' :
36
					$this->vider();
37
		    		$this->lireFichierXml();
38
					break;
39
		    	case 'vider' :
40
		    		$this->vider();
41
				default :
42
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
43
			}
44
		} catch (Exception $e) {
45
			$this->traiterErreur($e->getMessage());
46
		}
47
    }
48
 
49
 
50
    /**
51
	 * Lit le fichier OSM et lance l'analyse des noeuds xml en fonction de leur type.
52
	 */
53
	private function lireFichierXml() {
54
		$termes = array_keys($this->types);
55
		$this->lecteur = new XMLReader();
56
		if ($this->ouvrirFichierXml()) {
57
			while ($this->lecteur->read()) {
58
				if ($this->lecteur->nodeType == XMLREADER::ELEMENT) {
59
					if (in_array($this->lecteur->localName, $termes)) {
60
						$noeud = $this->obtenirNoeudSimpleXml();
61
						$type = $this->lecteur->localName;
62
						$this->traiterTermes($this->types[$type], $noeud->children());
63
					} else if ($this->lecteur->localName == 'STATE_GROUPS') {
64
						$noeud = $this->obtenirNoeudSimpleXml();
65
						$this->traiterGroupes($noeud->children());
66
					}
67
				}
68
			}
69
			if (count($this->lotsTermes) > 0) {
70
				$this->insererLotDeTermes();
71
			}
72
			if (count($this->lotsRelations) > 0) {
73
				$this->insererLotDeRelations();
74
			}
75
		}
76
	}
77
 
78
	private function ouvrirFichierXml() {
79
		if ($this->lecteur->open($this->fichier) === false) {
80
			throw new Exception("Impossible d'ouvrir le fichier XML : $this->fichier");
81
		}
82
		return true;
83
	}
84
 
85
	private function obtenirNoeudSimpleXml() {
86
		$doc = new DOMDocument;
87
		$element = $this->lecteur->expand();
88
		$noeud = simplexml_import_dom($doc->importNode($element, true));
89
		return $noeud;
90
	}
91
 
92
	private function traiterTermes($type, $termes) {
93
		foreach ($termes as $terme) {
94
			$id = (int) $terme->attributes()->GLOBALID;
95
			if (isset($lotsTermes[$id]) === false) {
96
				$valeur = array();
97
				$valeur[] = (int) $id;
98
				$valeur[] = (int) $type;
99
				$valeur[] = (string) $terme->attributes()->term;
100
				$valeur[] = (string) $this->obtenirDefinition($terme);
101
				$valeur[] = (int) $this->obtenirPreference($terme);
102
				$valeur[] = (int) $this->obtenirAuteur($terme);
103
				$valeur[] = (int) $terme->attributes()->citationREF;
104
				$valeur[] = (int) $this->obtenirImage($terme);
105
				$this->lotsTermes[$id] = $valeur;
106
			}
107
			if (isset($terme->attributes()->parentID)) {
108
				$relation = array();
109
				$relation[] = (int) $terme->attributes()->GLOBALID;
110
				$relation[] = (int) $terme->attributes()->parentID;
111
				$relation[] = 'A POUR PARENT';
112
				$this->lotsRelations[] = $relation;
113
			}
114
			if (isset($terme->attributes()->stateOfNEWPROPERTYID)) {
115
				$relation = array();
116
				$relation[] = (int) $terme->attributes()->GLOBALID;
117
				$relation[] = (int) $terme->attributes()->stateOfNEWPROPERTYID;
118
				$relation[] = 'A POUR NOUVELLE QUALITE';
119
				$this->lotsRelations[] = $relation;
120
			}
121
		}
122
	}
123
 
124
	private function obtenirDefinition($terme) {
125
		$definition = null;
126
		if (isset($terme->DEFINITION)) {
127
			$definition = $terme->DEFINITION;
128
		}
129
		return $definition;
130
	}
131
 
132
	private function obtenirPreference($terme) {
133
		$preference = '1';
134
		if (isset($terme->attributes()->PREFERRED_TERM)) {
135
			$valeur = (string) $terme->attributes()->PREFERRED_TERM;
136
			$preference = (trim($valeur) == 'Disallowed Term') ? '0' : '1';
137
		}
138
		return $preference;
139
	}
140
 
141
	private function obtenirAuteur($terme) {
142
		$auteur = 0;
143
		if (isset($terme->attributes()->authorID)) {
144
			$auteur = $terme->attributes()->authorID;
145
		} elseif (isset($terme->attributes()->authorREF)) {
146
			$auteur = $terme->attributes()->authorREF;
147
		}
148
		return $auteur;
149
	}
150
 
151
	private function obtenirImage($terme) {
152
		$image = 0;
153
		if (isset($terme->attributes()->pictureREF)) {
154
			$image = $terme->attributes()->pictureREF;
155
		}
156
		return $image;
157
	}
158
 
159
	private function traiterGroupes($groupes) {
160
		$lotsTermes = $lotsRelations = array();
161
		foreach ($groupes as $groupe) {
162
			$id = $this->baseIdGroupe + (int) $groupe->attributes()->GROUP_ID;
163
			if (isset($lotsTermes[$id]) === false) {
164
				$valeur = array();
165
				$valeur[] = (int) $id;
166
				$valeur[] = 18;
167
				$valeur[] = (string) $groupe->attributes()->groupName;
168
				$valeur[] = '';
169
				$valeur[] = 1;
170
				$valeur[] = 0;
171
				$valeur[] = 0;
172
				$valeur[] = 0;
173
				$this->lotsTermes[$id] = $valeur;
174
			}
175
			if (isset($groupe->STRUCTURES_LINKED_TO_GROUP)) {
176
				foreach ($groupe->STRUCTURES_LINKED_TO_GROUP->children() as $structure) {
177
					$relation = array();
178
					$relation[] = (int) $structure->attributes()->GLOBALID;
179
					$relation[] = (int) $id;
180
					$relation[] = 'A POUR GROUPE';
181
					$this->lotsRelations[] = $relation;
182
				}
183
			}
184
			if (isset($groupe->STATES_IN_GROUP)) {
185
				foreach ($groupe->STATES_IN_GROUP->children() as $etat) {
186
					$relation = array();
187
					$relation[] = (int) $id;
188
					$relation[] = (int) $etat->attributes()->GLOBALID;
189
					$relation[] = 'A POUR ETAT';
190
					$this->lotsRelations[] = $relation;
191
				}
192
			}
193
			if (isset($groupe->STATESUBGROUPS)) {
194
				$this->traiterSousGroupes($id, $groupe->STATESUBGROUPS->children());
195
			}
196
		}
197
	}
198
 
199
	private function traiterSousGroupes($idGroupe, $sousGroupes) {
200
		$lotsTermes = $lotsRelations = array();
201
		foreach ($sousGroupes as $sg) {
202
			$id = $this->baseIdSousGroupe + (int) $sg->attributes()->STATESUBGROUP_GLOBALID;
203
			if (isset($lotsTermes[$id]) === false) {
204
				$valeur = array();
205
				$valeur[] = (int) $id;
206
				$valeur[] = 19;
207
				$valeur[] = (string) $sg->attributes()->subgGroupName;
208
				$valeur[] = '';
209
				$valeur[] = 1;
210
				$valeur[] = 0;
211
				$valeur[] = 0;
212
				$valeur[] = 0;
213
				$this->lotsTermes[$id] = $valeur;
214
 
215
				$relation = array();
216
				$relation[] = (int) $idGroupe;
217
				$relation[] = (int) $id;
218
				$relation[] = 'A POUR SOUS-GROUPE';
219
				$this->lotsRelations[] = $relation;
220
			}
221
			if (isset($sg->STATES_IN_SUBGROUP)) {
222
				foreach ($sg->STATES_IN_SUBGROUP->children() as $etat) {
223
					$relation = array();
224
					$relation[] = (int) $id;
225
					$relation[] = (int) $etat->attributes()->GLOBALID;
226
					$relation[] = 'A POUR ETAT';
227
					$this->lotsRelations[] = $relation;
228
				}
229
			}
230
		}
231
	}
232
 
233
	private function insererLotDeTermes() {
234
		$champs = implode(',', array('id_terme', 'ce_type', 'terme', 'definition', 'preference', 'ce_auteur', 'ce_publication', 'ce_image'));
235
		$values = $this->creerValues($this->lotsTermes);
236
		$requete = "INSERT INTO ontologie_terme ($champs) VALUES $values";
237
		$this->executerSql($requete);
238
	}
239
 
240
	private function insererLotDeRelations() {
241
		$champs = implode(',', array('id_terme_01', 'id_terme_02', 'relation'));
242
		$values = $this->creerValues($this->lotsRelations);
243
		$requete = "INSERT INTO ontologie_relation ($champs) VALUES $values";
244
		$this->executerSql($requete);
245
	}
246
 
247
	private function creerValues($valeurs) {
248
		$values = array();
249
		foreach ($valeurs as $valeur) {
250
			foreach ($valeur as $id => $val) {
251
				$valeur[$id] = $this->etreVide($val) ? 'NULL' : $this->proteger(trim($val));
252
			}
253
			$values[]  = '('.implode(',', $valeur).')';
254
		}
255
		$values = implode(',', $values);
256
		return $values;
257
	}
258
 
259
	private function etreVide($val) {
260
		$vide = ($val === null || trim($val) === '') ? true : false;
261
		return $vide;
262
	}
263
 
264
	private function executerSql($requete) {
265
		$this->bdd->requeter($requete);
266
	}
267
 
268
	private function proteger($chaine) {
269
		return $this->bdd->proteger($chaine);
270
	}
271
 
272
	private function vider() {
273
		$requete = 'TRUNCATE TABLE ontologie_terme';
274
		$this->executerSql($requete);
275
		$requete = 'TRUNCATE TABLE ontologie_relation';
276
		$this->executerSql($requete);
277
	}
278
}
279
?>