Subversion Repositories Applications.projet

Rev

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

<?php

class MigrationWiki extends ProjetService {
        
        const dry_run = false;
        
        private function executerRequeteManip($requete, $mode_fetch = false) {
                // Fonction de commodité pour afficher les requetes au lieu de les executer
                if (self::dry_run) {
                        echo str_replace('),','),'.NL, $requete);       
                        return true;
                } else {
                        if($mode_fetch) { 
                                return $this->bdd->query($requete)->fetchAll($mode_fetch);
                        } else {
                                return $this->bdd->query($requete);
                        }
                }
        }
        
        private function executerRequete($requete, $mode_fetch = false) {
                if (self::dry_run) {
                        echo '<pre>'.str_replace('),','),'.NL, $requete).'</pre>';
                }

                $retour = $this->bdd->query($requete)->fetchAll($mode_fetch);
                return $retour;
        }
        
        public function __construct($config, $demarrer_session= true) {
                parent::__construct($config, $demarrer_session);
                define('NL',"\n"); 
        }

        public function getElement($uid){
                exit;
                $requete_projets_wikinis = 'SELECT * FROM gen_wikini';
                $wikinis_projets = $this->executerRequete($requete_projets_wikinis, PDO::FETCH_ASSOC);
                                
                foreach($wikinis_projets as $wikini) {

                        // test sur un wiki
                        if($wikini['gewi_code_alpha_wikini'] != 'relais') {
                                continue;
                        }

                        $base = $wikini['gewi_bdd_nom'];
                        $prefixe = $wikini['gewi_table_prefix'];
                        
                        $chemin_wiki_sur_site = $this->config['appli']['chemin_wikis_integres'];
                        $chemin_wiki = $wikini['gewi_chemin'];
                        
                        echo NL.'Traitement du wiki '.$chemin_wiki.NL;
                        echo NL.$chemin_wiki.NL;
                        
                        if(!file_exists($chemin_wiki)) {
                                echo 'Impossible de migrer le wikini '.$wikini['gewi_code_alpha_wikini'].' : le dossier n\'existe pas'.NL.NL.NL.NL;
                                continue;
                        }
                        
                        $chemin_base_wiki_defaut = $this->config['appli']['chemin_wiki_defaut'];
                        
                        $nom_admin = $this->config['appli']['nom_admin_wiki'];
                        $mail_admin = $this->config['appli']['adresse_admin_wiki'];     
                        $pass_admin = $this->config['appli']['pass_admin_wiki'];
                        
                        $infos_admin = array('name' =>$nom_admin, 'email' => $mail_admin, 'password' => $pass_admin);
                                                                        
                        //if(!$this->verifierPresenceTableTriple($base, $prefixe)) {
                                echo 'migration du wiki '.$wikini['gewi_code_alpha_wikini'].NL;
                        //      $this->creerTableTriple($base, $prefixe);
                        //      $this->ajouterUtilisateurAdmin($base,$prefixe, $infos_admin);   
                                $this->copierFichiersWikini($chemin_base_wiki_defaut, $chemin_wiki);
                                $this->changerVersionWakkaConfig($chemin_wiki);
                                echo 'migration du wiki '.$wikini['gewi_code_alpha_wikini'].' effectuée '.NL.NL.NL.NL;
                        //} else {
                        //      echo 'wiki dejà à jour '.$wikini['gewi_code_alpha_wikini'].NL.NL.NL.NL;
                        //}
                }
        }

        private function verifierPresenceTableTriple($base, $prefixe) {
                
                $requete_presence_table = 'SELECT COUNT(*) as table_existe FROM information_schema.tables
                WHERE TABLE_SCHEMA = "'.$base.'" AND TABLE_NAME = "'.$prefixe.'triples"';
                                
                $resultat_presence_table = $this->executerRequete($requete_presence_table);
                
                $presence_table = false;
                
                foreach($resultat_presence_table as $table_est_presente) {
                        if($table_est_presente['table_existe'] >= 1) {
                                $presence_table = true;
                                break;
                        }
                }
                
                return $presence_table;
        }

