460 |
delphine |
1 |
<?php
|
|
|
2 |
// Encodage : UTF-8
|
|
|
3 |
// +-------------------------------------------------------------------------------------------------------------------+
|
|
|
4 |
/**
|
|
|
5 |
* Export des données de SOPHY
|
|
|
6 |
*
|
|
|
7 |
* Description : classe permettant d'exporter les tableaux phytosociologiques de la banque de données SOPHY
|
|
|
8 |
* Utilisation : php script.php export
|
|
|
9 |
*
|
|
|
10 |
* @category PHP 5.3
|
|
|
11 |
* @package phytosocio
|
|
|
12 |
//Auteur original :
|
|
|
13 |
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
|
|
|
14 |
* @copyright Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
|
|
|
15 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
|
|
|
16 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
|
|
|
17 |
* @version $Id$
|
|
|
18 |
*
|
|
|
19 |
* /opt/lampp/bin/php -d memory_limit=2048M cli.php sophy/export -a rien -c publication/strate/flore
|
|
|
20 |
* -f annee:sup:1990/auteur:like:ma%/departement:in:14,50,61,76,27/nomsci:like:trifolium%/fournier:neg: >log.txt
|
|
|
21 |
*
|
|
|
22 |
*
|
|
|
23 |
*
|
|
|
24 |
* export pour le crpf
|
|
|
25 |
*
|
|
|
26 |
*
|
|
|
27 |
* export pour les plantes de la liste rouge des suisses
|
|
|
28 |
* /opt/lampp/bin/php -d memory_limit=2048M cli.php sophy/export -a rien -f nomsci:in:"'Adenophora liliifolia (L.) A. DC.','Adenostyles leucophylla (Willd.) Rchb.','Allium angulosum L.','Allium rotundum L.','Anagallis minima (L.) E. H. L. Krause','Anagallis tenella (L.) L.','Anchusa officinalis L.','Andromeda polifolia L.','Androsace brevis (Hegetschw.) Ces.','Anemone baldensis L.','Anemone sylvestris L.','Anogramma leptophylla (L.) Link','Apium repens (Jacq.) Lag.','Aquilegia einseleana F. W. Schultz','Artemisia nivalis Braun-Blanq.','Asperugo procumbens L.','Asplenium adulterinum Milde','Asplenium billotii F. W. Schultz','Asplenium foreziense Magnier','Astragalus australis (L.) Lam.','Barbarea stricta Andrz.','Blackstonia acuminata (W. D. J. Koch & Ziz) Domin','Bromus grossus DC.','Bufonia paniculata Dubois','Campanula excisa Murith','Campanula latifolia L.','Cardamine kitaibelii Bech.','Cardamine matthioli Moretti','Carduus crispus L.','Carex baldensis L.','Carex fimbriata Schkuhr','Carex hartmanii Cajander','Carpesium cernuum L.','Chenopodium ficifolium Sm.','Chenopodium rubrum L.','Clematis alpina (L.) Mill.','Corydalis intermedia (L.) Mérat','Corydalis solida (L.) Clairv.','Crepis pygmaea L.','Crepis terglouensis (Hacq.) A. Kern.','Cuscuta europaea L.','Cytisus decumbens (Durande) Spach','Cytisus emeriflorus Rchb.','Cytisus nigricans L.','Cytisus scoparius (L.) Link','Deschampsia littoralis (Gaudin) Reut.','Dianthus gratianopolitanus Vill.','Dianthus seguieri Vill.','Diphasiastrum complanatum (L.) Holub','Diplotaxis muralis (L.) DC.','Draba fladnizensis Wulfen','Draba hoppeana Rchb.','Draba ladina Braun-Blanq.','Draba siliquosa M. Bieb.','Draba tomentosa Clairv.','Equisetum ramosissimum Desf.','Eriophorum gracile Roth','Erythronium dens-canis L.','Euonymus latifolius (L.) Mill.','Euphrasia christii Gremli','Falcaria vulgaris Bernh.','Fragaria viridis Duchesne','Gagea pratensis (Pers.) Dumort.','Galeopsis pubescens Besser','Gentiana engadinensis (Wettst.) Braun-Blanq. & Sam.','Geranium rivulare Vill.','Gladiolus imbricatus L.','Gladiolus italicus Mill.','Gladiolus palustris Gaudin','Gratiola officinalis L.','Hammarbya paludosa (L.) Kuntze','Helianthemum salicifolium (L.) Mill.','Iberis saxatilis L.','Inula britannica L.','Inula helvetica Weber','Inula spiraeifolia L.','Isopyrum thalictroides L.','Juniperus sabina L.','Knautia godetii Reut.','Lathyrus sphaericus Retz.','Leucanthemum halleri (Vitman) Ducommun','Leucojum aestivum L.','Linaria alpina subsp. petraea (Jord.) Rouy','Lindernia procumbens (Krock.) Borbás','Linnaea borealis L.','Littorella uniflora (L.) Asch.','Minuartia cherlerioides subsp. rionii (Gremli) Friedrich','Myosotis rehsteineri Wartm.','Myrrhis odorata (L.) Scop.','Nigella arvensis L.','Notholaena marantae (L.) Desv.','Ononis rotundifolia L.','Orchis laxiflora Lam.','Orchis papilionacea L.','Orchis provincialis DC.','Orchis spitzelii W. D. J. Koch','Ostrya carpinifolia Scop.','Pedicularis oederi Hornem.','Peucedanum verticillare (L.) Mert. & W. D. J. Koch','Phyteuma humile Gaudin','Pilularia globulifera L.','Pinguicula grandiflora Lam. s.str.','Polygonum minus Huds.','Potentilla alpicola Fauc.','Potentilla caulescens L.','Potentilla grammopetala Moretti','Potentilla inclinata Vill.','Primula daonensis (Leyb.) Leyb.','Primula latifolia Lapeyr.','Pulmonaria helvetica Bolliger','Ranunculus gramineus L.','Ranunculus parnassiifolius L.','Rhodiola rosea L.','Rhynchospora alba (L.) Vahl','Rumex nivalis Hegetschw.','Sagina nodosa (L.) Fenzl','Saponaria lutea L.','Saxifraga adscendens L.','Saxifraga aphylla Sternb.','Saxifraga diapensioides Bellardi','Saxifraga mutata L.','Scorzonera laciniata L. s.str.','Scutellaria alpina L.','Sedum anacampseros L.','Sedum cepaea L.','Sedum rubens L.','Senecio aquaticus Hill','Senecio halleri Dandy','Senecio incanus subsp. insubricus (Chenevard) Braun-Blanq.','Senecio paludosus L.','Silene pusilla Waldst. & Kit.','Silene suecica (Lodd.) Greuter & Burdet','Sisymbrium supinum L.','Teucrium scordium L.','Thlaspi rotundifolium subsp. corymbosum Gremli','Trifolium saxatile All.','Trochiscanthes nodiflora (All.) W. D. J. Koch','Typha minima Hoppe','Valeriana celtica L.','Valeriana supina Ard.','Viola cenisia L.','Viola elatior Fr.','Viola lutea Huds.','Viola persicifolia Schreb.','Viola pinnata L.','Viola pyrenaica DC.','Woodsia alpina (Bolton) Gray'"/departement:in:14,39,25,90,68,74
|
|
|
29 |
*
|
|
|
30 |
*/
|
|
|
31 |
// +-------------------------------------------------------------------------------------------------------------------+
|
|
|
32 |
class Export extends EfloreScript {
|
|
|
33 |
|
|
|
34 |
protected $tableauTaxon;
|
|
|
35 |
protected $dao;
|
|
|
36 |
protected $observations;
|
|
|
37 |
// Paramêtres autorisées lors de l'appel au script en ligne de commande
|
|
|
38 |
protected $parametres_autorises = array(
|
|
|
39 |
'-n' => array(false, 'export', 'Nom du fichier à créer'),
|
|
|
40 |
'-c' => array(false, 'tous', 'Liste des champs à récupérer'),
|
|
|
41 |
'-f' => array(false, true, 'Liste des filtres à executer'));
|
|
|
42 |
|
|
|
43 |
// à utiliser dans les valeurs des parametres pour séparer les champs ou filtres
|
|
|
44 |
protected $separateur = "/";
|
|
|
45 |
|
|
|
46 |
// correspondances parametres/champs de la bd
|
|
|
47 |
protected $champs_exportes = array(
|
|
|
48 |
"code_identifiant" => "CONCAT(o.so_id_publi,'.',o.so_id_tableau,'.',o.so_id_releve,'.',
|
|
|
49 |
o.so_num_ligne,'.',o.so_id_taxon,'.',o.so_id_strate) AS code_identifiant",
|
|
|
50 |
"numero_publication" => "sp_id_publi AS numero_publication",
|
|
|
51 |
"publication" => "CONCAT (sp_auteur, '. ', sp_revue, ' ', sp_volume, ' ', sp_tome, ' ', sp_fascicule, ', ', sp_date,
|
|
|
52 |
'. ', sp_titre, ', p.', sp_page_debut, '-', sp_page_fin, '.') AS publication",
|
|
|
53 |
"auteur" => "sp_auteur AS auteur",
|
|
|
54 |
"annee" => "sp_date AS annee",
|
|
|
55 |
"numero_tableau" => "sr_id_tableau AS numero_tableau",
|
|
|
56 |
"numero_releve" => "sr_id_releve AS numero_releve",
|
|
|
57 |
"nom_station" => "ss_localisation AS nom_station",
|
|
|
58 |
"code_insee" => "IF (ss_code_departement != 0,
|
|
|
59 |
CONCAT(ss_code_departement, ss_code_insee_commune), ss_code_insee_calculee) AS code_insee",
|
|
|
60 |
"code_insee_calcule" => "ss_code_insee_calculee AS code_insee_calcule",
|
|
|
61 |
"altitude" => "ss_altitude AS altitude",
|
|
|
62 |
"coordonnees_wgs" => "ss_latitude_wgs, ss_longitude_wgs",
|
|
|
63 |
"coordonnees_utm" => "ss_utmEasting, ss_utmNorthing, ss_utmZone",
|
|
|
64 |
"precision_geographique" => "ss_ce_precision_geographique",
|
|
|
65 |
"nom_scientifique" => "st_nom AS nom_scientifique",
|
|
|
66 |
"flore" => "st_ce_num_fournier, st_ce_num_floeur, st_ce_num_algues, st_ce_num_characees, st_ce_num_bryo,
|
|
|
67 |
st_ce_num_lichen, st_ce_num_syntri, st_ce_num_bdnff, st_ce_num_ciff, st_ce_num_codefr94",
|
|
|
68 |
"strate" => "so_id_strate AS strate",
|
|
|
69 |
"code_abondance" => "so_ce_abondance AS code_abondance");
|
|
|
70 |
protected $autresChamps = array(
|
|
|
71 |
"signification_precision" => "spg_num_precision, spg_valeur",
|
|
|
72 |
"signification_strate" => "so_id_strate",
|
|
|
73 |
"signification_abondance" => "sa_valeur");
|
|
|
74 |
|
|
|
75 |
protected $filtres_existants = array(
|
|
|
76 |
"annee" => "sp_date",
|
|
|
77 |
"auteur" => "sp_auteur",
|
|
|
78 |
"departement" => "",
|
|
|
79 |
"commune" => "",
|
|
|
80 |
"precisiongeo" => "ss_ce_precision_geographique",
|
|
|
81 |
"nomsci" => "st_nom",
|
|
|
82 |
"fournier" => "st_ce_num_fournier",
|
|
|
83 |
"floeur" => "st_ce_num_floeur",
|
|
|
84 |
"algues" => "st_ce_num_algues",
|
|
|
85 |
"characees" => "st_ce_num_characees",
|
|
|
86 |
"bryo" => "st_ce_num_bryo",
|
|
|
87 |
"lichen" => "st_ce_num_lichen",
|
|
|
88 |
"syntri" => "st_ce_num_syntri",
|
|
|
89 |
"bdnff" => "st_ce_num_bdnff",
|
|
|
90 |
"ciff" => "st_ce_num_ciff",
|
|
|
91 |
"codefr" => "st_ce_num_codefr94");
|
|
|
92 |
protected $operateurs = array(
|
|
|
93 |
"inf" => "<",
|
|
|
94 |
"sup" => ">",
|
|
|
95 |
"eg" => "=",
|
|
|
96 |
"neg" => "!=",
|
|
|
97 |
"infeg" => "<=",
|
|
|
98 |
"supeg" => ">=",
|
|
|
99 |
"in" => " IN ",
|
|
|
100 |
"like" => " LIKE ");
|
|
|
101 |
// +-------------------------------------------------------------------------------------------------------------------+
|
|
|
102 |
public function executer() {
|
|
|
103 |
include_once dirname(__FILE__).'/bibliotheque/Dao.php';
|
|
|
104 |
Config::charger(dirname(__FILE__).'/sophy.ini');
|
|
|
105 |
|
|
|
106 |
$this->dao = new Dao();
|
|
|
107 |
// Récupération de paramétres
|
|
|
108 |
$requete['nomFichier'] = $this->getParametre('n');
|
|
|
109 |
$requete['champs'] = $this->formaterChamps($this->getParametre('c'));
|
|
|
110 |
$requete['filtres'] = $this->formaterFiltres($this->getParametre('f'));
|
|
|
111 |
$donnees = $this->recupererDonnees($requete);
|
|
|
112 |
if ($donnees === false) {
|
|
|
113 |
$info = "Pas de données";
|
|
|
114 |
} else {
|
|
|
115 |
$titre = $this->formaterTitre($this->getParametre('c')); //à revoir
|
|
|
116 |
$this->exportCSV($requete['nomFichier'], $titre, $donnees);
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
protected function formaterFiltres($filtres) {
|
|
|
121 |
$where = array();
|
|
|
122 |
if ($filtres != '') {
|
|
|
123 |
$liste_filtres = explode($this->separateur, $filtres);
|
|
|
124 |
foreach ($liste_filtres as $filtre) {
|
|
|
125 |
$morceaux_filtre = explode(':', $filtre);
|
|
|
126 |
if (isset($this->filtres_existants[$morceaux_filtre[0]])) {
|
|
|
127 |
if (isset($this->operateurs[$morceaux_filtre[1]])) {
|
|
|
128 |
$where[] = $this->traiterFiltres($morceaux_filtre);
|
|
|
129 |
} else {
|
|
|
130 |
echo "L'operateur demandé {$morceaux_filtre[1]} n'existe pas. Les opérateurs existants sont :\n".
|
|
|
131 |
implode(', ', array_keys($this->operateurs));
|
|
|
132 |
}
|
|
|
133 |
} else {
|
|
|
134 |
echo "Le filtre demandé {$morceaux_filtre[0]} n'existe pas. Les filtres existants sont :\n".
|
|
|
135 |
implode(', ', array_keys($this->filtres_existants));
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
$where = ' WHERE '.implode(' AND ', $where);
|
|
|
139 |
}
|
|
|
140 |
return $where;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
protected function traiterFiltres($morceaux) {
|
|
|
144 |
$where = '';
|
|
|
145 |
if ($this->operateurs[$morceaux[1]] == ' IN ') {
|
|
|
146 |
$where = $this->operateurs[$morceaux[1]].'('.$morceaux[2].')';
|
|
|
147 |
} else {
|
|
|
148 |
$where= $this->operateurs[$morceaux[1]].$this->getBdd()->proteger($morceaux[2]);
|
|
|
149 |
}
|
|
|
150 |
switch ($morceaux[0]) {
|
|
|
151 |
case 'departement' :
|
|
|
152 |
$where = "( ss_code_departement".$where.
|
|
|
153 |
" OR substring( `ss_code_insee_calculee`, -5, 2 ) ".$where.") ";
|
|
|
154 |
break;
|
|
|
155 |
case 'commune' :
|
|
|
156 |
$where = "( CONCAT(ss_code_departement, ss_code_insee_commune) ".$where.
|
|
|
157 |
" OR `ss_code_insee_calculee` ".$where.") ";
|
|
|
158 |
break;
|
|
|
159 |
default : $where= $this->filtres_existants[$morceaux[0]].$where;
|
|
|
160 |
break;
|
|
|
161 |
}
|
|
|
162 |
return $where;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
protected function formaterChamps($champs_demandes) {
|
|
|
166 |
$champs_demandes = explode($this->separateur, $champs_demandes);
|
|
|
167 |
if ($champs_demandes[0] == 'tous') {
|
|
|
168 |
$champs = implode(', ', array_values($this->champs_exportes));
|
|
|
169 |
} else {
|
|
|
170 |
foreach ($champs_demandes as $champ) {
|
|
|
171 |
if (isset($this->champs_exportes[$champ])) {
|
|
|
172 |
$champs[] = $this->champs_exportes[$champ];
|
|
|
173 |
} else {
|
|
|
174 |
echo "Le champ demandé {$champ} n'existe pas. Les champs existants sont :\n".
|
|
|
175 |
implode(', ', array_keys($this->champs_exportes));
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
$champs = implode(', ', $champs);
|
|
|
179 |
}
|
|
|
180 |
return $champs;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
protected function formaterTitre($champs) {
|
|
|
184 |
$liste_champs = ($champs == 'tous') ? array_keys($this->champs_exportes) :explode(',', $champs);
|
|
|
185 |
foreach ($liste_champs as $champs) {
|
|
|
186 |
switch ($champs) {
|
|
|
187 |
case "flore" : $titre[] = "fournier"; $titre[] = "floeur"; $titre[] = "algues"; $titre[] = "characees";
|
|
|
188 |
$titre[] = "bryo"; $titre[] = "lichen"; $titre[] = "syntri"; $titre[] = "bdnff"; $titre[] = "ciff";
|
|
|
189 |
$titre[] = "code france 94"; break;
|
|
|
190 |
case "coordonnees_wgs": $titre[] = "latitude (wgs)"; $titre[] = "longitude (wgs)"; break;
|
|
|
191 |
case "coordonnees_utm" :
|
|
|
192 |
$titre[] = "Easting (utm)"; $titre[] = "Northing (utm)"; $titre[] = "zone utm"; break;
|
|
|
193 |
default: $titre[] = $champs; break;
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
return $titre;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
// Requête de création et d'insertion sur table sophy_tapir
|
|
|
200 |
public function recupererDonnees($parametre) {
|
|
|
201 |
$bdd = new Bdd();
|
|
|
202 |
$requete = "SELECT {$parametre['champs']}
|
|
|
203 |
FROM sophy_observation o LEFT JOIN sophy_taxon t ON (o.so_id_taxon = t.st_id_taxon)
|
|
|
204 |
LEFT JOIN sophy_releve r ON (r.sr_id_publi = o.so_id_publi AND r.sr_id_tableau = o.so_id_tableau AND r.sr_id_releve = o.so_id_releve )
|
|
|
205 |
LEFT JOIN sophy_station s ON (r.sr_id_station = s.ss_id_station)
|
|
|
206 |
LEFT JOIN sophy_publication p ON (r.sr_id_publi = p.sp_id_publi)
|
|
|
207 |
{$parametre['filtres']}";
|
|
|
208 |
//INTO OUTFILE '/home/delphine/web/eflore-projets/scripts/{$parametre['nomFichier']}.csv';";
|
|
|
209 |
echo $requete;
|
|
|
210 |
$reponse = $bdd->recupererTous($requete);
|
|
|
211 |
return $reponse;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
function exportCSV($nomFichier, $titre, $data) {
|
|
|
215 |
$outstream = fopen("./{$nomFichier}.csv", 'w');
|
|
|
216 |
fputcsv($outstream, $titre, ';', '"');
|
|
|
217 |
function __outputCSV(&$vals, $key, $filehandler) {
|
|
|
218 |
fputcsv($filehandler, $vals, ';', '"'); //\t = chr(9)
|
|
|
219 |
}
|
|
|
220 |
array_walk($data, '__outputCSV', $outstream);
|
|
|
221 |
fclose($outstream);
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
?>
|