Subversion Repositories Applications.referentiel

Rev

Rev 56 | Rev 65 | 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';
55 jpm 22
	const MANUEL_VERSION = "2.1";
46 jpm 23
 
24
	private $projet = null;
25
	private $traitement = null;
51 jpm 26
	private $meta = null;
55 jpm 27
	private $version_courante = null;
49 jpm 28
	private $messages = null;
46 jpm 29
	private $manuel = null;
55 jpm 30
	private $manuel_nom = null;
31
	private $manuel_chemin = null;
32
	private $zip_chemin_dossier = null;
33
	private $zip_chemin_fichier = null;
34
 
51 jpm 35
	private $noms = null;
36
	private $noms_precedents = null;
37
	private $noms_stat = null;
38
	private $champs_ordre = null;
39
	private $champs_nom = null;
40
	private $diff_modif_types = null;
41
	private $signature_md5 = null;
46 jpm 42
 
43
	private $resultatDao = null;
44
	private $traitementDao = null;
51 jpm 45
	private $metaDao = null;
46 jpm 46
	private $tableStructureDao = null;
47
	private $referentielDao = null;
48
 
49
	public function executer() {
50
		// Récupération de paramêtres
51
		$this->projet = $this->getParam('p');
52
 
53
		// Récupération du dernier traitement demandé
54
		$this->traitementDao = new TraitementDao();
55
		$this->traitement = $this->traitementDao->getDernierTraitement($this->projet, self::SCRIPT_NOM);
56
		if (isset($this->traitement)) {
57
			Debug::printr($this->traitement);
58
			// Écriture de la date de début du traitement
59
			Debug::printr('Debute:'.$this->traitementDao->debuterTraitement($this->traitement['id_traitement']));
60
 
61
			// Nettoyage des traitements obsolètes
62
			$traitements_obsoletes = $this->traitementDao->getTraitementsObsoletes($this->projet, self::SCRIPT_NOM);
63
			if (isset($traitements_obsoletes)) {
64
				Debug::printr('Supp. obsoletes:'.$this->traitementDao->supprimer($traitements_obsoletes));
65
			}
66
 
67
			// Lancement du test demandé
68
			$cmd = $this->getParam('a');
69
	    	switch ($cmd) {
70
				case 'tout' :
55 jpm 71
					$this->initialiserScript();
51 jpm 72
					Debug::printr('Départ lancement versionnage:');
46 jpm 73
					$this->lancerVersionnage();
74
					break;
75
				default :
76
					$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
77
			}
78
			// Écriture de la date de fin du traitement
79
			Debug::printr('Termine:'.$this->traitementDao->terminerTraitement($this->traitement['id_traitement']));
80
		}
81
    }
55 jpm 82
 
83
    private function initialiserScript() {
84
		$this->metaDao = new MetaDao();
85
		$this->resultatDao = new ResultatDao();
86
		$this->referentielDao = new ReferentielDao();
87
 
88
		$this->manuel_nom = 'mtpr_v'.str_replace('.', '_', self::MANUEL_VERSION).'.pdf';
89
		$this->manuel_chemin = Config::get('chemin_appli').DS.'..'.DS.'configurations'.DS;
90
 
91
		$manuel_config_nom = 'referentiel_v'.self::MANUEL_VERSION.'.ini';
92
		$this->manuel = parse_ini_file($this->manuel_chemin.$manuel_config_nom);
93
	}
94
 
46 jpm 95
    public function lancerVersionnage() {
51 jpm 96
		$this->chargerTraitementParametre();
55 jpm 97
		$this->initialiserNomVersionCOurante();
98
		$this->initialiserCheminsZip();
99
		$this->creerDossierZip();
51 jpm 100
		$this->archiver();
101
    	$this->chargerNomsATraiter();
102
    	$this->analyserNomsATraiter();
103
		$this->creerFichiers();
55 jpm 104
		$this->nettoyerFichiers();
51 jpm 105
		$this->traiterMessages();
106
	}
47 jpm 107
 
51 jpm 108
	private function chargerTraitementParametre() {
109
		$this->meta = unserialize($this->traitement['script_parametres']);
46 jpm 110
	}
111
 
