Subversion Repositories eFlore/Applications.del

Rev

Rev 1951 | Rev 1970 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
// declare(encoding='UTF-8');
/**
 * Script gérant la maintenance de DEL.
 *
 * @category   DEL
 * @package    Scripts
 * @subpackage Maintenance
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
 */
class Maintenance extends DelScript {

        protected $tables_referentiel = array();
        
        public function __construct($conteneur) {
                $this->conteneur = new Conteneur($this->parametres);
                $this->construireTableauReferentiels();
        }
        
        public function executer() {    
                $this->affecterNnsAuxPropositionsViables();
                //$this->affecterPropositionsRetenues();
        }
        
        private function affecterPropositionsRetenues() {
                // TODO: est ce vraiment une bonne idée ?
                // mettre un filtre sur la date ?
                // sur le nombre de votes ?
                $requete = "UPDATE del_commentaire dc ".
                                        "SET proposition_retenue = 1 ".
                                        "INNER JOIN del_observation do ".
                                        "       ON do.id_observation = dc.ce_observation ".
                                        "       AND do.nom_sel_nn = dc.nom_sel_nn ".
                                        "       AND do.nom_referentiel = dc.nom_referentiel ".
                                        "WHERE ce_observation IN ( ".
                                                "SELECT ce_observation FROM del_commentaire WHERE proposition_retenue = 0 ".
                                        ") ".
                                        "AND ce_observation NOT IN ( ".
                                                "SELECT ce_observation FROM del_commentaire WHERE proposition_retenue = 1 ".
                                        ") ";
                echo $requete;exit;
                $modif = $this->executer($requete);
                return $modif;
        }
        
        private function affecterNnsAuxPropositionsViables() {
                
                echo "Affectation de nn aux propositions qui le permettent \n";
                $propositions = $this->obtenirPropositionsPotentiellementViables();
                
                $noms_ordonnes = array();
                $nb_propositions_traitees = 0;
                $nb_prop_orphelines = count($propositions);
                $nb_prop_augmentees = 0;
                $nb_noms_trouves = 0;
                $nb_prop_inchangees = 0;
                
                echo $nb_prop_orphelines.' propositions sont potentiellement améliorables'."\n";
                
                foreach($propositions as $prop) {
                                
                        $referentiel = in_array($prop['referentiel_proposition'], array_keys($this->tables_referentiel)) ?
                        $prop['referentiel_proposition'] : $prop['referentiel_observation'];
                        $referentiel = in_array($referentiel, array_keys($this->tables_referentiel)) ?
                        $referentiel : 'tous';
                        $referentiel = substr($referentiel, 0, 5);
                                
                        if($referentiel == 'tous') {
                                $noms_trouves = 0;
                                foreach(array_keys($this->tables_referentiel) as $ref) {
                                        $noms_possibles[$referentiel] = $this->rechercherNomsPossibles($ref, $prop['nom_sel']);
                                        $nb_noms_trouves += count($noms_possibles[$referentiel]);
                                        $noms_trouves += empty($noms_possibles[$referentiel]) ? 1 : 0;
                                }
                                $nb_prop_inchangees += $noms_trouves != 0 ? 1 : 0;
                                
                                // Un seul référentiel correspond (c'est déjà bon signe)
                                if(count($noms_possibles) == 1) {
                                        $nom_dans_ref = reset($noms_possibles);
                                        $nom_referentiel = key($noms_possibles);
                                        // Un seul nom trouvé : OK
                                        if(count($nom_dans_ref) == 1) {
                                                if(isset($nom_dans_ref['num_nom'])) {
                                                        $this->mettreAjourProposition($prop['id_commentaire'], $nom_referentiel, $nom_dans_ref);
                                                        $nb_prop_augmentees ++;
                                                }
                                        }
                                }
                        } else {
                                $noms_possibles = $this->rechercherNomsPossibles($referentiel, $prop['nom_sel']);
                                $noms_ordonnes[$prop['id_commentaire']]['nom_trouves'] = $noms_possibles;
                                $nb_noms_trouves += count($noms_possibles);
                                $nb_prop_inchangees += empty($noms_possibles) ? 1 : 0;
                                
                                // On ne met à jour qu'en étant absolument sur (si l'on a trouvé un seul nom)
                                if(count($noms_possibles) == 1) {
                                        $nom_dans_ref = array_pop($noms_possibles);
                                        if(isset($nom_dans_ref['num_nom'])) {
                                                $this->mettreAjourProposition($prop['id_commentaire'], $referentiel, array_pop($noms_possibles));
                                                $nb_prop_augmentees ++; 
                                        }
                                }
                        }
                                
                        $this->afficherAvancement('propositions traitées ', 1);
                }
                
                $noms_ordonnes[$prop['id_commentaire']]['proposition'] = $prop;
                
                echo "\n";
                echo $nb_noms_trouves.' noms ont été trouvés'."\n";
                echo $nb_prop_augmentees.' propositions ont été améliorées'."\n";
                echo $nb_prop_inchangees.' propositions n\'ont pas pu être améliorées'."\n";
        }
        
        
        private function obtenirPropositionsPotentiellementViables() {
                $requete = 'SELECT DISTINCT dc.id_commentaire, dc.nom_sel, dc.nom_referentiel as referentiel_proposition, '.
                                                        'do.nom_referentiel as referentiel_observation '.
                                        'FROM del_commentaire dc '.
                                        'INNER JOIN del_observation do '.
                                        '       ON do.id_observation = dc.ce_observation '.
                                        'WHERE dc.nom_sel != "" AND (dc.nom_sel_nn = 0 OR dc.nom_sel_nn IS NULL)';

                $propositions = $this->conteneur->getBdd()->recupererTous($requete);
                
                return $propositions;
        }
        
        private function rechercherNomsPossibles($referentiel, $nom) {
                $nom = trim($nom);
                $requete = "SELECT num_nom, num_nom_retenu, nom_sci, CONCAT(nom_sci, ' ', auteur) as nom_sci_etendu FROM ".$this->tables_referentiel[$referentiel]." ".
                                "WHERE CONCAT(nom_sci, ' ', auteur) = ".$this->conteneur->getBdd()->proteger($nom)." ".
                                "               OR nom_sci = ".$this->conteneur->getBdd()->proteger($nom)." ".
                                "ORDER BY CONCAT(nom_sci, ' ', auteur) ASC";
                
                $noms = $this->conteneur->getBdd()->recupererTous($requete);
                return $noms;
        }
        
        private function mettreAjourProposition($id_proposition, $referentiel, $infos) {
                $requete = "UPDATE del_commentaire ".
                                        "SET nom_sel_nn = ".$this->conteneur->getBdd()->proteger($infos['num_nom']).", ".
                                        "        nom_referentiel = ".$this->conteneur->getBdd()->proteger($referentiel)." ".
                                        "WHERE id_commentaire = ".$this->conteneur->getBdd()->proteger($id_proposition);
                return $this->conteneur->getBdd()->requeter($requete);
        }
        
        private function construireTableauReferentiels() {
                $referentiels_dispos = explode(',', $this->conteneur->getParametre('referentiels'));
                foreach($referentiels_dispos as $ref) {
                        $this->tables_referentiel[$ref] = $this->conteneur->getParametre('table_referentiel_'.$ref);
                }
        }
}