Subversion Repositories Applications.referentiel

Rev

Rev 51 | Rev 55 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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