Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 691 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 691 Rev 692
1
<?php
1
<?php
2
/** Exemple lancement:
2
/** Exemple lancement:
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvps
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvps
4
 * 		-a chargerTous
4
 * 		-a chargerTous
5
 * Options :
5
 * Options :
6
 * -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).
6
 * -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).
7
*/
7
*/
8
class Nvps extends EfloreScript {
8
class Nvps extends EfloreScript {
9
 
9
 
10
	private $nomsIndex = array();
10
	private $nomsIndex = array();
11
	private $numeroIndex = 1;
11
	private $numeroIndex = 1;
12
 
12
 
13
	protected $parametres_autorises = array(
13
	protected $parametres_autorises = array(
14
		'-t' => array(false, false, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));
14
		'-t' => array(false, false, 'Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).'));
15
 
15
 
16
	public function executer() {
16
	public function executer() {
17
		// Lancement de l'action demandée
17
		// Lancement de l'action demandée
18
		try {
18
		try {
19
			$this->initialiserProjet('nvps');
19
			$this->initialiserProjet('nvps');
20
 
20
 
21
			$cmd = $this->getParametre('a');
21
			$cmd = $this->getParametre('a');
22
		    switch ($cmd) {
22
			switch ($cmd) {
23
		    	case 'chargerTous' :
23
				case 'chargerTous' :
24
		    		$this->chargerStructureSql();
24
					$this->chargerStructureSql();
25
		    		$this->chargerNvps();
25
					$this->chargerNvps();
26
		    		break;
26
					break;
27
	    		case 'chargerStructure' :
27
				case 'chargerStructure' :
28
	    			$this->chargerStructureSql();
28
					$this->chargerStructureSql();
29
	    			break;
29
					break;
30
				case 'chargerNvps' :
30
				case 'chargerNvps' :
31
					$this->chargerNvps();
31
					$this->chargerNvps();
32
					break;
32
					break;
33
				case 'supprimerTous' :
33
				case 'supprimerTous' :
34
					$this->supprimerTous();
34
					$this->supprimerTous();
35
					break;
35
					break;
36
				default :
36
				default :
37
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
37
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
38
			}
38
			}
39
		} catch (Exception $e) {
39
		} catch (Exception $e) {
40
			$this->traiterErreur($e->getMessage());
40
			$this->traiterErreur($e->getMessage());
41
		}
41
		}
42
    }
42
	}
43
 
43
 
44
    /**
44
	/**
45
     * Charge le fichier en créant un id pour chaque nom vernaculaire.
45
	* Charge le fichier en créant un id pour chaque nom vernaculaire.
46
     */
46
	*/
47
	private function chargerNvps() {
47
	private function chargerNvps() {
48
		//Debug::printr(Config::get('fichiers'));
48
		//Debug::printr(Config::get('fichiers'));
49
		$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvps'));
49
		$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvps'));
50
		$donnees = $this->analyserFichier($fichierOuvert);
50
		$donnees = $this->analyserFichier($fichierOuvert);
51
		fclose($fichierOuvert);
51
		fclose($fichierOuvert);
52
		foreach ($donnees as $donnee) {
52
		foreach ($donnees as $donnee) {
-
 
53
			$table = Config::get('tables.nvps');
-
 
54
			$fields = implode(', ', array_keys($donnee));
-
 
55
			$values = implode(', ', $donnee);
-
 
56
 
53
			$requete = 'INSERT INTO '.Config::get('tables.nvps').' VALUES ('.implode(', ', $donnee).')';
57
			$requete = "INSERT INTO $table ($fields) VALUES ($values) ";
54
			$this->getBdd()->requeter($requete);
58
			$this->getBdd()->requeter($requete);
55
 
59
 
56
			$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
60
			$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
57
			if ($this->stopperLaBoucle($this->getParametre('t'))) {
61
			if ($this->stopperLaBoucle($this->getParametre('t'))) {
58
				break;
62
				break;
59
			}
63
			}
60
		}
64
		}
61
		echo "\n";
65
		echo "\n";
62
	}
66
	}
-
 
67
 
-
 
68
	private function ouvrirFichier($chemin) {
-
 
69
		$fichierOuvert = false;
-
 
70
		if ($chemin) {
-
 
71
			if (file_exists($chemin) === true) {
-
 
72
				$fichierOuvert = fopen($chemin, 'r');
-
 
73
				if ($fichierOuvert == false) {
-
 
74
					throw new Exception("Le fichier $chemin n'a pas pu être ouvert.");
-
 
75
				}
-
 
76
			} else {
-
 
77
				throw new Exception("Le fichier $chemin est introuvable.");
-
 
78
			}
-
 
79
		} else {
-
 
80
			throw new Exception("Aucun chemin de fichier n'a été fourni.");
-
 
81
		}
-
 
82
		return $fichierOuvert;
-
 
83
	}
63
 
84
 
-
 
85
	private function analyserFichier($fichierOuvert) {
-
 
86
		$entetesCsv = explode("\t", trim(fgets($fichierOuvert)));
64
	private function analyserFichier($fichierOuvert) {
87
 
65
		$donnees = array();
-
 
66
		$entetesCsv = fgets($fichierOuvert);
88
		$donnees = array();
67
		while ($ligneCsv = fgets($fichierOuvert)) {
89
		while ($ligneCsv = fgets($fichierOuvert)) {
68
			$champs = explode("\t", trim($ligneCsv));
90
			$champs = explode("\t", trim($ligneCsv));
69
			if (count($champs) > 0) {
91
			if (count($champs) > 0) {
70
				if (isset($champs[2])) {
92
				$infos = array();
71
					$nomVernaculaire = $champs[2];
93
				foreach ($entetesCsv as $ordre => $champNom) {
72
					$indexCourrant = $this->getIndexNomVernaculaire($nomVernaculaire);
-
 
73
					$champs = array_merge(array($indexCourrant), $champs);
94
					$valeur = isset($champs[$ordre]) ? $champs[$ordre] : '';
74
					$donnees[] = $this->protegerValeursDesChamps($champs);
95
					$infos[$champNom] = $valeur;
75
				}
96
				}
-
 
97
				$infos['id'] = $this->getIndexNomVernaculaire($infos['nom_vernaculaire']);
-
 
98
				$donnees[] = $this->getBdd()->protegerTableau($infos);
76
			}
99
			}
77
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
100
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
78
			if ($this->stopperLaBoucle()) {
101
			if ($this->stopperLaBoucle()) {
79
				break;
102
				break;
80
			}
103
			}
81
		}
104
		}
82
		echo "\n";
105
		echo "\n";
-
 
106
 
83
		return $donnees;
107
		return $donnees;
84
	}
108
	}
85
 
109
 
86
	private function getIndexNomVernaculaire($nomVernaculaire) {
110
	private function getIndexNomVernaculaire($nomVernaculaire) {
87
		$indexCourrant = null;
111
		$indexCourrant = null;
88
		if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
112
		if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
89
			$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
113
			$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
90
		}
114
		}
91
		$indexCourrant = $this->nomsIndex[$nomVernaculaire];
115
		$indexCourrant = $this->nomsIndex[$nomVernaculaire];
92
		return $indexCourrant;
116
		return $indexCourrant;
93
	}
117
	}
