390 |
aurelien |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class MigrationWiki extends ProjetService {
|
|
|
4 |
|
|
|
5 |
const dry_run = false;
|
|
|
6 |
|
|
|
7 |
public function __construct($config, $demarrer_session= true) {
|
|
|
8 |
parent::__construct($config, $demarrer_session);
|
|
|
9 |
define('NL',"\n");
|
|
|
10 |
}
|
|
|
11 |
|
|
|
12 |
private function executerRequeteManip($requete, $mode_fetch = false) {
|
|
|
13 |
// Fonction de commodité pour afficher les requetes au lieu de les executer
|
|
|
14 |
if (self::dry_run) {
|
|
|
15 |
echo '<pre>'.str_replace('),','),'.NL, $requete).'</pre>';
|
|
|
16 |
return true;
|
|
|
17 |
} else {
|
|
|
18 |
if($mode_fetch) {
|
|
|
19 |
return $this->bdd->query($requete)->fetchAll($mode_fetch);
|
|
|
20 |
} else {
|
|
|
21 |
return $this->bdd->query($requete);
|
|
|
22 |
}
|
|
|
23 |
}
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
private function executerRequete($requete, $mode_fetch = false) {
|
|
|
27 |
if (self::dry_run) {
|
|
|
28 |
echo '<pre>'.str_replace('),','),'.NL, $requete).'</pre>'.NL.NL;
|
|
|
29 |
}
|
|
|
30 |
return $this->bdd->query($requete)->fetchAll($mode_fetch);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
public function getElement($uid){
|
|
|
34 |
|
|
|
35 |
$nom_admin = $this->config['appli']['nom_admin_wiki'];
|
|
|
36 |
$mail_admin = $this->config['appli']['adresse_admin_wiki'];
|
|
|
37 |
$pass_admin = $this->config['appli']['pass_admin_wiki'];
|
|
|
38 |
|
|
|
39 |
$requete_projets_wikinis = 'SELECT * FROM gen_wikini';
|
|
|
40 |
$wikinis_projets = $this->executerRequete($requete_projets_wikinis, PDO::FETCH_ASSOC);
|
|
|
41 |
|
|
|
42 |
foreach($wikinis_projets as $wikini) {
|
|
|
43 |
|
|
|
44 |
// test sur un wiki
|
|
|
45 |
/*if($wikini['gewi_code_alpha_wikini'] != 'bibliobota') {
|
|
|
46 |
continue;
|
|
|
47 |
}*/
|
|
|
48 |
|
|
|
49 |
$base = $wikini['gewi_bdd_nom'];
|
|
|
50 |
$prefixe = $wikini['gewi_table_prefix'];
|
|
|
51 |
|
|
|
52 |
$chemin_wiki_sur_site = $this->config['appli']['chemin_wikis_integres'];
|
|
|
53 |
//$chemin_wiki = $wikini['gewi_chemin'];
|
|
|
54 |
$chemin_wiki = '/home/aurelien/web/wikini/'.$wikini['gewi_code_alpha_wikini'];
|
|
|
55 |
|
|
|
56 |
echo NL.'Traitement du wiki '.$chemin_wiki.NL;
|
|
|
57 |
echo NL.$chemin_wiki.NL;
|
|
|
58 |
|
|
|
59 |
if(!file_exists($chemin_wiki)) {
|
|
|
60 |
echo 'Impossible de migrer le wikini '.$wikini['gewi_code_alpha_wikini'].' : le dossier n\'existe pas'.NL.NL.NL.NL;
|
|
|
61 |
continue;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$chemin_base_wiki_defaut = $this->config['appli']['chemin_wiki_defaut'];
|
|
|
65 |
|
|
|
66 |
$infos_admin = array('name' =>$nom_admin, 'email' => $mail_admin, 'password' => $pass_admin);
|
|
|
67 |
|
|
|
68 |
if(!$this->verifierPresenceTableTriple($base, $prefixe)) {
|
|
|
69 |
echo 'migration du wiki '.$wikini['gewi_code_alpha_wikini'].NL;
|
|
|
70 |
$this->creerTableTriple($base, $prefixe);
|
|
|
71 |
$this->ajouterUtilisateurAdmin($base,$prefixe, $infos_admin);
|
|
|
72 |
$this->copierFichiersWikini($chemin_base_wiki_defaut, $chemin_wiki);
|
|
|
73 |
$this->changerVersionWakkaConfig($chemin_wiki);
|
|
|
74 |
|
|
|
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 |
$this->ajoutPageFormatees($chemin_wiki, $infos_admin);
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
private function verifierPresenceTableTriple($base, $prefixe) {
|
|
|
85 |
|
|
|
86 |
$requete_presence_table = 'SELECT COUNT(*) as table_existe FROM information_schema.tables
|
|
|
87 |
WHERE TABLE_SCHEMA = "'.$base.'" AND TABLE_NAME = "'.$prefixe.'triples"';
|
|
|
88 |
|
|
|
89 |
$resultat_presence_table = $this->executerRequete($requete_presence_table);
|
|
|
90 |
|
|
|
91 |
$presence_table = false;
|
|
|
92 |
|
|
|
93 |
foreach($resultat_presence_table as $table_est_presente) {
|
|
|
94 |
if($table_est_presente['table_existe'] >= 1) {
|
|
|
95 |
$presence_table = true;
|
|
|
96 |
break;
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
return $presence_table;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
private function creerTableTriple($base, $prefixe) {
|
|
|
104 |
|
|
|
105 |
echo 'Création de la table triple '.NL;
|
|
|
106 |
|
|
|
107 |
$requete_creation_table = 'CREATE TABLE '.$base.'.'.$prefixe.'triples (' .
|
|
|
108 |
' id int(10) unsigned NOT NULL auto_increment,' .
|
|
|
109 |
' resource varchar(255) NOT NULL default \'\',' .
|
|
|
110 |
' property varchar(255) NOT NULL default \'\',' .
|
|
|
111 |
' value text NOT NULL default \'\',' .
|
|
|
112 |
' PRIMARY KEY (id),' .
|
|
|
113 |
' KEY resource (resource),' .
|
|
|
114 |
' KEY property (property)' .
|
|
|
115 |
') TYPE=MyISAM';
|
|
|
116 |
|
|
|
117 |
return $this->executerRequeteManip($requete_creation_table);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
private function verifierPresenceTableUsers($base, $prefixe) {
|
|
|
121 |
|
|
|
122 |
$requete_presence_table = 'SELECT COUNT(*) as table_existe FROM information_schema.tables
|
|
|
123 |
WHERE TABLE_SCHEMA = "'.$base.'" AND TABLE_NAME = "'.$prefixe.'users"';
|
|
|
124 |
|
|
|
125 |
$resultat_presence_table = $this->executerRequete($requete_presence_table);
|
|
|
126 |
|
|
|
127 |
$presence_table = false;
|
|
|
128 |
|
|
|
129 |
foreach($resultat_presence_table as $table_est_presente) {
|
|
|
130 |
if($table_est_presente['table_existe'] >= 1) {
|
|
|
131 |
$presence_table = true;
|
|
|
132 |
break;
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
return $presence_table;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
private function ajouterUtilisateurAdmin($base, $prefixe, $infos_admin) {
|
|
|
140 |
|
|
|
141 |
if(!$this->verifierPresenceTableUsers($base, $prefixe)) {
|
|
|
142 |
echo 'Impossible d\'ajouter l\'adminwiki : '.$prefixe.'_users n\'existe_pas'.NL;
|
|
|
143 |
return;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
echo 'Ajout de l\'utilisateur wikiAdmin '.NL;
|
|
|
147 |
|
|
|
148 |
$requete_suppression_si_admin_present = 'DELETE FROM '.$base.'.'.$prefixe.'users '.
|
|
|
149 |
'WHERE name ="'.$infos_admin['name'].'"';
|
|
|
150 |
|
|
|
151 |
$this->executerRequeteManip($requete_suppression_si_admin_present);
|
|
|
152 |
|
|
|
153 |
$requete_creation_admin = "insert into ".$base.'.'.$prefixe."users set ".
|
|
|
154 |
"signuptime = now(), ".
|
|
|
155 |
"name = '".$infos_admin['name']."', ".
|
|
|
156 |
"email = '".$infos_admin['email']."', ".
|
|
|
157 |
"password = md5('".$infos_admin['password']."')";
|
|
|
158 |
|
|
|
159 |
return $this->executerRequeteManip($requete_creation_admin);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
private function changerVersionWakkaConfig($chemin_wikini) {
|
|
|
163 |
|
|
|
164 |
if(file_exists($chemin_wikini.'wakka.config.php')) {
|
|
|
165 |
|
|
|
166 |
$contenu_wakka_config = file_get_contents($chemin_wikini.'wakka.config.php');
|
|
|
167 |
$contenu_wakka_config_maj = str_replace('"wikini_version" => "0.4.3"','"wikini_version" => "0.5.0"',$contenu_wakka_config);
|
|
|
168 |
$contenu_wakka_config_maj = str_replace("'wikini_version' => '0.4.3'","'wikini_version' => '0.5.0'",$contenu_wakka_config_maj);
|
|
|
169 |
|
|
|
170 |
if(self::dry_run) {
|
|
|
171 |
echo 'Wakka Config mis à jour '.$contenu_wakka_config_maj.NL;
|
|
|
172 |
} else {
|
|
|
173 |
//file_put_contents($chemin_wikini.'wakka.config.php', $contenu_wakka_config_maj);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
echo 'Changement de la version de wikini '.NL;
|
|
|
177 |
} else {
|
|
|
178 |
echo 'Attention ! le fichier '.$chemin_wikini.'wakka.config.php n\'existe pas '.NL;
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
private function copierFichiersWikini($chemin_base_wiki_defaut, $chemin_wiki_dest) {
|
|
|
183 |
|
|
|
184 |
if(self::dry_run) {
|
|
|
185 |
echo 'copie de '.$chemin_base_wiki_defaut.' vers '.$chemin_wiki_dest.NL;
|
|
|
186 |
$copie_recursive = true;
|
|
|
187 |
} else {
|
|
|
188 |
if(trim($chemin_base_wiki_defaut) != '/' && trim($chemin_wiki_dest) != '/') {
|
|
|
189 |
$copie_recursive = shell_exec( " cp -Rf -a ".$chemin_base_wiki_defaut."* ".$chemin_wiki_dest." 2>&1 " );
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
return $copie_recursive;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
private function ajoutPageFormatees($chemin_wikini, $utilisateur) {
|
|
|
197 |
|
|
|
198 |
$chemin_base_wiki_defaut = $this->config['appli']['chemin_wiki_defaut'];
|
|
|
199 |
require_once($chemin_base_wiki_defaut.'wakka_class.php');
|
|
|
200 |
|
|
|
201 |
$nom_admin = $this->config['appli']['nom_admin_wiki'];
|
|
|
202 |
|
|
|
203 |
$chemin_wikini = rtrim($chemin_wikini,'/');
|
|
|
204 |
require_once($chemin_wikini.'/wakka.config.php');
|
|
|
205 |
|
|
|
206 |
// la variable $wakkaConfig est contenu dans le fichier wakka.config.php
|
|
|
207 |
$config = $wakkaConfig;
|
|
|
208 |
|
|
|
209 |
$wiki = new Wiki($config);
|
|
|
210 |
$wiki->SetGroupACL("admins", $nom_admin);
|
|
|
211 |
|
|
|
212 |
$wiki->setUser($utilisateur);
|
|
|
213 |
|
|
|
214 |
//insertion des pages de documentation et des pages standards
|
|
|
215 |
$d = dir($chemin_base_wiki_defaut.'/setup/doc/');
|
|
|
216 |
|
|
|
217 |
while ($doc = $d->read()){
|
|
|
218 |
|
|
|
219 |
if (is_dir($doc) || substr($doc, -4) != '.txt') {continue;}
|
|
|
220 |
|
|
|
221 |
$pagecontent = implode ('', file($chemin_base_wiki_defaut.'/setup/doc/'.$doc));
|
|
|
222 |
$pagecontent = mb_convert_encoding($pagecontent,'UTF-8','ISO-8859-15');
|
|
|
223 |
if ($doc=='_root_page.txt'){
|
|
|
224 |
$pagename = $config["root_page"];
|
|
|
225 |
}else{
|
|
|
226 |
$pagename = substr($doc,0,strpos($doc,'.txt'));
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
$base = $config['mysql_database'];
|
|
|
230 |
|
|
|
231 |
$requete_existence_page = "Select tag from ".$base.".".$config['table_prefix']."pages where tag='$pagename'";
|
|
|
232 |
$existence_page = $this->executerRequete($requete_existence_page);
|
|
|
233 |
if (is_array($existence_page) && count($existence_page) > 0) {
|
|
|
234 |
echo 'la page '.$pagename.' existe déjà '.NL;
|
|
|
235 |
} else {
|
|
|
236 |
|
|
|
237 |
echo 'Ajout de la page '.$pagename.NL;
|
|
|
238 |
|
|
|
239 |
$requete_maj_pages_anciennes = "UPDATE ".$base.".".$config['table_prefix']."pages ".
|
|
|
240 |
"SET ".
|
|
|
241 |
"latest = 'N'".
|
|
|
242 |
"WHERE tag = '$pagename'";
|
|
|
243 |
|
|
|
244 |
$reussite = $this->executerRequeteManip($requete_maj_pages_anciennes);
|
|
|
245 |
|
|
|
246 |
$requete_insertion_page = "INSERT INTO ".$base.".".$config['table_prefix']."pages ".
|
|
|
247 |
"SET tag = '$pagename', ".
|
|
|
248 |
"body = ".$this->bdd->quote($pagecontent).", ".
|
|
|
249 |
"user = '".$nom_admin."', ".
|
|
|
250 |
"owner = '".$nom_admin."', " .
|
|
|
251 |
"time = now(), ".
|
|
|
252 |
"latest = 'Y'";
|
|
|
253 |
|
|
|
254 |
$reussite = $this->executerRequeteManip($requete_insertion_page);
|
|
|
255 |
|
|
|
256 |
if(!$reussite) {
|
|
|
257 |
echo 'echec de l\'ajout de la page '.$pagename.NL;
|
|
|
258 |
} else {
|
|
|
259 |
|
|
|
260 |
// mise à jour des liens entre pages
|
|
|
261 |
$wiki->SetPage($wiki->LoadPage($pagename,"",0));
|
|
|
262 |
$wiki->ClearLinkTable();
|
|
|
263 |
$wiki->StartLinkTracking();
|
|
|
264 |
$wiki->TrackLinkTo($pagename);
|
|
|
265 |
$wiki->StopLinkTracking();
|
|
|
266 |
$wiki->WriteLinkTable();
|
|
|
267 |
$wiki->ClearLinkTable();
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
?>
|