Subversion Repositories eFlore/Projets.eflore-projets

Rev

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