Subversion Repositories Applications.referentiel

Rev

Rev 49 | Rev 54 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 49 Rev 51
1
<?php
1
<?php
2
// Encodage : UTF-8
2
// Encodage : UTF-8
3
// +-------------------------------------------------------------------------------------------------------------------+
3
// +-------------------------------------------------------------------------------------------------------------------+
4
/**
4
/**
5
* Versionnage de référentiels de nomenclature et taxonomie
5
* Versionnage de référentiels de nomenclature et taxonomie
6
*
6
*
7
* Description : classe permettant de versionner les référentiels selon le manuel technique
7
* Description : classe permettant de versionner les référentiels selon le manuel technique
8
* Utilisation : php script.php versionnage -p bdnff -a tout
8
* Utilisation : php script.php versionnage -p bdnff -a tout
9
*
9
*
10
//Auteur original :
10
//Auteur original :
11
* @author       Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
* @author       Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
* @copyright	Tela-Botanica 1999-2010
12
* @copyright	Tela-Botanica 1999-2010
13
* @link			http://www.tela-botanica.org/wikini/RTaxMethodo/wakka.php?wiki=MaNuel
13
* @link			http://www.tela-botanica.org/wikini/RTaxMethodo/wakka.php?wiki=MaNuel
14
* @licence		GPL v3 & CeCILL v2
14
* @licence		GPL v3 & CeCILL v2
15
* @version		$Id$
15
* @version		$Id$
16
*/
16
*/
17
// +-------------------------------------------------------------------------------------------------------------------+
17
// +-------------------------------------------------------------------------------------------------------------------+
18
// TODO : supprimer l'utilisation du paramêtres 'p' et chercher les infos depuis la bdd
18
// TODO : supprimer l'utilisation du paramêtres 'p' et chercher les infos depuis la bdd
19
class Versionnage extends ScriptCommande {
19
class Versionnage extends ScriptCommande {
20
	
20
	
21
	const SCRIPT_NOM = 'versionnage';
21
	const SCRIPT_NOM = 'versionnage';
22
	
22
	
23
	private $projet = null;
23
	private $projet = null;
24
	private $traitement = null;
24
	private $traitement = null;
-
 
25
	private $meta = null;
25
	private $messages = null;
26
	private $messages = null;
26
	private $manuel = null;
27
	private $manuel = null;
-
 
28
	private $noms = null;
-
 
29
	private $noms_precedents = null;
-
 
30
	private $noms_stat = null;
-
 
31
	private $champs_ordre = null;
-
 
32
	private $champs_nom = null;
-
 
33
	private $diff_modif_types = null;
-
 
34
	private $signature_md5 = null;
27
	
35
	
28
	private $resultatDao = null;
36
	private $resultatDao = null;
29
	private $traitementDao = null;
37
	private $traitementDao = null;
-
 
38
	private $metaDao = null;
30
	private $tableStructureDao = null;
39
	private $tableStructureDao = null;
31
	private $referentielDao = null;
40
	private $referentielDao = null;
32
	
41
	
33
	public function executer() {
42
	public function executer() {
34
		// Récupération de paramêtres
43
		// Récupération de paramêtres
35
		$this->projet = $this->getParam('p');
44
		$this->projet = $this->getParam('p');
36
		
45
		
37
		// Récupération du dernier traitement demandé
46
		// Récupération du dernier traitement demandé
38
		$this->traitementDao = new TraitementDao();
47
		$this->traitementDao = new TraitementDao();
39
		$this->traitement = $this->traitementDao->getDernierTraitement($this->projet, self::SCRIPT_NOM);
48
		$this->traitement = $this->traitementDao->getDernierTraitement($this->projet, self::SCRIPT_NOM);
40
		if (isset($this->traitement)) {
49
		if (isset($this->traitement)) {
41
			Debug::printr($this->traitement);
50
			Debug::printr($this->traitement);
42
			// Écriture de la date de début du traitement
51
			// Écriture de la date de début du traitement
43
			Debug::printr('Debute:'.$this->traitementDao->debuterTraitement($this->traitement['id_traitement']));
52
			Debug::printr('Debute:'.$this->traitementDao->debuterTraitement($this->traitement['id_traitement']));
44
			
53
			
45
			// Nettoyage des traitements obsolètes
54
			// Nettoyage des traitements obsolètes
46
			$traitements_obsoletes = $this->traitementDao->getTraitementsObsoletes($this->projet, self::SCRIPT_NOM);
55
			$traitements_obsoletes = $this->traitementDao->getTraitementsObsoletes($this->projet, self::SCRIPT_NOM);
47
			if (isset($traitements_obsoletes)) {
56
			if (isset($traitements_obsoletes)) {
48
				Debug::printr('Supp. obsoletes:'.$this->traitementDao->supprimer($traitements_obsoletes));
57
				Debug::printr('Supp. obsoletes:'.$this->traitementDao->supprimer($traitements_obsoletes));
49
			}
58
			}
50
			
59
			
51
			// Lancement du test demandé
60
			// Lancement du test demandé
52
			$cmd = $this->getParam('a');
61
			$cmd = $this->getParam('a');
53
	    	switch ($cmd) {
62
	    	switch ($cmd) {
54
				case 'tout' :
63
				case 'tout' :
-
 
64
					$this->metaDao = new MetaDao();
55
					$this->resultatDao = new ResultatDao();
65
					$this->resultatDao = new ResultatDao();
56
					$this->referentielDao = new ReferentielDao();
66
					$this->referentielDao = new ReferentielDao();
57
					$this->manuel = parse_ini_file(Config::get('chemin_appli').DS.'..'.DS.'configurations'.DS.'referentiel_v2.1.ini');
67
					$this->manuel = parse_ini_file(Config::get('chemin_appli').DS.'..'.DS.'configurations'.DS.'referentiel_v2.1.ini');
58
					Debug::printr('Départ lancement test:');
68
					Debug::printr('Départ lancement versionnage:');
59
					$this->lancerVersionnage();
69
					$this->lancerVersionnage();
60
					break;
70
					break;
61
				default :
71
				default :
62
					$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
72
					$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
63
			}
73
			}
64
			// Écriture de la date de fin du traitement
74
			// Écriture de la date de fin du traitement
65
			Debug::printr('Termine:'.$this->traitementDao->terminerTraitement($this->traitement['id_traitement']));
75
			Debug::printr('Termine:'.$this->traitementDao->terminerTraitement($this->traitement['id_traitement']));
66
		}
76
		}
67
    }
77
    }
68
 
78
 
69
    public function lancerVersionnage() {
79
    public function lancerVersionnage() {
70
		// Récupération des données à tester
80
		$this->chargerTraitementParametre();
-
 
81
		$this->archiver();
71
		$noms = $this->referentielDao->getTout($this->projet);
82
    	$this->chargerNomsATraiter();
72
		Debug::printr('Nbre noms :'.count($noms));
83
    	$this->analyserNomsATraiter();
-
 
84
		$this->creerFichiers();
-
 
85
		$this->traiterMessages();
-
 
86
	}
-
 
87
	
-
 
88
	private function chargerTraitementParametre() {
-
 
89
		$this->meta = unserialize($this->traitement['script_parametres']);
-
 
90
	}
-
 
91
	
-
 
92
	private function archiver() {
-
 
93
		$ok = $this->referentielDao->archiver($this->projet, $this->meta['version']);
-
 
94
		if ($ok) {
73
	
95
			$m = "L'archivage de la version '{$this->meta['version']}' du référentiel '{$this->projet}' a réussi";
-
 
96
			$this->ajouterMessage($m);
-
 
97
		} else {
-
 
98
			$m = "L'archivage de la version '{$this->meta['version']}' du référentiel '{$this->projet}' a échoué";
-
 
99
			$this->ajouterMessage($m);
-
 
100
		}
-
 
101
	}
-
 
102
	
-
 
103
	private function chargerNomsATraiter() {
-
 
104
		$archive_courante = strtolower($this->projet).'_v'.str_replace('.', '_', $this->meta['version']);
-
 
105
		Debug::printr("Nom archive courante :".$archive_courante);
-
 
106
		$this->noms = $this->referentielDao->getTout($archive_courante);
74
		$this->versionnerBdnt($noms);
107
	}
-
 
108
	
-
 
109
	private function analyserNomsATraiter() {
75
		
110
		$this->noms_stat['combinaison'] = $this->getNombreCombinaison();
-
 
111
		$this->noms_stat['taxon'] = $this->getNombreTaxon();
-
 
112
		Debug::printr("Stats :".print_r($this->noms_stat, true));
-
 
113
	}
-
 
114
	
-
 
115
	private function getNombreCombinaison() {
-
 
116
		return count($this->noms);
-
 
117
	}
-
 
118
	
-
 
119
	private function getNombreTaxon() {
-
 
120
		$nbre = 0;
-
 
121
		foreach ($this->noms as $nom) {
-
 
122
			if ($nom['num_nom_retenu'] == $nom['num_nom']) {
-
 
123
				$nbre++;
-
 
124
			}
-
 
125
		}
76
		$this->traiterMessagesErreur();
126
		return $nbre;
-
 
127
	}
-
 
128
	
-
 
129
	private function creerFichiers() {
-
 
130
		// Respecter l'ordre de traitement : BDNT puis DIFF puis META
-
 
131
		$this->creerFichierBdnt();
-
 
132
		$this->creerFichierDiff();
-
 
133
		$this->creerFichierMeta();
77
	}
134
	}
78
	
135
	
79
	private function versionnerBdnt($noms) {
136
	private function creerFichierBdnt() {
80
		Debug::printr("Element courrant du tableau des noms : ".print_r(current($noms),true));
137
		Debug::printr("Element courrant du tableau des noms : ".print_r(current($this->noms),true));
81
		//Debug::printr("Taille mémoire du tableau des noms : ".Debug::tailleMemoireVar($noms));
138
		//Debug::printr("Taille mémoire du tableau des noms : ".Debug::tailleMemoireVar($this->noms));
82
		$champs_ordre = $this->determinerOrdreDesChamps(current($noms));
139
		$this->determinerOrdreDesChamps();
83
		Debug::printr("Tableau ordre des champs : ".print_r($champs_ordre, true));
140
		$this->definirNomDesChamps();
84
		
-
 
85
		$donnees = array();
-
 
86
		$donnees['champs'] = array_flip($champs_ordre);
141
		
87
		//$donnees['noms'] = $noms;
-
 
88
		
-
 
89
		foreach ($noms as $id => $nom) {
142
		$donnees = array();
90
			// Suppression de la ligne du tableau pour gagner de l'espace mémoire
143
		$donnees['champs'] = $this->champs_nom;
91
			unset($noms[$id]);
144
		foreach ($this->noms as &$nom) {
92
			$infos = array();
145
			$infos = array();
93
			foreach ($nom as $champ => $valeur) {
146
			foreach ($nom as $champ => &$valeur) {
94
				if (isset($champs_ordre[$champ])) {
147
				if (isset($this->champs_ordre[$champ])) {
95
					$ordre = $champs_ordre[$champ];
148
					$ordre = $this->champs_ordre[$champ];
96
					$infos[$ordre] = $valeur;
149
					$infos[$ordre] = trim($valeur);
97
				} else {
150
				} else {
98
					$e = "Le champ '$champ' n'a pas été pris en compte dans l'attribution de l'ordre des champs.";
151
					$e = "Le champ '$champ' n'a pas été pris en compte dans l'attribution de l'ordre des champs.";
99
					$this->ajouterMessage($e);
152
					$this->ajouterMessage($e);
100
				}
153
				}
101
			}
154
			}
102
			$donnees['noms'][] = $infos;
155
			$donnees['noms'][] = $infos;
103
		}
156
		}
104
		
157
		
105
		$bdtn_tsv = $this->getVue('versionnage/squelettes/bdnt', $donnees, '.tpl.tsv');
-
 
106
		if (file_put_contents(Config::get('chemin_referentiel_zip').'bdnt.txt', $bdtn_tsv) == false) {
158
		$bdnt_tsv = $this->getVue('versionnage/squelettes/bdnt', $donnees, '.tpl.tsv');
107
			Debug::printr("une erreur est survenu lors de l'écriture du fichier");
-
 
108
		} else {
-
 
109
			
-
 
110
		}
159
		$this->ecrireFichierBdnt($bdnt_tsv);
111
	}
160
	}
112
	
161
	
113
	private function determinerOrdreDesChamps($nom) {
162
	private function determinerOrdreDesChamps() {
-
 
163
		$champs_ordre = explode(',', $this->manuel['champs']);
114
		$champs_ordre = explode(',', $this->manuel['champs']);
164
		$champs_ordre = array_flip($champs_ordre);
115
		$champs_ordre = array_flip($champs_ordre);
165
		$nom_courant = current($this->noms);
116
		$champs_ordre = $this->attribuerOrdreChampsSupplémentaires($champs_ordre, $nom);
166
		$champs_ordre = $this->attribuerOrdreChampsSupplémentaires($champs_ordre, $nom_courant);
-
 
167
		asort($champs_ordre);
-
 
168
		$this->champs_ordre = $champs_ordre;
-
 
169
	}
-
 
170
	
117
		asort($champs_ordre);
171
	private function definirNomDesChamps() {
118
		return $champs_ordre;
172
		$this->champs_nom = array_flip($this->champs_ordre);
119
	}
173
	}
120
	
174
	
121
	private function attribuerOrdreChampsSupplémentaires($champs_ordre, $nom) {
175
	private function attribuerOrdreChampsSupplémentaires($champs_ordre, $nom) {
122
		foreach ($nom as $champ => $info) {
176
		foreach ($nom as $champ => $info) {
123
			if (!isset($champs_ordre[$champ])) {
177
			if (!isset($champs_ordre[$champ])) {
124
				$champs_ordre[$champ] = count($champs_ordre);
178
				$champs_ordre[$champ] = count($champs_ordre);
125
			}
179
			}
126
		}
180
		}
127
		return $champs_ordre;
181
		return $champs_ordre;
128
	}
182
	}
129
	
183
	
130
	private function ajouterMessage($message) {
184
	private function ajouterMessage($message) {
131
		$titre = self::SCRIPT_NOM.' #'.$this->traitement['id_traitement'];
185
		$titre = self::SCRIPT_NOM.' #'.$this->traitement['id_traitement'];
132
		$this->messages[] = array($titre, $message);
186
		$this->messages[] = array($titre, $message);
133
	}
187
	}
-
 
188
	
-
 
189
	private function ecrireFichierBdnt($contenu) {
-
 
190
		$fichier_nom = $this->getBaseNomFichier().'_ref.txt';
-
 
191
		$fichier_chemin = Config::get('chemin_referentiel_zip').$fichier_nom;
-
 
192
		
-
 
193
		if ($this->ecrireFichier($fichier_chemin, $contenu)) {
-
 
194
			$this->ajouterMessage("Écriture du fichier bdnt réussie.");
-
 
195
			$this->signature_md5 = md5_file($fichier_chemin);
-
 
196
			$this->ajouterMessage("Signature MD5 du fichier bdnt :".$this->signature_md5);
-
 
197
			$this->ajouterMessage("Nombre de noms traités : ".count($noms));
-
 
198
			
-
 
199
		}
-
 
200
	}
-
 
201
	
-
 
202
	private function getBaseNomFichier() {
-
 
203
		return strtolower($this->meta['acronyme'].'_v'.$this->meta['version']);
-
 
204
	}
-
 
205
	
-
 
206
	private function ecrireFichier($fichier_chemin, $contenu) {
-
 
207
		$retour = true;
-
 
208
		if (file_put_contents($fichier_chemin, $contenu) == false) {
-
 
209
			$e = "Une erreur est survenu lors de l'écriture du fichier : $fichier_chemin";
-
 
210
			$this->ajouterMessage($e);
-
 
211
			$retour = false;
-
 
212
		}
-
 
213
		return $retour;
-
 
214
	}
-
 
215
	
-
 
216
	private function creerFichierDiff() {
-
 
217
		$derniere_meta = $this->metaDao->getDerniere($this->projet);
-
 
218
		if ($derniere_meta === false) {
-
 
219
			$this->ajouterMessage("Premier versionnage pour ce projet, aucun fichier différentiel ne sera créé.");
-
 
220
		} else {
-
 
221
			$code_projet_precedent = strtolower($derniere_meta['code']).'_v'.str_replace('.', '_', $derniere_meta['version']);
-
 
222
			$this->noms_precedents = $this->referentielDao->getTout($code_projet_precedent);
-
 
223
			$donnees = array();
-
 
224
			$donnees['diff'] = $this->realiserDiff();
-
 
225
			$donnees['champs'] = $this->champs_nom;
-
 
226
			$diff_tsv = $this->getVue('versionnage/squelettes/diff', $donnees, '.tpl.tsv');
-
 
227
			$this->ecrireFichierDiff($diff_tsv);
-
 
228
		}
-
 
229
	}
-
 
230
	
-
 
231
	private function realiserDiff() {
-
 
232
		$diff = array();
-
 
233
		$this->noms_stat['modification'] = 0;
-
 
234
		foreach ($this->noms as $id => &$nom) {
-
 
235
			if (!isset($this->noms_precedents[$id])) {
-
 
236
				$diff[$id] = $nom;
-
 
237
				$diff[$id]['modification_type'] = 'A';
-
 
238
				$diff[$id]['modification_type_1'] = '0';
-
 
239
				$diff[$id]['modification_type_2'] = '0';
-
 
240
				$diff[$id]['modification_type_3'] = '0';
-
 
241
				$this->noms_stat['modification']++;
-
 
242
			} else {
-
 
243
				$nom_precedent =& $this->noms_precedents[$id];
-
 
244
				$nom_diff = array_diff_assoc($noms, $nom_precedent);
-
 
245
				if (count($nom_diff) > 0) {
-
 
246
					$this->noms_stat['modification']++;
-
 
247
					$modif['modification_type'] = 'M';
-
 
248
					$modif['modification_type_1'] = '0';
-
 
249
					$modif['modification_type_2'] = '0';
-
 
250
					$modif['modification_type_3'] = '0';
-
 
251
					$this->chargerTableauChampsModifTypes();
-
 
252
					foreach ($this->champs_nom as $champ) {
-
 
253
						if (isset($nom_diff[$champ])) {
-
 
254
							$diff[$id][$champ] = $nom_diff[$champ];
-
 
255
							$type = $this->getDiffType($champ);
-
 
256
							$modif['modification_type_'.$type] = '1';
-
 
257
						} else {
-
 
258
							$diff[$id][$champ] = '';
-
 
259
						}
-
 
260
					}
-
 
261
					foreach ($modif as $cle => $val) {
-
 
262
						$diff[$id][$cle] = $val;
-
 
263
					}
-
 
264
				}
-
 
265
			}
-
 
266
		}
-
 
267
		return $diff;
-
 
268
	}
-
 
269
	
-
 
270
	private function chargerTableauChampsModifTypes() {
-
 
271
		$champs = explode(',', $this->manuel['champs_diff_type']);
-
 
272
		foreach ($champs as $champ) {
-
 
273
			list($champ_nom, $type) = explode('=', $champ);
-
 
274
			$this->diff_modif_types[$champ_nom] = $type;
-
 
275
		}
-
 
276
	}
-
 
277
	
-
 
278
	private function getDiffType($champ_nom) {
-
 
279
		$type = isset($this->diff_modif_types[$champ_nom]) ? $this->diff_modif_types[$champ_nom] : '3';
-
 
280
		return $type;
-
 
281
	}
-
 
282
	
-
 
283
	private function ecrireFichierDiff($contenu) {
-
 
284
		$fichier_nom = $this->getBaseNomFichier().'diff.txt';
-
 
285
		$fichier_chemin = Config::get('chemin_referentiel_zip').$fichier_nom;
-
 
286
		
-
 
287
		if ($this->ecrireFichier($fichier_chemin, $contenu)) {
-
 
288
			$this->ajouterMessage("Écriture du fichier diff réussie.");
-
 
289
		}
-
 
290
	}
-
 
291
	
-
 
292
	private function creerFichierMeta() {
-
 
293
		$donnees = array();
-
 
294
		$donnees = $this->meta;
-
 
295
		$donnees['stats'] = $this->noms_stat;
-
 
296
		$donnees['signature'] = $this->signature_md5;
-
 
297
		$meta_tsv = $this->getVue('versionnage/squelettes/meta', $donnees, '.tpl.tsv');
-
 
298
		$this->ecrireFichierMeta($meta_tsv);
-
 
299
	}
-
 
300
	
-
 
301
	private function ecrireFichierMeta($contenu) {
-
 
302
		$fichier_nom = $this->getBaseNomFichier().'_meta.txt';
-
 
303
		$fichier_chemin = Config::get('chemin_referentiel_zip').$fichier_nom;
-
 
304
		
-
 
305
		if ($this->ecrireFichier($fichier_chemin, $contenu)) {
-
 
306
			$this->ajouterMessage("Écriture du fichier meta réussie.");
-
 
307
			$this->archiverMetadonnees();
-
 
308
		}
-
 
309
	}
-
 
310
	
-
 
311
	private function archiverMetadonnees() {
-
 
312
		$metadonnees = $this->meta;
-
 
313
		$metadonnees['code'] = $this->meta['acronyme'];
-
 
314
		unset($metadonnees['acronyme']);
-
 
315
		$metadonnees['domaine_taxo'] = $this->meta['dom_tax'];
-
 
316
		unset($metadonnees['dom_tax']);
-
 
317
		$metadonnees['domaine_geo'] = $this->meta['dom_geo'];
-
 
318
		unset($metadonnees['dom_geo']);
-
 
319
		$metadonnees['domaine_nom'] = $this->meta['dom_code'];
-
 
320
		unset($metadonnees['dom_code']);
-
 
321
		$metadonnees['auteur'] = $this->meta['auteur_principal'];
-
 
322
		unset($metadonnees['auteur_principal']);
-
 
323
		$metadonnees['date_production'] = $this->meta['date_prod'];
-
 
324
		unset($metadonnees['date_prod']);
-
 
325
		$metadonnees['droit'] = $this->meta['copyright'];
-
 
326
		unset($metadonnees['copyright']);
-
 
327
		
-
 
328
		$ok = $this->metaDao->ajouter($metadonnees);
-
 
329
		if ($ok === false) {
-
 
330
			$this->ajouterMessage("L'archivage des métadonnées a échoué.");
-
 
331
		}
-
 
332
	}
134
	
333
	
135
	private function traiterMessages() {
334
	private function traiterMessages() {
136
		if (isset($this->messages)) {
335
		if (isset($this->messages)) {
137
			foreach ($this->messages as $message) {
336
			foreach ($this->messages as $message) {
138
				$this->resultatDao->ajouter($this->traitement['id_traitement'], $message);
337
				$this->resultatDao->ajouter($this->traitement['id_traitement'], $message);
139
			}
338
			}
140
		}
339
		}
141
	}
340
	}
142
}
341
}
143
?>
342
?>