        private function creerTableTriple($base, $prefixe) {
                
                echo 'Création de la table triple '.NL;
                
                $requete_creation_table = 'CREATE TABLE '.$base.'.'.$prefixe.'triples (' . 
                        ' id int(10) unsigned NOT NULL auto_increment,' . 
                        ' resource varchar(255) NOT NULL default \'\',' . 
                        ' property varchar(255) NOT NULL default \'\',' . 
                        ' value text NOT NULL default \'\',' . 
                        ' PRIMARY KEY  (id),' . 
                        ' KEY resource (resource),' . 
                        ' KEY property (property)' . 
                        ') TYPE=MyISAM';

                return $this->executerRequeteManip($requete_creation_table);
        }
        
        private function verifierPresenceTableUsers($base, $prefixe) {
                
                $requete_presence_table = 'SELECT COUNT(*) as table_existe FROM information_schema.tables
                WHERE TABLE_SCHEMA = "'.$base.'" AND TABLE_NAME = "'.$prefixe.'users"';
                                
                $resultat_presence_table = $this->executerRequete($requete_presence_table);
                
                $presence_table = false;
                
                foreach($resultat_presence_table as $table_est_presente) {
                        if($table_est_presente['table_existe'] >= 1) {
                                $presence_table = true;
                                break;
                        }
                }
                
                return $presence_table;
        }
        
        private function ajouterUtilisateurAdmin($base, $prefixe, $infos_admin) {
                
                if(!$this->verifierPresenceTableUsers($base, $prefixe)) {
                        echo 'Impossible d\'ajouter l\'adminwiki : '.$prefixe.'_users n\'existe_pas'.NL;
                        return; 
                }
                
                echo 'Ajout de l\'utilisateur wikiAdmin '.NL;
                
                $requete_suppression_si_admin_present = 'DELETE FROM '.$base.'.'.$prefixe.'users '.
                                                                                                'WHERE name ="'.$infos_admin['name'].'"';
                
                $this->executerRequeteManip($requete_suppression_si_admin_present);
                
                $requete_creation_admin = "INSERT INTO ".$base.'.'.$prefixe."users SET ".
                "signuptime = now(), ".
                "name = '".$infos_admin['name']."', ".
                "email = '".$infos_admin['email']."', ".
                "password = md5('".$infos_admin['password']."')";
                                
                return $this->executerRequeteManip($requete_creation_admin);
        }
        
        private function changerVersionWakkaConfig($chemin_wikini) {

                if(file_exists($chemin_wikini.'/wakka.config.php')) { 
                        
                        $contenu_wakka_config = file_get_contents($chemin_wikini.'/wakka.config.php');
                        $contenu_wakka_config_maj = str_replace('"wikini_version" => "0.4.3"','"wikini_version" => "0.5.0"',$contenu_wakka_config);
                        $contenu_wakka_config_maj = str_replace("'wikini_version' => '0.4.3'","'wikini_version' => '0.5.0'",$contenu_wakka_config_maj);
                        
                        if(self::dry_run) {
                                echo 'Wakka Config mis à jour '.$contenu_wakka_config_maj.NL;
                        } else {
                                file_put_contents($chemin_wikini.'/wakka.config.php', $contenu_wakka_config_maj);
                        }
                        
                        echo 'Changement de la version de wikini '.NL;
                } else {
                        echo 'Attention ! le fichier '.$chemin_wikini.'/wakka.config.php n\'existe pas '.NL;
                }
        }
        
        private function copierFichiersWikini($chemin_base_wiki_defaut, $chemin_wiki_dest) {
                
                echo 'copie de '.$chemin_base_wiki_defaut.' vers '.$chemin_wiki_dest.NL;        
                if(self::dry_run) {
                        $copie_recursive = true;
                } else {
                        if(trim($chemin_base_wiki_defaut) != '/' && trim($chemin_wiki_dest) != '/') { 
                                echo " cp -Rf -a ".$chemin_base_wiki_defaut."* ".$chemin_wiki_dest."/ 2>&1 ";
                                $copie_recursive = shell_exec( " cp -Rf -a ".$chemin_base_wiki_defaut."* ".$chemin_wiki_dest."/ 2>&1 " );
                        }
                }
                
                return $copie_recursive;
        }
}
?>