Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
577 delphine 1
<?php
2
/** Exemple lancement:
3
 * /opt/lampp/bin/php -d memory_limit=3500M cli.php sptb -a chargerTous
4
*/
5
class Sptb extends EfloreScript {
6
 
7
	public function executer() {
8
		// Lancement de l'action demandée
9
		try {
10
			$this->initialiserProjet('sptb');
11
 
12
			$cmd = $this->getParametre('a');
13
		    switch ($cmd) {
14
		    	case 'chargerTous' :
15
		    		$this->chargerStructureSql();
16
		    		$this->chargerDonnees('especes');
17
		    		$this->chargerDonnees('lois');
1187 delphine 18
		    		$this-> ajouterChamps();
639 aurelien 19
		    		$this->genererChampNumNomRetenu();
577 delphine 20
		    		break;
21
	    		case 'chargerStructureSql' :
22
	    			$this->chargerStructureSql();
23
	    			break;
24
				case 'chargerDonnees' :
25
		    		$this->chargerDonnees('especes');
26
		    		$this->chargerDonnees('lois');
1187 delphine 27
		    		$this-> ajouterChamps();
639 aurelien 28
		    		$this->genererChampNumNomRetenu();
577 delphine 29
					break;
639 aurelien 30
				case 'genererChampNumNomRetenu' :
1187 delphine 31
		    		$this-> ajouterChamps();
639 aurelien 32
					$this->genererChampNumNomRetenu();
33
					break;
577 delphine 34
				case 'supprimerTous' :
35
					$this->supprimerTous();
36
					break;
37
				default :
38
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
39
			}
40
		} catch (Exception $e) {
41
			$this->traiterErreur($e->getMessage());
42
		}
43
    }
44
 
1186 delphine 45
	protected function chargerDonnees($type) {
577 delphine 46
		$chemin = Config::get('chemins.'.$type);
47
		$table = Config::get('tables.'.$type);
48
		$requete = "LOAD DATA INFILE '$chemin' ".
49
			"REPLACE INTO TABLE $table ".
50
			'CHARACTER SET utf8 '.
51
			'FIELDS '.
52
			"	TERMINATED BY '\t' ".
53
			"	ENCLOSED BY '' ".
54
			"	ESCAPED BY '\\\' ".
55
			'IGNORE 1 LINES';
56
		$this->getBdd()->requeter($requete);
57
	}
639 aurelien 58
 
59
	private function genererChampNumNomRetenu() {
60
		$this->preparerTablePrChpNumNomRetenu();
61
		$this->genererNumNomRetenu();
62
	}
63
 
64
	private function preparerTablePrChpNumNomRetenu() {
65
		$table = Config::get('tables.especes');
66
		$requete = "SHOW COLUMNS FROM $table LIKE 'num_nom_retenu' ";
67
		$resultat = $this->getBdd()->recuperer($requete);
68
		if ($resultat === false) {
69
			$requete = 	"ALTER TABLE $table ".
70
					'ADD num_nom_retenu INT(10) '.
71
					'NULL DEFAULT NULL AFTER num_nom';
72
			$this->getBdd()->requeter($requete);
73
		}
74
	}
1186 delphine 75
	private function ajouterChamps() {
76
		$table = Config::get('tables.especes');
77
 
78
		$requete = 'ALTER TABLE '.$table.' ADD `num_nom` INT( 10 ) NOT NULL ,
79
					ADD `num_nom_retenu` INT( 10 ) NOT NULL ,
80
					ADD `nom_sci` VARCHAR( 70 ) NOT NULL ';
81
		$this->getBdd()->requeter($requete);
82
	}
639 aurelien 83
 
84
	private function genererNumNomRetenu() {
85
		$table = Config::get('tables.especes');
86
		$table_referentiel = Config::get('tables.referentielTaxo');
87
 
88
		$requete = 'UPDATE '.$table.' s, '.$table_referentiel.' r '.
1186 delphine 89
		           'SET s.num_nom_retenu = r.num_nom_retenu, s.num_nom = r.num_nom, s.nom_sci = r.nom_sci '.
90
		           '	WHERE s.cd_nom = r.cd_nom ';
639 aurelien 91
 
92
		$this->getBdd()->requeter($requete);
93
	}
577 delphine 94
 
95
	private function supprimerTous() {
96
		$requete = "DROP TABLE IF EXISTS sptb_meta, sptb_especes_v2012, sptb_lois_v2012";
97
		$this->getBdd()->requeter($requete);
98
		Debug::printr('suppression');
99
	}
100
}
101
?>