Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev 122 Rev 1124
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * EfloreScript est une classe abstraite qui doit être implémenté par les classes éxecutant des scripts
4
 * EfloreScript est une classe abstraite qui doit être implémenté par les classes éxecutant des scripts
5
 * en ligne de commande pour les projets d'eFlore.
5
 * en ligne de commande pour les projets d'eFlore.
6
 *
6
 *
7
 * @category	PHP 5.2
7
 * @category	PHP 5.2
8
 * @package		Eflore/Scripts
8
 * @package		Eflore/Scripts
9
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
10
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
10
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
11
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
11
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
12
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
12
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
13
 * @since 		0.3
13
 * @since 		0.3
14
 * @version		$Id$
14
 * @version		$Id$
15
 * @link		/doc/framework/
15
 * @link		/doc/framework/
16
 */
16
 */
17
abstract class EfloreScript extends Script {
17
abstract class EfloreScript extends Script {
18
 
18
 
19
	private $Bdd = null;
19
	private $Bdd = null;
20
	private $projetNom = null;
20
	private $projetNom = null;
-
 
21
	protected $conteneur; // mélange cracra, n'était pas utilisé jusqu'à présent (2014-09-29)
-
 
22
 
-
 
23
	public function __construct($script_nom, $parametres_cli) {
-
 
24
		parent::__construct($script_nom, $parametres_cli);
-
 
25
		$this->conteneur = new Conteneur();
-
 
26
	}
21
 
27
 
22
	public function getProjetNom() {
28
	public function getProjetNom() {
23
		return $this->projetNom;
29
		return $this->projetNom;
24
	}
30
	}
25
 
31
 
26
	protected function initialiserProjet($projetNom) {
32
	protected function initialiserProjet($projetNom) {
27
		$this->projetNom = $projetNom;
33
		$this->projetNom = $projetNom;
28
		$this->chargerConfigDuProjet();
34
		$this->chargerConfigDuProjet();
29
	}
35
	}
30
 
36
 
31
	//+------------------------------------------------------------------------------------------------------+
37
	//+------------------------------------------------------------------------------------------------------+
32
	// Méthodes d'accès aux objets du Framework
38
	// Méthodes d'accès aux objets du Framework
33
	/**
39
	/**
34
	* Méthode de connection à la base de données sur demande.
40
	* Méthode de connection à la base de données sur demande.
35
	* Tous les scripts n'ont pas besoin de s'y connecter.
41
	* Tous les scripts n'ont pas besoin de s'y connecter.
36
	*/
42
	*/
37
	protected function getBdd() {
43
	protected function getBdd() {
38
		if (! isset($this->Bdd)) {
44
		if (! isset($this->Bdd)) {
39
			$this->Bdd = new Bdd();
45
			$this->Bdd = new Bdd();
40
		}
46
		}
41
		return $this->Bdd;
47
		return $this->Bdd;
42
	}
48
	}
43
 
49
 
44
	//+------------------------------------------------------------------------------------------------------+
50
	//+------------------------------------------------------------------------------------------------------+
45
	// Méthodes communes aux projets d'eFlore
51
	// Méthodes communes aux projets d'eFlore
46
 
52
 
47
	protected function chargerConfigDuProjet() {
53
	protected function chargerConfigDuProjet() {
48
		$fichierIni = $this->getScriptChemin().$this->getProjetNom().'.ini';
54
		$fichierIni = $this->getScriptChemin().$this->getProjetNom().'.ini';
49
		if (file_exists($fichierIni)) {
55
		if (file_exists($fichierIni)) {
50
			Config::charger($fichierIni);
56
			Config::charger($fichierIni);
51
		} else {
57
		} else {
52
			$m = "Veuillez configurer le projet en créant le fichier '{$this->projetNom}.ini' ".
58
			$m = "Veuillez configurer le projet en créant le fichier '{$this->projetNom}.ini' ".
53
				"dans le dossier du module de script du projet à partir du fichier '{$this->projetNom}.defaut.ini'.";
59
				"dans le dossier du module de script du projet à partir du fichier '{$this->projetNom}.defaut.ini'.";
54
			throw new Exception($m);
60
			throw new Exception($m);
55
		}
61
		}
56
	}
62
	}
57
 
63
 
58
	protected function chargerStructureSql() {
64
	protected function chargerStructureSql() {
59
		$contenuSql = $this->recupererContenu(Config::get('chemins.structureSql'));
65
		$contenuSql = $this->recupererContenu(Config::get('chemins.structureSql'));
60
		$this->executerScripSql($contenuSql);
66
		$this->executerScripSql($contenuSql);
61
	}
67
	}
62
 
68
 
63
	protected function executerScripSql($sql) {
69
	protected function executerScripSql($sql) {
64
		$requetes = Outils::extraireRequetes($sql);
70
		$requetes = Outils::extraireRequetes($sql);
65
		foreach ($requetes as $requete) {
71
		foreach ($requetes as $requete) {
66
			$this->getBdd()->requeter($requete);
72
			$this->getBdd()->requeter($requete);
67
		}
73
		}
68
	}
74
	}
69
 
75
 
70
	protected function recupererContenu($chemin) {
76
	protected function recupererContenu($chemin) {
71
		$contenu = file_get_contents($chemin);
77
		$contenu = file_get_contents($chemin);
72
		if ($contenu === false){
78
		if ($contenu === false){
73
			throw new Exception("Impossible d'ouvrir le fichier SQL : $chemin");
79
			throw new Exception("Impossible d'ouvrir le fichier SQL : $chemin");
74
		}
80
		}
75
		return $contenu;
81
		return $contenu;
76
	}
82
	}
77
 
83
 
78
	protected function stopperLaBoucle($limite = false) {
84
	protected function stopperLaBoucle($limite = false) {
79
		$stop = false;
85
		$stop = false;
80
		if ($limite) {
86
		if ($limite) {
81
			static $ligneActuelle = 1;
87
			static $ligneActuelle = 1;
82
			if ($limite == $ligneActuelle++) {
88
			if ($limite == $ligneActuelle++) {
83
				$stop = true;
89
				$stop = true;
84
			}
90
			}
85
		}
91
		}
86
		return $stop;
92
		return $stop;
87
	}
93
	}
-
 
94
 
-
 
95
	/**
-
 
96
	 * Consulte une URL et retourne le résultat (ou déclenche une erreur), en
-
 
97
	 * admettant qu'il soit au format JSON
-
 
98
	 *
-
 
99
	 * @param string $url l'URL du service
-
 
100
	 */
-
 
101
	protected function chargerDonnees($url, $decoderJSON = true) {
-
 
102
		$resultat = $this->conteneur->getRestClient()->consulter($url);
-
 
103
		$entete = $this->conteneur->getRestClient()->getReponseEntetes();
-
 
104
 
-
 
105
		// Si le service meta-donnees fonctionne correctement, l'entete comprend la clé wrapper_data
-
 
106
		if (isset($entete['wrapper_data'])) {
-
 
107
			if ($decoderJSON) {
-
 
108
				$resultat = json_decode($resultat, true);
-
 
109
				$this->entete = (isset($resultat['entete'])) ? $resultat['entete'] : null;
-
 
110
			}
-
 
111
		} else {
-
 
112
			$m = "L'url <a href=\"$url\">$url</a> lancée via RestClient renvoie une erreur";
-
 
113
			trigger_error($m, E_USER_WARNING);
-
 
114
		}
-
 
115
		return $resultat;
-
 
116
	}
88
}
117
}
89
?>
118
?>