Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
710 delphine 1
<?php
2
/** Exemple lancement:
722 aurelien 3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nva -a chargerTous
710 delphine 4
*/
5
class Nva extends EfloreScript {
722 aurelien 6
 
710 delphine 7
	public function executer() {
8
		// Lancement de l'action demandée
9
		try {
10
			$this->initialiserProjet('nva');
11
 
12
			$cmd = $this->getParametre('a');
13
		    switch ($cmd) {
14
		    	case 'chargerTous' :
15
		    		$this->chargerStructureSql();
16
		    		$this->chargerDonnees();
722 aurelien 17
		    		$this->genererChampCodeLangue();
18
		    		$this->genererChampNumTax();
710 delphine 19
		    		break;
20
	    		case 'chargerStructureSql' :
21
	    			$this->chargerStructureSql();
22
	    			break;
23
				case 'chargerDonnees' :
722 aurelien 24
					$this->chargerDonnees();
710 delphine 25
					break;
722 aurelien 26
				case 'genererChampNumTax' :
27
					$this->genererChampNumTax();
28
					break;
29
				case 'genererChampCodeLangue' :
30
					$this->genererChampCodeLangue();
31
					break;
710 delphine 32
				case 'supprimerTous' :
33
					$this->supprimerTous();
34
					break;
35
				default :
36
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
37
			}
38
		} catch (Exception $e) {
39
			$this->traiterErreur($e->getMessage());
40
		}
41
    }
42
 
43
	private function chargerDonnees() {
722 aurelien 44
		$chemin = Config::get('chemins.nva');
710 delphine 45
		$table = Config::get('tables.nva');
46
		$requete = "LOAD DATA INFILE '$chemin' ".
47
			"REPLACE INTO TABLE $table ".
48
			'CHARACTER SET utf8 '.
49
			'FIELDS '.
50
			"	TERMINATED BY '\t' ".
51
			"	ENCLOSED BY '' ".
52
			"	ESCAPED BY '\\\' ".
53
			'IGNORE 1 LINES';
54
		$this->getBdd()->requeter($requete);
55
	}
56
 
722 aurelien 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";
141
	}
710 delphine 142
 
143
	private function supprimerTous() {
722 aurelien 144
		$requete = "DROP TABLE IF EXISTS ".Config::get('tables.nva').", nva_meta ";
710 delphine 145
		$this->getBdd()->requeter($requete);
146
	}
147
}
148
?>