Subversion Repositories eFlore/Projets.eflore-projets

Rev

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