94
 
-
 
95
	private function ouvrirFichier($chemin) {
-
 
96
		$fichierOuvert = false;
-
 
97
		if ($chemin) {
-
 
98
			if (file_exists($chemin) === true) {
-
 
99
				$fichierOuvert = fopen($chemin, 'r');
-
 
100
				if ($fichierOuvert == false) {
-
 
101
					throw new Exception("Le fichier $chemin n'a pas pu être ouvert.");
-
 
102
				}
-
 
103
			} else {
-
 
104
				throw new Exception("Le fichier $chemin est introuvable.");
-
 
105
			}
-
 
106
		} else {
-
 
107
			throw new Exception("Aucun chemin de fichier n'a été fourni.");
-
 
108
		}
-
 
109
		return $fichierOuvert;
-
 
110
	}
-
 
111
 
-
 
112
	private function protegerValeursDesChamps($champs) {
-
 
113
		$champsProteges = array();
-
 
114
		for ($i = 0; $i < 9; $i++) {
-
 
115
			$valeur = isset($champs[$i]) ? $champs[$i] : '';
-
 
116
			$champsProteges[] = $this->getBdd()->proteger($valeur);
-
 
117
		}
-
 
118
		return $champsProteges;
-
 
119
	}
-
 
120
 
-
 
121
 
-
 
122
 
118
 
123
	private function supprimerTous() {
119
	private function supprimerTous() {
124
		$requete = "DROP TABLE IF EXISTS nvps_meta, nvps_v2007, nvps_v2012";
120
		$requete = "DROP TABLE IF EXISTS nvps_meta, nvps_v2007, nvps_v2012";
125
		$this->getBdd()->requeter($requete);
121
		$this->getBdd()->requeter($requete);
126
	}
122
	}
127
}
123
}
128
?>
124
?>