Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
// commande : /opt/lampp/bin/php cli.php description_sp -a tester -n /home/jennifer/Tela-botanica_projets/Coste/descriptions/html
class Description {

        const DOSSIER_V0 = '../../../donnees/coste/0.00/';
        const DOSSIER_DSC_HTML = '../../../donnees/coste/descriptions/html/';
        const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
        const DOSSIER_LOG = 'log/';

        private $dossierBase = '';
        private $conteneur = null;
        private $outils = null;
        private $messages = null;
        private $action = '';
        private $nomFichier = '';
        private $nomDossier = '';
        private $listeFichiers = array();
        private $fichierNum = '';
        private $fichier = '';
        private $log = '';
        private $correspondance = array();
        private $infosCorrespondanceBase = array(
                'nom_sci' => '',
                'rang' => 290,
                'auteur' => '',
                'nom_francais' => '',
                'nom_coste' => '',
                'num_nom_coste' => '',
                'num_tax_sup_coste' => '',
                'tome' => '',
                'page' => '',
                'bdnff_nn' => '',
                'bdnff_nt' => '');

        public function __construct(Conteneur $conteneur) {
                $this->conteneur = $conteneur;
                $this->outils = $conteneur->getOutils();
                $this->messages = $conteneur->getMessages();
                $this->dossierBase = dirname(__FILE__).'/';
        }

