Subversion Repositories Applications.referentiel

Rev

Rev 49 | Rev 54 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// Encodage : UTF-8
// +-------------------------------------------------------------------------------------------------------------------+
/**
* Versionnage de référentiels de nomenclature et taxonomie
*
* Description : classe permettant de versionner les référentiels selon le manuel technique
* Utilisation : php script.php versionnage -p bdnff -a tout
*
//Auteur original :
* @author       Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @copyright    Tela-Botanica 1999-2010
* @link                 http://www.tela-botanica.org/wikini/RTaxMethodo/wakka.php?wiki=MaNuel
* @licence              GPL v3 & CeCILL v2
* @version              $Id$
*/
// +-------------------------------------------------------------------------------------------------------------------+
// TODO : supprimer l'utilisation du paramêtres 'p' et chercher les infos depuis la bdd
class Versionnage extends ScriptCommande {
        
        const SCRIPT_NOM = 'versionnage';
        
        private $projet = null;
        private $traitement = null;
        private $meta = null;
        private $messages = null;
        private $manuel = null;
        private $noms = null;
        private $noms_precedents = null;
        private $noms_stat = null;
        private $champs_ordre = null;
        private $champs_nom = null;
        private $diff_modif_types = null;
        private $signature_md5 = null;
        
        private $resultatDao = null;
        private $traitementDao = null;
        private $metaDao = null;
        private $tableStructureDao = null;
        private $referentielDao = null;
        
        public function executer() {
                // Récupération de paramêtres
                $this->projet = $this->getParam('p');
                
                // Récupération du dernier traitement demandé
                $this->traitementDao = new TraitementDao();
                $this->traitement = $this->traitementDao->getDernierTraitement($this->projet, self::SCRIPT_NOM);
                if (isset($this->traitement)) {
                        Debug::printr($this->traitement);
                        // Écriture de la date de début du traitement
                        Debug::printr('Debute:'.$this->traitementDao->debuterTraitement($this->traitement['id_traitement']));
                        
                        // Nettoyage des traitements obsolètes
                        $traitements_obsoletes = $this->traitementDao->getTraitementsObsoletes($this->projet, self::SCRIPT_NOM);
                        if (isset($traitements_obsoletes)) {
                                Debug::printr('Supp. obsoletes:'.$this->traitementDao->supprimer($traitements_obsoletes));
                        }
                        
                        // Lancement du test demandé
                        $cmd = $this->getParam('a');
                switch ($cmd) {
                                case 'tout' :
                                        $this->metaDao = new MetaDao();
                                        $this->resultatDao = new ResultatDao();
                                        $this->referentielDao = new ReferentielDao();
                                        $this->manuel = parse_ini_file(Config::get('chemin_appli').DS.'..'.DS.'configurations'.DS.'referentiel_v2.1.ini');
                                        Debug::printr('Départ lancement versionnage:');
                                        $this->lancerVersionnage();
                                        break;
                                default :
                                        $this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
                        }
                        // Écriture de la date de fin du traitement
                        Debug::printr('Termine:'.$this->traitementDao->terminerTraitement($this->traitement['id_traitement']));
                }
    }

    public function lancerVersionnage() {
                $this->chargerTraitementParametre();
                $this->archiver();
        $this->chargerNomsATraiter();
        $this->analyserNomsATraiter();
                $this->creerFichiers();
                $this->traiterMessages();
        }
        
        private function chargerTraitementParametre() {
                $this->meta = unserialize($this->traitement['script_parametres']);
        }
        
        private function archiver() {
                $ok = $this->referentielDao->archiver($this->projet, $this->meta['version']);
                if ($ok) {
                        $m = "L'archivage de la version '{$this->meta['version']}' du référentiel '{$this->projet}' a réussi";
                        $this->ajouterMessage($m);
                } else {
                        $m = "L'archivage de la version '{$this->meta['version']}' du référentiel '{$this->projet}' a échoué";
                        $this->ajouterMessage($m);
                }
        }
        
        private function chargerNomsATraiter() {
                $archive_courante = strtolower($this->projet).'_v'.str_replace('.', '_', $this->meta['version']);
                Debug::printr("Nom archive courante :".$archive_courante);
                $this->noms = $this->referentielDao->getTout($archive_courante);
        }
        
        private function analyserNomsATraiter() {
                $this->noms_stat['combinaison'] = $this->getNombreCombinaison();
                $this->noms_stat['taxon'] = $this->getNombreTaxon();
                Debug::printr("Stats :".print_r($this->noms_stat, true));
        }
        
        private function getNombreCombinaison() {
                return count($this->noms);
        }
        