51 jpm 112
	private function archiver() {
113
		$ok = $this->referentielDao->archiver($this->projet, $this->meta['version']);
114
		if ($ok) {
115
			$m = "L'archivage de la version '{$this->meta['version']}' du référentiel '{$this->projet}' a réussi";
116
			$this->ajouterMessage($m);
117
		} else {
118
			$m = "L'archivage de la version '{$this->meta['version']}' du référentiel '{$this->projet}' a échoué";
119
			$this->ajouterMessage($m);
120
		}
121
	}
122
 
55 jpm 123
	private function initialiserNomVersionCOurante() {
124
		$this->version_courante = strtolower($this->projet).'_v'.str_replace('.', '_', $this->meta['version']);
125
		Debug::printr("Nom archive courante :".$this->version_courante);
126
	}
127
 
128
	private function initialiserCheminsZip() {
129
		$this->zip_chemin_dossier = Config::get('chemin_referentiel_zip').$this->version_courante.DIRECTORY_SEPARATOR;
130
		$this->zip_chemin_fichier = Config::get('chemin_referentiel_zip').$this->version_courante.'.zip';
131
	}
132
 
133
	private function creerDossierZip() {
134
		$recursivite = true;
135
		if (mkdir($this->zip_chemin_dossier, 0777, $recursivite) === false) {
136
			$this->ajouterMessage("La création du dossier '$this->zip_chemin_dossier' devant contenir les fichiers a échouée.");
137
		}
138
	}
139
 
51 jpm 140
	private function chargerNomsATraiter() {
55 jpm 141
		$this->noms = $this->referentielDao->getTout($this->version_courante);
51 jpm 142
	}
143
 
144
	private function analyserNomsATraiter() {
145
		$this->noms_stat['combinaison'] = $this->getNombreCombinaison();
146
		$this->noms_stat['taxon'] = $this->getNombreTaxon();
147
		Debug::printr("Stats :".print_r($this->noms_stat, true));
148
	}
149
 
150
	private function getNombreCombinaison() {
151
		return count($this->noms);
152
	}
153
 
154
	private function getNombreTaxon() {
155
		$nbre = 0;
156
		foreach ($this->noms as $nom) {
157
			if ($nom['num_nom_retenu'] == $nom['num_nom']) {
158
				$nbre++;
159
			}
160
		}
161
		return $nbre;
162
	}
163
 
164
	private function creerFichiers() {
165
		// Respecter l'ordre de traitement : BDNT puis DIFF puis META
166
		$this->creerFichierBdnt();
167
		$this->creerFichierDiff();
168
		$this->creerFichierMeta();
55 jpm 169
		$this->nettoyerMemoire();
170
		$this->copierManuel();
171
		$this->creerFichierZip();
51 jpm 172
	}
173
 
174
	private function creerFichierBdnt() {
54 jpm 175
		$noms =& $this->noms;
176
		reset($this->noms);
177
		Debug::printr("Element courrant du tableau des noms : ".count($this->noms).'-'.print_r(current($this->noms),true));
178
 
51 jpm 179
		$this->determinerOrdreDesChamps();
180
		$this->definirNomDesChamps();
46 jpm 181
 
182
		$donnees = array();
51 jpm 183
		$donnees['champs'] = $this->champs_nom;
184
		foreach ($this->noms as &$nom) {
46 jpm 185
			$infos = array();
51 jpm 186
			foreach ($nom as $champ => &$valeur) {
187
				if (isset($this->champs_ordre[$champ])) {
188
					$ordre = $this->champs_ordre[$champ];
54 jpm 189
					$infos[(int) $ordre] = trim($valeur);
46 jpm 190
				} else {
191
					$e = "Le champ '$champ' n'a pas été pris en compte dans l'attribution de l'ordre des champs.";
49 jpm 192
					$this->ajouterMessage($e);
46 jpm 193
				}
194
			}
54 jpm 195
			ksort($infos);
58 jpm 196
			$infos = $this->remplacerTabulation($infos);
197
			$infos = $this->remplacerSautsDeLigne($infos);
47 jpm 198
			$donnees['noms'][] = $infos;
199
		}
54 jpm 200
		Debug::printr("Dernier element du tableau des noms [trié] : ".print_r($infos,true));
58 jpm 201
		$bdnt_tsv =& $this->getVue('versionnage/squelettes/bdnt', $donnees, '.tpl.tsv');
51 jpm 202
		$this->ecrireFichierBdnt($bdnt_tsv);
46 jpm 203
	}
204
 
