Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 435 | Rev 437 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 435 Rev 436
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 {
4
 
4
 
5
	const DOSSIER_V0 = '../../../donnees/coste/0.00/';
5
	const DOSSIER_V0 = '../../../donnees/coste/0.00/';
6
	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/';
7
	const DOSSIER_DSC_TXT = '../../../donnees/coste/descriptions/txt/';
8
	const DOSSIER_LOG = 'log/';
8
	const DOSSIER_LOG = 'log/';
-
 
9
 
9
 
10
	private $atueurs = array();
10
	private $dossierBase = '';
11
	private $dossierBase = '';
11
	private $conteneur = null;
12
	private $conteneur = null;
12
	private $outils = null;
13
	private $outils = null;
13
	private $messages = null;
14
	private $messages = null;
14
	private $action = '';
15
	private $action = '';
15
	private $nomFichier = '';
16
	private $nomFichier = '';
16
	private $nomDossier = '';
17
	private $nomDossier = '';
17
	private $listeFichiers = array();
18
	private $listeFichiers = array();
18
	private $fichierNum = '';
19
	private $fichierNum = '';
19
	private $fichier = '';
20
	private $fichier = '';
20
	private $log = '';
21
	private $log = '';
21
	private $correspondance = array();
22
	private $correspondance = array();
22
	private $infosCorrespondanceBase = array(
23
	private $infosCorrespondanceBase = array(
23
		'nom_sci' => '',
24
		'nom_sci' => '',
24
		'rang' => 290,
-
 
25
		'auteur' => '',
25
		'auteur' => '',
-
 
26
		'nom_addendum' => '',
26
		'annee' => '',
27
		'annee' => '',
27
		'biblio_coste' => '',
28
		'biblio_coste' => '',
-
 
29
		'rang' => 290,
28
		'nom_francais' => '',
30
		'nom_francais' => '',
29
		'nom_coste' => '',
31
		'nom_coste' => '',
30
		'auteur_coste' => '',
32
		'auteur_coste' => '',
31
		'num_nom_coste' => '',
33
		'num_nom_coste' => '',
32
		'num_nom_retenu_coste' => '',
34
		'num_nom_retenu_coste' => '',
33
		'num_tax_sup_coste' => '',
35
		'num_tax_sup_coste' => '',
34
		'tome' => '',
36
		'tome' => '',
35
		'page' => '',
37
		'page' => '',
36
		'synonymie_coste' => '',
38
		'synonymie_coste' => '',
37
		'bdnff_nn' => '',
39
		'bdnff_nn' => '',
38
		'bdnff_nt' => '');
40
		'bdnff_nt' => '');
39
 
41
 
40
	public function __construct(Conteneur $conteneur) {
42
	public function __construct(Conteneur $conteneur) {
41
		$this->conteneur = $conteneur;
43
		$this->conteneur = $conteneur;
42
		$this->outils = $conteneur->getOutils();
44
		$this->outils = $conteneur->getOutils();
43
		$this->messages = $conteneur->getMessages();
45
		$this->messages = $conteneur->getMessages();
44
		$this->dossierBase = dirname(__FILE__).'/';
46
		$this->dossierBase = dirname(__FILE__).'/';
45
	}
47
	}
46
 
48
 
47
	public function genererDescriptionTxt() {
49
	public function genererDescriptionTxt() {
48
		$this->chargerFichiers();
50
		$this->chargerFichiers();
49
 
51
 
50
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
52
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
51
			$this->messages->afficherAvancement("Création des descriptions au format txt");
53
			$this->messages->afficherAvancement("Création des descriptions au format txt");
52
			$this->genererFichier();
54
			$this->genererFichier();
53
		}
55
		}
54
		echo "\n";
56
		echo "\n";
55
	}
57
	}
56
 
58
 
57
	public function verifierDescriptionTxt() {
59
	public function verifierDescriptionTxt() {
58
		$this->chargerFichiers();
60
		$this->chargerFichiers();
59
 
61
 
60
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
62
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
61
			$this->messages->afficherAvancement("Analyse des descriptions");
63
			$this->messages->afficherAvancement("Analyse des descriptions");
62
			$this->verifierFichier();
64
			$this->verifierFichier();
63
		}
65
		}
64
		echo "\n";
66
		echo "\n";
65
 
-
 
66
		$fichierTxt = $this->dossierBase.self::DOSSIER_LOG.'analyse.log';
-
 
67
		file_put_contents($fichierTxt, $this->log);
67
		$this->ecrireLogs();
68
	}
68
	}
69
 
69
 
