Subversion Repositories eFlore/Projets.eflore-projets

Rev

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