Rev 471 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
class Index {
const DOSSIER_V0 = '../../../donnees/coste/0.00/';
const DOSSIER_V2 = '../../../donnees/coste/2.00/';
const DOSSIER_LOG = 'log/';
private $conteneur = null;
private $outils = null;
private $messages = null;
private $dossierBase = '';
private $spIndex = array();
private $supraSpIndex = array();
private $indexFinal = array();
private $tableauParDefaut = array();
private $enteteFinal = array(
'num_nom',
'num_nom_retenu',
'num_tax_sup',
'rang',
'nom_sci',
'auteur',
'nom_addendum',
'annee',
'biblio_origine',
'nom_francais',
'nom_coste',
'auteur_coste',
'biblio_coste',
'num_nom_coste',
'num_nom_retenu_coste',
'num_tax_sup_coste',
'synonymie_coste',
'tome',
'page',
'bdnff_nn',
'bdnff_nt');
public function __construct(Conteneur $conteneur) {
mb_internal_encoding('UTF-8');
setlocale(LC_ALL, 'fr_FR.UTF-8');
$this->conteneur = $conteneur;
$this->outils = $conteneur->getOutils();
$this->messages = $conteneur->getMessages();
$this->dossierBase = dirname(__FILE__).'/';
}
public function fusionnerIndex() {
$this->chargerIndexSp();
$this->chargerIndexSupraSp();
$this->initialiserTableauLigneIndexFinal();
$this->creerIndexFinal();
$this->ajouterChampsDansIndexFinal();
$this->creerFichierCsvIndexFinal();
}
private function chargerIndexSp() {
$spIndexFichier = $this->dossierBase.self::DOSSIER_V0.'index_general_sp.tsv';
$index = $this->transformerTxtTsvEnTableau($spIndexFichier);
$index = $this->reindexerParNumNomCoste($index);
foreach ($index as $numNomCoste => $infos) {
$numTaxSup = '';
if ($infos['num_nom_coste'] == $infos['num_nom_retenu_coste']) {
$numTaxSup = $infos['num_tax_sup_coste'];
} else {
$infosNomRetenu = $index[$infos['num_nom_retenu_coste']];
$numTaxSup = $infosNomRetenu['num_tax_sup_coste'];
}
$this->spIndex[$numTaxSup][] = $infos;
}
}
private function reindexerParNumNomCoste($index) {
$nouvelIndex = array();
foreach ($index as $infos) {
$nouvelIndex[$infos['num_nom_coste']] = $infos;
}
return $nouvelIndex;
}
/**
* @link http://gist.github.com/385876
*/
private function transformerTxtTsvEnTableau($file = '', $delimiter = "\t") {
$str = file_get_contents($file);
$lines = explode("\n", $str);
$field_names = explode($delimiter, array_shift($lines));
foreach ($lines as $line) {
// Skip the empty line
if (empty($line)) continue;
$fields = explode($delimiter, $line);
$_res = array();
foreach ($field_names as $key => $f) {
$_res[$f] = isset($fields[$key]) ? $fields[$key] : '';
}
$res[] = $_res;
}
return $res;
}
private function chargerIndexSupraSp() {
$infraSpIndexFichier = $this->dossierBase.self::DOSSIER_V0.'index_general.tsv';
$this->supraSpIndex = $this->transformerTxtTsvEnTableau($infraSpIndexFichier);
foreach ($this->supraSpIndex as $cle => $infos) {
$this->supraSpIndex[$cle]['num_nom_retenu_coste'] = $infos['num_nom_coste'];
}
}
private function initialiserTableauLigneIndexFinal() {
$this->tableauParDefaut = array();
foreach ($this->enteteFinal as $cle) {
$this->tableauParDefaut[$cle] = '';
}
}
private function creerIndexFinal() {
foreach ($this->supraSpIndex as $infos) {
$this->ajouterDansIndexFinal($infos);
if (preg_match('/^G[0-9]+$/', $infos['num_nom_coste'])) {
foreach ($this->spIndex[$infos['num_nom_coste']] as $infosSp) {
$this->ajouterDansIndexFinal($infosSp);
}
}
}
}
private function ajouterDansIndexFinal($infos) {
$infos = array_merge($this->tableauParDefaut, $infos);
$infos['num_nom'] = count($this->indexFinal);
$this->indexFinal[$infos['num_nom_coste']] = $infos;
}
private function ajouterChampsDansIndexFinal() {
foreach ($this->indexFinal as $nnc => $infos) {
if ($infos['num_nom_coste'] == $infos['num_nom_retenu_coste']) {
$infos['num_nom_retenu'] = $infos['num_nom'];
if ($nnc != 'R') {
$nomSuperieur = $this->indexFinal[$infos['num_tax_sup_coste']];
$infos['num_tax_sup'] = $nomSuperieur['num_nom'];
}
} else {
$nomRetenu = $this->indexFinal[$infos['num_nom_retenu_coste']];
$infos['num_nom_retenu'] = $nomRetenu['num_nom'];
}
$this->indexFinal[$nnc] = $infos;
}
}
private function creerFichierCsvIndexFinal() {
$lignes = array();
array_unshift($this->indexFinal, $this->enteteFinal);
foreach ($this->indexFinal as $infos) {
$lignes[] = implode("\t", $infos);
}
$txt = '';
$txt = implode("\n", $lignes);
$fichierTsvIndexFinal = $this->dossierBase.self::DOSSIER_V2.'index.tsv';
file_put_contents($fichierTsvIndexFinal, $txt);
}
}
?>