Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
424 jpm 1
<?php
2
// commande : /opt/lampp/bin/php cli.php description_sp -a tester -n /home/jennifer/Tela-botanica_projets/Coste/descriptions/html
3
class Description {
4
 
428 jpm 5
	const DOSSIER_V0 = '../../../donnees/coste/0.00/';
6
	const DOSSIER_DSC_HTML = '../../../donnees/coste/descriptions/html/';
7
	const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
8
	const DOSSIER_LOG = 'log/';
424 jpm 9
 
428 jpm 10
	private $dossierBase = '';
424 jpm 11
	private $conteneur = null;
12
	private $outils = null;
13
	private $messages = null;
428 jpm 14
	private $action = '';
15
	private $nomFichier = '';
16
	private $nomDossier = '';
17
	private $listeFichiers = array();
424 jpm 18
	private $fichierNum = '';
428 jpm 19
	private $fichier = '';
20
	private $log = '';
21
	private $correspondance = array();
22
	private $infosCorrespondanceBase = array(
23
		'nom_sci' => '',
24
		'rang' => 290,
25
		'auteur' => '',
26
		'nom_francais' => '',
27
		'nom_coste' => '',
28
		'num_nom_coste' => '',
29
		'num_tax_sup_coste' => '',
30
		'tome' => '',
31
		'page' => '',
32
		'bdnff_nn' => '',
33
		'bdnff_nt' => '');
424 jpm 34
 
35
	public function __construct(Conteneur $conteneur) {
36
		$this->conteneur = $conteneur;
37
		$this->outils = $conteneur->getOutils();
38
		$this->messages = $conteneur->getMessages();
428 jpm 39
		$this->dossierBase = dirname(__FILE__).'/';
424 jpm 40
	}
41
 
42
	public function genererDescriptionTxt() {
428 jpm 43
		$this->chargerFichiers();
44
 
45
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
46
			$this->messages->afficherAvancement("Création des descriptions au format txt");
47
			$this->genererFichier();
48
		}
49
		echo "\n";
424 jpm 50
	}
51
 
428 jpm 52
	public function verifierDescriptionTxt() {
53
		$this->chargerFichiers();
54
 
55
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
56
			$this->messages->afficherAvancement("Analyse des descriptions");
57
			$this->verifierFichier();
58
		}
59
		echo "\n";
60
 
61
		$fichierTxt = $this->dossierBase.self::DOSSIER_LOG.'analyse.log';
62
		file_put_contents($fichierTxt, $this->log);
63
	}
64
 
65
	public function genererCorrespondance() {
66
		$this->chargerFichiers();
67
 
68
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
69
			$this->messages->afficherAvancement("Création du fichier de correspondance");
70
			$this->extraireInfosCorrespondance();
71
		}
72
		echo "\n";
73
 
74
		$fichierTxt = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
75
		$txtCorrespondance = $this->creerFichierCsvCorrespondance();
76
		file_put_contents($fichierTxt, $txtCorrespondance);
77
	}
78
 
79
	private function chargerFichiers() {
80
		$this->nomDossier = $this->dossierBase.self::DOSSIER_DSC_HTML;
81
		if (file_exists($this->nomDossier) === true) {
82
			if (is_dir($this->nomDossier)) {
83
				if ($dossierOuvert = opendir($this->nomDossier)) {
84
					while (($this->nomFichier = readdir($dossierOuvert)) !== false) {
85
						if (! is_dir($this->nomFichier) && preg_match('/e([0-9]{4})\.htm/', $this->nomFichier, $match)) {
86
							$this->listeFichiers[$match[1]] = $this->nomDossier.'/'.$this->nomFichier;
424 jpm 87
						}
88
					}
89
					closedir($dossierOuvert);
90
				} else {
428 jpm 91
					$this->messages->traiterErreur("Le dossier {$this->nomDossier} n'a pas pu être ouvert.");
424 jpm 92
				}
93
			} else {
428 jpm 94
				$this->messages->traiterErreur("{$this->nomDossier} n'est pas un dossier.");
424 jpm 95
			}
96
		} else {
428 jpm 97
			$this->messages->traiterErreur("Le dossier {$this->nomDossier} est introuvable.");
424 jpm 98
		}
428 jpm 99
 
100
		asort($this->listeFichiers);
424 jpm 101
	}