        private function getNombreTaxon() {
                $nbre = 0;
                foreach ($this->noms as $nom) {
                        if ($nom['num_nom_retenu'] == $nom['num_nom']) {
                                $nbre++;
                        }
                }
                return $nbre;
        }
        
        private function creerFichiers() {
                // Respecter l'ordre de traitement : BDNT puis DIFF puis META
                $this->creerFichierBdnt();
                $this->creerFichierDiff();
                $this->creerFichierMeta();
        }
        
        private function creerFichierBdnt() {
                Debug::printr("Element courrant du tableau des noms : ".print_r(current($this->noms),true));
                //Debug::printr("Taille mémoire du tableau des noms : ".Debug::tailleMemoireVar($this->noms));
                $this->determinerOrdreDesChamps();
                $this->definirNomDesChamps();
                
                $donnees = array();
                $donnees['champs'] = $this->champs_nom;
                foreach ($this->noms as &$nom) {
                        $infos = array();
                        foreach ($nom as $champ => &$valeur) {
                                if (isset($this->champs_ordre[$champ])) {
                                        $ordre = $this->champs_ordre[$champ];
                                        $infos[$ordre] = trim($valeur);
                                } else {
                                        $e = "Le champ '$champ' n'a pas été pris en compte dans l'attribution de l'ordre des champs.";
                                        $this->ajouterMessage($e);
                                }
                        }
                        $donnees['noms'][] = $infos;
                }
                
                $bdnt_tsv = $this->getVue('versionnage/squelettes/bdnt', $donnees, '.tpl.tsv');
                $this->ecrireFichierBdnt($bdnt_tsv);
        }
        
        private function determinerOrdreDesChamps() {
                $champs_ordre = explode(',', $this->manuel['champs']);
                $champs_ordre = array_flip($champs_ordre);
                $nom_courant = current($this->noms);
                $champs_ordre = $this->attribuerOrdreChampsSupplémentaires($champs_ordre, $nom_courant);
                asort($champs_ordre);
                $this->champs_ordre = $champs_ordre;
        }
        
        private function definirNomDesChamps() {
                $this->champs_nom = array_flip($this->champs_ordre);
        }
        
        private function attribuerOrdreChampsSupplémentaires($champs_ordre, $nom) {
                foreach ($nom as $champ => $info) {
                        if (!isset($champs_ordre[$champ])) {
                                $champs_ordre[$champ] = count($champs_ordre);
                        }
                }
                return $champs_ordre;
        }
        
        private function ajouterMessage($message) {
                $titre = self::SCRIPT_NOM.' #'.$this->traitement['id_traitement'];
                $this->messages[] = array($titre, $message);
        }
        
        private function ecrireFichierBdnt($contenu) {
                $fichier_nom = $this->getBaseNomFichier().'_ref.txt';
                $fichier_chemin = Config::get('chemin_referentiel_zip').$fichier_nom;
                
                if ($this->ecrireFichier($fichier_chemin, $contenu)) {
                        $this->ajouterMessage("Écriture du fichier bdnt réussie.");
                        $this->signature_md5 = md5_file($fichier_chemin);
                        $this->ajouterMessage("Signature MD5 du fichier bdnt :".$this->signature_md5);
                        $this->ajouterMessage("Nombre de noms traités : ".count($noms));
                        
                }
        }
        
        private function getBaseNomFichier() {
                return strtolower($this->meta['acronyme'].'_v'.$this->meta['version']);
        }
        
        private function ecrireFichier($fichier_chemin, $contenu) {
                $retour = true;
                if (file_put_contents($fichier_chemin, $contenu) == false) {
                        $e = "Une erreur est survenu lors de l'écriture du fichier : $fichier_chemin";
                        $this->ajouterMessage($e);
                        $retour = false;
                }
                return $retour;
        }
        
        private function creerFichierDiff() {
                $derniere_meta = $this->metaDao->getDerniere($this->projet);
                if ($derniere_meta === false) {
                        $this->ajouterMessage("Premier versionnage pour ce projet, aucun fichier différentiel ne sera créé.");
                } else {
                        $code_projet_precedent = strtolower($derniere_meta['code']).'_v'.str_replace('.', '_', $derniere_meta['version']);
                        $this->noms_precedents = $this->referentielDao->getTout($code_projet_precedent);
                        $donnees = array();
                        $donnees['diff'] = $this->realiserDiff();
                        $donnees['champs'] = $this->champs_nom;
                        $diff_tsv = $this->getVue('versionnage/squelettes/diff', $donnees, '.tpl.tsv');
                        $this->ecrireFichierDiff($diff_tsv);
                }
        }
        