70
	public function genererCorrespondance() {
70
	public function genererCorrespondance() {
71
		$this->chargerFichiers();
71
		$this->chargerFichiers();
72
 
72
 
-
 
73
		$this->ajouterLogSyno(implode("\t", array_keys($this->infosCorrespondanceBase)));
73
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
74
		foreach ($this->listeFichiers as $this->fichierNum => $this->fichier) {
74
			$this->messages->afficherAvancement("Création du fichier de correspondance");
75
			$this->messages->afficherAvancement("Création du fichier de correspondance");
75
			$this->extraireInfosCorrespondance();
76
			$this->extraireInfosCorrespondance();
76
		}
77
		}
77
		echo "\n";
78
		echo "\n";
78
 
79
 
79
		$fichierTxt = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
80
		$fichierTxt = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
80
		$txtCorrespondance = $this->creerFichierCsvCorrespondance();
81
		$txtCorrespondance = $this->creerFichierCsvCorrespondance();
81
		file_put_contents($fichierTxt, $txtCorrespondance);
82
		file_put_contents($fichierTxt, $txtCorrespondance);
82
 
-
 
83
		$fichierLog = $this->dossierBase.self::DOSSIER_LOG.'synonymes.log';
83
 
84
		file_put_contents($fichierLog, $this->log);
84
		$this->ecrireLogs();
85
	}
85
	}
86
 
86
 
87
	private function chargerFichiers() {
87
	private function chargerFichiers() {
88
		$this->nomDossier = $this->dossierBase.self::DOSSIER_DSC_HTML;
88
		$this->nomDossier = $this->dossierBase.self::DOSSIER_DSC_HTML;
89
		if (file_exists($this->nomDossier) === true) {
89
		if (file_exists($this->nomDossier) === true) {
90
			if (is_dir($this->nomDossier)) {
90
			if (is_dir($this->nomDossier)) {
91
				if ($dossierOuvert = opendir($this->nomDossier)) {
91
				if ($dossierOuvert = opendir($this->nomDossier)) {
92
					while (($this->nomFichier = readdir($dossierOuvert)) !== false) {
92
					while (($this->nomFichier = readdir($dossierOuvert)) !== false) {
93
						if (! is_dir($this->nomFichier) && preg_match('/e([0-9]{4})\.htm/', $this->nomFichier, $match)) {
93
						if (! is_dir($this->nomFichier) && preg_match('/e([0-9]{4})\.htm/', $this->nomFichier, $match)) {
94
							$this->listeFichiers[$match[1]] = $this->nomDossier.'/'.$this->nomFichier;
94
							$this->listeFichiers[$match[1]] = $this->nomDossier.'/'.$this->nomFichier;
95
						}
95
						}
96
					}
96
					}
97
					closedir($dossierOuvert);
97
					closedir($dossierOuvert);
98
				} else {
98
				} else {
99
					$this->messages->traiterErreur("Le dossier {$this->nomDossier} n'a pas pu être ouvert.");
99
					$this->messages->traiterErreur("Le dossier {$this->nomDossier} n'a pas pu être ouvert.");
100
				}
100
				}
101
			} else {
101
			} else {
102
				$this->messages->traiterErreur("{$this->nomDossier} n'est pas un dossier.");
102
				$this->messages->traiterErreur("{$this->nomDossier} n'est pas un dossier.");
103
			}
103
			}
104
		} else {
104
		} else {
105
			$this->messages->traiterErreur("Le dossier {$this->nomDossier} est introuvable.");
105
			$this->messages->traiterErreur("Le dossier {$this->nomDossier} est introuvable.");
106
		}
106
		}
107
 
107
 
108
		arsort($this->listeFichiers);
108
		arsort($this->listeFichiers);
109
	}
109
	}
110
 
110
 
111
	private function verifierFichier() {
111
	private function verifierFichier() {
112
		$donnees = $this->analyserFichier();
112
		$donnees = $this->analyserFichier();
113
		$txt = $donnees['texte'];
113
		$txt = $donnees['texte'];
114
		$this->verifierOuvertureFermetureBalise($txt);
114
		$this->verifierOuvertureFermetureBalise($txt);
115
		$this->verifierAbscenceEgale($txt);
115
		$this->verifierAbscenceEgale($txt);
116
		$this->verifierNumero($txt);
116
		$this->verifierNumero($txt);
117
		// TODO : vérifier les noms vernauclaire qui sont abrégés
117
		// TODO : vérifier les noms vernauclaire qui sont abrégés
118
	}
118
	}
119
 
119
 
