Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 348 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 348 Rev 687
1
<?php
1
<?php
2
//declare(encoding='UTF-8');
2
//declare(encoding='UTF-8');
3
/**
3
/**
4
 * Exemple de lancement du script : :
4
 * Exemple de lancement du script : :
5
 * /opt/lampp/bin/php cli.php cel -a chargerTous
5
 * /opt/lampp/bin/php cli.php cel -a chargerTous
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.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
12
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
12
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
13
 * @version		$Id$
13
 * @version		$Id$
14
 */
14
 */
15
class Cel extends EfloreScript {
15
class Cel extends EfloreScript {
16
 
16
 
17
	public function executer() {
17
	public function executer() {
18
		try {
18
		try {
19
			$this->initialiserProjet('cel');
19
			$this->initialiserProjet('cel');
20
 
20
 
21
			// Lancement de l'action demandée
21
			// Lancement de l'action demandée
22
			$cmd = $this->getParametre('a');
22
			$cmd = $this->getParametre('a');
23
			switch ($cmd) {
23
			switch ($cmd) {
24
				case 'chargerTous' :
24
				case 'chargerTous' :
25
					$this->chargerStructureSql();
25
					$this->chargerStructureSql();
26
					$this->chargerCel();
26
					$this->chargerCel();
27
					break;
27
					break;
28
				case 'supprimerTous' :
28
				case 'supprimerTous' :
29
					$this->supprimerTous();
29
					$this->supprimerTous();
30
					break;
30
					break;
31
				default :
31
				default :
32
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
32
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
33
			}
33
			}
34
		} catch (Exception $e) {
34
		} catch (Exception $e) {
35
			$this->traiterErreur($e->getMessage());
35
			$this->traiterErreur($e->getMessage());
36
		}
36
		}
37
	}
37
	}
38
 
38
 
39
	protected function initialiserProjet($projet) {
39
	protected function initialiserProjet($projet) {
40
		$bases = $this->getListeBases();
40
		$bases = $this->getListeBases();
41
		parent::initialiserProjet($projet);
41
		parent::initialiserProjet($projet);
42
		$this->verifierPresenceBdd($bases);
42
		$this->verifierPresenceBdd($bases);
43
	}
43
	}
44
 
44
 
45
	private function getListeBases() {
45
	private function getListeBases() {
46
		$requete = "SHOW DATABASES";
46
		$requete = "SHOW DATABASES";
47
		$bases = $this->getBdd()->recupererTous($requete);
47
		$bases = $this->getBdd()->recupererTous($requete);
48
		return $bases;
48
		return $bases;
49
	}
49
	}
50
 
50
 
51
	private function verifierPresenceBdd($bases) {
51
	private function verifierPresenceBdd($bases) {
52
		$bddNom = Config::get('bdd_nom');
52
		$bddNom = Config::get('bdd_nom');
53
		$existe = false;
53
		$existe = false;
54
		foreach ($bases as $base) {
54
		foreach ($bases as $base) {
55
			if ($base['Database'] == $bddNom) {
55
			if ($base['Database'] == $bddNom) {
56
				$existe = true;
56
				$existe = true;
57
				break;
57
				break;
58
			}
58
			}
59
		}
59
		}
60
		if ($existe === false) {
60
		if ($existe === false) {
61
			$message = "Veuillez créer la base de données '$bddNom'.";
61
			$message = "Veuillez créer la base de données '$bddNom'.";
62
			throw new Exception($message);
62
			throw new Exception($message);
63
		}
63
		}
64
	}
64
	}
65
 
65
 
66
	public function chargerCel() {
66
	public function chargerCel() {
67
		$tablesCodes = array_keys(Config::get('tables'));
67
		$tablesCodes = array_keys(Config::get('tables'));
68
		foreach ($tablesCodes as $code) {
68
		foreach ($tablesCodes as $code) {
69
			echo "Chargement de la table : $code\n";
69
			echo "Chargement de la table : $code\n";
70
			$this->chargerFichierTsvDansTable($code);
70
			$this->chargerFichierTsvDansTable($code);
71
		}
71
		}
72
	}
72
	}
73
 
73
 
74
	private function chargerFichierTsvDansTable($code) {
74
	private function chargerFichierTsvDansTable($code) {
75
		$chemin = Config::get('chemins.'.$code);
75
		$chemin = Config::get('chemins.'.$code);
76
		$table = Config::get('tables.'.$code);
76
		$table = Config::get('tables.'.$code);
77
		$requete = "LOAD DATA INFILE '$chemin' ".
77
		$requete = "LOAD DATA INFILE '$chemin' ".
78
			"REPLACE INTO TABLE $table ".
78
			"REPLACE INTO TABLE $table ".
79
			'CHARACTER SET utf8 '.
79
			'CHARACTER SET utf8 '.
80
			'FIELDS '.
80
			'FIELDS '.
81
			"	TERMINATED BY '\t' ".
81
			"	TERMINATED BY '\t' ".
82
			"	ENCLOSED BY '' ".
82
			"	ENCLOSED BY '' ".
83
			"	ESCAPED BY '\\\' ".
83
			"	ESCAPED BY '\\\' ".
84
			'IGNORE 1 LINES';
84
			'IGNORE 1 LINES';
85
		$this->getBdd()->requeter($requete);
85
		$this->getBdd()->requeter($requete);
86
	}
86
	}
87
 
87
 
88
	private function supprimerTous() {
88
	private function supprimerTous() {
89
		$requete = "DROP TABLE IF EXISTS cel_images, cel_inventory, cel_mots_cles_images, cel_mots_cles_obs, ".
89
		$requete = "DROP TABLE IF EXISTS cel_meta, cel_images, cel_inventory, cel_mots_cles_images, cel_mots_cles_obs, ".
90
			"cel_obs_images, locations ";
90
			"cel_obs_images, locations ";
91
		$this->getBdd()->requeter($requete);
91
		$this->getBdd()->requeter($requete);
92
	}
92
	}
93
}
93
}
94
?>
94
?>