Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev 687 Rev 1074
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
				case 'genererScriptTableReference' :
-
 
32
					$this->genererScriptTableReference();
-
 
33
					break;
31
				default :
34
				default :
32
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
35
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
33
			}
36
			}
34
		} catch (Exception $e) {
37
		} catch (Exception $e) {
35
			$this->traiterErreur($e->getMessage());
38
			$this->traiterErreur($e->getMessage());
36
		}
39
		}
37
	}
40
	}
38
 
41
 
39
	protected function initialiserProjet($projet) {
42
	protected function initialiserProjet($projet) {
40
		$bases = $this->getListeBases();
43
		$bases = $this->getListeBases();
41
		parent::initialiserProjet($projet);
44
		parent::initialiserProjet($projet);
42
		$this->verifierPresenceBdd($bases);
45
		$this->verifierPresenceBdd($bases);
43
	}
46
	}
44
 
47
 
45
	private function getListeBases() {
48
	private function getListeBases() {
46
		$requete = "SHOW DATABASES";
49
		$requete = "SHOW DATABASES";
47
		$bases = $this->getBdd()->recupererTous($requete);
50
		$bases = $this->getBdd()->recupererTous($requete);
48
		return $bases;
51
		return $bases;
49
	}
52
	}
50
 
53
 
51
	private function verifierPresenceBdd($bases) {
54
	private function verifierPresenceBdd($bases) {
52
		$bddNom = Config::get('bdd_nom');
55
		$bddNom = Config::get('bdd_nom');
53
		$existe = false;
56
		$existe = false;
54
		foreach ($bases as $base) {
57
		foreach ($bases as $base) {
55
			if ($base['Database'] == $bddNom) {
58
			if ($base['Database'] == $bddNom) {
56
				$existe = true;
59
				$existe = true;
57
				break;
60
				break;
58
			}
61
			}
59
		}
62
		}
60
		if ($existe === false) {
63
		if ($existe === false) {
61
			$message = "Veuillez créer la base de données '$bddNom'.";
64
			$message = "Veuillez créer la base de données '$bddNom'.";
62
			throw new Exception($message);
65
			throw new Exception($message);
63
		}
66
		}
64
	}
67
	}
65
 
68
 
66
	public function chargerCel() {
69
	public function chargerCel() {
67
		$tablesCodes = array_keys(Config::get('tables'));
70
		$tablesCodes = array_keys(Config::get('tables'));
68
		foreach ($tablesCodes as $code) {
71
		foreach ($tablesCodes as $code) {
69
			echo "Chargement de la table : $code\n";
72
			echo "Chargement de la table : $code\n";
70
			$this->chargerFichierTsvDansTable($code);
73
			$this->chargerFichierTsvDansTable($code);
71
		}
74
		}
72
	}
75
	}
73
 
76
 
74
	private function chargerFichierTsvDansTable($code) {
77
	private function chargerFichierTsvDansTable($code) {
75
		$chemin = Config::get('chemins.'.$code);
78
		$chemin = Config::get('chemins.'.$code);
76
		$table = Config::get('tables.'.$code);
79
		$table = Config::get('tables.'.$code);
77
		$requete = "LOAD DATA INFILE '$chemin' ".
80
		$requete = "LOAD DATA INFILE '$chemin' ".
78
			"REPLACE INTO TABLE $table ".
81
			"REPLACE INTO TABLE $table ".
79
			'CHARACTER SET utf8 '.
82
			'CHARACTER SET utf8 '.
80
			'FIELDS '.
83
			'FIELDS '.
81
			"	TERMINATED BY '\t' ".
84
			"	TERMINATED BY '\t' ".
82
			"	ENCLOSED BY '' ".
85
			"	ENCLOSED BY '' ".
83
			"	ESCAPED BY '\\\' ".
86
			"	ESCAPED BY '\\\' ".
84
			'IGNORE 1 LINES';
87
			'IGNORE 1 LINES';
85
		$this->getBdd()->requeter($requete);
88
		$this->getBdd()->requeter($requete);
86
	}
89
	}
87
 
90
 
88
	private function supprimerTous() {
91
	private function supprimerTous() {
89
		$requete = "DROP TABLE IF EXISTS cel_meta, cel_images, cel_inventory, cel_mots_cles_images, cel_mots_cles_obs, ".
92
		$requete = "DROP TABLE IF EXISTS cel_meta, cel_images, cel_inventory, cel_mots_cles_images, cel_mots_cles_obs, ".
90
			"cel_obs_images, locations ";
93
			"cel_obs_images, locations ";
91
		$this->getBdd()->requeter($requete);
94
		$this->getBdd()->requeter($requete);
92
	}
95
	}
-
 
96
	
-
 
97
	private function genererScriptTableReference() {
-
 
98
		$script_sql = file_get_contents(__DIR__.'/cel_references.tpl.sql');
-
 
99
		$config_references = Config::get('references');
-
 
100
		foreach($config_references as $a_remplacer => $remplacement) {
-
 
101
			$script_sql = str_replace($a_remplacer, $remplacement, $script_sql);
-
 
102
			file_put_contents(__DIR__.'/cel_references_gen.sql', $script_sql);
-
 
103
		}
-
 
104
	}
93
}
105
}
94
?>
106
?>