120
	private function verifierOuvertureFermetureBalise($txt) {
120
	private function verifierOuvertureFermetureBalise($txt) {
121
		$b_ouvert = substr_count($txt, '<B>');
121
		$b_ouvert = substr_count($txt, '<B>');
122
		$b_ferme = substr_count($txt, '</B>');
122
		$b_ferme = substr_count($txt, '</B>');
123
		if ($b_ouvert != $b_ferme) {
123
		if ($b_ouvert != $b_ferme) {
124
			$this->log .= "{$this->fichierNum} contient $b_ouvert balises B ouvertes et $b_ferme fermées\n";
124
			$message = "{$this->fichierNum} contient $b_ouvert balises B ouvertes et $b_ferme fermées";
-
 
125
			$this->ajouterLogAnalyse($message);
125
		}
126
		}
126
		$i_ouvert = substr_count($txt, '<I>');
127
		$i_ouvert = substr_count($txt, '<I>');
127
		$i_ferme = substr_count($txt, '</I>');
128
		$i_ferme = substr_count($txt, '</I>');
128
		if ($i_ouvert != $i_ferme) {
129
		if ($i_ouvert != $i_ferme) {
129
			$this->log .= "{$this->fichierNum} contient $i_ouvert balises I ouvertes et $i_ferme fermées\n";
130
			$message = "{$this->fichierNum} contient $i_ouvert balises I ouvertes et $i_ferme fermées";
-
 
131
			$this->ajouterLogAnalyse($message);
130
		}
132
		}
131
	}
133
	}
132
 
134
 
133
	private function verifierAbscenceEgale($txt) {
135
	private function verifierAbscenceEgale($txt) {
134
		if (strripos($txt, '=') === false) {
136
		if (strripos($txt, '=') === false) {
135
			$this->log .= "{$this->fichierNum} ne contient pas le séparateur de phénologie (=)\n";
137
			$message = "{$this->fichierNum} ne contient pas le séparateur de phénologie (=)";
-
 
138
			$this->ajouterLogAnalyse($message);
136
		}
139
		}
137
	}
140
	}
138
 
141
 
139
	private function verifierNumero($txt) {
142
	private function verifierNumero($txt) {
140
		if (preg_match('/^<B>[0-9]{1,4}. – /', $txt) == 0) {
143
		if (preg_match('/^<B>[0-9]{1,4}. – /', $txt) == 0) {
141
			$this->log .= "{$this->fichierNum} ne contient pas un numéro bien formaté\n";
144
			$message = "{$this->fichierNum} ne contient pas un numéro bien formaté";
-
 
145
			$this->ajouterLogAnalyse($message);
142
		}
146
		}
143
	}
147
	}
144
 
148
 