        public function genererDescriptionTxt() {
                $this->chargerFichiers();

                foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
                        $this->messages->afficherAvancement("Création des descriptions au format txt");
                        $this->genererFichier();
                }
                echo "\n";
        }

        public function verifierDescriptionTxt() {
                $this->chargerFichiers();

                foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
                        $this->messages->afficherAvancement("Analyse des descriptions");
                        $this->verifierFichier();
                }
                echo "\n";

                $fichierTxt = $this->dossierBase.self::DOSSIER_LOG.'analyse.log';
                file_put_contents($fichierTxt, $this->log);
        }

        public function genererCorrespondance() {
                $this->chargerFichiers();

                foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
                        $this->messages->afficherAvancement("Création du fichier de correspondance");
                        $this->extraireInfosCorrespondance();
                }
                echo "\n";

                $fichierTxt = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
                $txtCorrespondance = $this->creerFichierCsvCorrespondance();
                file_put_contents($fichierTxt, $txtCorrespondance);
        }

        private function chargerFichiers() {
                $this->nomDossier = $this->dossierBase.self::DOSSIER_DSC_HTML;
                if (file_exists($this->nomDossier) === true) {
                        if (is_dir($this->nomDossier)) {
                                if ($dossierOuvert = opendir($this->nomDossier)) {
                                        while (($this->nomFichier = readdir($dossierOuvert)) !== false) {
                                                if (! is_dir($this->nomFichier) && preg_match('/e([0-9]{4})\.htm/', $this->nomFichier, $match)) {
                                                        $this->listeFichiers[$match[1]] = $this->nomDossier.'/'.$this->nomFichier;
                                                }
                                        }
                                        closedir($dossierOuvert);
                                } else {
                                        $this->messages->traiterErreur("Le dossier {$this->nomDossier} n'a pas pu être ouvert.");
                                }
                        } else {
                                $this->messages->traiterErreur("{$this->nomDossier} n'est pas un dossier.");
                        }
                } else {
                        $this->messages->traiterErreur("Le dossier {$this->nomDossier} est introuvable.");
                }

                asort($this->listeFichiers);
        }

        private function verifierFichier() {
                $donnees = $this->analyserFichier();
                $txt = $donnees['texte'];
                $this->verifierOuvertureFermetureBalise($txt);
                $this->verifierAbscenceEgale($txt);
                $this->verifierNumero($txt);
        }

        private function verifierOuvertureFermetureBalise($txt) {
                $b_ouvert = substr_count($txt, '<B>');
                $b_ferme = substr_count($txt, '</B>');
                if ($b_ouvert != $b_ferme) {
                        $this->log .= "{$this->fichierNum} contient $b_ouvert balises B ouvertes et $b_ferme fermées\n";
                }
                $i_ouvert = substr_count($txt, '<I>');
                $i_ferme = substr_count($txt, '</I>');
                if ($i_ouvert != $i_ferme) {
                        $this->log .= "{$this->fichierNum} contient $i_ouvert balises I ouvertes et $i_ferme fermées\n";
                }
        }

        private function verifierAbscenceEgale($txt) {
                if (strripos($txt, '=') === false) {
                        $this->log .= "{$this->fichierNum} ne contient pas le séparateur de phénologie (=)\n";
                }
        }

        private function verifierNumero($txt) {
                if (preg_match('/^<B>[0-9]{1,4}. – /', $txt) == 0) {
                        $this->log .= "{$this->fichierNum} ne contient pas un numéro bien formaté\n";
                }
        }

        private function extraireInfosCorrespondance() {
                $donnees = $this->analyserFichier();
                $infos = $this->infosCorrespondanceBase;

                $titre = $donnees['titre'];
                if (preg_match('/^Coste ([0-9]+) - ([^ ]+ [^ ]+) - F[0-9]+, [^ ]+ - (G[0-9]+), T([123])[.]p([0-9]+)$/', $titre, $match)) {
                        $infos['num_nom_coste'] = $match[1];
                        $infos['nom_sci'] = $match[2];
                        $infos['num_tax_sup_coste'] = $match[3];
                        $infos['tome'] = $match[4];
                        $infos['page'] = $match[5];
                } else {
                        $this->messages->traiterErreur("Le titre du fichier {$this->fichierNum} est mal formaté.");
                }

                $corres = $donnees['correspondance'];
                if (preg_match('/^Bdnff ([0-9]+) -[^-]+-[^-]+- Tax=([0-9]+)$/', $corres, $match)) {
                        $infos['bdnff_nn'] = $match[1];
                        $infos['bdnff_nt'] = $match[2];

                } else {
                        $this->messages->traiterErreur("La correspondance du fichier {$this->fichierNum} est mal formatée.");
                }

                $txt = $donnees['texte'];
                $txt = $this->corrigerDescription($txt);
                if (preg_match('/^<B>[0-9]{1,4}[.] – ([^<]+)<\/B> ([^–]+)– (?:<I>([^<]+)<\/I>|[A-Z])/u', $txt, $match)) {
                        $infos['nom_coste'] = $match[1];
                        $infos['auteur'] = $match[2];
                        $infos['nom_francais'] = isset($match[3]) ? $match[3] : '';
                } else {
                        $this->messages->traiterErreur("La texte du fichier {$this->fichierNum} est mal formaté.");
                }

                $this->correspondance[] = $infos;
        }

        private function creerFichierCsvCorrespondance() {
                $lignes[] = implode("\t", array_keys($this->infosCorrespondanceBase));
                foreach ($this->correspondance as $infos) {
                        $lignes[] = implode("\t", $infos);
                }

                $txt = '';
                $txt = implode("\n", $lignes);
                return $txt;
        }

        private function genererFichier() {
                $donnees = $this->analyserFichier();

                $txt = $this->nettoyerDescription($donnees['texte']);
                $txt = $this->corrigerDescription($txt);
                $txt = $this->remplacerHtmlParSyntaxeWiki($txt);
                $fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
                file_put_contents($fichierTxt, $txt);

                unset($donnees['texte']);
                $txt = implode("\n", $donnees);
                $fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.json';
                file_put_contents($fichierTxt, $txt);
        }

        private function analyserFichier() {
                $donnees = array('titre' => '', 'texte' => '', 'correspondance' => '');
                if ($fichierOuvert = fopen($this->fichier, 'r')) {
                        $i = 1;
                        while ($ligne = fgets($fichierOuvert)) {
                                if ($i == 24) {
                                        $donnees['titre'] = $this->supprimerHtml($ligne);
                                } elseif ($i >= 45 && $i <= 62) {
                                        $donnees['texte'] .= $this->traiterLigneDescription($ligne);
                                } elseif (($i == 63 || $i == 67) && preg_match('/Bdnff /ui', $ligne)) {
                                        $donnees['correspondance'] = $this->supprimerHtml($ligne);
                                }
                                $i++;
                        }
                        if ($i > 94) {
                                $this->messages->traiterErreur("Le fichier {$this->fichier} contient plus de 94 lignes ($i).");
                        }
                        fclose($fichierOuvert);
                } else {
                        $this->messages->traiterErreur("Le fichier {$this->fichier} n'a pas pu être ouvert.");
                }
                return $donnees;
        }

        private function supprimerHtml($txt) {
                $txt = strip_tags($txt,'<b>,<i>');
                $txt = str_replace('&#173;', '', $txt);
                $txt = str_ireplace('&nbsp;', ' ', $txt);
                $txt = trim($txt);
                $txt = preg_replace('/^<\/I>/', '', $txt);
                $txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
                return $txt;
        }

        private function traiterLigneDescription($txt) {
                $txt = $this->supprimerHtml($txt);
                $txt = $txt."\n";
                return $txt;
        }

        private function nettoyerDescription($txt) {
                $txt = preg_replace("/\n{2,}+/", "\n", $txt);
                $txt = trim($txt);
                return $txt;
        }

        private function corrigerDescription($txt) {
                $txt = preg_replace("/– <I>([^.]+?)[.] – <\/I>/", "– <I>$1</I>. – ", $txt);
                $txt = preg_replace("/– <I>([^.]+?)[.] – ([A-Z])<\/I>/", "– <I>$1</I>. – $2", $txt);
                $txt = preg_replace("/– <I>([^.]+?) – <\/I>/", "– <I>$1</I>. – ", $txt);
                return $txt;
        }

        private function remplacerHtmlParSyntaxeWiki($txt) {
                $txt = str_replace(array('<B>', '</B>'), '**', $txt);
                $txt = str_replace(array('<I>', '</I>'), '//', $txt);
                return $txt;
        }
}
?>