        private function realiserDiff() {
                $diff = array();
                $this->noms_stat['modification'] = 0;
                foreach ($this->noms as $id => &$nom) {
                        if (!isset($this->noms_precedents[$id])) {
                                $diff[$id] = $nom;
                                $diff[$id]['modification_type'] = 'A';
                                $diff[$id]['modification_type_1'] = '0';
                                $diff[$id]['modification_type_2'] = '0';
                                $diff[$id]['modification_type_3'] = '0';
                                $this->noms_stat['modification']++;
                        } else {
                                $nom_precedent =& $this->noms_precedents[$id];
                                $nom_diff = array_diff_assoc($noms, $nom_precedent);
                                if (count($nom_diff) > 0) {
                                        $this->noms_stat['modification']++;
                                        $modif['modification_type'] = 'M';
                                        $modif['modification_type_1'] = '0';
                                        $modif['modification_type_2'] = '0';
                                        $modif['modification_type_3'] = '0';
                                        $this->chargerTableauChampsModifTypes();
                                        foreach ($this->champs_nom as $champ) {
                                                if (isset($nom_diff[$champ])) {
                                                        $diff[$id][$champ] = $nom_diff[$champ];
                                                        $type = $this->getDiffType($champ);
                                                        $modif['modification_type_'.$type] = '1';
                                                } else {
                                                        $diff[$id][$champ] = '';
                                                }
                                        }
                                        foreach ($modif as $cle => $val) {
                                                $diff[$id][$cle] = $val;
                                        }
                                }
                        }
                }
                return $diff;
        }
        
        private function chargerTableauChampsModifTypes() {
                $champs = explode(',', $this->manuel['champs_diff_type']);
                foreach ($champs as $champ) {
                        list($champ_nom, $type) = explode('=', $champ);
                        $this->diff_modif_types[$champ_nom] = $type;
                }
        }
        
        private function getDiffType($champ_nom) {
                $type = isset($this->diff_modif_types[$champ_nom]) ? $this->diff_modif_types[$champ_nom] : '3';
                return $type;
        }
        
        private function ecrireFichierDiff($contenu) {
                $fichier_nom = $this->getBaseNomFichier().'diff.txt';
                $fichier_chemin = Config::get('chemin_referentiel_zip').$fichier_nom;
                
                if ($this->ecrireFichier($fichier_chemin, $contenu)) {
                        $this->ajouterMessage("Écriture du fichier diff réussie.");
                }
        }
        
        private function creerFichierMeta() {
                $donnees = array();
                $donnees = $this->meta;
                $donnees['stats'] = $this->noms_stat;
                $donnees['signature'] = $this->signature_md5;
                $meta_tsv = $this->getVue('versionnage/squelettes/meta', $donnees, '.tpl.tsv');
                $this->ecrireFichierMeta($meta_tsv);
        }
        
        private function ecrireFichierMeta($contenu) {
                $fichier_nom = $this->getBaseNomFichier().'_meta.txt';
                $fichier_chemin = Config::get('chemin_referentiel_zip').$fichier_nom;
                
                if ($this->ecrireFichier($fichier_chemin, $contenu)) {
                        $this->ajouterMessage("Écriture du fichier meta réussie.");
                        $this->archiverMetadonnees();
                }
        }
        
        private function archiverMetadonnees() {
                $metadonnees = $this->meta;
                $metadonnees['code'] = $this->meta['acronyme'];
                unset($metadonnees['acronyme']);
                $metadonnees['domaine_taxo'] = $this->meta['dom_tax'];
                unset($metadonnees['dom_tax']);
                $metadonnees['domaine_geo'] = $this->meta['dom_geo'];
                unset($metadonnees['dom_geo']);
                $metadonnees['domaine_nom'] = $this->meta['dom_code'];
                unset($metadonnees['dom_code']);
                $metadonnees['auteur'] = $this->meta['auteur_principal'];
                unset($metadonnees['auteur_principal']);
                $metadonnees['date_production'] = $this->meta['date_prod'];
                unset($metadonnees['date_prod']);
                $metadonnees['droit'] = $this->meta['copyright'];
                unset($metadonnees['copyright']);
                
                $ok = $this->metaDao->ajouter($metadonnees);
                if ($ok === false) {
                        $this->ajouterMessage("L'archivage des métadonnées a échoué.");
                }
        }
        
        private function traiterMessages() {
                if (isset($this->messages)) {
                        foreach ($this->messages as $message) {
                                $this->resultatDao->ajouter($this->traitement['id_traitement'], $message);
                        }
                }
        }
}
?>