Subversion Repositories eFlore/Projets.eflore-projets

Rev

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