Subversion Repositories Applications.referentiel

Rev

Rev 49 | Rev 54 | 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() {
137
		Debug::printr("Element courrant du tableau des noms : ".print_r(current($this->noms),true));
138
		//Debug::printr("Taille mémoire du tableau des noms : ".Debug::tailleMemoireVar($this->noms));
139
		$this->determinerOrdreDesChamps();
140
		$this->definirNomDesChamps();
46 jpm 141
 
142
		$donnees = array();
51 jpm 143
		$donnees['champs'] = $this->champs_nom;
144
		foreach ($this->noms as &$nom) {
46 jpm 145
			$infos = array();
51 jpm 146
			foreach ($nom as $champ => &$valeur) {
147
				if (isset($this->champs_ordre[$champ])) {
148
					$ordre = $this->champs_ordre[$champ];
149
					$infos[$ordre] = trim($valeur);
46 jpm 150
				} else {
151
					$e = "Le champ '$champ' n'a pas été pris en compte dans l'attribution de l'ordre des champs.";
49 jpm 152
					$this->ajouterMessage($e);
46 jpm 153
				}
154
			}
47 jpm 155
			$donnees['noms'][] = $infos;
156
		}
46 jpm 157
 
51 jpm 158
		$bdnt_tsv = $this->getVue('versionnage/squelettes/bdnt', $donnees, '.tpl.tsv');
159
		$this->ecrireFichierBdnt($bdnt_tsv);
46 jpm 160
	}
161
 
51 jpm 162
	private function determinerOrdreDesChamps() {
46 jpm 163
		$champs_ordre = explode(',', $this->manuel['champs']);
164
		$champs_ordre = array_flip($champs_ordre);
51 jpm 165
		$nom_courant = current($this->noms);
166
		$champs_ordre = $this->attribuerOrdreChampsSupplémentaires($champs_ordre, $nom_courant);
46 jpm 167
		asort($champs_ordre);
51 jpm 168
		$this->champs_ordre = $champs_ordre;
46 jpm 169
	}
170
 
51 jpm 171
	private function definirNomDesChamps() {
172
		$this->champs_nom = array_flip($this->champs_ordre);
173
	}
174
 
46 jpm 175
	private function attribuerOrdreChampsSupplémentaires($champs_ordre, $nom) {
176
		foreach ($nom as $champ => $info) {
177
			if (!isset($champs_ordre[$champ])) {
178
				$champs_ordre[$champ] = count($champs_ordre);
179
			}
180
		}
181
		return $champs_ordre;
182
	}
183
 
49 jpm 184
	private function ajouterMessage($message) {
46 jpm 185
		$titre = self::SCRIPT_NOM.' #'.$this->traitement['id_traitement'];
49 jpm 186
		$this->messages[] = array($titre, $message);
46 jpm 187
	}
188
 
51 jpm 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
	}
333
 
49 jpm 334
	private function traiterMessages() {
335
		if (isset($this->messages)) {
336
			foreach ($this->messages as $message) {
337
				$this->resultatDao->ajouter($this->traitement['id_traitement'], $message);
338
			}
46 jpm 339
		}
340
	}
341
}
342
?>