51 jpm 205
	private function determinerOrdreDesChamps() {
46 jpm 206
		$champs_ordre = explode(',', $this->manuel['champs']);
207
		$champs_ordre = array_flip($champs_ordre);
51 jpm 208
		$nom_courant = current($this->noms);
209
		$champs_ordre = $this->attribuerOrdreChampsSupplémentaires($champs_ordre, $nom_courant);
46 jpm 210
		asort($champs_ordre);
51 jpm 211
		$this->champs_ordre = $champs_ordre;
54 jpm 212
		Debug::printr("Ordre des champs : ".print_r($this->champs_ordre,true));
46 jpm 213
	}
214
 
51 jpm 215
	private function definirNomDesChamps() {
216
		$this->champs_nom = array_flip($this->champs_ordre);
217
	}
218
 
46 jpm 219
	private function attribuerOrdreChampsSupplémentaires($champs_ordre, $nom) {
220
		foreach ($nom as $champ => $info) {
221
			if (!isset($champs_ordre[$champ])) {
222
				$champs_ordre[$champ] = count($champs_ordre);
223
			}
224
		}
225
		return $champs_ordre;
226
	}
227
 
49 jpm 228
	private function ajouterMessage($message) {
46 jpm 229
		$titre = self::SCRIPT_NOM.' #'.$this->traitement['id_traitement'];
54 jpm 230
		$this->messages[] = array('message' => $message, 'resultat' => true);
46 jpm 231
	}
232
 
58 jpm 233
	private function ecrireFichierBdnt(&$contenu) {
55 jpm 234
		$fichier_nom = $this->getBaseNomFichier().$this->manuel['ext_fichier_bdnt'];
235
		$fichier_chemin = $this->zip_chemin_dossier.$fichier_nom;
51 jpm 236
 
237
		if ($this->ecrireFichier($fichier_chemin, $contenu)) {
238
			$this->ajouterMessage("Écriture du fichier bdnt réussie.");
239
			$this->signature_md5 = md5_file($fichier_chemin);
240
			$this->ajouterMessage("Signature MD5 du fichier bdnt :".$this->signature_md5);
54 jpm 241
			$this->ajouterMessage("Nombre de combinaisons traités : ".$this->noms_stat['combinaison']);
51 jpm 242
		}
243
	}
244
 
245
	private function getBaseNomFichier() {
246
		return strtolower($this->meta['acronyme'].'_v'.$this->meta['version']);
247
	}
248
 
58 jpm 249
	private function ecrireFichier($fichier_chemin, &$contenu) {
51 jpm 250
		$retour = true;
251
		if (file_put_contents($fichier_chemin, $contenu) == false) {
252
			$e = "Une erreur est survenu lors de l'écriture du fichier : $fichier_chemin";
253
			$this->ajouterMessage($e);
254
			$retour = false;
255
		}
256
		return $retour;
257
	}
258
 
259
	private function creerFichierDiff() {
260
		$derniere_meta = $this->metaDao->getDerniere($this->projet);
54 jpm 261
		if (is_null($derniere_meta === false)) {
262
			$this->ajouterMessage("Un problème est survenu lors de la récupération des métadonnées précédentes.");
263
		} else if (is_null($derniere_meta)) {
51 jpm 264
			$this->ajouterMessage("Premier versionnage pour ce projet, aucun fichier différentiel ne sera créé.");
265
		} else {
266
			$code_projet_precedent = strtolower($derniere_meta['code']).'_v'.str_replace('.', '_', $derniere_meta['version']);
55 jpm 267
			if ($code_projet_precedent == $this->version_courante) {
268
				$e = "La code de la version préalablement versionnée ($code_projet_precedent) est le même que celui ".
269
					"de la demande actuel ({$this->version_courante}) pour ce projet, aucun fichier différentiel ne sera créé.";
270
				$this->ajouterMessage($e);
271
			} else {
272
				$this->noms_precedents = $this->referentielDao->getTout($code_projet_precedent);
273
				$donnees = array();
58 jpm 274
				$donnees['diff'] =& $this->realiserDiff();
55 jpm 275
				$donnees['champs'] = $this->champs_nom;
58 jpm 276
				$diff_tsv = $this->getVue('versionnage/squelettes/diff', &$donnees, '.tpl.tsv');
55 jpm 277
				$this->ecrireFichierDiff($diff_tsv);
278
			}
51 jpm 279
		}
280
	}
281
 
