Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 228 | Rev 232 | Go to most recent revision | Details | Compare with Previous | 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
 */
228 jpm 6
class Prometheus extends Script {
225 jpm 7
 
8
	private $lecteur = null;
9
	private $fichier = '';
10
	private $lotsTermes = array();
11
	private $lotsRelations = array();
226 jpm 12
	private $lotsImages = array();
13
	private $lotsPublications = array();
14
	private $lotsAuteurs = array();
15
	private $lotsHierarchie = array();
225 jpm 16
	private $baseIdGroupe = 10000;
17
	private $baseIdSousGroupe = 20000;
18
	private $types = array(
19
		'FREQUENCY_MODIFIERS' => 2,
20
		'QUALIFIERS' => 3,
21
		'RELATIVE_MODIFIERS' => 4,
22
		'RELATIVE_VALUES' => 5,
23
		'SPATIAL_MODIFIERS' => 6,
24
		'LOCATER_REGIONS' => 7,
25
		'TEMPORAL_MODIFIERS' => 8,
26
		'UNIT_TERMS' => 9,
27
		'QUANTITATIVE_PROPERTIES' => 10,
28
		'NEW_QUALITATIVE_PROPERTIES' => 11,
29
		'DISALLOWED_TERMS' => 20,
226 jpm 30
		'QUALITATIVE_STATES' => 13,
31
		'TYPE_OF_STRUCTURE_TERMS' => 14,
32
		'STRUCTURE_TERMS' => 15,
33
		'REGION_TERMS' => 16,
34
		'GENERIC_STRUCTURES' => 17);
225 jpm 35
 
36
	public function executer() {
37
		try {
38
			$this->bdd = new Bdd();
230 jpm 39
			$this->fichier = realpath(dirname(__FILE__)).'/../../../donnees/prometheus/v1.00_2003-02-18/Ontology.xml';
225 jpm 40
			// Lancement de l'action demandée
41
			$cmd = $this->getParametre('a');
42
		    switch ($cmd) {
43
		    	case 'analyser' :
44
					$this->vider();
45
		    		$this->lireFichierXml();
46
					break;
47
		    	case 'vider' :
48
		    		$this->vider();
49
				default :
50
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
51
			}
52
		} catch (Exception $e) {
53
			$this->traiterErreur($e->getMessage());
54
		}
55
    }
56
 
57
 
58
    /**
59
	 * Lit le fichier OSM et lance l'analyse des noeuds xml en fonction de leur type.
60
	 */
61
	private function lireFichierXml() {
62
		$termes = array_keys($this->types);
63
		$this->lecteur = new XMLReader();
64
		if ($this->ouvrirFichierXml()) {
65
			while ($this->lecteur->read()) {
66
				if ($this->lecteur->nodeType == XMLREADER::ELEMENT) {
67
					if (in_array($this->lecteur->localName, $termes)) {
68
						$noeud = $this->obtenirNoeudSimpleXml();
69
						$type = $this->lecteur->localName;
70
						$this->traiterTermes($this->types[$type], $noeud->children());
71
					} else if ($this->lecteur->localName == 'STATE_GROUPS') {
72
						$noeud = $this->obtenirNoeudSimpleXml();
73
						$this->traiterGroupes($noeud->children());
226 jpm 74
					} else if ($this->lecteur->localName == 'NewQualitativeProperties') {
75
						$noeud = $this->obtenirNoeudSimpleXml();
76
						$this->traiterNouvellesQualites($noeud->children());
77
					} else if ($this->lecteur->localName == 'RELATIONSHIPS') {
78
						$noeud = $this->obtenirNoeudSimpleXml();
79
						$this->traiterRelations($noeud->children());
80
					} else if ($this->lecteur->localName == 'PICTURES') {
81
						$noeud = $this->obtenirNoeudSimpleXml();
82
						$this->traiterImages($noeud->children());
83
					} else if ($this->lecteur->localName == 'CITATIONS') {
84
						$noeud = $this->obtenirNoeudSimpleXml();
85
						$this->traiterCitations($noeud->children());
86
					} else if ($this->lecteur->localName == 'AUTHORS') {
87
						$noeud = $this->obtenirNoeudSimpleXml();
88
						$this->traiterAuteurs($noeud->children());
89
					} else if ($this->lecteur->localName == 'TreeNode') {
90
						$noeud = $this->obtenirNoeudSimpleXml();
91
						$this->traiterHierarchie($noeud);
225 jpm 92
					}
93
				}
94
			}
95
			if (count($this->lotsTermes) > 0) {
96
				$this->insererLotDeTermes();
97
			}
98
			if (count($this->lotsRelations) > 0) {
99
				$this->insererLotDeRelations();
100
			}
226 jpm 101
			if (count($this->lotsImages) > 0) {
102
				$this->insererLotImages();
103
			}
104
			if (count($this->lotsPublications) > 0) {
105
				$this->insererLotDePublications();
106
			}
107
			if (count($this->lotsAuteurs) > 0) {
108
				$this->insererLotAuteurs();
109
			}
110
			if (count($this->lotsHierarchie) > 0) {
111
				$this->insererLotHierarchie();
112
			}
225 jpm 113
		}
114
	}
115
 
116
	private function ouvrirFichierXml() {
117
		if ($this->lecteur->open($this->fichier) === false) {
118
			throw new Exception("Impossible d'ouvrir le fichier XML : $this->fichier");
119
		}
120
		return true;
121
	}
122
 
123
	private function obtenirNoeudSimpleXml() {
124
		$doc = new DOMDocument;
125
		$element = $this->lecteur->expand();
126
		$noeud = simplexml_import_dom($doc->importNode($element, true));
127
		return $noeud;
128
	}
129
 
130
	private function traiterTermes($type, $termes) {
131
		foreach ($termes as $terme) {
132
			$id = (int) $terme->attributes()->GLOBALID;
226 jpm 133
			if (isset($this->lotsTermes[$id]) === false) {
225 jpm 134
				$valeur = array();
135
				$valeur[] = (int) $id;
136
				$valeur[] = (int) $type;
137
				$valeur[] = (string) $terme->attributes()->term;
138
				$valeur[] = (string) $this->obtenirDefinition($terme);
139
				$valeur[] = (int) $this->obtenirPreference($terme);
140
				$valeur[] = (int) $this->obtenirAuteur($terme);
226 jpm 141
				$valeur[] = (int) $this->obtenirCitation($terme);
225 jpm 142
				$valeur[] = (int) $this->obtenirImage($terme);
143
				$this->lotsTermes[$id] = $valeur;
144
			}
145
			if (isset($terme->attributes()->parentID)) {
146
				$relation = array();
147
				$relation[] = (int) $terme->attributes()->GLOBALID;
148
				$relation[] = (int) $terme->attributes()->parentID;
149
				$relation[] = 'A POUR PARENT';
226 jpm 150
				$this->ajouterRelation($relation);
225 jpm 151
			}
226 jpm 152
			if (isset($terme->attributes()->PARENT_STRUCTURE_ID)) {
153
				$relation = array();
154
				$relation[] = (int) $terme->attributes()->GLOBALID;
155
				$relation[] = (int) $terme->attributes()->PARENT_STRUCTURE_ID;
156
				$relation[] = 'A POUR STRUCTURE PARENTE';
157
				$this->ajouterRelation($relation);
158
			}
225 jpm 159
			if (isset($terme->attributes()->stateOfNEWPROPERTYID)) {
160
				$relation = array();
161
				$relation[] = (int) $terme->attributes()->GLOBALID;
162
				$relation[] = (int) $terme->attributes()->stateOfNEWPROPERTYID;
163
				$relation[] = 'A POUR NOUVELLE QUALITE';
226 jpm 164
				$this->ajouterRelation($relation);
225 jpm 165
			}
166
		}
167
	}
168
 
169
	private function obtenirDefinition($terme) {
170
		$definition = null;
171
		if (isset($terme->DEFINITION)) {
172
			$definition = $terme->DEFINITION;
173
		}
174
		return $definition;
175
	}
176
 
177
	private function obtenirPreference($terme) {
178
		$preference = '1';
179
		if (isset($terme->attributes()->PREFERRED_TERM)) {
180
			$valeur = (string) $terme->attributes()->PREFERRED_TERM;
181
			$preference = (trim($valeur) == 'Disallowed Term') ? '0' : '1';
182
		}
183
		return $preference;
184
	}
185
 
186
	private function obtenirAuteur($terme) {
187
		$auteur = 0;
188
		if (isset($terme->attributes()->authorID)) {
189
			$auteur = $terme->attributes()->authorID;
190
		} elseif (isset($terme->attributes()->authorREF)) {
191
			$auteur = $terme->attributes()->authorREF;
192
		}
193
		return $auteur;
194
	}
195
 
226 jpm 196
	private function obtenirCitation($terme) {
197
		$citation = 0;
198
		if (isset($terme->attributes()->citationID)) {
199
			$citation = $terme->attributes()->citationID;
200
		} elseif (isset($terme->attributes()->citationREF)) {
201
			$citation = $terme->attributes()->citationREF;
202
		}
203
		return $citation;
204
	}
205
 
225 jpm 206
	private function obtenirImage($terme) {
207
		$image = 0;
208
		if (isset($terme->attributes()->pictureREF)) {
209
			$image = $terme->attributes()->pictureREF;
210
		}
211
		return $image;
212
	}
213
 
214
	private function traiterGroupes($groupes) {
215
		foreach ($groupes as $groupe) {
216
			$id = $this->baseIdGroupe + (int) $groupe->attributes()->GROUP_ID;
226 jpm 217
			if (isset($this->lotsTermes[$id]) === false) {
225 jpm 218
				$valeur = array();
219
				$valeur[] = (int) $id;
220
				$valeur[] = 18;
221
				$valeur[] = (string) $groupe->attributes()->groupName;
222
				$valeur[] = '';
223
				$valeur[] = 1;
224
				$valeur[] = 0;
225
				$valeur[] = 0;
226
				$valeur[] = 0;
227
				$this->lotsTermes[$id] = $valeur;
228
			}
229
			if (isset($groupe->STRUCTURES_LINKED_TO_GROUP)) {
230
				foreach ($groupe->STRUCTURES_LINKED_TO_GROUP->children() as $structure) {
231
					$relation = array();
232
					$relation[] = (int) $structure->attributes()->GLOBALID;
233
					$relation[] = (int) $id;
234
					$relation[] = 'A POUR GROUPE';
226 jpm 235
					$this->ajouterRelation($relation);
225 jpm 236
				}
237
			}
238
			if (isset($groupe->STATES_IN_GROUP)) {
239
				foreach ($groupe->STATES_IN_GROUP->children() as $etat) {
240
					$relation = array();
241
					$relation[] = (int) $id;
242
					$relation[] = (int) $etat->attributes()->GLOBALID;
243
					$relation[] = 'A POUR ETAT';
226 jpm 244
					$this->ajouterRelation($relation);
225 jpm 245
				}
246
			}
247
			if (isset($groupe->STATESUBGROUPS)) {
248
				$this->traiterSousGroupes($id, $groupe->STATESUBGROUPS->children());
249
			}
250
		}
251
	}
252
 
253
	private function traiterSousGroupes($idGroupe, $sousGroupes) {
254
		foreach ($sousGroupes as $sg) {
255
			$id = $this->baseIdSousGroupe + (int) $sg->attributes()->STATESUBGROUP_GLOBALID;
226 jpm 256
			if (isset($this->lotsTermes[$id]) === false) {
225 jpm 257
				$valeur = array();
258
				$valeur[] = (int) $id;
259
				$valeur[] = 19;
260
				$valeur[] = (string) $sg->attributes()->subgGroupName;
261
				$valeur[] = '';
262
				$valeur[] = 1;
263
				$valeur[] = 0;
264
				$valeur[] = 0;
265
				$valeur[] = 0;
266
				$this->lotsTermes[$id] = $valeur;
267
 
268
				$relation = array();
269
				$relation[] = (int) $idGroupe;
270
				$relation[] = (int) $id;
271
				$relation[] = 'A POUR SOUS-GROUPE';
226 jpm 272
				$this->ajouterRelation($relation);
225 jpm 273
			}
274
			if (isset($sg->STATES_IN_SUBGROUP)) {
275
				foreach ($sg->STATES_IN_SUBGROUP->children() as $etat) {
276
					$relation = array();
277
					$relation[] = (int) $id;
278
					$relation[] = (int) $etat->attributes()->GLOBALID;
279
					$relation[] = 'A POUR ETAT';
226 jpm 280
					$this->ajouterRelation($relation);
225 jpm 281
				}
282
			}
283
		}
284
	}
285
 
226 jpm 286
	private function traiterNouvellesQualites($qualites) {
287
		foreach ($qualites as $qualite) {
288
			$id = (int) $qualite->attributes()->IDSEQ;
289
			if (isset($this->lotsTermes[$id]) === false) {
290
				$valeur = array();
291
				$valeur[] = (int) $id;
292
				$valeur[] = 11;
293
				$valeur[] = (string) $qualite->attributes()->term;
294
				$valeur[] = (string) $this->obtenirDefinition($terme);
295
				$valeur[] = (int) $this->obtenirPreference($terme);
296
				$valeur[] = (int) $this->obtenirAuteur($terme);
297
				$valeur[] = (int) $this->obtenirCitation($terme);
298
				$valeur[] = (int) $this->obtenirImage($terme);
299
				$this->lotsTermes[$id] = $valeur;
300
			}
301
			if (isset($qualite->attributes()->ParentPropertyID)) {
302
				$relation = array();
303
				$relation[] = (int) $qualite->attributes()->IDSEQ;
304
				$relation[] = (int) $qualite->attributes()->ParentPropertyID;
305
				$relation[] = 'A POUR PARENT';
306
				$this->ajouterRelation($relation);
307
			}
308
			if (isset($qualite->MemberStates)) {
309
				$etats = $qualite->MemberStates->children();
310
				$idParent = $qualite->attributes()->IDSEQ;
311
				$this->traiterEtatsMembre($etats, $idParent);
312
			}
313
			if (isset($qualite->Structures_linked_to_Property)) {
314
				$structures = $qualite->Structures_linked_to_Property->children();
315
				$idParent = $qualite->attributes()->IDSEQ;
316
				$this->traiterStructuresLiees($structures, $idParent);
317
			}
318
			if (isset($qualite->ContextGroups)) {
319
				$contextes = $qualite->ContextGroups->children();
320
				if (count($contextes) > 0) {
321
					foreach ($contextes as $contexte) {
322
						$idParent = $contexte->attributes()->ID;
323
						$structures = $contexte->Structures_linked_to_Context->children();
324
						$this->traiterStructuresLiees($structures, $idParent);
325
						$etats = $contexte->MemberStates->children();
326
						$this->traiterEtatsMembre($etats, $idParent);
327
					}
328
				}
329
			}
330
		}
331
	}
332
 
333
	private function ajouterRelation($relation) {
334
		$id = implode('-', $relation);
335
		if (isset($this->lotsRelations[$id]) === false) {
336
			$this->lotsRelations[$id] = $relation;
337
		}
338
	}
339
 
340
	private function traiterEtatsMembre($etats, $idParent) {
341
		if (count($etats) > 0) {
342
			foreach ($etats as $etat) {
343
				$relation = array();
344
				$relation[] = (int) $idParent;
345
				$relation[] = (int) $etat->attributes()->RefID;
346
				$relation[] = 'A POUR ETAT';
347
				$this->ajouterRelation($relation);
348
			}
349
		}
350
	}
351
 
352
	private function traiterStructuresLiees($structures, $idParent) {
353
		if (count($structures) > 0) {
354
			foreach ($structures as $structure) {
355
				$relation = array();
356
				$relation[] = (int) $structure->attributes()->RefID;
357
				$relation[] = (int) $idParent;
358
				$relation[] = 'A POUR PROPRIETE';
359
				$this->ajouterRelation($relation);
360
			}
361
		}
362
	}
363
 
364
	private function traiterRelations($relations) {
365
		foreach ($relations as $rel) {
366
			$relation = array();
367
			$relation[] = (int) $rel->attributes()->term1REF;
368
			$relation[] = (int) $rel->attributes()->term2REF;
369
			$relation[] = (string) $this->obtenirTypeRelation($rel->attributes()->relationship);
370
			$this->ajouterRelation($relation);
371
		}
372
	}
373
 
374
	private function obtenirTypeRelation($type) {
375
		switch ($type) {
376
			case 'ASSOCIATED WITH' :
377
				$relation = 'ASSOCIE AVEC';
378
				break;
379
			case 'IS A PART OF' :
380
				$relation = 'EST UNE PARTIE DE';
381
				break;
382
			case 'IS A TYPE OF' :
383
				$relation = 'EST UN TYPE DE';
384
				break;
385
			default :
386
				$relation = '';
387
		}
388
		return $relation;
389
	}
390
 
391
	private function traiterImages($images) {
392
		foreach ($images as $img) {
393
			$valeur = array();
394
			$valeur[] = (int) $img->attributes()->ID;
395
			$valeur[] = (string) $img->attributes()->NAME;
396
			$valeur[] = (int) $img->attributes()->CITATION_REFID;
397
			$this->lotsImages[] = $valeur;
398
		}
399
	}
400
 
401
	private function traiterCitations($citations) {
402
		foreach ($citations as $publi) {
403
			$valeur = array();
404
			$valeur[] = (int) $publi->attributes()->ID;
405
			$valeur[] = (int) $publi->attributes()->primaryAuthorREF;
406
			$valeur[] = (string) $publi->PUBLICATION;
407
			$valeur[] = (string) $publi->DATE;
408
			$valeur[] = (string) $publi->PAGE;
409
			$this->lotsPublications[] = $valeur;
410
		}
411
	}
412
 
413
	private function traiterAuteurs($auteurs) {
414
		foreach ($auteurs as $auteur) {
415
			$valeur = array();
416
			$valeur[] = (int) $auteur->attributes()->ID;
417
			$valeur[] = (string) $auteur->attributes()->givenNames;
418
			$valeur[] = (string) $auteur->attributes()->surname;
419
			$valeur[] = $this->obtenirDateNaissance((string) $auteur->attributes()->born);
420
			$valeur[] = (string) $auteur->attributes()->died;
421
			$this->lotsAuteurs[] = $valeur;
422
		}
423
	}
424
 
425
	private function obtenirDateNaissance($annee) {
426
		$date = $annee.'-00-00';
427
		return $date;
428
	}
429
 
430
	private function traiterHierarchie($noeud) {
431
		$valeur = array();
432
		$valeur[] = (int) $noeud->attributes()->ID;
433
		$valeur[] = (int) $noeud->attributes()->ParentNodeID;
434
		$valeur[] = (string) $noeud->attributes()->pathAsNames;
435
		$valeur[] = (string) $noeud->attributes()->pathAsID;
436
		$valeur[] = (int) $noeud->attributes()->TermID;
437
		$this->lotsHierarchie[] = $valeur;
438
	}
439
 
225 jpm 440
	private function insererLotDeTermes() {
441
		$champs = implode(',', array('id_terme', 'ce_type', 'terme', 'definition', 'preference', 'ce_auteur', 'ce_publication', 'ce_image'));
442
		$values = $this->creerValues($this->lotsTermes);
230 jpm 443
		$requete = "INSERT INTO prometheus_ontologie_terme_v1_00 ($champs) VALUES $values";
225 jpm 444
		$this->executerSql($requete);
445
	}
446
 
447
	private function insererLotDeRelations() {
448
		$champs = implode(',', array('id_terme_01', 'id_terme_02', 'relation'));
449
		$values = $this->creerValues($this->lotsRelations);
230 jpm 450
		$requete = "INSERT INTO prometheus_ontologie_relation_v1_00 ($champs) VALUES $values";
225 jpm 451
		$this->executerSql($requete);
452
	}
453
 
226 jpm 454
	private function insererLotImages() {
455
		$champs = implode(',', array('id_image', 'uri', 'ce_publication'));
456
		$values = $this->creerValues($this->lotsImages);
230 jpm 457
		$requete = "INSERT INTO prometheus_ontologie_image_v1_00 ($champs) VALUES $values";
226 jpm 458
		$this->executerSql($requete);
459
	}
460
 
461
	private function insererLotDePublications() {
462
		$champs = implode(',', array('id_publication', 'ce_auteur_principal', 'titre', 'date', 'uri'));
463
		$values = $this->creerValues($this->lotsPublications);
230 jpm 464
		$requete = "INSERT INTO prometheus_ontologie_publication_v1_00 ($champs) VALUES $values";
226 jpm 465
		$this->executerSql($requete);
466
	}
467
 
468
	private function insererLotAuteurs() {
469
		$champs = implode(',', array('id_auteur', 'prenom', 'nom', 'naissance_date', 'deces_date'));
470
		$values = $this->creerValues($this->lotsAuteurs);
230 jpm 471
		$requete = "INSERT INTO prometheus_ontologie_auteur_v1_00 ($champs) VALUES $values";
226 jpm 472
		$this->executerSql($requete);
473
	}
474
 
475
	private function insererLotHierarchie() {
476
		$champs = implode(',', array('id_noeud', 'id_noeud_parent', 'chemin_noms', 'chemin_ids', 'ce_terme'));
477
		$values = $this->creerValues($this->lotsHierarchie);
230 jpm 478
		$requete = "INSERT INTO prometheus_ontologie_hierarchie_v1_00 ($champs) VALUES $values";
226 jpm 479
		$this->executerSql($requete);
480
	}
481
 
225 jpm 482
	private function creerValues($valeurs) {
483
		$values = array();
484
		foreach ($valeurs as $valeur) {
485
			foreach ($valeur as $id => $val) {
486
				$valeur[$id] = $this->etreVide($val) ? 'NULL' : $this->proteger(trim($val));
487
			}
488
			$values[]  = '('.implode(',', $valeur).')';
489
		}
490
		$values = implode(',', $values);
491
		return $values;
492
	}
493
 
494
	private function etreVide($val) {
495
		$vide = ($val === null || trim($val) === '') ? true : false;
496
		return $vide;
497
	}
498
 
499
	private function executerSql($requete) {
500
		$this->bdd->requeter($requete);
501
	}
502
 
503
	private function proteger($chaine) {
504
		return $this->bdd->proteger($chaine);
505
	}
506
 
507
	private function vider() {
230 jpm 508
		$requete = 'TRUNCATE TABLE prometheus_ontologie_auteur_v1_00';
225 jpm 509
		$this->executerSql($requete);
230 jpm 510
		$requete = 'TRUNCATE TABLE prometheus_ontologie_hierarchie_v1_00';
226 jpm 511
		$this->executerSql($requete);
230 jpm 512
		$requete = 'TRUNCATE TABLE prometheus_ontologie_image_v1_00';
226 jpm 513
		$this->executerSql($requete);
230 jpm 514
		$requete = 'TRUNCATE TABLE prometheus_ontologie_publication_v1_00';
226 jpm 515
		$this->executerSql($requete);
230 jpm 516
		$requete = 'TRUNCATE TABLE prometheus_ontologie_relation_v1_00';
225 jpm 517
		$this->executerSql($requete);
230 jpm 518
		$requete = 'TRUNCATE TABLE prometheus_ontologie_terme_v1_00';
226 jpm 519
		$this->executerSql($requete);
225 jpm 520
	}
521
}
522
?>