Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 424 | Rev 430 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 424 Rev 428
Line 1... Line 1...
1
<?php
1
<?php
2
// commande : /opt/lampp/bin/php cli.php description_sp -a tester -n /home/jennifer/Tela-botanica_projets/Coste/descriptions/html
2
// commande : /opt/lampp/bin/php cli.php description_sp -a tester -n /home/jennifer/Tela-botanica_projets/Coste/descriptions/html
3
class Description {
3
class Description {
Line -... Line 4...
-
 
4
 
4
 
5
	const DOSSIER_V0 = '../../../donnees/coste/0.00/';
5
	const DOSSIER_DSC_HTML = '../donnees/coste/descriptions/html/';
6
	const DOSSIER_DSC_HTML = '../../../donnees/coste/descriptions/html/';
-
 
7
	const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
Line -... Line 8...
-
 
8
	const DOSSIER_LOG = 'log/';
6
	const DOSSIER_DSC_TXT = '../donnees/coste/descriptions/txt/';
9
 
7
 
10
	private $dossierBase = '';
8
	private $conteneur = null;
11
	private $conteneur = null;
-
 
12
	private $outils = null;
-
 
13
	private $messages = null;
-
 
14
	private $action = '';
-
 
15
	private $nomFichier = '';
9
	private $outils = null;
16
	private $nomDossier = '';
-
 
17
	private $listeFichiers = array();
-
 
18
	private $fichierNum = '';
10
	private $messages = null;
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' => '',
Line 11... Line 32...
11
	private $fichierNum = '';
32
		'bdnff_nn' => '',
12
	private $fichiersSpeciaux = array('2053', '0113', '0102');
33
		'bdnff_nt' => '');
13
 
34
 
14
	public function __construct(Conteneur $conteneur) {
35
	public function __construct(Conteneur $conteneur) {
-
 
36
		$this->conteneur = $conteneur;
15
		$this->conteneur = $conteneur;
37
		$this->outils = $conteneur->getOutils();
Line 16... Line 38...
16
		$this->outils = $conteneur->getOutils();
38
		$this->messages = $conteneur->getMessages();
-
 
39
		$this->dossierBase = dirname(__FILE__).'/';
-
 
40
	}
-
 
41
 
-
 
42
	public function genererDescriptionTxt() {
17
		$this->messages = $conteneur->getMessages();
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";
-
 
50
	}
-
 
51
 
-
 
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
		}
18
	}
59
		echo "\n";
Line 19... Line 60...
19
 
60
 
-
 
61
		$fichierTxt = $this->dossierBase.self::DOSSIER_LOG.'analyse.log';
-
 
62
		file_put_contents($fichierTxt, $this->log);
-
 
63
	}
-
 
64
 
20
	public function genererDescriptionTxt() {
65
	public function genererCorrespondance() {
-
 
66
		$this->chargerFichiers();
21
		$this->ouvrirDossier();
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();
22
	}
76
		file_put_contents($fichierTxt, $txtCorrespondance);
23
 
77
	}
24
	private function ouvrirDossier() {
78
 
25
		$nomDossier = self::DOSSIER_DSC_HTML;
79
	private function chargerFichiers() {
26
		$tableaux = null;
80
		$this->nomDossier = $this->dossierBase.self::DOSSIER_DSC_HTML;
27
		if (file_exists($nomDossier) === true) {
-
 
28
			if (is_dir($nomDossier)) {
81
		if (file_exists($this->nomDossier) === true) {
29
				if ($dossierOuvert = opendir($nomDossier)) {
-
 
30
					while (($nomFichier = readdir($dossierOuvert)) !== false) {
-
 
31
						if (! is_dir($nomFichier) && preg_match('/e([0-9]{4})\.htm/', $nomFichier, $match)) {
82
			if (is_dir($this->nomDossier)) {
32
							$this->fichierNum = $match[1];
83
				if ($dossierOuvert = opendir($this->nomDossier)) {
33
							$nomFichier = $nomDossier.'/'.$nomFichier;
84
					while (($this->nomFichier = readdir($dossierOuvert)) !== false) {
34
							$this->traiterFichier($nomFichier);
85
						if (! is_dir($this->nomFichier) && preg_match('/e([0-9]{4})\.htm/', $this->nomFichier, $match)) {
35
							$this->messages->afficherAvancement("Création fichier txt");
86
							$this->listeFichiers[$match[1]] = $this->nomDossier.'/'.$this->nomFichier;
36
						}
87
						}
37
					}
88
					}
38
					closedir($dossierOuvert);
89
					closedir($dossierOuvert);
39
				} else {
90
				} else {
40
					$this->messages->traiterErreur("Le dossier $nomDossier n'a pas pu être ouvert.");
91
					$this->messages->traiterErreur("Le dossier {$this->nomDossier} n'a pas pu être ouvert.");
41
				}
92
				}
42
			} else {
93
			} else {
-
 
94
				$this->messages->traiterErreur("{$this->nomDossier} n'est pas un dossier.");
43
				$this->messages->traiterErreur("$nomDossier n'est pas un dossier.");
95
			}
44
			}
96
		} else {
Line 45... Line 97...
45
		} else {
97
			$this->messages->traiterErreur("Le dossier {$this->nomDossier} est introuvable.");
-
 
98
		}
-
 
99
 
-
 
100
		asort($this->listeFichiers);
-
 
101
	}
-
 
102
 
-
 
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
 
46
			$this->messages->traiterErreur("Le dossier $nomDossier est introuvable.");
179
		$txt = '';
-
 
180
		$txt = implode("\n", $lignes);
47
		}
181
		return $txt;
-
 
182
	}
