Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 471 | Go to most recent revision | Details | 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();
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',
36
		'bdnff_nn',
37
		'bdnff_nt');
38
 
39
	public function __construct(Conteneur $conteneur) {
40
		mb_internal_encoding('UTF-8');
41
		setlocale(LC_ALL, 'fr_FR.UTF-8');
42
		$this->conteneur = $conteneur;
43
		$this->outils = $conteneur->getOutils();
44
		$this->messages = $conteneur->getMessages();
45
		$this->dossierBase = dirname(__FILE__).'/';
46
	}
47
 
48
	public function fusionnerIndex() {
49
		$this->chargerIndexSp();
50
		$this->chargerIndexSupraSp();
51
		$this->initialiserTableauLigneIndexFinal();
52
		$this->creerIndexFinal();
53
		$this->ajouterChampsDansIndexFinal();
54
		$this->creerFichierCsvIndexFinal();
55
	}
56
 
57
	private function chargerIndexSp() {
58
		$spIndexFichier = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
59
		$index = $this->transformerTxtTsvEnTableau($spIndexFichier);
60
		$index = $this->reindexerParNumNomCoste($index);
61
		foreach ($index as $numNomCoste => $infos) {
62
			$numTaxSup = '';
63
			if ($infos['num_nom_coste'] == $infos['num_nom_retenu_coste']) {
64
				$numTaxSup = $infos['num_tax_sup_coste'];
65
			} else {
66
				$infosNomRetenu = $index[$infos['num_nom_retenu_coste']];
67
				$numTaxSup = $infosNomRetenu['num_tax_sup_coste'];
68
			}
69
			$this->spIndex[$numTaxSup][] = $infos;
70
		}
71
	}
72
 
73
	private function reindexerParNumNomCoste($index) {
74
		$nouvelIndex = array();
75
		foreach ($index as $infos) {
76
			$nouvelIndex[$infos['num_nom_coste']] = $infos;
77
		}
78
		return $nouvelIndex;
79
	}
80
 
81
	/**
82
	 * @link http://gist.github.com/385876
83
	 */
84
	private function transformerTxtTsvEnTableau($file = '', $delimiter = "\t") {
85
		$str = file_get_contents($file);
86
		$lines = explode("\n", $str);
87
		$field_names = explode($delimiter, array_shift($lines));
88
		foreach ($lines as $line) {
89
			// Skip the empty line
90
			if (empty($line)) continue;
91
			$fields = explode($delimiter, $line);
92
			$_res = array();
93
			foreach ($field_names as $key => $f) {
94
				$_res[$f] = isset($fields[$key]) ? $fields[$key] : '';
95
			}
96
			$res[] = $_res;
97
		}
98
		return $res;
99
	}
100
 
101
	private function chargerIndexSupraSp() {
102
		$infraSpIndexFichier = $this->dossierBase.self::DOSSIER_V0.'index_general.tsv';
103
		$this->supraSpIndex = $this->transformerTxtTsvEnTableau($infraSpIndexFichier);
104
		foreach ($this->supraSpIndex as $cle => $infos) {
105
			$this->supraSpIndex[$cle]['num_nom_retenu_coste'] = $infos['num_nom_coste'];
106
		}
107
	}
108
 
109
	private function initialiserTableauLigneIndexFinal() {
110
		$this->tableauParDefaut = array();
111
		foreach ($this->enteteFinal as $cle) {
112
			$this->tableauParDefaut[$cle] = '';
113
		}
114
	}
115
 
116
	private function creerIndexFinal() {
117
		foreach ($this->supraSpIndex as $infos) {
118
			$this->ajouterDansIndexFinal($infos);
119
			if (preg_match('/^G[0-9]+$/', $infos['num_nom_coste'])) {
120
				foreach ($this->spIndex[$infos['num_nom_coste']] as $infosSp) {
121
					$this->ajouterDansIndexFinal($infosSp);
122
				}
123
			}
124
		}
125
	}
126
 
127
	private function ajouterDansIndexFinal($infos) {
128
		$infos = array_merge($this->tableauParDefaut, $infos);
129
		$infos['num_nom'] = count($this->indexFinal);
130
		$this->indexFinal[$infos['num_nom_coste']] = $infos;
131
	}
132
 
133
	private function ajouterChampsDansIndexFinal() {
134
		foreach ($this->indexFinal as $nnc => $infos) {
135
			if ($infos['num_nom_coste'] == $infos['num_nom_retenu_coste']) {
136
				$infos['num_nom_retenu'] = $infos['num_nom'];
137
				if ($nnc != 'R') {
138
					$nomSuperieur = $this->indexFinal[$infos['num_tax_sup_coste']];
139
					$infos['num_tax_sup'] = $nomSuperieur['num_nom'];
140
				}
141
			} else {
142
				$nomRetenu = $this->indexFinal[$infos['num_nom_retenu_coste']];
143
				$infos['num_nom_retenu'] = $nomRetenu['num_nom'];
144
			}
145
			$this->indexFinal[$nnc] = $infos;
146
		}
147
	}
148
 
149
	private function creerFichierCsvIndexFinal() {
150
		$lignes = array();
151
		array_unshift($this->indexFinal, $this->enteteFinal);
152
		foreach ($this->indexFinal as $infos) {
153
			$lignes[] = implode("\t", $infos);
154
		}
155
 
156
		$txt = '';
157
		$txt = implode("\n", $lignes);
158
 
159
		$fichierTsvIndexFinal = $this->dossierBase.self::DOSSIER_V2.'index.tsv';
160
		file_put_contents($fichierTsvIndexFinal, $txt);
161
	}
162
 
163
 
164
}
165
?>