282
	private function realiserDiff() {
283
		$diff = array();
284
		$this->noms_stat['modification'] = 0;
58 jpm 285
		$i = 0;
51 jpm 286
		foreach ($this->noms as $id => &$nom) {
58 jpm 287
			$infos = array();
51 jpm 288
			if (!isset($this->noms_precedents[$id])) {
58 jpm 289
				$infos = $nom;
290
				$infos['modification_type'] = 'A';
291
				$infos['modification_type_1'] = '0';
292
				$infos['modification_type_2'] = '0';
293
				$infos['modification_type_3'] = '0';
51 jpm 294
				$this->noms_stat['modification']++;
295
			} else {
296
				$nom_precedent =& $this->noms_precedents[$id];
54 jpm 297
				$nom_diff = array_diff_assoc($nom, $nom_precedent);
58 jpm 298
				Debug::printr($nom_diff);
51 jpm 299
				if (count($nom_diff) > 0) {
300
					$this->noms_stat['modification']++;
301
					$modif['modification_type'] = 'M';
302
					$modif['modification_type_1'] = '0';
303
					$modif['modification_type_2'] = '0';
304
					$modif['modification_type_3'] = '0';
305
					$this->chargerTableauChampsModifTypes();
306
					foreach ($this->champs_nom as $champ) {
307
						if (isset($nom_diff[$champ])) {
58 jpm 308
							$infos[$champ] = $nom_diff[$champ];
51 jpm 309
							$type = $this->getDiffType($champ);
310
							$modif['modification_type_'.$type] = '1';
311
						} else {
58 jpm 312
							$infos[$champ] = '';
51 jpm 313
						}
314
					}
315
					foreach ($modif as $cle => $val) {
58 jpm 316
						$infos[$cle] = $val;
51 jpm 317
					}
318
				}
319
			}
58 jpm 320
			$infos = $this->remplacerTabulation($infos);
321
			$infos = $this->remplacerSautsDeLigne($infos);
322
			Debug::printr($infos);
323
			Debug::printr("-------------------------------------------------");
324
			$diff[$id][] = $infos;
325
			if ($i == 100) break;
51 jpm 326
		}
327
		return $diff;
328
	}
329
 
330
	private function chargerTableauChampsModifTypes() {
331
		$champs = explode(',', $this->manuel['champs_diff_type']);
332
		foreach ($champs as $champ) {
58 jpm 333
			list($champ_nom, $type) = explode('=', trim($champ));
51 jpm 334
			$this->diff_modif_types[$champ_nom] = $type;
335
		}
336
	}
337
 
338
	private function getDiffType($champ_nom) {
339
		$type = isset($this->diff_modif_types[$champ_nom]) ? $this->diff_modif_types[$champ_nom] : '3';
340
		return $type;
341
	}
342
 
343
	private function ecrireFichierDiff($contenu) {
54 jpm 344
		$fichier_nom = $this->getBaseNomFichier().$this->manuel['ext_fichier_diff'];
55 jpm 345
		$fichier_chemin = $this->zip_chemin_dossier.$fichier_nom;
51 jpm 346
 
347
		if ($this->ecrireFichier($fichier_chemin, $contenu)) {
348
			$this->ajouterMessage("Écriture du fichier diff réussie.");
349
		}
350
	}
351
 
352
	private function creerFichierMeta() {
353
		$donnees = array();
354
		$donnees = $this->meta;
355
		$donnees['stats'] = $this->noms_stat;
356
		$donnees['signature'] = $this->signature_md5;
58 jpm 357
 
358
		$donnees = $this->remplacerTabulation($donnees);
359
		$donnees = $this->remplacerSautsDeLigne($donnees);
360
 
51 jpm 361
		$meta_tsv = $this->getVue('versionnage/squelettes/meta', $donnees, '.tpl.tsv');
362
		$this->ecrireFichierMeta($meta_tsv);
363
	}
364
 
58 jpm 365
	private function remplacerTabulation($doc) {
366
		if (is_string($doc)) {
367
			$doc = str_replace("\t", ' ', $doc);
368
		} else if (is_array($doc) && count($doc) > 0) {
369
			foreach ($doc as $cle => $valeur) {
370
				$doc[$cle] = $this->remplacerTabulation($valeur);
371
			}
372
		}
373
		return $doc;
374
	}
375
 