-
 
183
 
48
		echo "\n";
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);
49
	}
190
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
50
 
191
		file_put_contents($fichierTxt, $txt);
Line 51... Line 192...
51
	private function traiterFichier($nomFichier) {
192
 
52
		$donnees = $this->analyserFichier($nomFichier);
193
		unset($donnees['texte']);
53
		$txt = $this->nettoyerTxt($donnees['texte']);
-
 
54
		$fichierTxt = self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
194
		$txt = implode("\n", $donnees);
55
		file_put_contents($fichierTxt, $txt);
195
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.json';
56
	}
196
		file_put_contents($fichierTxt, $txt);
57
 
197
	}
58
	private function analyserFichier($nomFichier) {
198
 
59
		$this->fichier = $nomFichier;
-
 
60
		$donnees = null;
-
 
61
		if ($fichierOuvert = fopen($nomFichier, 'r')) {
-
 
62
			$i = 1;
199
	private function analyserFichier() {
63
			while ($ligne = fgets($fichierOuvert)) {
200
		$donnees = array('titre' => '', 'texte' => '', 'correspondance' => '');
64
				if ($i == 24) {
201
		if ($fichierOuvert = fopen($this->fichier, 'r')) {
65
					$donnees['titre'] = $this->traiterHtml($ligne);
-
 
66
				} elseif ($i == 45) {
-
 
67
					$donnees['texte'] = $this->traiterHtml($ligne, $au = true);
202
			$i = 1;
68
					$this->donnees['texte'] = $this->traiterHtml($ligne);
203
			while ($ligne = fgets($fichierOuvert)) {
69
				} elseif ($i >= 46 && $i <= 62) {
-
 
70
					$donnees['texte'] .= $this->traiterHtml($ligne);
204
				if ($i == 24) {
71
				} elseif ($i == 63 || preg_match('/Bdnff /ui', $ligne)) {
205
					$donnees['titre'] = $this->supprimerHtml($ligne);
72
					$donnees['correspondance'] = $this->traiterHtml($ligne);
206
				} elseif ($i >= 45 && $i <= 62) {
73
				} elseif ($i == 67 || preg_match('/Bdnff /ui', $ligne)) {
207
					$donnees['texte'] .= $this->traiterLigneDescription($ligne);
74
					$donnees['correspondance'] = $this->traiterHtml($ligne);
208
				} elseif (($i == 63 || $i == 67) && preg_match('/Bdnff /ui', $ligne)) {
75
				}
209
					$donnees['correspondance'] = $this->supprimerHtml($ligne);
76
 
210
				}
77
				$i++;
211
				$i++;
78
			}
212
			}
79
			if ($i > 94) {
213
			if ($i > 94) {
80
				$this->messages->traiterErreur("Le fichier $nomFichier contient plus de 94 lignes ($i).");
214
				$this->messages->traiterErreur("Le fichier {$this->fichier} contient plus de 94 lignes ($i).");
Line 81... Line 215...
81
			}
215
			}
82
			fclose($fichierOuvert);
216
			fclose($fichierOuvert);
83
		} else {
-
 
84
			$this->messages->traiterErreur("Le fichier $nomFichier n'a pas pu être ouvert.");
217
		} else {
85
		}
218
			$this->messages->traiterErreur("Le fichier {$this->fichier} n'a pas pu être ouvert.");
86
		return $donnees;
219
		}
87
	}
220
		return $donnees;
88
 
221
	}
89
	private function traiterHtml($donnees, $au = null) {
-
 
90
		$donnees_traitees = strip_tags($donnees,'<b>,<i>');
222
 
91
		$donnees_traitees = html_entity_decode($donnees_traitees, ENT_NOQUOTES, 'UTF-8');
223
	private function supprimerHtml($txt) {
Line -... Line 224...
-
 
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');
92
		$donnees_traitees = str_replace(array('<B>', '</B>'), '**', $donnees_traitees);
230
		return $txt;
93
		$donnees_traitees = str_replace(array('<I>', '</I>'), '//', $donnees_traitees);
231
	}
94
		$donnees_traitees = trim($donnees_traitees)."\n";
232
 
95
		if ($au) {
233
	private function traiterLigneDescription($txt) {
96
			$donnees_traitees = str_replace('&#173;', '', $donnees_traitees);
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);
-
 
241
		$txt = trim($txt);
-
 
242
		return $txt;
-
 
243
	}
-
 
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);
97
		}
248
		$txt = preg_replace("/– <I>([^.]+?) – <\/I>/", "– <I>$1</I>. – ", $txt);
98
		return $donnees_traitees;
249
		return $txt;
99
	}
250
	}