145
	private function extraireInfosCorrespondance() {
149
	private function extraireInfosCorrespondance() {
146
		$donnees = $this->analyserFichier();
150
		$donnees = $this->analyserFichier();
147
		$infos = $this->infosCorrespondanceBase;
151
		$infos = $this->infosCorrespondanceBase;
148
 
152
 
149
		$titre = $donnees['titre'];
153
		$titre = $donnees['titre'];
150
		if (preg_match('/^Coste ([0-9]+) - ((?:(?! - ).)+) - F[0-9]+, (?:(?! - ).)+ - (G[0-9]+), T([123])[.]p([0-9]+)$/', $titre, $match)) {
154
		if (preg_match('/^Coste ([0-9]+) - ((?:(?! - ).)+) - F[0-9]+, (?:(?! - ).)+ - (G[0-9]+), T([123])[.]p([0-9]+)$/', $titre, $match)) {
151
			$infos['num_nom_coste'] = $match[1];
155
			$infos['num_nom_coste'] = $match[1];
152
			$infos['nom_sci'] = $match[2];
156
			$infos['nom_sci'] = $match[2];
153
			$infos['num_tax_sup_coste'] = $match[3];
157
			$infos['num_tax_sup_coste'] = $match[3];
154
			$infos['tome'] = $match[4];
158
			$infos['tome'] = $match[4];
155
			$infos['page'] = $match[5];
159
			$infos['page'] = $match[5];
156
		} else {
160
		} else {
157
			$this->messages->traiterErreur("Le titre du fichier {$this->fichierNum} est mal formaté.");
161
			$this->messages->traiterErreur("Le titre du fichier {$this->fichierNum} est mal formaté.");
158
		}
162
		}
159
 
163
 
160
		$corres = $donnees['correspondance'];
164
		$corres = $donnees['correspondance'];
161
		if (preg_match('/^Bdnff ([0-9]+) - (?:(?! - ).)+ - (?:(?! - ).)+ - Tax=([0-9]+)$/', $corres, $match)) {
165
		if (preg_match('/^Bdnff ([0-9]+) - (?:(?! - ).)+ - (?:(?! - ).)+ - Tax=([0-9]+)$/', $corres, $match)) {
162
			$infos['bdnff_nn'] = $match[1];
166
			$infos['bdnff_nn'] = $match[1];
163
			$infos['bdnff_nt'] = $match[2];
167
			$infos['bdnff_nt'] = $match[2];
164
		} else {
168
		} else {
165
			$this->messages->traiterErreur("La correspondance du fichier {$this->fichierNum} est mal formatée.");
169
			$this->messages->traiterErreur("La correspondance du fichier {$this->fichierNum} est mal formatée.");
166
		}
170
		}
167
 
171
 
168
		$txt = $donnees['texte'];
172
		$txt = $donnees['texte'];
169
		$txt = $this->corrigerDescription($txt);
173
		$txt = $this->corrigerDescription($txt);
170
		if (preg_match('/^<B>[0-9]{1,4}[.] – ([^<]+)<\/B> ([^–]*)– (?:<I>([^<]+)<\/I>[.] – |<I>([^<]+)<\/I>( \([^)]+\))[.] – |[A-Z]|<I>(?:Sous-|Espèce|Turion|Souche|Plante|Feuille|Racine))/u', $txt, $match)) {
174
		if (preg_match('/^<B>[0-9]{1,4}[.] – ([^<]+)<\/B> ([^–]*)– (?:<I>([^<]+)<\/I>[.] – |<I>([^<]+)<\/I>( \([^)]+\))[.] – |[A-Z]|<I>(?:Sous-|Espèce|Turion|Souche|Plante|Feuille|Racine))/u', $txt, $match)) {
171
			$infos['nom_coste'] = trim($match[1]);
175
			$infos['nom_coste'] = trim($match[1]);
172
			$infos['auteur_coste'] = trim($match[2]);
176
			$infos['auteur_coste'] = trim($match[2]);
173
			$infos['nom_francais'] = isset($match[3]) ? $match[3] : '';
177
			$infos['nom_francais'] = isset($match[3]) ? $match[3] : '';
174
			$infos['nom_francais'] = isset($match[4]) && isset($match[5]) ? $match[4].$match[5] : $infos['nom_francais'];
178
			$infos['nom_francais'] = isset($match[4]) && isset($match[5]) ? $match[4].$match[5] : $infos['nom_francais'];
175
			if (strpos($infos['auteur_coste'], '(' ) !== false) {
179
			if (strpos($infos['auteur_coste'], '(' ) !== false) {
176
				if (preg_match('/^([^(]*)\(([^)]+)\)(?:[.]?| ; ((?:(?! – ).)+))$/', $infos['auteur_coste'], $match)) {
180
				if (preg_match('/^([^(]*)\(([^)]+)\)(?:[.]?| ; ((?:(?! – ).)+))$/', $infos['auteur_coste'], $match)) {
177
					$infos['auteur_coste'] = trim($match[1]);
181
					$infos['auteur_coste'] = trim($match[1]);
178
					$infos['auteur'] = $this->traiterAuteur($infos['auteur_coste']);
182
					$infos['auteur'] = $this->traiterAuteur($infos['auteur_coste']);
179
					$parentheseContenu = trim($match[2]);
183
					$parentheseContenu = trim($match[2]);
180
					if (preg_match('/^[0-9]+$/', $parentheseContenu)) {
184
					if (preg_match('/^[0-9]+$/', $parentheseContenu)) {
181
						$infos['annee'] = $parentheseContenu;
185
						$infos['annee'] = $parentheseContenu;
182
					} else {
186
					} else {
183
						$infos['synonymie_coste'] = $parentheseContenu;
187
						$infos['synonymie_coste'] = $parentheseContenu;
184
						$infos['biblio_coste'] = isset($match[3]) ? trim($match[3]) : '';
188
						$infos['biblio_coste'] = isset($match[3]) ? trim($match[3]) : '';
185
					}
189
					}
186
				} else {
190
				} else {
187
					$this->messages->traiterErreur("L'auteur du nom sciencitifique du fichier {$this->fichierNum} est mal formaté.");
191
					$this->messages->traiterErreur("L'auteur du nom sciencitifique du fichier {$this->fichierNum} est mal formaté.");
188
				}
192
				}
189
			}
193
			}
190
		} else {
194
		} else {
191
			$this->messages->traiterErreur("La texte du fichier {$this->fichierNum} est mal formaté.");
195
			$this->messages->traiterErreur("La texte du fichier {$this->fichierNum} est mal formaté.");
192
		}
196
		}
193
 
197
 
194
		$this->correspondance[] = $infos;
198
		$this->correspondance[] = $infos;
195
 
199
 
196
		if ($infos['synonymie_coste'] != '') {
200
		if ($infos['synonymie_coste'] != '') {
197
			$this->traiterSynonymie($infos);
201
			$this->traiterSynonymie($infos);
198
		}
202
		}
199
	}
203
	}
200
 
204
 
201
	private function traiterAuteur($auteurCoste) {
205
	private function traiterAuteur($auteurCoste) {
202
		$auteur = '';
206
		$auteur = '';
203
		if ($auteurCoste != '') {
207
		if ($auteurCoste != '') {
204
			$auteur = str_replace('et', '&', $auteurCoste);
208
			$auteur = str_replace('et', '&', $auteurCoste);
205
		}
209
		}
206
		return $auteur;
210
		return $auteur;
207
	}
211
	}
