Rev 600 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php/** Exemple lancement:* /opt/lampp/bin/php -d memory_limit=3500M ~/web/eflore-projets/scripts/cli.php baseveg -a chargerTous*/class Baseveg extends EfloreScript {private $ligne_num;private $fichierDonnees = '';private $synonymes;private $niveaux;private $log = '';private $colonne_valeur;private $colonne_num;private $nb_erreurs;private $erreurs_ligne;private $motifs = array();public function executer() {try {$this->initialiserProjet('baseveg');$cmd = $this->getParametre('a');switch ($cmd) {case 'supprimerTous' :$this->supprimerTous();break;case 'chargerStructureSql' :$this->chargerStructureSql();break;case 'chargerMetadonnees' :$this->chargerMetadonnees();break;case 'chargerDonnees' :$this->chargerDonnees();break;case 'verifierFichier' ://cette étape met en avant les valeurs qui vont poser des problèmes (ontologies..)$this->verifierFichier();break;case 'chargerOntologies' :$this->chargerOntologies();break;case 'chargerTous' :$this->supprimerTous();$this->chargerStructureSql();$this->chargerMetadonnees();$this->chargerDonnees();$this->chargerOntologies();break;default :throw new Exception("Erreur : la commande '$cmd' n'existe pas!");}} catch (Exception $e) {$this->traiterErreur($e->getMessage());}}private function chargerOntologies() {$chemin = Config::get('chemins.ontologies');$table = Config::get('tables.ontologies');$requete = "TRUNCATE TABLE $table ;LOAD DATA INFILE '$chemin' "."REPLACE INTO TABLE $table ".'CHARACTER SET utf8 '.'FIELDS '." TERMINATED BY '\t' "." ENCLOSED BY '' "." ESCAPED BY '\\\' ";$this->getBdd()->requeter($requete);}private function chargerDonnees() {$table = Config::get('tables.donnees');$requete = "LOAD DATA INFILE '".Config::get('chemins.donnees')."' "."REPLACE INTO TABLE $table ".'CHARACTER SET utf8 '.'FIELDS '." TERMINATED BY '\t' "." ENCLOSED BY '' "." ESCAPED BY '\\\'";$this->getBdd()->requeter($requete);}protected function chargerMetadonnees() {$contenuSql = $this->recupererContenu(Config::get('chemins.metadonnees'));$this->executerScripSql($contenuSql);}private function supprimerTous() {$requete = "DROP TABLE IF EXISTS baseveg_v2012_07_04, baseveg_meta, baseveg_ontologies ";$this->getBdd()->requeter($requete);}/* --------------------------------- Vérification de fichiers ----------------------------------*///identiquesprivate function verifierFichier(){$this->initialiserParametresVerif();$lignes = file($this->fichierDonnees, FILE_IGNORE_NEW_LINES);if ($lignes != false) {$this->ajouterAuLog("!!! REGARDEZ LES COLONNES DANS NUMERO_COLONNES_IMPORTANT.TXT.");foreach ($lignes as $this->ligne_num => $ligne) {$this->verifierErreursLigne($ligne);$this->afficherAvancement("Vérification des lignes");}echo "\n";} else {$this->traiterErreur("Le fichier {$this->fichierDonnees} ne peut pas être ouvert.");}if ($this->nb_erreurs == 0) {$this->ajouterAuLog("Il n'y a pas d'erreurs.");}$this->traiterInfo($this->nb_erreurs." erreurs");$this->ecrireFichierLog();}private function verifierColonne(){$motif = $this->motifs[$this->colonne_num];if (preg_match($motif, $this->colonne_valeur) == 0 && $this->verifierSiVide() == false){$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;}}private function verifierSiVide(){$vide = ($this->colonne_valeur == '') ? true : false;return $vide;}private function controlerErreursLigne() {$nbreErreursLigne = count($this->erreurs_ligne);$this->nb_erreurs += $nbreErreursLigne;if ($nbreErreursLigne != 0) {$this->ajouterAuLog("Erreurs sur la ligne {$this->ligne_num}");$ligneLog = '';foreach ($this->erreurs_ligne as $cle => $v){$ligneLog .= "colonne $cle : $v - ";}$this->ajouterAuLog($ligneLog);}}//changementsprivate function verifierErreursLigne($ligne){$this->erreurs_ligne = array();$colonnes = explode("\t", $ligne);if (isset($colonnes)) {foreach ($colonnes as $this->colonne_num => $this->colonne_valeur) {if ($this->colonne_num == 1 ) {$this->verifierColonne();} elseif ($this->colonne_num == 2 ) {$this->verifierColonne();} elseif ($this->colonne_num == 4 ) {$this->verifierNiveaux();break;}}} else {$message = "Ligne {$this->ligne_num} : pas de tabulation";$this->ajouterAuLog($message);}$this->controlerErreursLigne();}private function verifierNiveaux(){if (preg_match("/^syn(.+)$/", $this->colonne_valeur, $retour) == 1) {$synonymes = explode(' ', trim($retour[1]));foreach($synonymes as $syn){if (!in_array($syn, $this->synonymes)) {$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;}}} elseif($this->colonne_valeur != '') {if (!in_array($this->colonne_valeur , $this->niveaux)) {$this->erreurs_ligne[$this->colonne_num] = $this->colonne_valeur;}}}// changementsprivate function initialiserParametresVerif() {$this->nb_erreurs = 0;$this->fichierDonnees = Config::get('chemins.donnees');$this->niveaux = array('CLA','ALL','ORD','ASS','GRPT','SUBORD','SUBASS','BC','SUBCLA','DC','SUBALL');$this->synonymes = array('incl','=','?','illeg','pp','pmaxp','pminp','compl','ambig','non','inval','nn','ined');$this->motifs= $this->inverserTableau(array('/^[0-9]+$/' => 1,'/(?:[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));//présence de '=' , '= ?' et ',' dans les valeurs des paramètres. ne pas utiliser getParametresTableau.}//+------------------------------------------------------------------------------------------------------+// Gestion du Logprivate function ajouterAuLog($txt) {$this->log .= "$txt\n";}private function ecrireFichierLog() {$fichierLog = dirname(__FILE__).'/log/verification.log';file_put_contents($fichierLog, $this->log);}/*--------------------------------------------OUtils-------------------------------------------*///attention , dans les motifs !!private function inverserTableau($tableau) {$inverse = array();foreach ($tableau as $cle => $valeurs) {$valeurs = explode(';', $valeurs);foreach ($valeurs as $valeur) {$inverse[$valeur] = $cle;}}return $inverse;}}?>