102
 
428 jpm 103
	private function verifierFichier() {
104
		$donnees = $this->analyserFichier();
105
		$txt = $donnees['texte'];
106
		$this->verifierOuvertureFermetureBalise($txt);
107
		$this->verifierAbscenceEgale($txt);
108
		$this->verifierNumero($txt);
109
	}
110
 
111
	private function verifierOuvertureFermetureBalise($txt) {
112
		$b_ouvert = substr_count($txt, '<B>');
113
		$b_ferme = substr_count($txt, '</B>');
114
		if ($b_ouvert != $b_ferme) {
115
			$this->log .= "{$this->fichierNum} contient $b_ouvert balises B ouvertes et $b_ferme fermées\n";
116
		}
117
		$i_ouvert = substr_count($txt, '<I>');
118
		$i_ferme = substr_count($txt, '</I>');
119
		if ($i_ouvert != $i_ferme) {
120
			$this->log .= "{$this->fichierNum} contient $i_ouvert balises I ouvertes et $i_ferme fermées\n";
121
		}
122
	}
123
 
124
	private function verifierAbscenceEgale($txt) {
125
		if (strripos($txt, '=') === false) {
126
			$this->log .= "{$this->fichierNum} ne contient pas le séparateur de phénologie (=)\n";
127
		}
128
	}
129
 
130
	private function verifierNumero($txt) {
131
		if (preg_match('/^<B>[0-9]{1,4}. – /', $txt) == 0) {
132
			$this->log .= "{$this->fichierNum} ne contient pas un numéro bien formaté\n";
133
		}
134
	}
135
 
136
	private function extraireInfosCorrespondance() {
137
		$donnees = $this->analyserFichier();
138
		$infos = $this->infosCorrespondanceBase;
139
 
140
		$titre = $donnees['titre'];
141
		if (preg_match('/^Coste ([0-9]+) - ([^ ]+ [^ ]+) - F[0-9]+, [^ ]+ - (G[0-9]+), T([123])[.]p([0-9]+)$/', $titre, $match)) {
142
			$infos['num_nom_coste'] = $match[1];
143
			$infos['nom_sci'] = $match[2];
144
			$infos['num_tax_sup_coste'] = $match[3];
145
			$infos['tome'] = $match[4];
146
			$infos['page'] = $match[5];
147
		} else {
148
			$this->messages->traiterErreur("Le titre du fichier {$this->fichierNum} est mal formaté.");
149
		}
150
 
151
		$corres = $donnees['correspondance'];
152
		if (preg_match('/^Bdnff ([0-9]+) -[^-]+-[^-]+- Tax=([0-9]+)$/', $corres, $match)) {
153
			$infos['bdnff_nn'] = $match[1];
154
			$infos['bdnff_nt'] = $match[2];
155
 
156
		} else {
157
			$this->messages->traiterErreur("La correspondance du fichier {$this->fichierNum} est mal formatée.");
158
		}
159
 
160
		$txt = $donnees['texte'];
161
		$txt = $this->corrigerDescription($txt);
162
		if (preg_match('/^<B>[0-9]{1,4}[.] – ([^<]+)<\/B> ([^–]+)– (?:<I>([^<]+)<\/I>|[A-Z])/u', $txt, $match)) {
163
			$infos['nom_coste'] = $match[1];
164
			$infos['auteur'] = $match[2];
165
			$infos['nom_francais'] = isset($match[3]) ? $match[3] : '';
166
		} else {
167
			$this->messages->traiterErreur("La texte du fichier {$this->fichierNum} est mal formaté.");
168
		}
169
 
170
		$this->correspondance[] = $infos;
171
	}
172
 