208
 
212
 
209
	private function traiterSynonymie($infos) {
213
	private function traiterSynonymie($infos) {
210
		$synoCoste = $infos['synonymie_coste'];
214
		$synoCoste = $infos['synonymie_coste'];
211
		$synoCoste = preg_replace('/^et /', '', $synoCoste);
215
		$synoCoste = preg_replace('/^et /', '', $synoCoste);
212
		$synoCoste = preg_replace('/ et ([A-Z]{2,}|[A-Z][.] [A-Z]{2,})/', ' ; $1', $synoCoste);
216
		$synoCoste = preg_replace('/ et ([A-Z]{2,}|[A-Z][.] [A-Z]{2,})/', ' ; $1', $synoCoste);
213
		$synoCoste = preg_replace('/, ((?:(?!non |part[.]|an |G[.] G[.]|part[.]|centr[.])[^,]+))/', ' ;$1', $synoCoste);
217
		$synoCoste = preg_replace('/, ((?:(?!non |part[.]|an |G[.] G[.]|part[.]|centr[.])[^,]+))/', ' ;$1', $synoCoste);
214
 
218
 
215
		$synonymes = explode(';', $synoCoste);
219
		$synonymes = explode(';', $synoCoste);
216
 
220
 
217
		foreach ($synonymes as $syno) {
221
		foreach ($synonymes as $num => $syno) {
218
			$syno = trim($syno);
-
 
219
			$syno = trim($syno, ' ');
222
			$synoTraite = $this->traiterNomSyno($syno);
-
 
223
			$infosSyno = $this->infosCorrespondanceBase;
-
 
224
			$infosSyno['rang'] = $this->obtenirRangSyno($syno);
-
 
225
			$infosSyno['nom_sci'] = $this->obtenirNomSci($synoTraite, $infos, $synonymes, $num);
-
 
226
			$infosSyno['nom_coste'] = $synoTraite;
-
 
227
			$infosSyno['auteur'] =  $this->obtenirAuteur($synoTraite);
220
			$infosSyno = $this->infosCorrespondanceBase;
228
			$infosSyno['nom_addendum'] =  $this->obtenirNomAddendum($syno);
-
 
229
			$infosSyno['num_nom_coste'] = $infos['num_nom_coste'].'.'.($num + 1);
-
 
230
			$infosSyno['num_nom_retenu_coste'] = $infos['num_nom_coste'];
221
			$this->log .= $infos['num_nom_coste']."\t$syno\n";
231
			$this->ajouterLogSyno(implode("\t", $infosSyno));
222
			$this->correspondance[] = $infosSyno;
232
			$this->correspondance[] = $infosSyno;
223
		}
233
		}
224
	}
234
	}
-
 
235
 
-
 
236
	private function obtenirAuteur($syno) {
-
 
237
		$auteur = '';
-
 
238
		$nomSciRE = '(?:[^ ]+ [^ ]+ var. [^ ]+|[^ ]+ [^ ]+)';
-
 
239
		if (preg_match("/^$nomSciRE ((?!(?:auct.+|, non.+|p[.] p[.])$).+)$/", $syno, $match)) {
-
 
240
			$auteur = $match[1];
-
 
241
			$auteur = preg_replace('/(?: p[.] p[.]|, non .+|(?: Sp[.])? ed[.] [1-2])$/', '', $auteur);
-
 
242
			$this->ajouterAuteur($auteur);
-
 
243
		} else {
-
 
244
			$message = "Impossible de récupérer l'auteur pour le synonyme ($syno).";
-
 
245
			$this->ajouterLogProbleme($message);
-
 
246
		}
-
 
247
		return $auteur;
-
 
248
	}
-
 
249
 
-
 
250
	private function ajouterAuteur($auteur) {
-
 
251
		if (!isset($this->auteurs[$auteur])) {
-
 
252
			$this->auteurs[$auteur] = 1;
-
 
253
			$this->ajouterLogAuteur($auteur);
-
 
254
		} else {
-
 
255
			$this->auteurs[$auteur]++;
-
 
256
		}
-
 
257
	}
-
 
258
 
-
 
