Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 710 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 710 Rev 722
Line 1... Line 1...
1
<?php
1
<?php
2
/** Exemple lancement:
2
/** Exemple lancement:
3
 * /opt/lampp/bin/php -d memory_limit=3500M cli.php sptba -a chargerTous
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nva -a chargerTous
4
*/
4
*/
5
class Nva extends EfloreScript {
5
class Nva extends EfloreScript {
6
 
6
	
7
	public function executer() {
7
	public function executer() {
8
		// Lancement de l'action demandée
8
		// Lancement de l'action demandée
9
		try {
9
		try {
10
			$this->initialiserProjet('nva');
10
			$this->initialiserProjet('nva');
Line 11... Line 11...
11
 
11
 
12
			$cmd = $this->getParametre('a');
12
			$cmd = $this->getParametre('a');
13
		    switch ($cmd) {
13
		    switch ($cmd) {
14
		    	case 'chargerTous' :
14
		    	case 'chargerTous' :
15
		    		$this->chargerStructureSql();
15
		    		$this->chargerStructureSql();
-
 
16
		    		$this->chargerDonnees();
-
 
17
		    		$this->genererChampCodeLangue();
16
		    		$this->chargerDonnees();
18
		    		$this->genererChampNumTax();
17
		    		break;
19
		    		break;
18
	    		case 'chargerStructureSql' :
20
	    		case 'chargerStructureSql' :
19
	    			$this->chargerStructureSql();
21
	    			$this->chargerStructureSql();
20
	    			break;
22
	    			break;
21
				case 'chargerDonnees' :
23
				case 'chargerDonnees' :
-
 
24
					$this->chargerDonnees();
-
 
25
					break;
-
 
26
				case 'genererChampNumTax' :
-
 
27
					$this->genererChampNumTax();
-
 
28
					break;
-
 
29
				case 'genererChampCodeLangue' :
22
		    		$this->chargerDonnees();
30
					$this->genererChampCodeLangue();
23
					break;
31
					break;
24
				case 'supprimerTous' :
32
				case 'supprimerTous' :
25
					$this->supprimerTous();
33
					$this->supprimerTous();
26
					break;
34
					break;
Line 31... Line 39...
31
			$this->traiterErreur($e->getMessage());
39
			$this->traiterErreur($e->getMessage());
32
		}
40
		}
33
    }
41
    }
Line 34... Line 42...
34
 
42
 
35
	private function chargerDonnees() {
43
	private function chargerDonnees() {
36
		$chemin = Config::get('chemins.nva').Config::get('fichiers.nva');
44
		$chemin = Config::get('chemins.nva');
37
		$table = Config::get('tables.nva');
45
		$table = Config::get('tables.nva');
38
		$requete = "LOAD DATA INFILE '$chemin' ".
46
		$requete = "LOAD DATA INFILE '$chemin' ".
39
			"REPLACE INTO TABLE $table ".
47
			"REPLACE INTO TABLE $table ".
40
			'CHARACTER SET utf8 '.
48
			'CHARACTER SET utf8 '.
Line 44... Line 52...
44
			"	ESCAPED BY '\\\' ".
52
			"	ESCAPED BY '\\\' ".
45
			'IGNORE 1 LINES';
53
			'IGNORE 1 LINES';
46
		$this->getBdd()->requeter($requete);
54
		$this->getBdd()->requeter($requete);
47
	}
55
	}
Line -... Line 56...
-
 
56
	
-
 
57
	private function genererChampNumTax() {
-
 
58
		$this->preparerTablePrChpNumTax();
-
 
59
		$nns_taxons = $this->recupererTuplesPrNumTax();
-
 
60
		$this->remplirChpNumTax($nns_taxons);
-
 
61
	}
-
 
62
	
-
 
63
	private function preparerTablePrChpNumTax() {
-
 
64
		$table = Config::get('tables.nva');
-
 
65
		$requete = "SHOW COLUMNS FROM $table LIKE 'num_taxon' ";
-
 
66
		$resultat = $this->getBdd()->recuperer($requete);
-
 
67
		if ($resultat === false) {
-
 
68
			$requete = 	"ALTER TABLE $table ".
-
 
69
					'ADD num_taxon INT(10) ';
-
 
70
			$this->getBdd()->requeter($requete);
-
 
71
		}
-
 
72
	}
-
 
73
	
-
 
74
	private function recupererTuplesPrNumTax() {
-
 
75
		$table_ref = Config::get('tables.bdtxa');
-
 
76
		$requete = 'SELECT  num_tax, num_nom '.
-
 
77
					"FROM ".$table_ref." ";
-
 
78
		$resultat = $this->getBdd()->recupererTous($requete);
-
 
79
		return $resultat;
-
 
80
	}
-
 
81
	
-
 
82
	private function remplirChpNumTax($nns_taxons) {
-
 
83
		$table = Config::get('tables.nva');
-
 
84
		foreach ($nns_taxons as $nn) {
-
 
85
			if (!empty($nn['num_tax']) && is_numeric($nn['num_tax'])) {
-
 
86
				$requete = "UPDATE $table SET num_taxon = ".$nn['num_tax']." WHERE num_nom = ".$nn['num_nom']." ";
-
 
87
				$resultat = $this->getBdd()->requeter($requete);
-
 
88
			}
-
 
89
			if ($resultat === false) {
-
 
90
				throw new Exception("Erreur d'insertion pour le tuple $id");
-
 
91
			}
-
 
92
			$this->afficherAvancement("Insertion des nums taxonomiques dans la base en cours");
-
 
93
		}
-
 
94
		echo "\n";
-
 
95
	}
-
 
96
	
-
 
97
	private function genererChampCodeLangue() {
-
 
98
		$this->preparerTablePrChpCodeLangue();
-
 
99
		$codes_langues = $this->recupererTuplesPrCodeLangue();
-
 
100
		$this->remplirChpCodeLangue($codes_langues);
-
 
101
	}
-
 
102
	
-
 
103
	private function preparerTablePrChpCodeLangue() {
-
 
104
		$table = Config::get('tables.nva');
-
 
105
		$requete = "SHOW COLUMNS FROM $table LIKE 'code_langue' ";
-
 
106
		$resultat = $this->getBdd()->recuperer($requete);
-
 
107
		if ($resultat === false) {
-
 
108
			$requete = 	"ALTER TABLE $table ".
-
 
109
						'ADD code_langue VARCHAR(3) ';
-
 
110
			$this->getBdd()->requeter($requete);
-
 
111
		}
-
 
112
	}
-
 
113
	
-
 
114
	private function recupererTuplesPrCodeLangue() {
-
 
115
		$codes_langues = array();
-
 
116
		if (($handle = fopen(Config::get("dossierTsv")."codes_langues.csv", "r")) !== false) {
-
 
117
		    while (($data = fgetcsv($handle, 1000, ",")) !== false) {
-
 
118
		    	$codes_langues[] = array('code_iso_639_3' => $data[0],
-
 
119
		    						'nom' => $data[1]);
-
 
120
		    }
-
 
121
		    fclose($handle);
-
 
122
		}
-
 
123
				
-
 
124
		return $codes_langues;
-
 
125
	}
-
 
126
	
-
 
127
	private function remplirChpCodeLangue($codes_langues) {
-
 
128
		$table = Config::get('tables.nva');
-
 
129
		foreach ($codes_langues as $code) {
-
 
130
			if (!empty($code['nom'])) {
-
 
131
				$requete = "UPDATE $table SET code_langue = ".$this->getBdd()->proteger($code['code_iso_639_3'])." ".
-
 
132
						   "WHERE TRIM(langue) = ".$this->getBdd()->proteger(trim($code['nom']))." ";
-
 
133
				$resultat = $this->getBdd()->requeter($requete);
-
 
134
			}
-
 
135
			if ($resultat === false) {
-
 
136
				throw new Exception("Erreur d'insertion pour le tuple $id");
-
 
137
			}
-
 
138
			$this->afficherAvancement("Insertion des codes de langues dans la base en cours");
-
 
139
		}
-
 
140
		echo "\n";
Line 48... Line 141...
48
	
141
	}
49
 
142
 
50
	private function supprimerTous() {
143
	private function supprimerTous() {
51
		$requete = "DROP TABLE IF EXISTS nva_meta, nva_v2013_06";
-
 
52
		$this->getBdd()->requeter($requete);
144
		$requete = "DROP TABLE IF EXISTS ".Config::get('tables.nva').", nva_meta ";
53
		Debug::printr('suppression');
145
		$this->getBdd()->requeter($requete);
54
	}
146
	}
55
}
147
}