588 |
mathilde |
1 |
<?php
|
|
|
2 |
/** Exemple lancement:
|
|
|
3 |
* /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php baseveg -a chargerTous
|
|
|
4 |
*/
|
|
|
5 |
|
|
|
6 |
class Baseveg extends EfloreScript {
|
|
|
7 |
|
|
|
8 |
private $ligne_num;
|
|
|
9 |
private $fichierDonnees = '';
|
|
|
10 |
private $synonymes;
|
|
|
11 |
private $niveaux;
|
|
|
12 |
private $log = '';
|
|
|
13 |
private $colonne_valeur;
|
|
|
14 |
private $colonne_num;
|
|
|
15 |
private $nb_erreurs;
|
|
|
16 |
private $erreurs_ligne;
|
|
|
17 |
private $motifs = array();
|
|
|
18 |
|
|
|
19 |
public function executer() {
|
|
|
20 |
try {
|
|
|
21 |
$this->initialiserProjet('baseveg');
|
|
|
22 |
$cmd = $this->getParametre('a');
|
|
|
23 |
switch ($cmd) {
|
|
|
24 |
case 'supprimerTous' :
|
|
|
25 |
$this->supprimerTous();
|
|
|
26 |
break;
|
|
|
27 |
case 'chargerStructureSql' :
|
|
|
28 |
$this->chargerStructureSql();
|
|
|
29 |
break;
|
|
|
30 |
case 'chargerMetadonnees' :
|
|
|
31 |
$this->chargerMetadonnees();
|
|
|
32 |
break;
|
|
|
33 |
case 'chargerDonnees' :
|
|
|
34 |
$this->chargerDonnees();
|
|
|
35 |
break;
|
|
|
36 |
case 'verifierFichier' :
|
|
|
37 |
//cette étape met en avant les valeurs qui vont poser des problèmes (ontologies..)
|
|
|
38 |
$this->verifierFichier();
|
|
|
39 |
break;
|
|
|
40 |
case 'chargerOntologies' :
|
|
|
41 |
$this->chargerOntologies();
|
|
|
42 |
break;
|
|
|
43 |
case 'chargerTous' :
|
|
|
44 |
$this->supprimerTous();
|
|
|
45 |
$this->chargerStructureSql();
|
|
|
46 |
$this->chargerMetadonnees();
|
|
|
47 |
$this->chargerDonnees();
|
|
|
48 |
$this->chargerOntologies();
|
|
|
49 |
break;
|
|
|
50 |
default :
|
|
|
51 |
throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
|
|
|
52 |
}
|
|
|
53 |
} catch (Exception $e) {
|
|
|
54 |
$this->traiterErreur($e->getMessage());
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
private function chargerOntologies() {
|
|
|
59 |
$chemin = Config::get('chemins.ontologies');
|
|
|
60 |
$table = Config::get('tables.ontologies');
|
|
|
61 |
$requete = "TRUNCATE TABLE $table ;
|
|
|
62 |
LOAD DATA INFILE '$chemin' ".
|
|
|
63 |
"REPLACE INTO TABLE $table ".
|
|
|
64 |
'CHARACTER SET utf8 '.
|
|
|
65 |
'FIELDS '.
|
|
|
66 |
" TERMINATED BY '\t' ".
|
|
|
67 |
" ENCLOSED BY '' ".
|
|
|
68 |
" ESCAPED BY '\\\' "
|
|
|
69 |
;
|
|
|
70 |
$this->getBdd()->requeter($requete);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
private function chargerDonnees() {
|
|
|
74 |
$table = Config::get('tables.donnees');
|
|
|
75 |
$requete = "LOAD DATA INFILE '".Config::get('chemins.donnees')."' ".
|
|
|
76 |
"REPLACE INTO TABLE $table ".
|
|
|
77 |
'CHARACTER SET utf8 '.
|
|
|
78 |
'FIELDS '.
|
|
|
79 |
" TERMINATED BY '\t' ".
|
|
|
80 |
" ENCLOSED BY '' ".
|
|
|
81 |
" ESCAPED BY '\\\'";
|
|
|
82 |
$this->getBdd()->requeter($requete);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
protected function chargerMetadonnees() {
|
|
|
86 |
$contenuSql = $this->recupererContenu(Config::get('chemins.metadonnees'));
|
|
|
87 |
$this->executerScripSql($contenuSql);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
private function supprimerTous() {
|
|
|
91 |
$requete = "DROP TABLE IF EXISTS baseveg_v2012_07_04, baseveg_meta, baseveg_ontologies ";
|
|
|
92 |
$this->getBdd()->requeter($requete);
|
|
|
93 |
}
|
|
|
94 |
/* --------------------------------- Vérification de fichiers ----------------------------------*/
|
|
|
95 |
//identiques
|
|
|
96 |
private function verifierFichier(){
|
|
|
97 |
$this->initialiserParametresVerif();
|
|
|
98 |
$lignes = file($this->fichierDonnees, FILE_IGNORE_NEW_LINES);
|
|
|
99 |
if ($lignes != false) {
|
|
|
100 |
$this->ajouterAuLog("!!! REGARDEZ LES COLONNES DANS NUMERO_COLONNES_IMPORTANT.TXT.");
|
|
|
101 |
foreach ($lignes as $this->ligne_num => $ligne) {
|
|
|
102 |
$this->verifierErreursLigne($ligne);
|
|
|
103 |
$this->afficherAvancement("Vérification des lignes");
|
|
|
104 |
}
|
|
|
105 |
echo "\n";
|
|
|
106 |
} else {
|
|
|
107 |
$this->traiterErreur("Le fichier {$this->fichierDonnees} ne peut pas être ouvert.");
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
if ($this->nb_erreurs == 0) {
|
|
|
111 |
$this->ajouterAuLog("Il n'y a pas d'erreurs.");
|
|
|
112 |
}
|
|
|
113 |
$this->traiterInfo($this->nb_erreurs." erreurs");
|
|
|
114 |
|
|
|
115 |
$this->ecrireFichierLog();
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
private function verifierColonne(){
|
|
|
119 |
$motif = $this->motifs[$this->colonne_num];
|
|
|
120 |
if (preg_match($motif, $this->colonne_valeur) == 0 && $this->verifierSiVide() == false){
|
|
|
121 |
$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
private function verifierSiVide(){
|
|
|
126 |
$vide = ($this->colonne_valeur == '') ? true : false;
|
|
|
127 |
return $vide;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
private function controlerErreursLigne() {
|
|
|
131 |
$nbreErreursLigne = count($this->erreurs_ligne);
|
|
|
132 |
$this->nb_erreurs += $nbreErreursLigne;
|
|
|
133 |
if ($nbreErreursLigne != 0) {
|
|
|
134 |
$this->ajouterAuLog("Erreurs sur la ligne {$this->ligne_num}");
|
|
|
135 |
$ligneLog = '';
|
|
|
136 |
foreach ($this->erreurs_ligne as $cle => $v){
|
|
|
137 |
$ligneLog .= "colonne $cle : $v - ";
|
|
|
138 |
}
|
|
|
139 |
$this->ajouterAuLog($ligneLog);
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
//changements
|
|
|
144 |
private function verifierErreursLigne($ligne){
|
|
|
145 |
$this->erreurs_ligne = array();
|
|
|
146 |
$colonnes = explode("\t", $ligne);
|
|
|
147 |
if (isset($colonnes)) {
|
|
|
148 |
foreach ($colonnes as $this->colonne_num => $this->colonne_valeur) {
|
|
|
149 |
if ($this->colonne_num == 1 ) {
|
|
|
150 |
$this->verifierColonne();
|
|
|
151 |
} elseif ($this->colonne_num == 2 ) {
|
|
|
152 |
$this->verifierColonne();
|
|
|
153 |
} elseif ($this->colonne_num == 4 ) {
|
|
|
154 |
$this->verifierNiveaux();
|
|
|
155 |
break;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
} else {
|
|
|
159 |
$message = "Ligne {$this->ligne_num} : pas de tabulation";
|
|
|
160 |
$this->ajouterAuLog($message);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
$this->controlerErreursLigne();
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
private function verifierNiveaux(){
|
|
|
169 |
if (preg_match("/^syn(.+)$/", $this->colonne_valeur, $retour) == 1) {
|
|
|
170 |
|
|
|
171 |
$synonymes = explode(' ', trim($retour[1]));
|
|
|
172 |
foreach($synonymes as $syn){
|
|
|
173 |
if (!in_array($syn, $this->synonymes)) {
|
|
|
174 |
$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
} elseif($this->colonne_valeur != '') {
|
|
|
178 |
if (!in_array($this->colonne_valeur , $this->niveaux)) {
|
|
|
179 |
$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
// changements
|
|
|
185 |
private function initialiserParametresVerif() {
|
|
|
186 |
$this->nb_erreurs = 0;
|
|
|
187 |
$this->fichierDonnees = Config::get('chemins.donnees');
|
|
|
188 |
$this->niveaux = array('CLA','ALL','ORD','ASS','GRPT','SUBORD','SUBASS','BC','SUBCLA','DC','SUBALL');
|
|
|
189 |
$this->synonymes = array('incl','=','?','illeg','pp','pmaxp','pminp','compl','ambig','non','inval','nn','ined');
|
|
|
190 |
$this->motifs= $this->inverserTableau(array('/^[0-9]+$/' => 1,
|
|
|
191 |
'/(?:[0-9]{2}\/$|[0-9]{2}\/[0-9]\.$|[0-9]{2}\/(?:[0-9]\.){1,5}[0-9]$|[0-9]{2}\/(?:[0-9]\.){4,5}[0-9]\/[0-9]+(?:bis|ter|quater){0,1}$)|incertae sedis/' => 2));
|
|
|
192 |
//présence de '=' , '= ?' et ',' dans les valeurs des paramètres. ne pas utiliser getParametresTableau.
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
//+------------------------------------------------------------------------------------------------------+
|
|
|
196 |
// Gestion du Log
|
|
|
197 |
|
|
|
198 |
private function ajouterAuLog($txt) {
|
|
|
199 |
$this->log .= "$txt\n";
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
private function ecrireFichierLog() {
|
|
|
203 |
$fichierLog = dirname(__FILE__).'/log/verification.log';
|
|
|
204 |
file_put_contents($fichierLog, $this->log);
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/*--------------------------------------------OUtils-------------------------------------------*/
|
|
|
208 |
|
|
|
209 |
//attention , dans les motifs !!
|
|
|
210 |
|
|
|
211 |
private function inverserTableau($tableau) {
|
|
|
212 |
$inverse = array();
|
|
|
213 |
foreach ($tableau as $cle => $valeurs) {
|
|
|
214 |
$valeurs = explode(';', $valeurs);
|
|
|
215 |
foreach ($valeurs as $valeur) {
|
|
|
216 |
$inverse[$valeur] = $cle;
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
return $inverse;
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
?>
|