259
	private function obtenirNomAddendum($syno) {
-
 
260
		$nomAddendum = array();
-
 
261
		if (preg_match('/^((?:compr|incl)[.]) /', $syno, $match)) {
-
 
262
			$nomAddendum[] = '['.$match[1].']';
-
 
263
		}
-
 
264
		if (preg_match('/ (auct[.] .+)$/', $syno, $match)) {
-
 
265
			$nomAddendum[] = '['.$match[1].']';
-
 
266
		} else if (preg_match('/, (non .+)$/', $syno, $match)) {
-
 
267
			$nomAddendum[] = '['.$match[1].']';
-
 
268
		} else if (preg_match('/ (p[.] p[.])$/', $syno, $match)) {
-
 
269
			$nomAddendum[] = '['.$match[1].']';
-
 
270
		} else if (preg_match('/ (saltem part[.])$/', $syno, $match)) {
-
 
271
			$nomAddendum[] = '['.$match[1].']';
-
 
272
		}
-
 
273
 
-
 
274
		$nomAddendum = implode(' ; ', $nomAddendum);
-
 
275
		return $nomAddendum;
-
 
276
	}
-
 
277
 
-
 
278
	private function obtenirSynoPrecedent($synonymes, $num) {
-
 
279
		$synoPrecedent = '';
-
 
280
		if (isset($synonymes[($num - 1 )])) {
-
 
281
			$synoPrecedent = $this->traiterNomSyno($synonymes[($num - 1 )]);
-
 
282
		}
-
 
283
		return $synoPrecedent;
-
 
284
	}
-
 
285
 
-
 
286
	private function remplacerAbreviationGenre($syno, $infos, $synonymes, $num) {
-
 
287
		$nomSci = $syno;
-
 
288
 
-
 
289
		if (preg_match('/^(([A-Zƌ])[.]) /', $syno, $matchSyno)) {
-
 
290
			if ($matchSyno[2] == substr($infos['nom_coste'], 0, 1)) {
-
 
291
				if (preg_match('/^([^ ]+) /', $infos['nom_coste'], $matchNomRetenu)) {
-
 
292
					$nomSci = str_replace($matchSyno[1], $matchNomRetenu[1], $syno);
-
 
293
				}
-
 
294
			} else {
-
 
295
				$synoPrecedent = $this->obtenirSynoAvecGenreComplet($synonymes, $num);
-
 
296
 
-
 
297
				if ($matchSyno[2] == substr($synoPrecedent, 0, 1)) {
-
 
298
					if (preg_match('/^([^ ]+) /', $synoPrecedent, $matchSynoPrec)) {
-
 
299
						$nomSci = str_replace($matchSyno[1], $matchSynoPrec[1], $syno);
-
 
300
					}
-
 
301
				} else {
-
 
302
					$message = "L'initiale du synonyme ($syno) ne correspondant pas au nom retenu {$infos['num_nom_coste']} ({$infos['nom_coste']}) ".
-
 
303
							"ni au synonyme précédent ($synoPrecedent).";
-
 
304
					$this->ajouterLogProbleme($message);
-
 
305
				}
-
 
306
			}
-
 
307
		}
-
 
308
		return $nomSci;
-
 
309
	}
-
 
310
 
-
 
311
	private function obtenirSynoAvecGenreComplet($synonymes, $num) {
-
 
312
		$synoPrecedent = '';
-
 
313
		$synoOk = false;
-
 
314
		while ($synoOk == false) {
-
 
315
			if ($num < 0) {
-
 
316
				$synoOk = true;
-
 
317
			}
-
 
318
			$synoPrecedent = $this->obtenirSynoPrecedent($synonymes, $num);
-
 
319
			if (preg_match('/^[A-Zƌ][.] /', $synoPrecedent)) {
-
 
320
				$num = $num - 1;
-
 
321
			} else {
-
 
322
				$synoOk = true;
-
 
323
			}
-
 
324
		}
-
 
325
		return $synoPrecedent;
-
 
326
	}
-
 
327
 
-
 
328
	private function obtenirNomSci($syno, $infos, $synonymes, $num) {
-
 
329
		$nomSci = $this->remplacerAbreviationGenre($syno, $infos, $synonymes, $num);
-
 
330
 
-
 
331
		if (preg_match('/^([^ ]+) ([^ ]+) var. ([^ ]+)/', $nomSci, $match)) {
-
 
332
			$genre = ucfirst(strtolower($match[1]));
-
 
333
			$sp = strtolower($match[2]);
-
 
334
			$infrasp = strtolower($match[3]);
-
 
335
			$nomSci = "$genre $sp var. $infrasp";
-
 
336
		} else if (preg_match('/^([^ ]+) ([^ ]+)(?: |$)/', $nomSci, $match)) {
-
 
337
			$genre = ucfirst(strtolower($match[1]));
-
 
338
			$sp = strtolower($match[2]);
-
 
339
			$nomSci = "$genre $sp";
-
 
340
		} else {
-
 
341
			$message = "Le synonyme ($nomSci) du nom {$infos['num_nom_coste']} a une structure étrange.";
-
 
342
			$this->ajouterLogProbleme($message);
-
 
343
		}
-
 
344
		return $nomSci;
-
 
345
	}
