Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 639 | 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');
639 aurelien 18
		    		$this->genererChampNumNomRetenu();
577 delphine 19
		    		break;
20
	    		case 'chargerStructureSql' :
21
	    			$this->chargerStructureSql();
22
	    			break;
23
				case 'chargerDonnees' :
24
		    		$this->chargerDonnees('especes');
25
		    		$this->chargerDonnees('lois');
639 aurelien 26
		    		$this->genererChampNumNomRetenu();
577 delphine 27
					break;
639 aurelien 28
				case 'genererChampNumNomRetenu' :
29
					$this->genererChampNumNomRetenu();
30
					break;
577 delphine 31
				case 'supprimerTous' :
32
					$this->supprimerTous();
33
					break;
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
 
42
	private function chargerDonnees($type) {
43
		$chemin = Config::get('chemins.'.$type);
44
		$table = Config::get('tables.'.$type);
45
		$requete = "LOAD DATA INFILE '$chemin' ".
46
			"REPLACE INTO TABLE $table ".
47
			'CHARACTER SET utf8 '.
48
			'FIELDS '.
49
			"	TERMINATED BY '\t' ".
50
			"	ENCLOSED BY '' ".
51
			"	ESCAPED BY '\\\' ".
52
			'IGNORE 1 LINES';
53
		$this->getBdd()->requeter($requete);
54
	}
639 aurelien 55
 
56
	private function genererChampNumNomRetenu() {
57
		$this->preparerTablePrChpNumNomRetenu();
58
		$this->genererNumNomRetenu();
59
	}
60
 
61
	private function preparerTablePrChpNumNomRetenu() {
62
		$table = Config::get('tables.especes');
63
		$requete = "SHOW COLUMNS FROM $table LIKE 'num_nom_retenu' ";
64
		$resultat = $this->getBdd()->recuperer($requete);
65
		if ($resultat === false) {
66
			$requete = 	"ALTER TABLE $table ".
67
					'ADD num_nom_retenu INT(10) '.
68
					'NULL DEFAULT NULL AFTER num_nom';
69
			$this->getBdd()->requeter($requete);
70
		}
71
	}
72
 
73
	private function genererNumNomRetenu() {
74
		$table = Config::get('tables.especes');
75
		$table_referentiel = Config::get('tables.referentielTaxo');
76
 
77
		$requete = 'UPDATE '.$table.' s, '.$table_referentiel.' r '.
78
		           'SET s.num_nom_retenu = r.num_nom_retenu '.
79
		           '	WHERE s.num_nom = r.num_nom ';
80
 
81
		$this->getBdd()->requeter($requete);
82
	}
577 delphine 83
 
84
	private function supprimerTous() {
85
		$requete = "DROP TABLE IF EXISTS sptb_meta, sptb_especes_v2012, sptb_lois_v2012";
86
		$this->getBdd()->requeter($requete);
87
		Debug::printr('suppression');
88
	}
89
}
90
?>