Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
645 delphine 1
<?php
2
/** Exemple lancement:
3
 * /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php nvps
4
 * 		-a chargerTous
5
 * Options :
6
 * -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).
7
*/
8
class Nvps extends EfloreScript {
9
 
10
	private $nomsIndex = array();
11
	private $numeroIndex = 1;
12
 
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).'));
15
 
16
	public function executer() {
17
		// Lancement de l'action demandée
18
		try {
19
			$this->initialiserProjet('nvps');
20
 
21
			$cmd = $this->getParametre('a');
22
		    switch ($cmd) {
23
		    	case 'chargerTous' :
24
		    		$this->chargerStructureSql();
25
		    		$this->chargerNvps();
26
		    		break;
27
	    		case 'chargerStructure' :
28
	    			$this->chargerStructureSql();
29
	    			break;
30
				case 'chargerNvps' :
31
					$this->chargerNvps();
32
					break;
33
				case 'supprimerTous' :
34
					$this->supprimerTous();
35
					break;
36
				default :
37
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
38
			}
39
		} catch (Exception $e) {
40
			$this->traiterErreur($e->getMessage());
41
		}
42
    }
43
 
44
    /**
45
     * Charge le fichier en créant un id pour chaque nom vernaculaire.
46
     */
47
	private function chargerNvps() {
48
		//Debug::printr(Config::get('fichiers'));
49
		$fichierOuvert = $this->ouvrirFichier(Config::get('chemins.nvps'));
50
		$donnees = $this->analyserFichier($fichierOuvert);
51
		fclose($fichierOuvert);
52
		foreach ($donnees as $donnee) {
53
			$requete = 'INSERT INTO '.Config::get('tables.nvps').' VALUES ('.implode(', ', $donnee).')';
54
			$this->getBdd()->requeter($requete);
55
 
56
			$this->afficherAvancement("Insertion des noms vernaculaires dans la base de données");
57
			if ($this->stopperLaBoucle($this->getParametre('t'))) {
58
				break;
59
			}
60
		}
61
		echo "\n";
62
	}
63
 
64
	private function analyserFichier($fichierOuvert) {
65
		$donnees = array();
66
		$entetesCsv = fgets($fichierOuvert);
67
		while ($ligneCsv = fgets($fichierOuvert)) {
68
			$champs = explode("\t", trim($ligneCsv));
69
			if (count($champs) > 0) {
70
				if (isset($champs[2])) {
71
					$nomVernaculaire = $champs[2];
72
					$indexCourrant = $this->getIndexNomVernaculaire($nomVernaculaire);
73
					$champs = array_merge(array($indexCourrant), $champs);
74
					$donnees[] = $this->protegerValeursDesChamps($champs);
75
				}
76
			}
77
			$this->afficherAvancement("Analyse du fichier des noms vernaculaires");
78
			if ($this->stopperLaBoucle()) {
79
				break;
80
			}
81
		}
82
		echo "\n";
83
		return $donnees;
84
	}
85
 
86
	private function getIndexNomVernaculaire($nomVernaculaire) {
87
		$indexCourrant = null;
88
		if (array_key_exists($nomVernaculaire, $this->nomsIndex) == false) {
89
			$this->nomsIndex[$nomVernaculaire] = $this->numeroIndex++;
90
		}
91
		$indexCourrant = $this->nomsIndex[$nomVernaculaire];
92
		return $indexCourrant;
93
	}
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
 
123
	private function supprimerTous() {
691 jpm 124
		$requete = "DROP TABLE IF EXISTS nvps_meta, nvps_v2007, nvps_v2012";
645 delphine 125
		$this->getBdd()->requeter($requete);
126
	}
127
}
128
?>