-
 
346
 
-
 
347
	private function obtenirRangSyno($syno) {
-
 
348
		$rang = 290;
-
 
349
		if (strpos($syno, ' var. ')) {
-
 
350
			$rang = 340;
-
 
351
		}
-
 
352
		return $rang;
-
 
353
	}
-
 
354
 
-
 
355
	private function traiterNomSyno($syno) {
-
 
356
		$syno = trim($syno);
-
 
357
		$syno = trim($syno, ' ');
-
 
358
		$syno = preg_replace('/^(?:compr|incl)[.] /', '', $syno);
-
 
359
		return $syno;
-
 
360
	}
225
 
361
 
226
	private function creerFichierCsvCorrespondance() {
362
	private function creerFichierCsvCorrespondance() {
227
		$lignes[] = implode("\t", array_keys($this->infosCorrespondanceBase));
363
		$lignes[] = implode("\t", array_keys($this->infosCorrespondanceBase));
228
		foreach ($this->correspondance as $infos) {
364
		foreach ($this->correspondance as $infos) {
229
			$lignes[] = implode("\t", $infos);
365
			$lignes[] = implode("\t", $infos);
230
		}
366
		}
231
 
367
 
232
		$txt = '';
368
		$txt = '';
233
		$txt = implode("\n", $lignes);
369
		$txt = implode("\n", $lignes);
234
		return $txt;
370
		return $txt;
235
	}
371
	}
236
 
372
 
237
	private function genererFichier() {
373
	private function genererFichier() {
238
		$donnees = $this->analyserFichier();
374
		$donnees = $this->analyserFichier();
239
 
375
 
240
		$txt = $this->nettoyerDescription($donnees['texte']);
376
		$txt = $this->nettoyerDescription($donnees['texte']);
241
		$txt = $this->corrigerDescription($txt);
377
		$txt = $this->corrigerDescription($txt);
242
		$txt = $this->remplacerHtmlParSyntaxeWiki($txt);
378
		$txt = $this->remplacerHtmlParSyntaxeWiki($txt);
243
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
379
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.txt';
244
		file_put_contents($fichierTxt, $txt);
380
		file_put_contents($fichierTxt, $txt);
245
 
381
 
246
		unset($donnees['texte']);
382
		unset($donnees['texte']);
247
		$txt = implode("\n", $donnees);
383
		$txt = implode("\n", $donnees);
248
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.json';
384
		$fichierTxt = $this->dossierBase.self::DOSSIER_DSC_TXT.$this->fichierNum.'.json';
249
		file_put_contents($fichierTxt, $txt);
385
		file_put_contents($fichierTxt, $txt);
250
	}
386
	}
251
 
387
 
252
	private function analyserFichier() {
388
	private function analyserFichier() {
253
		$donnees = array('titre' => '', 'texte' => '', 'correspondance' => '');
389
		$donnees = array('titre' => '', 'texte' => '', 'correspondance' => '');
254
		if ($fichierOuvert = fopen($this->fichier, 'r')) {
390
		if ($fichierOuvert = fopen($this->fichier, 'r')) {
255
			$i = 1;
391
			$i = 1;
256
			while ($ligne = fgets($fichierOuvert)) {
392
			while ($ligne = fgets($fichierOuvert)) {
257
				if ($i == 24) {
393
				if ($i == 24) {
258
					$donnees['titre'] = $this->supprimerHtml($ligne);
394
					$donnees['titre'] = $this->supprimerHtml($ligne);
259
				} elseif ($i >= 45 && $i <= 60) {
395
				} elseif ($i >= 45 && $i <= 60) {
260
					$donnees['texte'] .= $this->traiterLigneDescription($ligne);
396
					$donnees['texte'] .= $this->traiterLigneDescription($ligne);
261
				} elseif ($i >= 61 && preg_match('/Bdnff /ui', $ligne)) {
397
				} elseif ($i >= 61 && preg_match('/Bdnff /ui', $ligne)) {
262
					$donnees['correspondance'] = $this->supprimerHtml($ligne);
398
					$donnees['correspondance'] = $this->supprimerHtml($ligne);
263
				}
399
				}
264
				$i++;
400
				$i++;
265
			}
401
			}
266
			fclose($fichierOuvert);
402
			fclose($fichierOuvert);
267
		} else {
403
		} else {
268
			$this->messages->traiterErreur("Le fichier {$this->fichier} n'a pas pu être ouvert.");
404
			$this->messages->traiterErreur("Le fichier {$this->fichier} n'a pas pu être ouvert.");
269
		}
405
		}
270
		return $donnees;
406
		return $donnees;
271
	}
407
	}
272
 
408
 
