Subversion Repositories Applications.projet

Rev

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

<?php

class MigrationWiki extends ProjetService {
        
        const dry_run = false;
        
        public function __construct($config, $demarrer_session= true) {
                parent::__construct($config, $demarrer_session);
                define('NL',"\n"); 
        }
        
        private function executerRequeteManip($requete, $mode_fetch = false) {
                // Fonction de commodité pour afficher les requetes au lieu de les executer
                if (self::dry_run) {
                        echo '<pre>'.str_replace('),','),'.NL, $requete).'</pre>';      
                        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>'.NL.NL;        
                }
                return $this->bdd->query($requete)->fetchAll($mode_fetch);
        }
        
        public function getElement($uid){
                
                $nom_admin = $this->config['appli']['nom_admin_wiki'];
                $mail_admin = $this->config['appli']['adresse_admin_wiki'];     
                $pass_admin = $this->config['appli']['pass_admin_wiki'];

                $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'] != 'bibliobota') {
                                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'];
                        $chemin_wiki = '/home/aurelien/web/wikini/'.$wikini['gewi_code_alpha_wikini'];
                        
                        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'];
                                                
                        $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;
                        }
                        
                        $this->ajoutPageFormatees($chemin_wiki, $infos_admin);
                }
        }

        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) {
                        
                if(self::dry_run) {
                        echo 'copie de '.$chemin_base_wiki_defaut.' vers '.$chemin_wiki_dest.NL;
                        $copie_recursive = true;
                } else {
                        if(trim($chemin_base_wiki_defaut) != '/' && trim($chemin_wiki_dest) != '/') { 
                                $copie_recursive = shell_exec( " cp -Rf -a ".$chemin_base_wiki_defaut."* ".$chemin_wiki_dest." 2>&1 " );
                        }
                }
                
                return $copie_recursive;
        }
        
        private function ajoutPageFormatees($chemin_wikini, $utilisateur) {
                
                $chemin_base_wiki_defaut = $this->config['appli']['chemin_wiki_defaut'];
                require_once($chemin_base_wiki_defaut.'wakka_class.php');
                
                $nom_admin = $this->config['appli']['nom_admin_wiki'];
                
                $chemin_wikini = rtrim($chemin_wikini,'/');
                require_once($chemin_wikini.'/wakka.config.php');
                
                // la variable $wakkaConfig est contenu dans le fichier wakka.config.php
                $config = $wakkaConfig;
                
                $wiki = new Wiki($config);
                $wiki->SetGroupACL("admins", $nom_admin);
                
                $wiki->setUser($utilisateur);
                                        
                //insertion des pages de documentation et des pages standards 
                $d = dir($chemin_base_wiki_defaut.'/setup/doc/');
                
                while ($doc = $d->read()){
                        
                        if (is_dir($doc) || substr($doc, -4) != '.txt') {continue;}
                        
                        $pagecontent = implode ('', file($chemin_base_wiki_defaut.'/setup/doc/'.$doc));
                        $pagecontent = mb_convert_encoding($pagecontent,'UTF-8','ISO-8859-15'); 
                        if ($doc=='_root_page.txt'){
                                $pagename = $config["root_page"];
                        }else{
                                $pagename = substr($doc,0,strpos($doc,'.txt'));
                        }
                        
                        $base = $config['mysql_database'];
        
                        $requete_existence_page = "Select tag from ".$base.".".$config['table_prefix']."pages where tag='$pagename'";
                        $existence_page = $this->executerRequete($requete_existence_page);
                        if (is_array($existence_page) && count($existence_page) > 0) {
                                echo 'la page '.$pagename.' existe déjà '.NL;
                        } else {
                                                                
                                echo 'Ajout de la page '.$pagename.NL;
                                
                                $requete_maj_pages_anciennes = "UPDATE ".$base.".".$config['table_prefix']."pages ".
                                        "SET ".
                                        "latest = 'N'".
                                        "WHERE tag = '$pagename'";
        
                                $reussite = $this->executerRequeteManip($requete_maj_pages_anciennes);
                                
                                $requete_insertion_page = "INSERT INTO ".$base.".".$config['table_prefix']."pages ".
                                        "SET tag = '$pagename', ".
                                        "body = ".$this->bdd->quote($pagecontent).", ".
                                        "user = '".$nom_admin."', ".
                                        "owner = '".$nom_admin."', " .
                                        "time = now(), ".
                                        "latest = 'Y'";
        
                                $reussite = $this->executerRequeteManip($requete_insertion_page);
                                
                                if(!$reussite) {
                                        echo 'echec de l\'ajout de la page '.$pagename.NL;      
                                } else {
                                
                                        // mise à jour des liens entre pages
                                        $wiki->SetPage($wiki->LoadPage($pagename,"",0));
                                        $wiki->ClearLinkTable();
                                        $wiki->StartLinkTracking();
                                        $wiki->TrackLinkTo($pagename);
                                        $wiki->StopLinkTracking();
                                        $wiki->WriteLinkTable();
                                        $wiki->ClearLinkTable();
                                }
                        }
                }       
        }
}
?>