376
	private function remplacerSautsDeLigne($doc) {
377
		if (is_string($doc)) {
378
			$a_remplacer = array("\r", "\n");
379
			$doc = str_replace($a_remplacer, ' ', $doc);
380
		} else if (is_array($doc) && count($doc) > 0) {
381
			foreach ($doc as $cle => $valeur) {
382
				$doc[$cle] = $this->remplacerSautsDeLigne($valeur);
383
			}
384
		}
385
		return $doc;
386
	}
387
 
51 jpm 388
	private function ecrireFichierMeta($contenu) {
54 jpm 389
		$fichier_nom = $this->getBaseNomFichier().$this->manuel['ext_fichier_meta'];
55 jpm 390
		$fichier_chemin = $this->zip_chemin_dossier.$fichier_nom;
51 jpm 391
 
392
		if ($this->ecrireFichier($fichier_chemin, $contenu)) {
393
			$this->ajouterMessage("Écriture du fichier meta réussie.");
394
			$this->archiverMetadonnees();
395
		}
396
	}
397
 
398
	private function archiverMetadonnees() {
399
		$metadonnees = $this->meta;
400
		$metadonnees['code'] = $this->meta['acronyme'];
401
		unset($metadonnees['acronyme']);
402
		$metadonnees['domaine_taxo'] = $this->meta['dom_tax'];
403
		unset($metadonnees['dom_tax']);
404
		$metadonnees['domaine_geo'] = $this->meta['dom_geo'];
405
		unset($metadonnees['dom_geo']);
406
		$metadonnees['domaine_nom'] = $this->meta['dom_code'];
407
		unset($metadonnees['dom_code']);
408
		$metadonnees['auteur'] = $this->meta['auteur_principal'];
409
		unset($metadonnees['auteur_principal']);
410
		$metadonnees['date_production'] = $this->meta['date_prod'];
411
		unset($metadonnees['date_prod']);
412
		$metadonnees['droit'] = $this->meta['copyright'];
413
		unset($metadonnees['copyright']);
414
 
415
		$ok = $this->metaDao->ajouter($metadonnees);
416
		if ($ok === false) {
417
			$this->ajouterMessage("L'archivage des métadonnées a échoué.");
418
		}
419
	}
420
 
55 jpm 421
	private function nettoyerMemoire() {
422
		$this->noms = null;
423
		$this->noms_precedents = null;
424
		$this->noms_stat = null;
425
	}
426
 
427
	private function copierManuel() {
428
		$fichier_source = $this->manuel_chemin.$this->manuel_nom;
429
		$fichier_destination = $this->zip_chemin_dossier.$this->manuel_nom;
430
		if (copy($fichier_source, $fichier_destination) === false) {
431
			$this->ajouterMessage("La copie du manuel '$fichier_destination' a échouée.");
432
		}
433
	}
434
 
435
	private function creerFichierZip() {
436
		$fichierZip = new PclZip($this->zip_chemin_fichier);
437
		if ($fichierZip->add($this->zip_chemin_dossier, PCLZIP_OPT_REMOVE_ALL_PATH) == 0) {
56 jpm 438
			$e = "La création du fichier zip '$this->zip_chemin_fichier' a échoué avec l'erreur : ".$fichierZip->errorInfo(true);
55 jpm 439
			$this->ajouterMessage($e);
440
		}
441
	}
442
 
443
	private function nettoyerFichiers() {
444
		$dossier = $this->zip_chemin_dossier;
445
		if (is_dir($dossier)) {
446
			$objets = scandir($dossier);
447
			foreach ($objets as $objet) {
448
				if ($objet != '.' && $objet != '..') {
449
					if (filetype($dossier.'/'.$objet) == 'dir') {
450
						rrmdir($dossier.'/'.$objet);
451
					} else {
452
						unlink($dossier.'/'.$objet);
453
					}
454
				}
455
			}
456
			reset($objets);
457
			rmdir($dossier);
458
		}
459
	}
460
 
49 jpm 461
	private function traiterMessages() {
462
		if (isset($this->messages)) {
54 jpm 463
			$num_message = 1;
49 jpm 464
			foreach ($this->messages as $message) {
54 jpm 465
				$message['nom'] = 'Message #'.$num_message++;
49 jpm 466
				$this->resultatDao->ajouter($this->traitement['id_traitement'], $message);
467
			}
46 jpm 468
		}
469
	}
470
}
471
?>