Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 965 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 965 Rev 1045
1
<?php
1
<?php
2
/** Exemple lancement:
2
/** Exemple lancement:
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php sauvages -a chargerTous
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php sauvages -a chargerTous
4
*/
4
*/
5
class Sauvages extends EfloreScript {
5
class Sauvages extends EfloreScript {
6
	private $contenu_fichier = array();
6
	private $contenu_fichier = array();
7
	protected $parametres_autorises = array(
7
	protected $parametres_autorises = array(
8
			'-f' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
8
			'-f' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
9
	private $caracteresAccentues = array(
9
	private $caracteresAccentues = array(
10
			'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î',
10
			'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î',
11
			'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß',
11
			'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß',
12
			'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î',
12
			'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î',
13
			'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ',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'Ǽ', 'ǽ', 'Ǿ', 'ǿ');
24
	private $caracteresNormaux = array(
24
	private $caracteresNormaux = array(
25
			'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I',
25
			'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I',
26
			'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's',
26
			'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's',
27
			'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i',
27
			'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i',
28
			'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a',
28
			'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a',
29
			'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd',
29
			'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd',
30
			'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g',
30
			'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g',
31
			'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i',
31
			'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i',
32
			'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l',
32
			'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l',
33
			'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R',
33
			'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R',
34
			'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't',
34
			'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't',
35
			'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y',
35
			'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y',
36
			'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I',
36
			'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I',
37
			'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
37
			'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
38
 
38
 
39
	public function executer() {
39
	public function executer() {
40
		// Lancement de l'action demandée
40
		// Lancement de l'action demandée
41
		try {
41
		try {
42
			$this->initialiserProjet('sauvages');
42
			$this->initialiserProjet('sauvages');
43
 
43
 
44
			$cmd = $this->getParametre('a');
44
			$cmd = $this->getParametre('a');
45
			switch ($cmd) {
45
			switch ($cmd) {
46
				case 'chargerTous' :
46
				case 'chargerTous' :
47
					$this->creerXmlTaxons();
47
					$this->creerXmlTaxons();
48
					$this->creerXmlCriteres();
48
					$this->creerXmlCriteres();
49
					break;
49
					break;
50
				case 'creerXmlCriteres' :
50
				case 'creerXmlCriteres' :
51
					$this->creerXmlCriteres();
51
					$this->creerXmlCriteres();
52
					break;
52
					break;
53
				case 'creerXmlTaxons' :
53
				case 'creerXmlTaxons' :
54
					$this->creerXmlTaxons();
54
					$this->creerXmlTaxons();
55
					break;
55
					break;
56
				default :
56
				default :
57
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
57
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
58
			}
58
			}
59
		} catch (Exception $e) {
59
		} catch (Exception $e) {
60
			$this->traiterErreur($e->getMessage());
60
			$this->traiterErreur($e->getMessage());
61
		}
61
		}
62
	}
62
	}
63
 
63
 
64
	public function creerXmlCriteres() {
64
	public function creerXmlCriteres() {
65
		$this->recupererContenuFichier();
65
		$this->recupererContenuFichier();
66
		$criteres_nom = $this->contenu_fichier[0];
66
		$criteres_nom = $this->contenu_fichier[0];
67
		unset($this->contenu_fichier[0]);		
67
		unset($this->contenu_fichier[0]);		
68
		$criteres_label = $this->contenu_fichier[1];
68
		$criteres_label = $this->contenu_fichier[1];
69
		unset($this->contenu_fichier[1]);
69
		unset($this->contenu_fichier[1]);
70
		$description_label = $this->contenu_fichier[2];
70
		$description_label = $this->contenu_fichier[2];
71
		unset($this->contenu_fichier[2]);
71
		unset($this->contenu_fichier[2]);
72
		$criteres_valeurs = $this->extraireValeurs();
72
		$criteres_valeurs = $this->extraireValeurs();
73
		$xml = $this->formerXmlCriteres($criteres_nom, $criteres_label, $description_label, $criteres_valeurs);//print_r($xml);
73
		$xml = $this->formerXmlCriteres($criteres_nom, $criteres_label, $description_label, $criteres_valeurs);//print_r($xml);
74
		file_put_contents("./xml_criteres.xml", $xml);
74
		file_put_contents("./xml_criteres.xml", $xml);
75
	}
75
	}
76
	
76
	
77
	
77
	
78
	public function formerXmlCriteres($nom, $label, $description, $valeurs) {
78
	public function formerXmlCriteres($nom, $label, $description, $valeurs) {
79
		$xml='<keys>
79
		$xml='<keys>
80
				<groupe id="1">
80
				<groupe id="1">
81
					<nom>Arbres</nom>
81
					<nom>Arbres</nom>
82
					<media>images/pictos/icone_arbre.png</media>
82
					<media>images/pictos/icone_arbre.png</media>
83
					<criteres>';
83
					<criteres>';
84
		foreach ($label as $id=>$criteres) {
84
		foreach ($label as $id=>$criteres) {
85
			if ($id > 2) {
85
			if ($id > 3) {
86
				$xml .= '<critere id="1.'.$id.'">
86
				$xml .= '<critere id="1.'.$id.'">
87
							<label>'.$criteres.'</label>
87
							<label>'.$criteres.'</label>
88
							<description>'.$description[$id].'</description>
88
							<description>'.$description[$id].'</description>
89
							<valeurs>';
89
							<valeurs>';
90
				foreach ($valeurs[$id] as $id_val=>$valeur) {
90
				foreach ($valeurs[$id] as $id_val=>$valeur) {
91
					if ($valeur != "0") {
91
					if ($valeur != "0") {
92
						$crit = str_replace(" ", "_", $this->supprimerAccents($nom[$id]));
92
						$crit = str_replace(" ", "_", $this->supprimerAccents($nom[$id]));
93
						$val = str_replace(" ", "_", $this->supprimerAccents($valeur));
93
						$val = str_replace(" ", "_", $this->supprimerAccents($valeur));
94
						$image = $crit."_".$val;
94
						$image = $crit."_".$val;
95
						$xml .= '<valeur code="1.'.$id.'.'.$id_val.'" media="images/pictos/'.$image.'.png">'.$valeur.'</valeur>';
95
						$xml .= '<valeur code="1.'.$id.'.'.$id_val.'" media="images/pictos/'.$image.'.png">'.$valeur.'</valeur>';
96
					}
96
					}
97
				}
97
				}
98
				$xml .= '</valeurs></critere>';
98
				$xml .= '</valeurs></critere>';
99
			}
99
			}
100
		}
100
		}
101
		$xml .= "</criteres>
101
		$xml .= "</criteres>
102
			</groupe>
102
			</groupe>
103
			</keys>";
103
			</keys>";
104
		return $xml;
104
		return $xml;
105
	}
105
	}
106
	
106
	
107
	private function extraireValeurs() {
107
	private function extraireValeurs() {
108
		$valeurs = array();
108
		$valeurs = array();
109
		foreach ($this->contenu_fichier as $ligne) {
109
		foreach ($this->contenu_fichier as $ligne) {
110
			$i = 0;
110
			$i = 0;
-
 
111
			if ($ligne != "") {
111
			foreach ($ligne as $critere) {
112
				foreach ($ligne as $critere) {
-
 
113
					$valeur_multiple = explode("/", $critere);
-
 
114
					foreach ($valeur_multiple as $valeur) {
112
				if (!isset($valeurs[$i]) || !in_array(trim($critere), $valeurs[$i])) {
115
						if (!isset($valeurs[$i]) || !in_array(trim($valeur), $valeurs[$i])) {
113
					$valeurs[$i][] = trim($critere);
116
							$valeurs[$i][] = trim($valeur);
-
 
117
						}
-
 
118
					}
-
 
119
					$i++;
114
				}
120
				}
115
				$i++;
-
 
116
			}
121
			}
117
		}
122
		}
118
		return $valeurs;
123
		return $valeurs;
119
	}
124
	}
120
	
125
	
121
	
126
	
122
	public function creerXmlTaxons() {
127
	public function creerXmlTaxons() {
123
		$this->recupererContenuFichier();
128
		$this->recupererContenuFichier();
124
		$criteres_label = array_flip($this->contenu_fichier[0]);
129
		$criteres_label = array_flip($this->contenu_fichier[0]);
125
		unset($this->contenu_fichier[0]);
130
		unset($this->contenu_fichier[0]);
126
		unset($this->contenu_fichier[1]);
131
		unset($this->contenu_fichier[1]);
127
		unset($this->contenu_fichier[2]);
132
		unset($this->contenu_fichier[2]);
128
		$criteres_valeurs = $this->extraireValeurs();
133
		$criteres_valeurs = $this->extraireValeurs();
129
		$xml = $this->formerXmlTaxons($criteres_label, $criteres_valeurs); //print_r($xml);
134
		$xml = $this->formerXmlTaxons($criteres_label, $criteres_valeurs); //print_r($xml);
130
		file_put_contents("./xml_taxons.xml", $xml);
135
		file_put_contents("./xml_taxons.xml", $xml);
131
	}
136
	}
132
	
137
	
133
	public function formerXmlTaxons($criteres_label, $criteres_valeurs) {
138
	public function formerXmlTaxons($criteres_label, $criteres_valeurs) {
134
		$xml='<?xml version="1.0" ?><TAXONS SUBJECT="XML">';
139
		$xml='<?xml version="1.0" ?><TAXONS SUBJECT="XML">';
135
		$rest = new RestClient();
140
		$rest = new RestClient();
136
		foreach ($this->contenu_fichier as $id=>$taxon) {
141
		foreach ($this->contenu_fichier as $id=>$taxon) {
-
 
142
			if ($taxon != "") {
137
			$infos_taxon = $this->rechercherInfosTaxon($rest, $taxon[$criteres_label["nom scientifique"]]);
143
				$infos_taxon = $this->rechercherInfosTaxon($rest, $taxon[$criteres_label["nom scientifique"]]);
138
			$xml .= '<TAXON id="'.$id.'" value="'.
144
				$xml .= '<TAXON id="'.$id.'" value="'.
139
				ucfirst($taxon[$criteres_label["nom vernaculaire"]]).'" sciName="'.trim($taxon[$criteres_label["nom scientifique"]]).'" groupe="1">
145
					ucfirst($taxon[$criteres_label["nom vernaculaire"]]).'" sciName="'.trim($taxon[$criteres_label["nom scientifique"]]).'" groupe="1">
140
			<DESCRIPTION>'.$infos_taxon["description"].'</DESCRIPTION>
146
				<DESCRIPTION>'.$infos_taxon["description"].'</DESCRIPTION>
141
			<PICTURES>';
147
				<PICTURES>';
142
			if ($infos_taxon["images"] != array()) {
148
				if ($infos_taxon["images"] != array()) {
-
 
149
					if (isset($infos_taxon["images"][$taxon[$criteres_label["image priorite"]]])) {
-
 
150
						$auteur = $this->rechercherInfosAuteurImage($infos_taxon["images"][$taxon[$criteres_label["image priorite"]]], $rest).", www.tela-botanica.org, CC-Licence (by-sa)";
-
 
151
						$xml .= '<PICTURE media="'.$infos_taxon["images"][$taxon[$criteres_label["image priorite"]]]["binaire.href"].'"><author>'.$auteur.'</author></PICTURE>';
-
 
152
						unset($infos_taxon["images"][$taxon[$criteres_label["image priorite"]]]);
-
 
153
					}
143
				foreach ($infos_taxon["images"] as $image) {
154
					foreach ($infos_taxon["images"] as $image) {
-
 
155
						$auteur = $this->rechercherInfosAuteurImage($image, $rest).", www.tela-botanica.org, CC-Licence (by-sa)";
144
					$xml .= '<PICTURE media="'.$image["binaire.href"].'"><author>'.$image["auteur.libelle"].'</author></PICTURE>';
156
						$xml .= '<PICTURE media="'.$image["binaire.href"].'"><author>'.$auteur.'</author></PICTURE>';
-
 
157
					}
145
				}
158
				}
146
			}
-
 
147
			$xml .= '</PICTURES><CRITERIAS>';
159
				$xml .= '</PICTURES><CRITERIAS>';
148
			for ($i=3; $i < count($taxon); $i++) {
160
				for ($i=4; $i < count($taxon); $i++) {
149
				if (trim($taxon[$i]) !== "0") {
161
					if (trim($taxon[$i]) !== "0") {
-
 
162
						$valeurs = array();
-
 
163
						foreach (explode("/",$taxon[$i]) as $valeur) {
150
					$xml .= '<VALUE code="1.'.$i.'.'.array_search(trim($taxon[$i]), $criteres_valeurs[$i]).'"/>';
164
							$valeurs[] = '1.'.$i.'.'.array_search(trim($valeur), $criteres_valeurs[$i]);
-
 
165
						}
-
 
166
						$xml .= '<VALUE code="'.implode(',', $valeurs).'"/>';
-
 
167
					}
151
				}
168
				}
-
 
169
				$xml .= '</CRITERIAS></TAXON>';
152
			}
170
			}
153
			$xml .= '</CRITERIAS></TAXON>';
-
 
154
		}
171
		}
155
		$xml .= "</TAXONS>";
172
		$xml .= "</TAXONS>";
156
		return $xml;
173
		return $xml;
157
	}
174
	}
-
 
175
	
-
 
176
	// interroge l'annuaire pour récupérer le pseudo
-
 
177
	private function rechercherInfosAuteurImage($image, $rest) {
-
 
178
		$courriel = $image["observation"]["auteur.courriel"];
-
 
179
		$url_auteur = "http://www.tela-botanica.org/service:annuaire:utilisateur/identite-par-courriel/".$courriel;
-
 
180
		$reponse = $rest->consulter($url_auteur);
-
 
181
		$reponse = json_decode($reponse, true);
-
 
182
		if (isset($reponse[$courriel]) && $reponse[$courriel]["pseudoUtilise"] == true) {
-
 
183
			$auteur = $reponse[$courriel]["pseudo"];
-
 
184
		} else{
-
 
185
			$auteur = $image["observation"]["auteur.prenom"]." ".$image["observation"]["auteur.nom"];
-
 
186
		}
-
 
187
		return $auteur;
-
 
188
	}
158
	
189
	
159
	private function rechercherInfosTaxon($rest, $ns) {
190
	private function rechercherInfosTaxon($rest, $ns) {
160
		$info = array("description" => "", "images" => array());
191
		$info = array("description" => "", "images" => array());
161
		
192
		
162
		$reponse = $this->consulterWebService($rest, "bdtfx", 
193
		$reponse = $this->consulterWebService($rest, "bdtfx", 
163
			"noms", "?retour.champs=nom_retenu.id,num_taxonomique&retour.tri=retenu&masque=".urlencode(trim($ns)));
194
			"noms", "?retour.champs=nom_retenu.id,num_taxonomique&retour.tri=retenu&masque=".urlencode(trim($ns)));
164
		if (is_array($reponse["resultat"])) {
195
		if (is_array($reponse["resultat"])) {
165
			$num_nom = $reponse["resultat"][key($reponse["resultat"])]["nom_retenu.id"];
196
			$num_nom = $reponse["resultat"][key($reponse["resultat"])]["nom_retenu.id"];
166
			$num_taxon = $reponse["resultat"][key($reponse["resultat"])]['num_taxonomique'];
197
			$num_taxon = $reponse["resultat"][key($reponse["resultat"])]['num_taxonomique'];
167
		}
198
		}
-
 
199
		
-
 
200
		$info["description"] = $this->rechercherDescription($num_taxon, $num_nom, $rest);		
-
 
201
		$info["images"] =array();
-
 
202
		$tags = array('fleur', 'feuille', 'rameau', 'port', 'fruit');
-
 
203
		$cles = array();
-
 
204
		foreach ($tags as $tag) {
-
 
205
			$url_image = "http://www.tela-botanica.org/service:del:0.1/images".
-
 
206
						"?masque.referentiel=bdtfx&masque.tag={$tag}&tri=votes&ordre=desc&protocole=3&navigation.limite=1&format=CRS&masque.nn=".$num_nom;
-
 
207
			$image = $rest->consulter($url_image);
-
 
208
			$image = json_decode($image, true);
-
 
209
			if ($image["resultats"] !== array()) {
-
 
210
				$info["images"][$tag]["binaire.href"] = $image["resultats"][key($image["resultats"])]["binaire.href"];
-
 
211
				$info["images"][$tag]["observation"] = $image["resultats"][key($image["resultats"])]["observation"];
-
 
212
				$cles[key($image["resultats"])] = "";
-
 
213
			}
-
 
214
		}
-
 
215
		
-
 
216
		//si image taguée absente
-
 
217
		$url_image = "http://www.tela-botanica.org/service:del:0.1/images".
-
 
218
									"?masque.referentiel=bdtfx&tri=votes&ordre=desc&protocole=3&navigation.limite=5&format=CRS&masque.nn=".$num_nom;
-
 
219
		$reponse = $rest->consulter($url_image);
-
 
220
		$reponse = json_decode($reponse, true); 
-
 
221
		
-
 
222
		if ($cles !== array()) {
-
 
223
			// supprimer les images communes au tag et les mieux notés
-
 
224
			$reponse["resultats"] = array_diff_key($reponse["resultats"], $cles);
-
 
225
			for ($i = 0; $i < 5-count($cles); $i++) {
-
 
226
				$image = array_shift($reponse["resultats"]);
-
 
227
				$info["images"][$i]["binaire.href"] = $image["binaire.href"];
-
 
228
				$info["images"][$i]["observation"] = $image["observation"];
-
 
229
			} 
-
 
230
		} else {
-
 
231
			$info["images"] = $reponse["resultats"];
-
 
232
		}
-
 
233
		
-
 
234
		return $info;
-
 
235
	}
-
 
236
	
-
 
237
	
-
 
238
	
168
		
239
	private function rechercherDescription($num_taxon, $num_nom, $rest) {
169
		$url_wiki = "http://www.tela-botanica.org/wikini/eFloreRedaction/api/rest/0.5/pages/SmartFloreBDTFXnt".
240
		$url_wiki = "http://www.tela-botanica.org/wikini/eFloreRedaction/api/rest/0.5/pages/SmartFloreBDTFXnt".
170
					$num_taxon."?txt.format=text/plain&txt.section.titre=Comment%20la%20reconnaître%20%3F";
241
		$num_taxon."?txt.format=text/plain&txt.section.titre=Comment%20la%20reconnaître%20%3F";
171
		$wiki = $rest->consulter($url_wiki); $wiki = json_decode($wiki, true);
242
		$wiki = $rest->consulter($url_wiki); $wiki = json_decode($wiki, true);
172
		if ($wiki['texte'] != ""){
243
		if ($wiki['texte'] != "" && strstr($wiki['texte'], 'Brève phrase sur son type biologique (simplifié)') === false ){
173
			$info["description"] = $this->formaterSmatflore($wiki['texte']);
244
			$description = $this->formaterSmartflore($wiki['texte']);
174
		} else {
245
		} else {
175
			$coste = $this->consulterWebService($rest, "coste", "textes", "/bdtfx.nn:".$num_nom);
246
			$coste = $this->consulterWebService($rest, "coste", "textes", "/bdtfx.nn:".$num_nom);
176
			if ($coste['resultats'] != array() && $coste['resultats'][key($coste["resultats"])]['texte'] != "") {
247
			if ($coste['resultats'] != array() && $coste['resultats'][key($coste["resultats"])]['texte'] != "") {
177
				$texte = str_replace("**", "", $coste['resultats'][key($coste["resultats"])]['texte']);
248
				$description = $this->formaterCoste($coste['resultats']);
178
				$texte = str_replace("//", "", $texte);
-
 
179
				$texte = explode(" – ", $texte);
-
 
180
				unset($texte[0],$texte[1]);
-
 
181
				if (strpos($texte[2], ";") === false) {unset($texte[2]);} //else {print_r($texte[2]);}
-
 
182
				
-
 
183
				if (preg_match('/^[\w|\s|\-|,]*\.*\s*$/', $texte[2]) == true) {print_r($texte[2]); unset($texte[2]);}
-
 
184
				$info["description"] = implode(" – ", $texte);
-
 
185
			} else {
249
			} else {
186
				$baseflor = $this->consulterWebService($rest, "baseflor", "informations", "/bdtfx.nn:".$num_nom);
250
				$baseflor = $this->consulterWebService($rest, "baseflor", "informations", "/bdtfx.nn:".$num_nom);
187
				$info["description"] = $this->formaterBaseflor($baseflor);
251
				$description = $this->formaterBaseflor($baseflor);
188
			}
252
			}
189
		}
253
		}
190
		if ($info["description"] == "En cours de rédaction.") {echo $num_nom." ".$ns;
-
 
191
			print_r($coste);
-
 
192
		}
-
 
193
		$reponse = $this->consulterWebService($rest, "cel", "images", "?navigation.limite=5&retour.format=CRS&masque.nn=".$num_nom);
-
 
194
		$info["images"] = $reponse["resultats"];
-
 
195
		return $info;
254
		return $description;
196
	}
255
	}
197
	
-
 
198
			
-
 
-
 
256
	
199
 
257
	//remplacement des \n par <br />
200
	private function formaterSmatflore($texte) {
258
	private function formaterSmartflore($texte) {
201
		$a_enlever = array(Chr(10).'=', '='.Chr(10), Chr(10).''.Chr(10));
259
		$a_enlever = array(Chr(10).'=', '='.Chr(10), Chr(10).''.Chr(10), '*');
202
		$texte = str_replace($a_enlever, '', $texte);
260
		$texte = str_replace($a_enlever, '', $texte);
-
 
261
		$texte = str_replace(Chr(10), '<br />', $texte);
-
 
262
		//supprimer image et lien
-
 
263
		$texte = preg_replace('/\[\[http\:\/\/upload.*\.jpg [a-z| |A-Z]*\]\]/', '', $texte);
-
 
264
		$texte = str_replace('[[http://fr.wikipedia.org/wiki/Corymbe corymbes]]', 'corymbes', $texte);
-
 
265
		$texte = preg_replace('/\[\[SmartFlore.*[0-9] (.*)\]\]/', '\1', $texte);
-
 
266
		$texte = preg_replace('/\[\[http\:\/\/www\..* (.*)\]\]/', '\1', $texte);
-
 
267
		$texte = trim(str_replace('<br /><br />', '<br />', $texte));
-
 
268
		return $texte;
-
 
269
	}
-
 
270
	
-
 
271
	
-
 
272
	private function formaterCoste($coste) {
203
		$texte = str_replace(Chr(10), '<br />', $texte);
273
		$texte = $this->mettreEnFormeCoste($coste[key($coste)]['texte']);
204
		return $texte;
274
		return $texte;
205
	}
275
	}
-
 
276
	
-
 
277
	static function mettreEnFormeCoste($texte) {
-
 
278
		$txt_fmt = array();
-
 
279
		//decouper elements remarquables avant le texte
-
 
280
		self::separerNomScientifique_a_NomCommun($texte, $txt_fmt);
-
 
281
		$txt_fmt = array();
-
 
282
		$texte = preg_replace('/\//','',$texte);
-
 
283
		//decouper elements remarquables  après le texte
-
 
284
		self::separerEcologie_a_Usages($texte, $txt_fmt);
-
 
285
		//le morceau qui reste est le gros de la description
-
 
286
		$texte = str_replace(';','</br> -','- '.$texte);
-
 
287
		$texte = str_replace('–','',$texte);
-
 
288
		$txt_fmt['description'] = $texte;
-
 
289
		$retour = "";
-
 
290
		foreach ($txt_fmt as $titre=>$parag) {
-
 
291
			$parag = preg_replace('/\- \.  /','- ',$parag);
-
 
292
			$parag = preg_replace('/\. \./','.',$parag);
-
 
293
			$retour .= ucfirst($titre)." : ".trim($parag)." ; ";
-
 
294
		}
-
 
295
		return $retour;
-
 
296
	}
-
 
297
	static function separerNomScientifique_a_NomCommun(&$txt, &$txt_fmt){
-
 
298
		if ( preg_match('/\*\*(.+)\*\*([^–]*)–/', $txt, $retour)){
-
 
299
			/* !! attention on enlève un tiret cadratin – pas un trait d'union - !! */
-
 
300
			$a_enlever  = array('/–/','/\./' );
-
 
301
			$txt_fmt['nom_scientifique'] = preg_replace($a_enlever,'',$retour[1]);
-
 
302
			if(preg_match('/\((.+)\)/',$retour[2],$synonymes)){
-
 
303
				$txt_fmt['synonymes'] = $synonymes[1];
-
 
304
			} else {
-
 
305
				$txt_fmt['nom_scientifique'] .=  $retour[2];
-
 
306
			}
-
 
307
			$txt = str_replace($retour[0],'',$txt);
-
 
308
		}
-
 
309
		/* !! attention il y a un espace avant les // du début !! */
-
 
310
		if ( preg_match('/^ \/\/([^\/\/]+)\/\//', $txt, $retour)){
-
 
311
			$a_enlever = array('/–/','/\./' );
-
 
312
			$txt_fmt['nom_commun'] = preg_replace($a_enlever,'',$retour[1]);
-
 
313
			$txt = str_replace($retour[0],'',$txt);
-
 
314
		}
-
 
315
	}
-
 
316
	
-
 
317
	
-
 
318
	static function separerEcologie_a_Usages(&$txt, &$txt_fmt) {
-
 
319
		if (preg_match('/\.\s*([A-ZÉÀÈ].+)$/',$txt, $retour)) {
-
 
320
			$txt_fmt['ecologie'] = $retour[1];
-
 
321
			$txt = str_replace($retour[0],'.',$txt);
-
 
322
			if (isset($txt_fmt['ecologie']) && preg_match('/–(.+)/', $txt_fmt['ecologie'] , $retour)){
-
 
323
				$txt_fmt['repartition'] = $retour[1];
-
 
324
				$txt_fmt['ecologie'] = str_replace($retour[0],'',$txt_fmt['ecologie']);
-
 
325
			}
-
 
326
			if (isset($txt_fmt['repartition']) &&  preg_match('/=(.+)$/', $txt_fmt['repartition'], $retour)){
-
 
327
				$txt_fmt['floraison'] = $retour[1];
-
 
328
				$txt_fmt['repartition'] = str_replace($retour[0],'',$txt_fmt['repartition']);
-
 
329
				$txt_fmt['repartition'] = str_replace(';',',',$txt_fmt['repartition']);
-
 
330
			}
-
 
331
			if (isset($txt_fmt['floraison']) &&  preg_match('/–(.+)$|\n(.+)$/',$txt_fmt['floraison'], $retour)){
-
 
332
				$txt_fmt['usages'] = isset($retour[1]) ? $retour[1] : $retour[2];
-
 
333
				$txt_fmt['floraison'] = str_replace($retour[0],'.',$txt_fmt['floraison']);
-
 
334
			}
-
 
335
			if (isset($txt_fmt['floraison']) &&  preg_match('/([Ff]l\.) (.+)/',$txt_fmt['floraison'], $retour)){
-
 
336
				$txt_fmt['floraison'] = $retour[2];
-
 
337
				$txt_fmt['floraison'] = str_replace($retour[1],'',$txt_fmt['floraison']);
-
 
338
			}
-
 
339
			if (isset($txt_fmt['floraison']) &&  preg_match('/([Ff]r\.) (.+)/',$txt_fmt['floraison'], $retour)){
-
 
340
				$txt_fmt['fructification'] = $retour[2];
-
 
341
				$txt_fmt['floraison'] = str_replace($retour[0],'',$txt_fmt['floraison']);
-
 
342
				$txt_fmt['floraison'] = str_replace(',','',$txt_fmt['floraison']);
-
 
343
				$txt_fmt['fructification'] = str_replace($retour[1],'',$txt_fmt['fructification']);
-
 
344
				$txt_fmt['fructification'] = str_replace('.','',$txt_fmt['fructification']);
-
 
345
			}
-
 
346
		}
-
 
347
	}
206
	
348
	
207
	private function formaterBaseflor($reponse) {
349
	private function formaterBaseflor($reponse) {
208
		$description = "En cours de rédaction.";
350
		$description = "En cours de rédaction.";
209
		if ($reponse["nom_sci"] != "") {
351
		if (isset($reponse["nom_sci"]) && $reponse["nom_sci"] != "") {
210
			$description = "Formation végétale : ".$reponse["form_vegetale"]."; ".
352
			$description = "Formation végétale : ".$reponse["form_vegetale"]."; ".
211
							"Inflorescence : ".$reponse["inflorescence"]."; ".
353
							"Inflorescence : ".$reponse["inflorescence"]."; ".
212
							"Couleur de la fleur : ".$reponse["couleur_fleur"]."; ".
354
							"Couleur de la fleur : ".$reponse["couleur_fleur"]."; ".
213
							"Sexualité : ".$reponse["sexualite"]."; ".
355
							"Sexualité : ".$reponse["sexualite"]."; ".
214
							"Fruit : ".$reponse["fruit"]."; ".
356
							"Fruit : ".$reponse["fruit"]."; ".
215
							"Pollinisation : ".$reponse["pollinisation"]."; ".
357
							"Pollinisation : ".$reponse["pollinisation"]."; ".
216
							"Dissémination : ".$reponse["dissemination"]."; ".
358
							"Dissémination : ".$reponse["dissemination"]."; ".
217
							"Ecologie : ".$reponse["carac_ecolo"]." – ".
359
							"Ecologie : ".$reponse["carac_ecolo"]." – ".
218
							"Chorologie : ".$reponse["chorologie"]."; ".
360
							"Chorologie : ".$reponse["chorologie"]."; ".
219
							"Floraison :  ".$reponse["floraison"];
361
							"Floraison :  ".$reponse["floraison"];
220
		}
362
		}
221
		return $description;
363
		return $description;
222
	}
364
	}
223
	
365
	
224
	private function consulterWebService($rest, $projet,$resssource,$parametres) {
366
	private function consulterWebService($rest, $projet,$resssource,$parametres) {
225
		$url_id = "http://www.tela-botanica.org/service:eflore:0.1/".$projet."/".$resssource.$parametres;
367
		$url_id = "http://api.tela-botanica.org/service:eflore:0.1/".$projet."/".$resssource.$parametres;
226
		$reponse = $rest->consulter($url_id);
368
		$reponse = $rest->consulter($url_id);
227
		return json_decode($reponse, true);
369
		return json_decode($reponse, true);
228
	}
370
	}
229
	
371
	
230
	
372
	
231
	private function recupererContenuFichier() {
373
	private function recupererContenuFichier() {
232
		$nomFichier = Config::get('dossierCsv').Config::get('projet');
374
		$nomFichier = Config::get('dossierCsv').Config::get('projet');
233
		if ($nomFichier && file_exists($nomFichier) ){
375
		if ($nomFichier && file_exists($nomFichier) ){
234
			$extensionFichier = strtolower(strrchr($nomFichier, '.'));
376
			$extensionFichier = strtolower(strrchr($nomFichier, '.'));
235
			if ($extensionFichier === ".csv"){
377
			if ($extensionFichier === ".csv"){
236
				$file = new SplFileObject($nomFichier);
378
				$file = new SplFileObject($nomFichier);
237
				$file->setFlags(SplFileObject::SKIP_EMPTY);
379
				$file->setFlags(SplFileObject::SKIP_EMPTY);
238
				$i = 0;
380
				$i = 0;
239
				echo "Traitement du fichier : ";
381
				echo "Traitement du fichier : ";
240
				while (!$file->eof()){
382
				while (!$file->eof()){
241
					$ligne_csv = $file->fgetcsv($delimiter = ';');
383
					$ligne_csv = $file->fgetcsv($delimiter = ';');
242
						$this->contenu_fichier[$i] = $ligne_csv;
384
						$this->contenu_fichier[$i] = $ligne_csv;
243
					echo str_repeat(chr(8), ( strlen( $i ) + 1 ))."\t".$i++;
385
					echo str_repeat(chr(8), ( strlen( $i ) + 1 ))."\t".$i++;
244
				}
386
				}
245
				echo "\n";
387
				echo "\n";
246
			} else {
388
			} else {
247
				$this->traiterErreur("Le fichier : $nomFichier n'est pas au format csv.");
389
				$this->traiterErreur("Le fichier : $nomFichier n'est pas au format csv.");
248
			}
390
			}
249
		} else {
391
		} else {
250
			$this->traiterErreur("Le fichier : $nomFichier n'existe pas.");
392
			$this->traiterErreur("Le fichier : $nomFichier n'existe pas.");
251
		}
393
		}
252
	}
394
	}
253
	
395
	
254
	public function supprimerAccents($chaine) {
396
	public function supprimerAccents($chaine) {
255
		return str_replace($this->caracteresAccentues, $this->caracteresNormaux, $chaine);
397
		return str_replace($this->caracteresAccentues, $this->caracteresNormaux, $chaine);
256
	}
398
	}
257
}
399
}
258
?>
400
?>