e5be17836c4f2b0927ac7ea33c80eda5999fb0d5
[mw_sourceml] / app / data / modules / sql / sml_data_install.php
1 <?php
2
3   class sml_data_install extends mw_data{
4
5     // ---------------------------------------------------------------------------------
6     //                                                                           install
7     //
8
9     function mw_sourceml_install_mysql($plugin, $env){
10       $sgbd = $this->sgbd();
11       try{
12         $EXISTS =
13             $sgbd->table_exists("#--sml_authors")
14         ||  $sgbd->table_exists("#--sml_classes")
15         ||  $sgbd->table_exists("#--sml_licences")
16         ||  $sgbd->table_exists("#--sml_sources")
17         ||  $sgbd->table_exists("#--sml_sources_access")
18         ||  $sgbd->table_exists("#--sml_sources_authors")
19         ||  $sgbd->table_exists("#--sml_sources_infos")
20         ||  $sgbd->table_exists("#--sml_source_cache")
21         ||  $sgbd->table_exists("#--sml_source_compositions")
22         ||  $sgbd->table_exists("#--sml_source_derivations")
23         ||  $sgbd->table_exists("#--sml_source_documents")
24         ||  $sgbd->table_exists("#--sml_sources_invitations");
25       }
26       catch(Exception $e){
27         return "impossible de savoir si les tables existent deja";
28       }
29       if($EXISTS){
30         return "des tables existent deja en base. installation annulee";
31       }
32       if(!($version = $env->version("mw_sourceml"))){
33         return "impossible de lire la version de sourceml";
34       }
35       try{
36         $sql =
37          "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\"";
38         $sgbd->query($sql);
39
40         $sql =
41          "CREATE TABLE `#--sml_authors`("
42         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
43         ."  `id_user` int(11) NOT NULL,"
44         ."  `nom` varchar(255) NOT NULL,"
45         ."  `image` varchar(255) DEFAULT NULL,"
46         ."  `description` text,"
47         ."  `email` varchar(255) NOT NULL,"
48         ."  `contact_form` tinyint(4) NOT NULL,"
49         ."  `captcha` tinyint(4) NOT NULL,"
50         ."  PRIMARY KEY (`id`),"
51         ."  KEY `id_user` (`id_user`)"
52         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
53         $sgbd->query($sql);
54
55         $sql =
56          "CREATE TABLE `#--sml_classes`("
57         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
58         ."  `nom` varchar(255) NOT NULL,"
59         ."  PRIMARY KEY (`id`)"
60         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
61         $sgbd->query($sql);
62
63         $sql =
64          "CREATE TABLE `#--sml_licences`("
65         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
66         ."  `nom` varchar(255) NOT NULL,"
67         ."  `url` varchar(255) NOT NULL,"
68         ."  PRIMARY KEY (`id`)"
69         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
70         $sgbd->query($sql);
71
72         $sql =
73          "CREATE TABLE `#--sml_sources`("
74         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
75         ."  `id_class` int(11) NOT NULL,"
76         ."  `reference` varchar(255) DEFAULT NULL,"
77         ."  `titre` varchar(255) DEFAULT NULL,"
78         ."  `licence` int(11) DEFAULT NULL,"
79         ."  `date_creation` date DEFAULT NULL,"
80         ."  `date_inscription` datetime NOT NULL,"
81         ."  PRIMARY KEY (`id`),"
82         ."  KEY `id_class` (`id_class`),"
83         ."  KEY `licence` (`licence`)"
84         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
85         $sgbd->query($sql);
86
87         $sql =
88          "CREATE TABLE `#--sml_sources_access`("
89         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
90         ."  `nom` varchar(255) NOT NULL,"
91         ."  `intitule` varchar(255) NOT NULL,"
92         ."  PRIMARY KEY (`id`)"
93         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
94         $sgbd->query($sql);
95
96         $sql =
97          "CREATE TABLE `#--sml_sources_authors`("
98         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
99         ."  `id_source` int(11) NOT NULL,"
100         ."  `id_author` int(11) NOT NULL,"
101         ."  `id_sources_access` int(11) NOT NULL,"
102         ."  PRIMARY KEY (`id`),"
103         ."  KEY `id_object` (`id_source`),"
104         ."  KEY `id_author` (`id_author`),"
105         ."  KEY `id_sources_access` (`id_sources_access`)"
106         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
107         $sgbd->query($sql);
108
109         $sql =
110          "CREATE TABLE `#--sml_sources_infos`("
111         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
112         ."  `id_source` int(11) NOT NULL,"
113         ."  `key` varchar(255) NOT NULL,"
114         ."  `value` text NOT NULL,"
115         ."  PRIMARY KEY (`id`),"
116         ."  KEY `id_source` (`id_source`)"
117         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
118         $sgbd->query($sql);
119
120         $sql =
121          "CREATE TABLE `#--sml_source_cache`("
122         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
123         ."  `url` varchar(255) NOT NULL,"
124         ."  `id_source` int(11) NOT NULL,"
125         ."  `last_update` datetime NOT NULL,"
126         ."  PRIMARY KEY (`id`)"
127         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
128         $sgbd->query($sql);
129
130         $sql =
131          "CREATE TABLE `#--sml_source_compositions`("
132         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
133         ."  `id_source` int(11) NOT NULL,"
134         ."  `id_composition` int(11) NOT NULL,"
135         ."  PRIMARY KEY (`id`),"
136         ."  KEY `id_source` (`id_source`),"
137         ."  KEY `id_composition` (`id_composition`)"
138         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
139         $sgbd->query($sql);
140
141         $sql =
142          "CREATE TABLE `#--sml_source_derivations`("
143         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
144         ."  `id_source` int(11) NOT NULL,"
145         ."  `derivation` varchar(255) NOT NULL,"
146         ."  PRIMARY KEY (`id`),"
147         ."  KEY `derivation` (`derivation`)"
148         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
149         $sgbd->query($sql);
150
151         $sql =
152          "CREATE TABLE `#--sml_source_documents`("
153         ."  `id` int(11) NOT NULL AUTO_INCREMENT,"
154         ."  `id_source` int(11) NOT NULL,"
155         ."  `nom` varchar(255) NOT NULL,"
156         ."  `url` varchar(255) NOT NULL,"
157         ."  PRIMARY KEY (`id`),"
158         ."  KEY `id_source` (`id_source`)"
159         .") DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
160         $sgbd->query($sql);
161
162         $sql =
163          "CREATE TABLE `#--sml_sources_invitations`("
164         ."  `id` INT(11) NOT NULL AUTO_INCREMENT,"
165         ."  `id_user` INT(11) NOT NULL,"
166         ."  `id_source` INT(11) NOT NULL,"
167         ."  `id_author` INT(11) NOT NULL,"
168         ."  `id_sources_access` INT(11) NOT NULL,"
169         ."  `date_invitation` datetime NOT NULL,"
170         ."  PRIMARY KEY (`id`)"
171         .")";
172         $sgbd->query($sql);
173
174       }
175       catch(Exception $e){
176         return "imposible de creer les tables en base";
177       }
178       try{
179         $sql =
180          "INSERT INTO `#--sml_licences` (`id`, `nom`, `url`) VALUES"
181         ."(1, 'Creative commons by-sa 2.0', 'http://creativecommons.org/licenses/by-sa/2.0/deed.fr'),"
182         ."(2, 'Creative Commons by-nc-nd 2.5', 'http://creativecommons.org/licenses/by-nc-nd/2.5/'),"
183         ."(3, 'Creative Commons by-nc-sa 2.5', 'http://creativecommons.org/licenses/by-nc-sa/2.5/'),"
184         ."(4, 'Creative Commons by-nc 2.5', 'http://creativecommons.org/licenses/by-nc/2.5/'),"
185         ."(5, 'Creative Commons by-nd 2.5', 'http://creativecommons.org/licenses/by-nd/2.5/'),"
186         ."(6, 'Creative Commons by-sa 2.5', 'http://creativecommons.org/licenses/by-sa/2.5/'),"
187         ."(7, 'Creative Commons by 2.5', 'http://creativecommons.org/licenses/by/2.5/'),"
188         ."(8, 'Licence Art Libre', 'http://artlibre.org/licence/lal/'),"
189         ."(9, 'Licence C Reaction', 'http://morne.free.fr/Necktar7/creactionfr.htm'),"
190         ."(10, 'Yellow OpenMusic License', 'http://openmusic.linuxtag.org/yellow.html'),"
191         ."(11, 'Green OpenMusic License', 'http://openmusic.linuxtag.org/green.html')";
192         $sgbd->query($sql);
193
194         $sql =
195          "INSERT INTO `#--sml_classes` (`id`, `nom`) VALUES"
196         ."(1, 'album'),"
197         ."(2, 'morceau'),"
198         ."(3, 'piste')";
199         $sgbd->query($sql);
200
201         $sql =
202          "INSERT INTO `#--sml_sources_access` (`id`, `nom`, `intitule`) VALUES"\r
203         ."(1, 'admin', 'administrateur'),"\r
204         ."(2, 'editeur', 'éditeur'),"\r
205         ."(3, 'contributeur', 'contributeur')";
206         $sgbd->query($sql);
207
208         $sql =
209          "INSERT INTO `#--versions` (`application`, `version`) VALUES"\r
210         ."('mw_sourceml', ".$this->eq($version).")";
211         $sgbd->query($sql);
212
213       }
214       catch(Exception $e){
215         return "les tables ont ete ajoutees en base mais impossible d'y enregistrer les valeurs par defaut.";
216       }
217       return true;
218     }
219
220     function mw_sourceml_install_sqlite($plugin, $env){
221       $sgbd = $this->sgbd();
222       try{
223         $EXISTS =
224             $sgbd->table_exists("#--sml_authors")
225         ||  $sgbd->table_exists("#--sml_classes")
226         ||  $sgbd->table_exists("#--sml_licences")
227         ||  $sgbd->table_exists("#--sml_sources")
228         ||  $sgbd->table_exists("#--sml_sources_access")
229         ||  $sgbd->table_exists("#--sml_sources_authors")
230         ||  $sgbd->table_exists("#--sml_sources_infos")
231         ||  $sgbd->table_exists("#--sml_source_cache")
232         ||  $sgbd->table_exists("#--sml_source_compositions")
233         ||  $sgbd->table_exists("#--sml_source_derivations")
234         ||  $sgbd->table_exists("#--sml_source_documents")
235         ||  $sgbd->table_exists("#--sml_sources_invitations");
236       }
237       catch(Exception $e){
238         return "impossible de savoir si les tables existent deja";
239       }
240       if($EXISTS){
241         return "des tables existent deja en base. installation annulee";
242       }
243       if(!($version = $env->version("mw_sourceml"))){
244         return "impossible de lire la version de sourceml";
245       }
246       try{
247
248         $sql =
249          "CREATE TABLE #--sml_authors("
250         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
251         ."  `id_user` INTEGER NOT NULL,"
252         ."  `nom` TEXT NOT NULL,"
253         ."  `image` TEXT DEFAULT NULL,"
254         ."  `description` TEXT,"
255         ."  `email` TEXT NOT NULL,"
256         ."  `contact_form` INTEGER NOT NULL,"
257         ."  `captcha` INTEGER NOT NULL"
258         .")";
259         $sgbd->query($sql);
260
261         $sql =
262          "CREATE TABLE #--sml_classes("
263         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
264         ."  `nom` TEXT NOT NULL"
265         .")";
266         $sgbd->query($sql);
267
268         $sql =
269          "CREATE TABLE #--sml_licences("
270         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
271         ."  `nom` TEXT NOT NULL,"
272         ."  `url` TEXT NOT NULL"
273         .")";
274         $sgbd->query($sql);
275
276         $sql =
277          "CREATE TABLE #--sml_sources("
278         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
279         ."  `id_class` INTEGER NOT NULL,"
280         ."  `reference` TEXT DEFAULT NULL,"
281         ."  `titre` TEXT DEFAULT NULL,"
282         ."  `licence` INTEGER DEFAULT NULL,"
283         ."  `date_creation` TEXT DEFAULT NULL,"
284         ."  `date_inscription` TEXT NOT NULL"
285         .")";
286         $sgbd->query($sql);
287
288         $sql =
289          "CREATE TABLE #--sml_sources_access("
290         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
291         ."  `nom` TEXT NOT NULL,"
292         ."  `intitule` TEXT NOT NULL"
293         .")";
294         $sgbd->query($sql);
295
296         $sql =
297          "CREATE TABLE #--sml_sources_authors("
298         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
299         ."  `id_source` INTEGER NOT NULL,"
300         ."  `id_author` INTEGER NOT NULL,"
301         ."  `id_sources_access` INTEGER NOT NULL"
302         .")";
303         $sgbd->query($sql);
304
305         $sql =
306          "CREATE TABLE #--sml_sources_infos("
307         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
308         ."  `id_source` INTEGER NOT NULL,"
309         ."  `key` TEXT NOT NULL,"
310         ."  `value` TEXT NOT NULL"
311         .")";
312         $sgbd->query($sql);
313
314         $sql =
315          "CREATE TABLE #--sml_source_cache("
316         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
317         ."  `url` TEXT NOT NULL,"
318         ."  `id_source` INTEGER NOT NULL,"
319         ."  `last_update` TEXT NOT NULL"
320         .")";
321         $sgbd->query($sql);
322
323         $sql =
324          "CREATE TABLE #--sml_source_compositions("
325         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
326         ."  `id_source` INTEGER NOT NULL,"
327         ."  `id_composition` INTEGER NOT NULL"
328         .")";
329         $sgbd->query($sql);
330
331         $sql =
332          "CREATE TABLE #--sml_source_derivations("
333         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
334         ."  `id_source` INTEGER NOT NULL,"
335         ."  `derivation` TEXT NOT NULL"
336         .")";
337         $sgbd->query($sql);
338
339         $sql =
340          "CREATE TABLE #--sml_source_documents("
341         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
342         ."  `id_source` INTEGER NOT NULL,"
343         ."  `nom` TEXT NOT NULL,"
344         ."  `url` TEXT NOT NULL"
345         .")";
346         $sgbd->query($sql);
347
348         $sql =
349          "CREATE TABLE #--sml_sources_invitations("
350         ."  `id` INTEGER PRIMARY KEY AUTOINCREMENT,"
351         ."  `id_user` INTEGER NOT NULL,"
352         ."  `id_source` INTEGER NOT NULL,"
353         ."  `id_author` INTEGER NOT NULL,"
354         ."  `id_sources_access` INTEGER NOT NULL,"
355         ."  `date_invitation` TEXT NOT NULL"
356         .")";
357         $sgbd->query($sql);
358
359       }
360       catch(Exception $e){
361         return "imposible de creer les tables en base";
362       }
363       try{
364         $sql =
365          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
366         ." VALUES(1, 'Creative commons by-sa 2.0', 'http://creativecommons.org/licenses/by-sa/2.0/deed.fr')";
367         $sgbd->query($sql);
368
369         $sql =
370          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
371         ." VALUES (2, 'Creative Commons by-nc-nd 2.5', 'http://creativecommons.org/licenses/by-nc-nd/2.5/')";
372         $sgbd->query($sql);
373
374         $sql =
375          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
376         ." VALUES (3, 'Creative Commons by-nc-sa 2.5', 'http://creativecommons.org/licenses/by-nc-sa/2.5/')";
377         $sgbd->query($sql);
378
379         $sql =
380          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
381         ." VALUES (4, 'Creative Commons by-nc 2.5', 'http://creativecommons.org/licenses/by-nc/2.5/')";
382         $sgbd->query($sql);
383
384         $sql =
385          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
386         ." VALUES (5, 'Creative Commons by-nd 2.5', 'http://creativecommons.org/licenses/by-nd/2.5/')";
387         $sgbd->query($sql);
388
389         $sql =
390          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
391         ." VALUES (6, 'Creative Commons by-sa 2.5', 'http://creativecommons.org/licenses/by-sa/2.5/')";
392         $sgbd->query($sql);
393
394         $sql =
395          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
396         ." VALUES (7, 'Creative Commons by 2.5', 'http://creativecommons.org/licenses/by/2.5/')";
397         $sgbd->query($sql);
398
399         $sql =
400          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
401         ." VALUES (8, 'Licence Art Libre', 'http://artlibre.org/licence/lal/')";
402         $sgbd->query($sql);
403
404         $sql =
405          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
406         ." VALUES (9, 'Licence C Reaction', 'http://morne.free.fr/Necktar7/creactionfr.htm')";
407         $sgbd->query($sql);
408
409         $sql =
410          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
411         ." VALUES (10, 'Yellow OpenMusic License', 'http://openmusic.linuxtag.org/yellow.html')";
412         $sgbd->query($sql);
413
414         $sql =
415          "INSERT INTO #--sml_licences(`id`, `nom`, `url`)"
416         ." VALUES (11, 'Green OpenMusic License', 'http://openmusic.linuxtag.org/green.html')";
417         $sgbd->query($sql);
418
419         $sql =
420          "INSERT INTO `#--sml_classes` (`id`, `nom`) VALUES (1, 'album')";
421         $sgbd->query($sql);
422
423         $sql =
424          "INSERT INTO `#--sml_classes` (`id`, `nom`) VALUES (2, 'morceau')";
425         $sgbd->query($sql);
426
427         $sql =
428          "INSERT INTO `#--sml_classes` (`id`, `nom`) VALUES (3, 'piste')";
429         $sgbd->query($sql);
430
431         $sql =
432          "INSERT INTO `#--sml_sources_access` (`id`, `nom`, `intitule`) VALUES (1, 'admin', 'administrateur')";\r
433         $sgbd->query($sql);
434
435         $sql =
436          "INSERT INTO `#--sml_sources_access` (`id`, `nom`, `intitule`) VALUES (2, 'editeur', 'éditeur')";
437         $sgbd->query($sql);
438
439         $sql =
440          "INSERT INTO `#--sml_sources_access` (`id`, `nom`, `intitule`) VALUES (3, 'contributeur', 'contributeur')";
441         $sgbd->query($sql);
442
443         $sql =
444          "INSERT INTO `#--versions` (`application`, `version`) VALUES ('mw_sourceml', ".$this->eq($version).")";
445         $sgbd->query($sql);
446
447       }
448       catch(Exception $e){
449         return "les tables ont ete ajoutees en base mais impossible d'y enregistrer les valeurs par defaut.";
450       }
451       return true;
452     }
453
454     // ---------------------------------------------------------------------------------
455     //                                                                         uninstall
456     //
457
458     function mw_sourceml_uninstall_sql($plugin, $env){
459       $sgbd = $this->sgbd();
460       if(!$plugin->disable($env)) return "impossible de desactiver le plugin";
461       if(!($version = $env->version("mw_sourceml"))){
462         return "impossible de lire la version de sourceml";
463       }
464       try{
465         $sgbd->query("DROP TABLE #--sml_authors");
466       }
467       catch(Exception $e){
468         return "impossible de supprimer la table #--sml_authors";
469       }
470       try{
471         $sgbd->query("DROP TABLE #--sml_classes");
472       }
473       catch(Exception $e){
474         return "impossible de supprimer la table #--sml_classes";
475       }
476       try{
477         $sgbd->query("DROP TABLE #--sml_licences");
478       }
479       catch(Exception $e){
480         return "impossible de supprimer la table #--sml_licences";
481       }
482       try{
483         $sgbd->query("DROP TABLE #--sml_sources");
484       }
485       catch(Exception $e){
486         return "impossible de supprimer la table #--sml_sources";
487       }
488       try{
489         $sgbd->query("DROP TABLE #--sml_sources_access");
490       }
491       catch(Exception $e){
492         return "impossible de supprimer la table #--sml_sources_access";
493       }
494       try{
495         $sgbd->query("DROP TABLE #--sml_sources_authors");
496       }
497       catch(Exception $e){
498         return "impossible de supprimer la table #--sml_sources_authors";
499       }
500       try{
501         $sgbd->query("DROP TABLE #--sml_sources_infos");
502       }
503       catch(Exception $e){
504         return "impossible de supprimer la table #--sml_sources_infos";
505       }
506       try{
507         $sgbd->query("DROP TABLE #--sml_source_cache");
508       }
509       catch(Exception $e){
510         return "impossible de supprimer la table #--sml_source_cache";
511       }
512       try{
513         $sgbd->query("DROP TABLE #--sml_source_compositions");
514       }
515       catch(Exception $e){
516         return "impossible de supprimer la table #--sml_source_compositions";
517       }
518       try{
519         $sgbd->query("DROP TABLE #--sml_source_derivations");
520       }
521       catch(Exception $e){
522         return "impossible de supprimer la table #--sml_source_derivations";
523       }
524       try{
525         $sgbd->query("DROP TABLE #--sml_source_documents");
526       }
527       catch(Exception $e){
528         return "impossible de supprimer la table #--sml_source_documents";
529       }
530       try{
531         $sgbd->query("DROP TABLE #--sml_sources_invitations");
532       }
533       catch(Exception $e){
534         return "impossible de supprimer la table #--sml_sources_invitations";
535       }
536       try{
537         $sgbd->query("DELETE FROM #--versions WHERE application='mw_sourceml'");
538       }
539       catch(Exception $e){
540         return "impossible de supprimer la table #--sml_sources_invitations";
541       }
542       return true;
543     }
544
545   }