173
	private function creerFichierCsvCorrespondance() {
174
		$lignes[] = implode("\t", array_keys($this->infosCorrespondanceBase));
175
		foreach ($this->correspondance as $infos) {
176
			$lignes[] = implode("\t", $infos);
177
		}
178
 
179
		$txt = '';
180
		$txt = implode("\n", $lignes);
181
		return $txt;
182
	}
183
 
184
	private function genererFichier() {
185
		$donnees = $this->analyserFichier();
186
 
187
		$txt = $this->nettoyerDescription($donnees['texte']);
188
		$txt = $this->corrigerDescription($txt);
189
		$txt = $this->remplacerHtmlParSyntaxeWiki($txt);
190
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
191
		file_put_contents($fichierTxt, $txt);
192
 
193
		unset($donnees['texte']);
194
		$txt = implode("\n", $donnees);
195
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.json';
424 jpm 196
		file_put_contents($fichierTxt, $txt);
197
	}
198
 
428 jpm 199
	private function analyserFichier() {
200
		$donnees = array('titre' => '', 'texte' => '', 'correspondance' => '');
201
		if ($fichierOuvert = fopen($this->fichier, 'r')) {
424 jpm 202
			$i = 1;
203
			while ($ligne = fgets($fichierOuvert)) {
204
				if ($i == 24) {
428 jpm 205
					$donnees['titre'] = $this->supprimerHtml($ligne);
206
				} elseif ($i >= 45 && $i <= 62) {
207
					$donnees['texte'] .= $this->traiterLigneDescription($ligne);
208
				} elseif (($i == 63 || $i == 67) && preg_match('/Bdnff /ui', $ligne)) {
209
					$donnees['correspondance'] = $this->supprimerHtml($ligne);
210
				}
424 jpm 211
				$i++;
212
			}
213
			if ($i > 94) {
428 jpm 214
				$this->messages->traiterErreur("Le fichier {$this->fichier} contient plus de 94 lignes ($i).");
424 jpm 215
			}
216
			fclose($fichierOuvert);
217
		} else {
428 jpm 218
			$this->messages->traiterErreur("Le fichier {$this->fichier} n'a pas pu être ouvert.");
424 jpm 219
		}
220
		return $donnees;
221
	}
222
 
428 jpm 223
	private function supprimerHtml($txt) {
224
		$txt = strip_tags($txt,'<b>,<i>');
225
		$txt = str_replace('&#173;', '', $txt);
226
		$txt = str_ireplace('&nbsp;', ' ', $txt);
227
		$txt = trim($txt);
228
		$txt = preg_replace('/^<\/I>/', '', $txt);
229
		$txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
230
		return $txt;
424 jpm 231
	}
232
 
428 jpm 233
	private function traiterLigneDescription($txt) {
234
		$txt = $this->supprimerHtml($txt);
235
		$txt = $txt."\n";
236
		return $txt;
237
	}
238
 
239
	private function nettoyerDescription($txt) {
240
		$txt = preg_replace("/\n{2,}+/", "\n", $txt);
424 jpm 241
		$txt = trim($txt);
242
		return $txt;
243
	}
428 jpm 244
 
245
	private function corrigerDescription($txt) {
246
		$txt = preg_replace("/– <I>([^.]+?)[.] – <\/I>/", "– <I>$1</I>. – ", $txt);
247
		$txt = preg_replace("/– <I>([^.]+?)[.] – ([A-Z])<\/I>/", "– <I>$1</I>. – $2", $txt);
248
		$txt = preg_replace("/– <I>([^.]+?) – <\/I>/", "– <I>$1</I>. – ", $txt);
249
		return $txt;
250
	}
251
 
252
	private function remplacerHtmlParSyntaxeWiki($txt) {
253
		$txt = str_replace(array('<B>', '</B>'), '**', $txt);
254
		$txt = str_replace(array('<I>', '</I>'), '//', $txt);
255
		return $txt;
256
	}
424 jpm 257
}
258
?>