50769d371380851544d59caf2cf791690d44409b
[mw_sourceml] / app / data / upgrades / sql / mw_sourceml_sql_version_1_1_2.php
1 <?php
2
3   class mw_sourceml_sql_version_1_1_2 extends mw_data_version{
4
5     function version(){
6       return "1.1.2";
7     }
8
9     function do_upgrade(){
10
11       $env = $this->env();
12       $data = $env->data();
13       $sgbd = $data->sgbd();
14
15       if($sgbd->table_exists("#--sources")){
16
17         /* ----------------------------------------------------------------------
18                                                      nouvelle table groupe_status
19         */
20
21         if(!$sgbd->table_exists("#--groupe_status")){
22           if(
23             !$data->data_create_table(
24               array(
25                 "table_name" => "groupe_status",
26                 "fields" => array(
27                   "id" => array("type" => "int", "autoincrement" => true),
28                   "nom" => array("type" => "varchar")
29                 ),
30                 "keys" => array(
31                   array(
32                     "fields" => array("id"),
33                     "primary" => true
34                   )
35                 ),
36                 "options" => array("default_charset" => "utf8")
37               )
38             )
39           ){
40             return "impossible de créer la table groupe_status";
41           }
42           try{
43             $sql = "INSERT INTO #--groupe_status(id, nom) VALUES (1, 'admin')";\r
44             $sgbd->query($sql);
45             $sql = "INSERT INTO #--groupe_status(id, nom) VALUES (2, 'editeur')";\r
46             $sgbd->query($sql);
47             $sql = "INSERT INTO #--groupe_status(id, nom) VALUES (3, 'contributeur')";
48             $sgbd->query($sql);
49           }
50           catch(Exception $e){
51             return "impossible de remplir la table groupe_status";
52           }
53         }
54
55         /* ----------------------------------------------------------------------
56                                                     nouvelle table source_groupes
57                             sources.id_groupe passe dans source_groupes.id_groupe
58         */
59
60         if(!$sgbd->table_exists("#--source_groupes")){
61           if(
62             !$data->data_create_table(
63               array(
64                 "table_name" => "source_groupes",
65                 "fields" => array(
66                   "id" => array("type" => "int", "autoincrement" => true),
67                   "id_source" => array("type" => "int"),
68                   "id_groupe" => array("type" => "int"),
69                   "id_groupe_status" => array("type" => "int")
70                 ),
71                 "keys" => array(
72                   array(
73                     "fields" => array("id"),
74                     "primary" => true
75                   )
76                 ),
77                 "options" => array("default_charset" => "utf8")
78               )
79             )
80           ){
81             return "impossible de créer la table source_groupes";
82           }
83           $sql =
84            "INSERT INTO #--source_groupes(id_source, id_groupe, id_groupe_status)"
85           ." SELECT"
86           ."  id"
87           .", id_groupe"
88           .", 1"
89           ." FROM #--sources";
90           try{
91             $sgbd->query($sql);
92           }
93           catch(Exception $e){
94             return "impossible d'importer les id_groupes dans source_groupes";
95           }
96         }
97
98         /* ----------------------------------------------------------------------
99                                                 nouvelle table source_derivations
100                          sources.derivation devient source_derivations.derivation
101         */
102
103         if(!$sgbd->table_exists("#--source_derivations")){
104           if(
105             !$data->data_create_table(
106               array(
107                 "table_name" => "source_derivations",
108                 "fields" => array(
109                   "id" => array("type" => "int", "autoincrement" => true),
110                   "id_source" => array("type" => "int"),
111                   "derivation" => array("type" => "varchar")
112                 ),
113                 "keys" => array(
114                   array(
115                     "fields" => array("id"),
116                     "primary" => true
117                   )
118                 ),
119                 "options" => array("default_charset" => "utf8")
120               )
121             )
122           ){
123             return "impossible de créer la table source_derivations";
124           }
125           $sql =
126            "INSERT INTO #--source_derivations(id_source, derivation)"
127           ." SELECT"
128           ."  id"
129           .", derivation"
130           ." FROM #--sources WHERE derivation IS NOT NULL";
131           try{
132             $sgbd->query($sql);
133           }
134           catch(Exception $e){
135             return "impossible d'importer les derivation dans source_derivations";
136           }
137         }
138
139         /* ----------------------------------------------------------------------
140                                                                           sources
141                                             maj source_infos : image avec id user
142         */
143
144         $SOURCE_HAS_ID_GROUPE = false;
145         try{
146           $sources_desc = $sgbd->desc_table("#--sources");
147           if(isset($sources_desc["attributs"]["id_groupe"])){
148             $SOURCE_HAS_ID_GROUPE = true;
149           }
150         }
151         catch(Exception $e){
152           return "impossible de lire les champs de la table sources";
153         }
154         if($SOURCE_HAS_ID_GROUPE){
155           try{
156             $sql =
157              "SELECT #--sources.id as id_source, #--sources.id_groupe"
158             .", #--source_infos.id as id_source_info, #--source_infos.`value` as image"
159             ." FROM #--sources"
160             ." LEFT JOIN #--source_infos"
161             ." ON (#--source_infos.id_source=#--sources.id AND #--source_infos.`key`='image')";
162             $rst = $sgbd->query($sql);
163             while($v_rst = $sgbd->fetch_assoc($rst)){
164               if($v_rst["image"] && !preg_match("/^[0-9]+\/.*$/", $v_rst["image"])){
165                 if(
166                   (
167                     $groupe = $data->data_read(
168                       array(
169                         "table_name" => "groupes",
170                         "index_name" => "id",
171                         "index_value" => $v_rst["id_groupe"]
172                       )
173                     )
174                   ) === false
175                 ){
176                   return "erreur lors de la lecture de l'utilisateur du groupe";
177                 }
178                 if(
179                   !$data->data_update(
180                     array(
181                       "table_name" => "source_infos",
182                       "index_name" => "id",
183                       "index_value" => $v_rst["id_source_info"],
184                       "values" => array(
185                         "value" => $groupe["id_user"]."/".$v_rst["image"]
186                       )
187                     )
188                   )
189                 ){
190                   return "impossible de mettre a jour l'image des sources dans source_infos";
191                 }
192               }
193             }
194             $sgbd->free_result($rst);
195           }
196           catch(Exception $e){
197             return "impossible de mettre a jour les images des sources";
198           }
199         }
200
201
202         /* ----------------------------------------------------------------------
203                                                                           groupes
204                                                            maj image avec id user
205         */
206
207         try{
208           $sql ="SELECT * FROM #--groupes";
209           $rst = $sgbd->query($sql);
210           while($v_rst = $sgbd->fetch_assoc($rst)){
211             if($v_rst["image"] && !preg_match("/^[0-9]+\/.*$/", $v_rst["image"])){
212               if(
213                 !$data->data_update(
214                   array(
215                     "table_name" => "groupes",
216                     "index_name" => "id",
217                     "index_value" => $v_rst["id"],
218                     "values" => array(
219                       "image" => $v_rst["id_user"]."/".$v_rst["image"]
220                     )
221                   )
222                 )
223               ){
224                 return "impossible de mettre a jour l'image des groupes";
225               }
226             }
227           }
228           $sgbd->free_result($rst);
229         }
230         catch(Exception $e){
231           return "impossible de mettre a jour les images des groupes";
232         }
233
234         /* ----------------------------------------------------------------------
235                                                                           sources
236                                                 sources.nom devient sources.titre
237                                           sources.status devient sources.id_class
238                                                  suppression de sources.id_groupe
239                                                 suppression de sources.derivation
240         */
241
242         try{
243           $sources_desc = $sgbd->desc_table("#--sources");
244         }
245         catch(Exception $e){
246           return "impossible de lire les champs de la table sources";
247         }
248         if(
249              !isset($sources_desc["attributs"]["titre"])
250           || isset($sources_desc["attributs"]["nom"])
251           || isset($sources_desc["attributs"]["status"])
252           || isset($sources_desc["attributs"]["id_groupe"])
253           || isset($sources_desc["attributs"]["derivation"])
254         ){
255           $sql = "ALTER TABLE #--sources RENAME TO #--sources_tmp";
256           try{
257             $sgbd->query($sql);
258           }
259           catch(Exception $e){
260             return "impossible de renommer la table sources en sources_tmp";
261           }
262           if(
263             !$data->data_create_table(
264               array(
265                 "table_name" => "sources",
266                 "fields" => array(
267                   "id" => array("type" => "int", "autoincrement" => true),
268                   "id_class" => array("type" => "int"),
269                   "reference" => array("type" => "varchar", "null" => true),
270                   "titre" => array("type" => "varchar", "null" => true),
271                   "licence" => array("type" => "int", "null" => true),
272                   "date_creation" => array("type" => "date", "null" => true),
273                   "date_inscription" => array("type" => "datetime")
274                 ),
275                 "keys" => array(
276                   array(
277                     "fields" => array("id"),
278                     "primary" => true
279                   ),
280                   array(
281                     "fields" => array("id_class")
282                   ),
283                   array(
284                     "fields" => array("licence")
285                   )
286                 ),
287                 "options" => array("default_charset" => "utf8")
288               )
289             )
290           ){
291             return "impossible de créer la table sources";
292           }
293           $attribut_names = array(
294             "id_class" => isset($sources_desc["attributs"]["status"]) ? "status" : "id_class",
295             "titre" => isset($sources_desc["attributs"]["nom"]) ? "nom" : "titre",
296           );
297           $sql =
298            "INSERT INTO #--sources(id, id_class, reference, titre, licence, date_creation, date_inscription)"
299           ." SELECT"
300           ."  id"
301           .", ".$attribut_names["id_class"]
302           .", reference"
303           .", ".$attribut_names["titre"]
304           .", licence"
305           .", date_creation"
306           .", date_inscription"
307           ." FROM #--sources_tmp";
308           try{
309             $sgbd->query($sql);
310           }
311           catch(Exception $e){
312             return "impossible d'importer sources_tmp dans sources";
313           }
314           try{
315             $sgbd->query("DROP TABLE #--sources_tmp");
316           }
317           catch(Exception $e){
318             return "impossible de supprimer la table sources_tmp";
319           }
320         }
321
322         /* ----------------------------------------------------------------------
323                                 suppression de la table source_status_composition
324         */
325
326         if($sgbd->table_exists("#--source_status_composition")){
327           try{
328             $sgbd->query("DROP TABLE #--source_status_composition");
329           }
330           catch(Exception $e){
331             return "impossible de supprimer la table source_status_composition";
332           }
333         }
334
335       } // if($sgbd->data_exists("sources"))
336
337
338       /* ###################################################################### */
339       /* ###################################################################### */
340       /* ----------------------------------------------------------------------
341                    migration vers les tables en sml_(...) du plugin mw_sourceml
342       */
343
344       /* ----------------------------------------------------------------------
345                                                     sources devient sml_sources
346       */
347
348       if($sgbd->table_exists("#--sources")){
349         try{
350           $sgbd->query("ALTER TABLE #--sources RENAME TO #--sml_sources");
351         }
352         catch(Exception $e){
353           return "impossible de renommer sources en sml_sources";
354         }
355       }
356
357
358       /* ----------------------------------------------------------------------
359                                      source_groupes devient sml_sources_authors
360                                                 elt id_groupe devient id_author
361                                  elt id_groupe_status devient id_sources_access
362       */
363
364       if($sgbd->table_exists("#--source_groupes")){
365         if(
366           !$data->data_create_table(
367             array(
368               "table_name" => "sml_sources_authors",
369               "fields" => array(
370                 "id" => array("type" => "int", "autoincrement" => true),
371                 "id_source" => array("type" => "int"),
372                 "id_author" => array("type" => "int"),
373                 "id_sources_access" => array("type" => "int"),
374               ),
375               "keys" => array(
376                 array(
377                   "fields" => array("id"),
378                   "primary" => true
379                 ),
380                 array(
381                   "fields" => array("id_source")
382                 ),
383                 array(
384                   "fields" => array("id_author")
385                 ),
386                 array(
387                   "fields" => array("id_sources_access")
388                 )
389               ),
390               "options" => array("default_charset" => "utf8")
391             )
392           )
393         ){
394           return "impossible de créer la table sources";
395         }
396         $sql =
397          "INSERT INTO #--sml_sources_authors(id_source, id_author, id_sources_access)"
398         ." SELECT"
399         ."  id_source"
400         .", id_groupe"
401         .", id_groupe_status"
402         ." FROM #--source_groupes";
403         try{
404           $sgbd->query($sql);
405         }
406         catch(Exception $e){
407           return "impossible d'importer les derivation dans source_derivations";
408         }
409         try{
410           $sgbd->query("DROP TABLE #--source_groupes");
411         }
412         catch(Exception $e){
413           return "impossible de supprimer la table source_groupes";
414         }
415       }
416
417
418       /* ----------------------------------------------------------------------
419                                        groupe_status devient sml_sources_access
420                                                             nouvel elt intitule
421       */
422
423       if($sgbd->table_exists("#--groupe_status")){
424         if(
425           !$data->data_create_table(
426             array(
427               "table_name" => "sml_sources_access",
428               "fields" => array(
429                 "id" => array("type" => "int", "autoincrement" => true),
430                 "nom" => array("type" => "varchar"),
431                 "intitule" => array("type" => "varchar"),
432               ),
433               "keys" => array(
434                 array(
435                   "fields" => array("id"),
436                   "primary" => true
437                 )
438               ),
439               "options" => array("default_charset" => "utf8")
440             )
441           )
442         ){
443           return "impossible de créer la table sources";
444         }
445         try{
446           $sql = "INSERT INTO #--sml_sources_access(id, nom, intitule) VALUES (1, 'admin', 'administrateur')";\r
447           $sgbd->query($sql);
448           $sql = "INSERT INTO #--sml_sources_access(id, nom, intitule) VALUES (2, 'editeur', 'éditeur')";\r
449           $sgbd->query($sql);
450           $sql = "INSERT INTO #--sml_sources_access(id, nom, intitule) VALUES (3, 'contributeur', 'contributeur')";\r
451           $sgbd->query($sql);
452         }
453         catch(Exception $e){
454           return "impossible de remplir la table sml_sources_access";
455         }
456         try{
457           $sgbd->query("DROP TABLE #--groupe_status");
458         }
459         catch(Exception $e){
460           return "impossible de supprimer la table groupe_status";
461         }
462       }
463
464       /* ----------------------------------------------------------------------
465                                                     groupes devient sml_authors
466       */
467
468       if($sgbd->table_exists("#--groupes")){
469         try{
470           $sgbd->query("ALTER TABLE #--groupes RENAME TO #--sml_authors");
471         }
472         catch(Exception $e){
473           return "impossible de renommer groupes en sml_authors";
474         }
475       }
476
477       /* ----------------------------------------------------------------------
478                                                   licences devient sml_licences
479       */
480
481       if($sgbd->table_exists("#--licences")){
482         try{
483           $sgbd->query("ALTER TABLE #--licences RENAME TO #--sml_licences");
484         }
485         catch(Exception $e){
486           return "impossible de renommer licences en sml_licences";
487         }
488       }
489
490       /* ----------------------------------------------------------------------
491                             source_compositions devient sml_source_compositions
492       */
493
494       if($sgbd->table_exists("#--source_compositions")){
495         try{
496           $sgbd->query("ALTER TABLE #--source_compositions RENAME TO #--sml_source_compositions");
497         }
498         catch(Exception $e){
499           return "impossible de renommer source_compositions en sml_source_compositions";
500         }
501       }
502
503       /* ----------------------------------------------------------------------
504                               source_derivations devient sml_source_derivations
505       */
506
507       if($sgbd->table_exists("#--source_derivations")){
508         try{
509           $sgbd->query("ALTER TABLE #--source_derivations RENAME TO #--sml_source_derivations");
510         }
511         catch(Exception $e){
512           return "impossible de renommer source_derivations en sml_source_derivations";
513         }
514       }
515
516       /* ----------------------------------------------------------------------
517                                           source_cache devient sml_source_cache
518       */
519
520       if($sgbd->table_exists("#--source_cache")){
521         try{
522           $sgbd->query("ALTER TABLE #--source_cache RENAME TO #--sml_source_cache");
523         }
524         catch(Exception $e){
525           return "impossible de renommer source_cache en sml_source_cache";
526         }
527       }
528
529       /* ----------------------------------------------------------------------
530                                               source_status devient sml_classes
531       */
532
533       if($sgbd->table_exists("#--source_status")){
534         try{
535           $sgbd->query("ALTER TABLE #--source_status RENAME TO #--sml_classes");
536         }
537         catch(Exception $e){
538           return "impossible de renommer source_status en sml_classes";
539         }
540       }
541
542       /* ----------------------------------------------------------------------
543                                          source_infos devient sml_sources_infos
544       */
545
546       if($sgbd->table_exists("#--source_infos")){
547         try{
548           $sgbd->query("ALTER TABLE #--source_infos RENAME TO #--sml_sources_infos");
549         }
550         catch(Exception $e){
551           return "impossible de renommer source_infos en sml_sources_infos";
552         }
553       }
554
555       /* ----------------------------------------------------------------------
556                                               source_documents devient sml_source_documents
557       */
558
559       if($sgbd->table_exists("#--source_documents")){
560         try{
561           $sgbd->query("ALTER TABLE #--source_documents RENAME TO #--sml_source_documents");
562         }
563         catch(Exception $e){
564           return "impossible de renommer source_documents en sml_source_documents";
565         }
566       }
567
568
569       /* ----------------------------------------------------------------------
570                                          nouvelle table sml_sources_invitations
571       */
572
573       if(!$sgbd->table_exists("#--sml_sources_invitations")){
574         if(
575           !$data->data_create_table(
576             array(
577               "table_name" => "sml_sources_invitations",
578               "fields" => array(
579                 "id" => array("type" => "int", "autoincrement" => true),
580                 "id_user" => array("type" => "int"),
581                 "id_source" => array("type" => "int"),
582                 "id_author" => array("type" => "int"),
583                 "id_sources_access" => array("type" => "int"),
584                 "date_invitation" => array("type" => "datetime")
585               ),
586               "keys" => array(
587                 array(
588                   "fields" => array("id"),
589                   "primary" => true
590                 )
591               ),
592               "options" => array("default_charset" => "utf8")
593             )
594           )
595         ){
596           return "impossible de créer la table sml_sources_invitations";
597         }
598       }
599
600       return true;
601     }
602
603   }