Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
469 jpm 1
<?php
2
class Index {
3
 
4
	const DOSSIER_V0 = '../../../donnees/coste/0.00/';
5
	const DOSSIER_V2 = '../../../donnees/coste/2.00/';
6
 
7
	private $conteneur = null;
8
	private $outils = null;
9
	private $messages = null;
10
	private $dossierBase = '';
11
	private $spIndex = array();
12
	private $supraSpIndex = array();
13
	private $indexFinal = array();
14
	private $tableauParDefaut = array();
471 jpm 15
	private $nbreTaxonInf = array();
469 jpm 16
	private $enteteFinal = array(
17
		'num_nom',
18
		'num_nom_retenu',
19
		'num_tax_sup',
20
		'rang',
21
		'nom_sci',
22
		'auteur',
23
		'nom_addendum',
24
		'annee',
25
		'biblio_origine',
26
		'nom_francais',
27
		'nom_coste',
28
		'auteur_coste',
29
		'biblio_coste',
30
		'num_nom_coste',
31
		'num_nom_retenu_coste',
32
		'num_tax_sup_coste',
33
		'synonymie_coste',
34
		'tome',
35
		'page',
471 jpm 36
		'nbre_taxons',
469 jpm 37
		'bdnff_nn',
471 jpm 38
		'bdnff_nt',
39
		'image',
40
		'page_wiki_dsc',
41
		'page_wiki_cle');
469 jpm 42
 
43
	public function __construct(Conteneur $conteneur) {
44
		mb_internal_encoding('UTF-8');
45
		setlocale(LC_ALL, 'fr_FR.UTF-8');
46
		$this->conteneur = $conteneur;
47
		$this->outils = $conteneur->getOutils();
48
		$this->messages = $conteneur->getMessages();
49
		$this->dossierBase = dirname(__FILE__).'/';
50
	}
51
 
52
	public function fusionnerIndex() {
53
		$this->chargerIndexSp();
54
		$this->chargerIndexSupraSp();
55
		$this->initialiserTableauLigneIndexFinal();
56
		$this->creerIndexFinal();
57
		$this->ajouterChampsDansIndexFinal();
58
		$this->creerFichierCsvIndexFinal();
59
	}
60
 
61
	private function chargerIndexSp() {
62
		$spIndexFichier = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
476 jpm 63
		$index = $this->outils->transformerTxtTsvEnTableau($spIndexFichier);
469 jpm 64
		$index = $this->reindexerParNumNomCoste($index);
65
		foreach ($index as $numNomCoste => $infos) {
66
			$numTaxSup = '';
67
			if ($infos['num_nom_coste'] == $infos['num_nom_retenu_coste']) {
68
				$numTaxSup = $infos['num_tax_sup_coste'];
69
			} else {
70
				$infosNomRetenu = $index[$infos['num_nom_retenu_coste']];
71
				$numTaxSup = $infosNomRetenu['num_tax_sup_coste'];
72
			}
73
			$this->spIndex[$numTaxSup][] = $infos;
74
		}
75
	}
76
 
77
	private function reindexerParNumNomCoste($index) {
78
		$nouvelIndex = array();
79
		foreach ($index as $infos) {
80
			$nouvelIndex[$infos['num_nom_coste']] = $infos;
81
		}
82
		return $nouvelIndex;
83
	}
84
 
85
	private function chargerIndexSupraSp() {
86
		$infraSpIndexFichier = $this->dossierBase.self::DOSSIER_V0.'index_general.tsv';
476 jpm 87
		$this->supraSpIndex = $this->outils->transformerTxtTsvEnTableau($infraSpIndexFichier);
469 jpm 88
		foreach ($this->supraSpIndex as $cle => $infos) {
89
			$this->supraSpIndex[$cle]['num_nom_retenu_coste'] = $infos['num_nom_coste'];
90
		}
91
	}
92
 
93
	private function initialiserTableauLigneIndexFinal() {
94
		$this->tableauParDefaut = array();
95
		foreach ($this->enteteFinal as $cle) {
96
			$this->tableauParDefaut[$cle] = '';
97
		}
98
	}
99
 
100
	private function creerIndexFinal() {
101
		foreach ($this->supraSpIndex as $infos) {
102
			$this->ajouterDansIndexFinal($infos);
103
			if (preg_match('/^G[0-9]+$/', $infos['num_nom_coste'])) {
104
				foreach ($this->spIndex[$infos['num_nom_coste']] as $infosSp) {
105
					$this->ajouterDansIndexFinal($infosSp);
106
				}
107
			}
108
		}
109
	}
110
 
111
	private function ajouterDansIndexFinal($infos) {
112
		$infos = array_merge($this->tableauParDefaut, $infos);
471 jpm 113
		$infos['num_nom'] = (count($this->indexFinal) + 1);
469 jpm 114
		$this->indexFinal[$infos['num_nom_coste']] = $infos;
115
	}
116
 
117
	private function ajouterChampsDansIndexFinal() {
471 jpm 118
		$this->genererNbreTaxons();
469 jpm 119
		foreach ($this->indexFinal as $nnc => $infos) {
120
			if ($infos['num_nom_coste'] == $infos['num_nom_retenu_coste']) {
121
				$infos['num_nom_retenu'] = $infos['num_nom'];
122
				if ($nnc != 'R') {
123
					$nomSuperieur = $this->indexFinal[$infos['num_tax_sup_coste']];
124
					$infos['num_tax_sup'] = $nomSuperieur['num_nom'];
125
				}
471 jpm 126
				$nomRetenu = $infos;
469 jpm 127
			} else {
128
				$nomRetenu = $this->indexFinal[$infos['num_nom_retenu_coste']];
129
				$infos['num_nom_retenu'] = $nomRetenu['num_nom'];
130
			}
471 jpm 131
			$infos['image'] = $this->obtenirNomFichierImg($nomRetenu);
132
			$infos['nbre_taxons'] = $this->obtenirNbreTaxon($infos);
476 jpm 133
			$nomRetenu['nbre_taxons'] = $infos['nbre_taxons'];
471 jpm 134
			$infos['page_wiki_dsc'] = $this->genererPageWikiDsc($nomRetenu);
135
			$infos['page_wiki_cle'] = $this->genererPageWikiCle($nomRetenu);
136
 
469 jpm 137
			$this->indexFinal[$nnc] = $infos;
138
		}
139
	}
140
 
471 jpm 141
	private function genererNbreTaxons() {
142
		foreach ($this->indexFinal as $infos) {
143
			if ($infos['num_tax_sup_coste'] != '') {
144
				if (isset($this->nbreTaxonInf[$infos['num_tax_sup_coste']])) {
145
					$this->nbreTaxonInf[$infos['num_tax_sup_coste']] += 1;
146
				} else {
147
					$this->nbreTaxonInf[$infos['num_tax_sup_coste']] = 1;
148
				}
149
			}
150
		}
151
	}
152
 
153
	private function genererPageWikiDsc($infos) {
154
		$prefixe = $this->genererPrefixePage($infos);
155
		$nomSci = str_replace(' ', '', ucwords(strtolower($infos['nom_sci'])));
156
		$pageWiki = $prefixe.$nomSci;
157
		return $pageWiki;
158
	}
159
 
160
	private function genererPageWikiCle($infos) {
161
		$pageWiki = '';
162
		if ($infos['nbre_taxons'] > 1) {
163
			$prefixe = $this->genererPrefixePage($infos);
164
			if ($infos['rang'] == '180') {
165
				$pageWiki = $prefixe.'TabGen';
166
			} else if ($infos['rang'] == '220') {
167
				$pageWiki = $prefixe.'TabSp';
168
			}
169
		}
170
		return $pageWiki;
171
	}
172
 
173
	private function genererPrefixePage($infos) {
174
		$prefixe = '';
175
		$num = preg_replace('/^[a-z]*([0-9.]+)$/i', '$1', $infos['num_nom_coste']);
176
		if (preg_match('/^([0-9]+)[.][0-9]+$/i', $infos['num_nom_coste'], $match)) {
177
			$num = sprintf('%04s', $match[1]);
178
		} else if ($infos['rang'] < 80 ) {
179
			$num = sprintf('%02s', $num);
180
		} else if ($infos['rang'] < 290 ) {
181
			$num = sprintf('%03s', $num);
182
		} else {
183
			$num = sprintf('%04s', $num);
184
		}
185
		$rangsTxt = array('20' => 'Reg', '40' => 'Emb', '80' => 'Cla', '180' => 'Fam', '220' => 'Gen', '290' => 'Esp', '340' => 'Var');
186
		$rang = $rangsTxt[$infos['rang']];
187
 
188
		$prefixe = $rang.$num;
189
		return $prefixe;
190
	}
191
 
192
	private function obtenirNbreTaxon($infos) {
193
		$nbre = '';
194
		if ($infos['rang'] == 180 || $infos['rang'] == 220) {
195
			if (isset($this->nbreTaxonInf[$infos['num_nom_coste']])) {
196
				$nbre = $this->nbreTaxonInf[$infos['num_nom_coste']];
197
			}
198
		}
199
		return $nbre;
200
	}
201
 
202
	private function obtenirNomFichierImg($infos) {
203
		$img = '';
204
		if ($infos['rang'] == '290' && ($infos['num_nom'] == $infos['num_nom_retenu'])) {
205
			$img = $infos['num_nom_coste'].'.png';
206
		}
207
		return $img;
208
	}
209
 
469 jpm 210
	private function creerFichierCsvIndexFinal() {
211
		$lignes = array();
212
		array_unshift($this->indexFinal, $this->enteteFinal);
213
		foreach ($this->indexFinal as $infos) {
214
			$lignes[] = implode("\t", $infos);
215
		}
216
 
217
		$txt = '';
218
		$txt = implode("\n", $lignes);
219
 
478 jpm 220
		$fichierTsvIndexFinal = $this->dossierBase.self::DOSSIER_V2.'coste_v2_00.tsv';
469 jpm 221
		file_put_contents($fichierTsvIndexFinal, $txt);
222
	}
223
}
224
?>