Subversion Repositories eFlore/Projets.eflore-projets

Rev

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