Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 348 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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