Subversion Repositories Applications.projet

Rev

Rev 431 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
431 mathias 1
<?php
2
 
3
class MigrationWiki extends ProjetService {
4
 
5
	const dry_run = false;
6
 
7
	private function executerRequeteManip($requete, $mode_fetch = false) {
8
		// Fonction de commodité pour afficher les requetes au lieu de les executer
9
		if (self::dry_run) {
10
			echo str_replace('),','),'.NL, $requete);
11
			return true;
12
		} else {
13
			if($mode_fetch) {
14
				return $this->bdd->query($requete)->fetchAll($mode_fetch);
15
			} else {
16
				return $this->bdd->query($requete);
17
			}
18
		}
19
	}
20
 
21
	private function executerRequete($requete, $mode_fetch = false) {
22
		if (self::dry_run) {
23
			echo '<pre>'.str_replace('),','),'.NL, $requete).'</pre>';
24
		}
25
 
26
		$retour = $this->bdd->query($requete)->fetchAll($mode_fetch);
27
		return $retour;
28
	}
29
 
30
	public function __construct($config, $demarrer_session= true) {
31
		parent::__construct($config, $demarrer_session);
32
		define('NL',"\n");
33
	}
34
 
35
	public function getElement($uid){
36
		exit;
37
		$requete_projets_wikinis = 'SELECT * FROM gen_wikini';
38
		$wikinis_projets = $this->executerRequete($requete_projets_wikinis, PDO::FETCH_ASSOC);
39
 
40
		foreach($wikinis_projets as $wikini) {
41
 
42
			// test sur un wiki
43
			if($wikini['gewi_code_alpha_wikini'] != 'relais') {
44
				continue;
45
			}
46
 
47
			$base = $wikini['gewi_bdd_nom'];
48
			$prefixe = $wikini['gewi_table_prefix'];
49
 
50
			$chemin_wiki_sur_site = $this->config['appli']['chemin_wikis_integres'];
51
			$chemin_wiki = $wikini['gewi_chemin'];
52
 
53
			echo NL.'Traitement du wiki '.$chemin_wiki.NL;
54
			echo NL.$chemin_wiki.NL;
55
 
56
			if(!file_exists($chemin_wiki)) {
57
				echo 'Impossible de migrer le wikini '.$wikini['gewi_code_alpha_wikini'].' : le dossier n\'existe pas'.NL.NL.NL.NL;
58
				continue;
59
			}
60
 
61
			$chemin_base_wiki_defaut = $this->config['appli']['chemin_wiki_defaut'];
62
 
63
			$nom_admin = $this->config['appli']['nom_admin_wiki'];
64
			$mail_admin = $this->config['appli']['adresse_admin_wiki'];
65
			$pass_admin = $this->config['appli']['pass_admin_wiki'];
66
 
67
			$infos_admin = array('name' =>$nom_admin, 'email' => $mail_admin, 'password' => $pass_admin);
68
 
69
			//if(!$this->verifierPresenceTableTriple($base, $prefixe)) {
70
				echo 'migration du wiki '.$wikini['gewi_code_alpha_wikini'].NL;
71
			//	$this->creerTableTriple($base, $prefixe);
72
			//	$this->ajouterUtilisateurAdmin($base,$prefixe, $infos_admin);
73
				$this->copierFichiersWikini($chemin_base_wiki_defaut, $chemin_wiki);
74
				$this->changerVersionWakkaConfig($chemin_wiki);
75
				echo 'migration du wiki '.$wikini['gewi_code_alpha_wikini'].' effectuée '.NL.NL.NL.NL;
76
			//} else {
77
			//	echo 'wiki dejà à jour '.$wikini['gewi_code_alpha_wikini'].NL.NL.NL.NL;
78
			//}
79
		}
80
	}
81
 
82
	private function verifierPresenceTableTriple($base, $prefixe) {
83
 
84
		$requete_presence_table = 'SELECT COUNT(*) as table_existe FROM information_schema.tables
85
		WHERE TABLE_SCHEMA = "'.$base.'" AND TABLE_NAME = "'.$prefixe.'triples"';
86
 
87
		$resultat_presence_table = $this->executerRequete($requete_presence_table);
88
 
89
		$presence_table = false;
90
 
91
		foreach($resultat_presence_table as $table_est_presente) {
92
			if($table_est_presente['table_existe'] >= 1) {
93
				$presence_table = true;
94
				break;
95
			}
96
		}
97
 
98
		return $presence_table;
99
	}
100
 
101
	private function creerTableTriple($base, $prefixe) {
102
 
103
		echo 'Création de la table triple '.NL;
104
 
105
		$requete_creation_table = 'CREATE TABLE '.$base.'.'.$prefixe.'triples (' .
106
			' id int(10) unsigned NOT NULL auto_increment,' .
107
			' resource varchar(255) NOT NULL default \'\',' .
108
			' property varchar(255) NOT NULL default \'\',' .
109
			' value text NOT NULL default \'\',' .
110
			' PRIMARY KEY  (id),' .
111
			' KEY resource (resource),' .
112
			' KEY property (property)' .
113
			') TYPE=MyISAM';
114
 
115
		return $this->executerRequeteManip($requete_creation_table);
116
	}
117
 
118
	private function verifierPresenceTableUsers($base, $prefixe) {
119
 
120
		$requete_presence_table = 'SELECT COUNT(*) as table_existe FROM information_schema.tables
121
		WHERE TABLE_SCHEMA = "'.$base.'" AND TABLE_NAME = "'.$prefixe.'users"';
122
 
123
		$resultat_presence_table = $this->executerRequete($requete_presence_table);
124
 
125
		$presence_table = false;
126
 
127
		foreach($resultat_presence_table as $table_est_presente) {
128
			if($table_est_presente['table_existe'] >= 1) {
129
				$presence_table = true;
130
				break;
131
			}
132
		}
133
 
134
		return $presence_table;
135
	}
136
 
137
	private function ajouterUtilisateurAdmin($base, $prefixe, $infos_admin) {
138
 
139
		if(!$this->verifierPresenceTableUsers($base, $prefixe)) {
140
			echo 'Impossible d\'ajouter l\'adminwiki : '.$prefixe.'_users n\'existe_pas'.NL;
141
			return;
142
		}
143
 
144
		echo 'Ajout de l\'utilisateur wikiAdmin '.NL;
145
 
146
		$requete_suppression_si_admin_present = 'DELETE FROM '.$base.'.'.$prefixe.'users '.
147
												'WHERE name ="'.$infos_admin['name'].'"';
148
 
149
		$this->executerRequeteManip($requete_suppression_si_admin_present);
150
 
151
		$requete_creation_admin = "INSERT INTO ".$base.'.'.$prefixe."users SET ".
152
		"signuptime = now(), ".
153
		"name = '".$infos_admin['name']."', ".
154
		"email = '".$infos_admin['email']."', ".
155
		"password = md5('".$infos_admin['password']."')";
156
 
157
		return $this->executerRequeteManip($requete_creation_admin);
158
	}
159
 
160
	private function changerVersionWakkaConfig($chemin_wikini) {
161
 
162
		if(file_exists($chemin_wikini.'/wakka.config.php')) {
163
 
164
			$contenu_wakka_config = file_get_contents($chemin_wikini.'/wakka.config.php');
165
			$contenu_wakka_config_maj = str_replace('"wikini_version" => "0.4.3"','"wikini_version" => "0.5.0"',$contenu_wakka_config);
166
			$contenu_wakka_config_maj = str_replace("'wikini_version' => '0.4.3'","'wikini_version' => '0.5.0'",$contenu_wakka_config_maj);
167
 
168
			if(self::dry_run) {
169
				echo 'Wakka Config mis à jour '.$contenu_wakka_config_maj.NL;
170
			} else {
171
				file_put_contents($chemin_wikini.'/wakka.config.php', $contenu_wakka_config_maj);
172
			}
173
 
174
			echo 'Changement de la version de wikini '.NL;
175
		} else {
176
			echo 'Attention ! le fichier '.$chemin_wikini.'/wakka.config.php n\'existe pas '.NL;
177
		}
178
	}
179
 
180
	private function copierFichiersWikini($chemin_base_wiki_defaut, $chemin_wiki_dest) {
181
 
182
		echo 'copie de '.$chemin_base_wiki_defaut.' vers '.$chemin_wiki_dest.NL;
183
		if(self::dry_run) {
184
			$copie_recursive = true;
185
		} else {
186
			if(trim($chemin_base_wiki_defaut) != '/' && trim($chemin_wiki_dest) != '/') {
187
				echo " cp -Rf -a ".$chemin_base_wiki_defaut."* ".$chemin_wiki_dest."/ 2>&1 ";
188
				$copie_recursive = shell_exec( " cp -Rf -a ".$chemin_base_wiki_defaut."* ".$chemin_wiki_dest."/ 2>&1 " );
189
			}
190
		}
191
 
192
		return $copie_recursive;
193
	}
194
}
195
?>