273
	private function supprimerHtml($txt) {
409
	private function supprimerHtml($txt) {
274
		$txt = strip_tags($txt,'<b>,<i>');
410
		$txt = strip_tags($txt,'<b>,<i>');
275
		$txt = str_replace('&#173;', '', $txt);
411
		$txt = str_replace('&#173;', '', $txt);
276
		$txt = str_ireplace('&nbsp;', ' ', $txt);
412
		$txt = str_ireplace('&nbsp;', ' ', $txt);
277
		$txt = trim($txt);
413
		$txt = trim($txt);
278
		$txt = preg_replace('/^<\/I>/', '', $txt);
414
		$txt = preg_replace('/^<\/I>/', '', $txt);
279
		$txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
415
		$txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
280
		return $txt;
416
		return $txt;
281
	}
417
	}
282
 
418
 
283
	private function traiterLigneDescription($txt) {
419
	private function traiterLigneDescription($txt) {
284
		$txt = $this->supprimerHtml($txt);
420
		$txt = $this->supprimerHtml($txt);
285
		$txt = $txt."\n";
421
		$txt = $txt."\n";
286
		return $txt;
422
		return $txt;
287
	}
423
	}
288
 
424
 
289
	private function nettoyerDescription($txt) {
425
	private function nettoyerDescription($txt) {
290
		$txt = preg_replace("/\n{2,}+/", "\n", $txt);
426
		$txt = preg_replace("/\n{2,}+/", "\n", $txt);
291
		$txt = trim($txt);
427
		$txt = trim($txt);
292
		return $txt;
428
		return $txt;
293
	}
429
	}
294
 
430
 
295
	private function corrigerDescription($txt) {
431
	private function corrigerDescription($txt) {
296
		$txt = preg_replace("/– <I>([^.]+?)[.] – <\/I>/", "– <I>$1</I>. – ", $txt);
432
		$txt = preg_replace("/– <I>([^.]+?)[.] – <\/I>/", "– <I>$1</I>. – ", $txt);
297
		$txt = preg_replace("/– <I>([^.]+?)[.] – ([A-Z])<\/I>/", "– <I>$1</I>. – $2", $txt);
433
		$txt = preg_replace("/– <I>([^.]+?)[.] – ([A-Z])<\/I>/", "– <I>$1</I>. – $2", $txt);
298
		$txt = preg_replace("/– <I>([^.]+?) – <\/I>/", "– <I>$1</I>. – ", $txt);
434
		$txt = preg_replace("/– <I>([^.]+?) – <\/I>/", "– <I>$1</I>. – ", $txt);
299
		return $txt;
435
		return $txt;
300
	}
436
	}
301
 
437
 
302
	private function remplacerHtmlParSyntaxeWiki($txt) {
438
	private function remplacerHtmlParSyntaxeWiki($txt) {
303
		$txt = str_replace(array('<B>', '</B>'), '**', $txt);
439
		$txt = str_replace(array('<B>', '</B>'), '**', $txt);
304
		$txt = str_replace(array('<I>', '</I>'), '//', $txt);
440
		$txt = str_replace(array('<I>', '</I>'), '//', $txt);
305
		return $txt;
441
		return $txt;
306
	}
442
	}
-
 
443
 
-
 
444
	private function ajouterLogAnalyse($txt) {
-
 
445
		if (isset($this->log['analyse']) == false) {
-
 
446
			$this->log['analyse'] = '';
-
 
447
		}
-
 
448
		$this->log['analyse'] .= "$txt\n";
-
 
449
	}
-
 
450
 
-
 
451
	private function ajouterLogSyno($txt) {
-
 
452
		if (isset($this->log['synonymes']) == false) {
-
 
453
			$this->log['synonymes'] = '';
-
 
454
		}
-
 
455
		$this->log['synonymes'] .= "$txt\n";
-
 
456
	}
-
 
457
 
-
 
458
	private function ajouterLogAuteur($txt) {
-
 
459
		if (isset($this->log['auteurs']) == false) {
-
 
460
			$this->log['auteurs'] = '';
-
 
461
		}
-
 
462
		$this->log['auteurs'] .= "$txt\n";
-
 
463
	}
-
 
464
 
-
 
465
	private function ajouterLogProbleme($txt) {
-
 
466
		if (isset($this->log['problemes']) == false) {
-
 
467
			$this->log['problemes'] = '';
-
 
468
		}
-
 
469
		$this->log['problemes'] .= "$txt\n";
-
 
470
	}
-
 
471
 
-
 
472
	private function ecrireLogs() {
-
 
473
		foreach ($this->log as $nom => $log) {
-
 
474
			$fichier = $this->dossierBase.self::DOSSIER_LOG.$nom.'.log';
-
 
475
			file_put_contents($fichier, $log);
-
 
476
		}
-
 
477
	}
307
}
478
}
308
?>
479
?>