1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
584
  
585
  
586
  
587
  
588
  
589
  
590
  
591
  
592
  
593
  
594
  
595
  
596
  
597
  
598
  
599
  
600
  
601
  
602
  
603
  
604
  
605
  
606
  
607
  
608
  
609
  
610
  
611
  
612
  
613
  
614
  
615
  
616
  
617
  
618
  
619
  
620
  
621
  
622
  
623
  
624
  
625
  
626
  
627
  
628
  
629
  
630
  
631
  
632
  
633
  
634
  
635
  
636
  
637
  
638
  
639
  
640
  
641
  
642
  
643
  
644
  
645
  
646
  
647
  
648
  
649
  
650
  
651
  
652
  
653
  
654
  
655
  
656
  
657
  
658
  
659
  
660
  
661
  
662
  
663
  
664
  
665
  
666
  
667
  
668
  
669
  
670
  
671
  
672
  
673
  
674
  
675
  
676
  
677
  
678
  
679
  
680
  
681
  
682
  
683
  
684
  
685
  
686
  
687
  
688
  
689
  
690
  
691
  
692
  
693
  
694
  
695
  
696
  
697
  
698
  
699
  
700
  
701
  
702
  
703
  
704
  
705
  
706
  
707
  
708
  
709
  
710
  
711
  
712
  
713
  
714
  
715
  
716
  
717
  
718
  
719
  
720
  
721
  
722
  
723
  
724
  
725
  
726
  
727
  
728
  
729
  
730
  
731
  
732
  
733
  
734
  
735
  
736
  
737
  
738
  
739
  
740
  
741
  
742
  
743
  
744
  
745
  
746
  
747
  
748
  
749
  
750
  
751
  
752
  
753
  
754
  
755
  
756
  
757
  
758
  
759
  
760
  
761
  
762
  
763
  
764
  
765
  
766
  
767
  
768
  
769
  
770
  
771
  
772
  
773
  
774
  
775
  
776
  
777
  
778
  
779
  
780
  
781
  
782
  
783
  
784
  
785
  
786
  
787
  
788
  
789
  
790
  
791
  
792
  
793
  
794
  
795
  
796
  
797
  
798
  
799
  
800
  
801
  
802
  
803
  
804
  
805
  
806
  
807
  
808
  
809
  
810
  
811
  
812
  
813
  
814
  
815
  
816
  
817
  
818
  
819
  
820
  
821
  
822
  
823
  
824
  
825
  
826
  
827
  
828
  
829
  
830
  
831
  
832
  
833
  
834
  
835
  
836
  
837
  
838
  
839
  
840
  
841
  
842
  
843
  
844
  
845
  
846
  
847
  
848
  
849
  
850
  
851
  
852
  
853
  
854
  
855
  
856
  
857
  
858
  
859
  
860
  
861
  
862
  
863
  
864
  
865
  
866
  
867
  
868
  
869
  
870
  
871
  
872
  
873
  
874
  
875
  
876
  
877
  
878
  
879
  
880
  
881
  
882
  
883
  
884
  
885
  
886
  
887
  
888
  
889
  
890
  
891
  
892
  
893
  
894
  
895
  
896
  
897
  
898
  
899
  
900
  
901
  
902
  
903
  
904
  
905
  
906
  
907
  
908
  
909
  
910
  
911
  
912
  
913
  
914
  
915
  
916
  
917
  
918
  
919
  
920
  
921
  
922
  
923
  
924
  
925
  
926
  
927
  
928
  
929
  
930
  
931
  
932
  
933
  
934
  
935
  
936
  
937
  
938
  
939
  
940
  
941
  
942
  
943
  
944
  
945
  
946
  
947
  
948
  
949
  
950
  
951
  
952
  
953
  
954
  
955
  
956
  
957
  
958
  
959
  
960
  
961
  
962
  
963
  
964
  
965
  
966
  
967
  
968
  
969
  
970
  
971
  
972
  
973
  
974
  
975
  
976
  
977
  
978
  
979
  
980
  
981
  
982
  
983
  
984
  
985
  
986
  
987
  
988
  
989
  
990
  
991
  
992
  
993
  
994
  
995
  
996
  
997
  
998
  
999
  
1000
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
1030
  
1031
  
1032
  
1033
  
1034
  
1035
  
1036
  
1037
  
1038
  
1039
  
1040
  
1041
  
1042
  
1043
  
1044
  
1045
  
1046
  
1047
  
1048
  
1049
  
1050
  
1051
  
1052
  
1053
  
1054
  
1055
  
1056
  
1057
  
1058
  
1059
  
1060
  
1061
  
1062
  
1063
  
1064
  
1065
  
1066
  
1067
  
1068
  
1069
  
1070
  
1071
  
1072
  
1073
  
1074
  
1075
  
1076
  
1077
  
1078
  
1079
  
1080
  
1081
  
1082
  
1083
  
1084
  
1085
  
1086
  
1087
  
1088
  
1089
  
1090
  
1091
  
1092
  
1093
  
1094
  
1095
  
1096
  
1097
  
1098
  
1099
  
1100
  
1101
  
1102
  
1103
  
1104
  
1105
  
1106
  
1107
  
1108
  
1109
  
1110
  
1111
  
1112
  
1113
  
1114
  
1115
  
1116
  
1117
  
1118
  
1119
  
1120
  
1121
  
1122
  
1123
  
1124
  
1125
  
1126
  
1127
  
1128
  
1129
  
1130
  
1131
  
1132
  
1133
  
1134
  
1135
  
1136
  
1137
  
1138
  
1139
  
1140
  
1141
  
1142
  
1143
  
1144
  
1145
  
1146
  
1147
  
1148
  
1149
  
1150
  
1151
  
1152
  
1153
  
1154
  
1155
  
1156
  
1157
  
1158
  
1159
  
1160
  
1161
  
1162
  
1163
  
1164
  
1165
  
1166
  
1167
  
1168
  
1169
  
1170
  
1171
  
1172
  
1173
  
1174
  
1175
  
1176
  
1177
  
1178
  
1179
  
1180
  
1181
  
1182
  
1183
  
1184
  
1185
  
1186
  
1187
  
1188
  
1189
  
1190
  
1191
  
1192
  
1193
  
1194
  
1195
  
1196
  
1197
  
1198
  
1199
  
1200
  
1201
  
1202
  
1203
  
1204
  
1205
  
1206
  
1207
  
1208
  
1209
  
1210
  
1211
  
1212
  
1213
  
1214
  
1215
  
1216
  
1217
  
1218
  
1219
  
1220
  
1221
  
1222
  
1223
  
1224
  
1225
  
1226
  
1227
  
1228
  
1229
  
1230
  
1231
  
1232
  
1233
  
1234
  
1235
  
1236
  
1237
  
1238
  
1239
  
1240
  
1241
  
1242
  
1243
  
1244
  
1245
  
1246
  
1247
  
1248
  
1249
  
1250
  
1251
  
1252
  
1253
  
1254
  
1255
  
1256
  
1257
  
1258
  
1259
  
1260
  
1261
  
1262
  
1263
  
1264
  
1265
  
1266
  
1267
  
1268
  
1269
  
1270
  
1271
  
1272
  
1273
  
1274
  
1275
  
1276
  
1277
  
1278
  
1279
  
1280
  
1281
  
1282
  
1283
  
1284
  
1285
  
1286
  
1287
  
1288
  
1289
  
1290
  
1291
  
1292
  
1293
  
1294
  
1295
  
1296
  
1297
  
1298
  
1299
  
1300
  
1301
  
1302
  
1303
  
1304
  
1305
  
1306
  
1307
  
1308
  
1309
  
1310
  
1311
  
1312
  
1313
  
1314
  
1315
  
1316
  
1317
  
1318
  
1319
  
1320
  
1321
  
1322
  
1323
  
1324
  
1325
  
1326
  
1327
  
1328
  
1329
  
1330
  
1331
  
1332
  
1333
  
1334
  
1335
  
1336
  
1337
  
1338
  
1339
  
1340
  
1341
  
1342
  
1343
  
1344
  
1345
  
1346
  
1347
  
1348
  
1349
  
1350
  
1351
  
1352
  
1353
  
1354
  
1355
  
1356
  
#include <config_interface.h> 
#include <config.h> 
#include <roxen.h> 
//<locale-token project="roxen_config">_</locale-token> 
#define _(X,Y)  _STR_LOCALE("roxen_config",X,Y) 
 
mapping actions = ([ 
  // name         title                      function   must be internal 
  "move":   ({  _(401,"Copy or rename database"),move_db,   0 }), 
  "delete": ({  _(402,"Delete this database"), delete_db, 0 }), 
  "group":  ({  _(324,"Change group for this database"), change_group, 0 }), 
  "clear":  ({  _(403,"Delete all tables"),    clear_db,  0 }), 
  "backup": ({  _(404,"Make a backup"),        backup_db, 1 }), 
  "charset":({  _(536, "Change default character set"), change_charset, 0 }), 
  "repair": ({  _(537, "Repair all tables"),     repair_db, 0 }), 
  "optimize":({  _(538, "Optimize all tables"),   optimize_db, 0 }), 
  "schedule":({  _(1109, "Change backup schedule"), change_schedule, 2 }), 
]); 
 
 
#define CU_AUTH id->misc->config_user->auth 
 
//  Size limit for single field and aggregated fields (per column) 
#define MAX_FIELD_FORMATTED_SIZE (32 * 1024)            //  32 K 
#define MAX_TOTAL_FORMATTED_SIZE (1024 * 1024)          //  1 MB 
 
 
#define VERIFY(X) do {                                          \ 
  if( !id->variables["yes.x"] )                                 \ 
  {                                                             \ 
    return                                                      \ 
      ("<table><tr><td colspan='2'>\n"+                         \ 
       sprintf((string)(X), db)+                                \ 
       "</td><tr><td><input type=hidden name=action value='&form.action;' />"\ 
       "<submit-gbutton2 name='yes' "            \ 
       " type='ok'>"+_(0,"Yes")+              \ 
       "</submit-gbutton2></td>\n"                              \ 
       "<td align=right><cf-no href='"+Roxen.html_encode_string(id->not_query)+\ 
       "?db="+Roxen.html_encode_string(id->variables->db)+      \ 
       "&amp;&usr.set-wiz-id;' />"+                             \ 
       "</td>\n</table>\n");                                    \ 
  }                                                             \ 
} while(0) 
 
 
mixed change_group( string db, RequestID id ) 
{ 
  if( !id->variables->group ) 
  { 
    string res ="" 
    "<input type=hidden name=action value='&form.action;' />" 
      "<cf-title>"+sprintf(_(423,"Changing group for %s"), db )+"</cf-title>" 
      "<p><b>"+_(445,"Old group")+":</b> " + 
      DBManager.get_group(DBManager.db_group(db))->lname+"</p>" 
      "<p><b>"+_(504,"New group")+":</b> <select name='group'>"; 
    foreach( DBManager.list_groups(), string g ) 
      if( g == DBManager.db_group( db ) ) 
        res += "<option selected value='"+g+"'>"+DBManager.get_group( g )->lname; 
      else 
        res += "<option value='"+g+"'>"+DBManager.get_group( g )->lname; 
    return res + "</select><submit-gbutton2 name='ok' type='ok'>"+(201,"OK")+ 
      "</submit-gbutton2></p>"; 
  } 
  DBManager.set_db_group( db, id->variables->group ); 
  return 0; 
} 
 
mixed change_charset( string db, RequestID id ) 
{ 
  if( !id->variables->default_charset ) 
  { 
    string old_charset = DBManager.get_db_default_charset(db); 
    string res ="" 
    "<input type=hidden name=action value='&form.action;' />" 
      "<cf-title>"+sprintf(_(539,"Changing default character set for %s"), db )+ 
      "</cf-title>"+ 
      (old_charset?("<p><b>"+_(540, "Old default character set")+":</b> " + 
                    old_charset+"</p>"):"")+ 
      "<p><b>"+_(541,"New default character set")+":</b> " 
      "<input type='string' name='default_charset' value='" + 
      Roxen.html_encode_string(old_charset||"") +"' />"; 
    return res + "</select><submit-gbutton2 name='ok'>"+(201,"OK")+ 
      "</submit-gbutton2></p>"; 
  } 
  DBManager.set_db_default_charset( db, id->variables->default_charset ); 
  return 0; 
} 
 
mixed change_schedule( string db, RequestID id ) 
{ 
  Sql.Sql sql = connect_to_my_mysql(0, "roxen"); 
  if( !id->variables->backup_schedule ) 
  { 
    string old_schedule, old_schedule_id; 
    array q = sql->query("SELECT schedule_id, schedule FROM dbs, db_schedules " 
                         " WHERE db_schedules.id = dbs.schedule_id " 
                         "   AND dbs.name = %s", db); 
    if (sizeof(q)) { 
      old_schedule = q[0]->schedule; 
      old_schedule_id = q[0]->schedule_id; 
    } 
    string res ="<br /><blockquote>" 
    "<input type=hidden name=action value='&form.action;' />" 
      "<h2>"+sprintf(_(1110,"Changing backup schedule for %s"), db )+ 
      "</h2>"+ 
      (old_schedule?("<b>"+_(1111, "Old backup schedule")+":</b> " + 
                     Roxen.html_encode_string(old_schedule)+"<br />"):"")+ 
      "<b>"+_(1112,"New backup schedule")+":</b> " 
      "<default name='backup_schedule' value='" + old_schedule_id + "'>" 
      "<select name='backup_schedule'>"; 
    foreach(sql->query("SELECT schedule, id FROM db_schedules " 
                       " ORDER BY schedule"), mapping(string:string) schedule) { 
      res += sprintf("<option value='%s'>%s</option>", 
                     schedule->id, 
                     Roxen.html_encode_string(schedule->schedule)); 
    } 
    res += sprintf("<option value='0'>%s</option>", 
                   Roxen.html_encode_string(_(1113, "None"))) + 
      "</select></default>"; 
    return res + "<submit-gbutton2 name='ok'>"+(201,"OK")+ 
      "</submit-gbutton2>"; 
  } 
  if (id->variables->backup_schedule == "0") { 
    sql->query("UPDATE dbs SET schedule_id = NULL " 
               " WHERE name = %s", db); 
  } else { 
    sql->query("UPDATE dbs SET schedule_id = %d " 
               " WHERE name = %s", (int)id->variables->backup_schedule, db); 
  } 
  return 0; 
} 
 
mixed repair_db( string db, RequestID id ) 
{ 
  string res = 
    "<p><link-gbutton type='back' href='browser.pike?db=" + db + "&amp;&usr.set-wiz-id;'>Back</link-gbutton></p>" 
    "<table id='tbl' class='nice tbl-details'>" 
    "<thead>" 
    "<tr>" 
    "<th>Target</th>" 
    "<th>Result</th>" 
    "<th>Time</th>" 
    "</tr>" 
    "</thead>" 
    "<tbody>"; 
 
  mixed m = query("SHOW TABLE STATUS IN " + db); 
  float t3 = 0; 
 
  if (sizeof(m)) { 
    foreach (m,m) { 
      string result = ""; 
      mixed q; 
      int t = time(); 
      float t1 = time(t); 
      float t2; 
 
      if ( mixed e = catch { q = query( "REPAIR TABLE `" + db + "`.`" + m->Name + "`" ); } ) { 
        result = "<span class='notify error inline'>Error: " + describe_error(e) + "</span>"; 
      } else { 
        t2 = (time(t)-t1); 
        t3 += t2; 
 
        if (q->Msg_text = "OK") 
          result = "<span class='notify ok inline'>Ok</span>"; 
        else 
          result = "<span class='notify error inline'>Failed: " + q->Msg_text + "</span>"; 
      } 
 
      res += "<tr>" + 
        "<td><a href='browser.pike?db=" + db + "&amp;&usr.set-wiz-id;'>" + db + "</a>.<a href='browser.pike?db=" + db + "&amp;table=" + m->Name + "&amp;&usr.set-wiz-id;'>" + m->Name + "</a></td>" + 
        "<td>" + result + "</td>" + 
        "<td>" + t2 + " sec</td>" + 
        "</tr>"; 
    } 
  } 
  res += 
    "</tbody>" 
    "<tfoot><tr><td colspan='2'>Total:</td><td>" + t3 + " sec</td></tr></tfoot>" 
    "</table>"; 
 
  return res; 
} 
 
mixed optimize_db( string db, RequestID id ) 
{ 
  // CSS stylesheet 
  string res = 
    "<p><link-gbutton type='back' href='browser.pike?db=" + 
        db + "&amp;&usr.set-wiz-id;'>Back</link-gbutton></p>" 
 
    "<table id='tbl' class='nice db-list'>" 
    "<thead>" 
    "<tr>" 
    "<th>Target</th>" 
    "<th>Result</th>" 
    "<th>Time</th>" 
    "</tr>" 
    "</thead>" 
    "<tbody>"; 
 
  mixed m = query("SHOW TABLE STATUS IN " + db); 
  float t3 = 0; 
 
  if (sizeof(m)) { 
    foreach (m,m) { 
      string result = ""; 
      mixed q; 
      int t = time(); 
      float t1 = time(t); 
      float t2; 
 
      if ( mixed e = catch { q = query( "OPTIMIZE TABLE `" + db + "`.`" + m->Name + "`" ); } ) { 
        result = "<span class='notify error inline'>Error: " + describe_error(e) + "</span>"; 
      } else { 
        t2 = (time(t)-t1); 
        t3 += t2; 
 
        if (q->Msg_text = "OK") 
          result = "<span class='notify ok inline'>Ok</span>"; 
        else 
          result = "<span class='notify error inline'>Failed: " + q->Msg_text + "</span>"; 
      } 
 
      res += "<tr>" + 
        "<td><a href='browser.pike?db=" + db + "&amp;&usr.set-wiz-id;'>" + db + "</a>.<a href='browser.pike?db=" + db + "&amp;table=" + m->Name + "&amp;&usr.set-wiz-id;'>" + m->Name + "</a></td>" + 
        "<td>" + result + "</td>" + 
        "<td>" + t2 + " sec</td>" + 
        "</tr>"; 
    } 
  } 
  res += 
    "</tbody>" 
    "<tfoot>" 
    "<tr><td colspan='2'>Total:</td><td>" + t3 + " sec</td></tr>" 
    "</tfoot></table>"; 
 
  return res;} 
 
mixed query( mixed ... args ) { 
        return connect_to_my_mysql( 0, "roxen" )->query( @args ); 
} 
 
mixed backup_db( string db, RequestID id ) 
{ 
  if( id->variables["ok.x"] ) 
  { 
    if (roxenloader->parse_mysql_location()->mysqldump) { 
      DBManager.dump 
        (db, id->variables->dir == "auto" ? 0 : id->variables->dir); 
    } else { 
      DBManager.backup 
        (db, id->variables->dir == "auto" ? 0 : id->variables->dir); 
    } 
    return 0; 
  } 
  return 
    "<b>"+_(405,"Directory")+":</b> <input name='dir' size='60' value='auto' /><br />" 
    "<i>" + sprintf (_(1061, #"\ 
The directory the backup will be saved in. If you chose auto, Roxen 
will generate a directory name that includes the database name and 
today's date in <tt>$VARDIR/backup</tt> (%s)."), 
                     combine_path (getcwd(), roxen_path ("$VARDIR/backup"))) + 
    "</i>" 
    "<table width='100%'><tr><td valign=top>" 
    "<input type=hidden name=action value='&form.action;' />" 
    "<cf-cancel href='"+ Roxen.html_encode_string(id->not_query)+ 
    "?db="+Roxen.html_encode_string(id->variables->db)+"&amp;&usr.set-wiz-id;'/>" 
    "<td valign=top align=right><submit-gbutton2 name='ok'>"+_(201,"OK")+ 
    "</submit-gbutton2></td>\n" 
    "</td>\n</table>\n"; 
} 
 
mixed move_db( string db, RequestID id ) 
{ 
  string warning=""; 
  int internal = DBManager.is_internal( db ); 
  if( id->variables["ok.x"] ) 
  { 
    if( !internal ) 
    { 
      if( !strlen(id->variables->url) ) 
        warning= "<font color='&usr.warncolor;'>" 
          +_(406,"Please specify an URL to define an external database")+ 
          "</font>"; 
      else if( mixed err = catch( Sql.Sql( id->variables->url ) ) ) 
        warning = sprintf("<font color='&usr.warncolor;'>"+ 
                          _(407,"It is not possible to connect to %s.")+ 
                          "<br /> (%s)" 
                          "</font>", 
                          id->variables->url, 
                          describe_error(err)); 
    } 
    if( !strlen( warning ) ) 
      switch( id->variables->name ) 
      { 
       case "": 
         warning =  "<font color='&usr.warncolor;'>"+ 
           _(408,"Please specify a name for the database")+ 
           "</font>"; 
         break; 
       case "mysql": 
       case "roxen": 
         warning = sprintf("<font color='&usr.warncolor;'>"+ 
                         _(409,"%s is an internal database, used by Roxen. " 
                           "Please select another name.")+ 
                         "</font>", id->variables->name ); 
         break; 
        default: 
         if( Roxen.is_mysql_keyword( id->variables->name ) ) 
           warning = sprintf("<font color='&usr.warncolor;'>"+ 
                             _(410,"%s is a MySQL keyword, used by MySQL. " 
                               "Please select another name.")+ 
                             "</font>", id->variables->name ); 
         catch { 
           if( DBManager.cached_get( id->variables->name ) && 
               db != id->variables->name ) 
             warning = sprintf("<font color='&usr.warncolor;'>"+ 
                               _(529,"the database %s does already exist")+ 
                               "</font>", id->variables->name ); 
           // FIXME: Also check if the name is a valid db name. 
         }; 
         break; 
      } 
    if( !strlen( warning ) ) 
    { 
      // In all cases, create the new db. 
      if(!(DBManager.get_db_url_info(id->variables->name))) { 
        DBManager.create_db(id->variables->name, id->variables->url, 
                            internal, id->variables->group); 
      } else { 
        DBManager.set_url(id->variables->name, 
                          id->variables->url, 
                          internal); 
      } 
 
      // Intern 
      //   Copy if name has changed. 
      // Extern 
      //   Just copy the meta information. 
      if(internal) 
      { 
        // Internal db, use the backup thingies to copy the data 
        if( db != id->variables->name ) 
        { 
          DBManager.backup( db, "/tmp/tmpdb" ); 
          DBManager.restore( db, "/tmp/tmpdb", id->variables->name ); 
          DBManager.delete_backup( db, "/tmp/tmpdb" ); 
        } 
      } 
      switch( id->variables->what ) 
      { 
        case "copy": // copy, no delete 
          if( db != id->variables->name ) 
            DBManager.copy_db_md( db, id->variables->name ); 
          // Done. 
          break; 
 
        case "move": // move & delete 
          // Delete the old data. 
          if( db != id->variables->name ) 
          { 
            DBManager.copy_db_md( db, id->variables->name ); 
            if( !strlen(warning) ) 
              DBManager.drop_db( db ); 
          } 
          break; 
      } 
      return Roxen.http_redirect( "/dbs/", id ); 
    } 
  } 
  if(!id->variables->name) 
    id->variables->name = db; 
 
  if( !id->variables->url ) 
    id->variables->url  = DBManager.db_url( db ) || ""; 
 
  if(!id->variables->what) 
    id->variables->what = "move"; 
 
  return 
    "<cf-title>"+_(414,"Copy or rename this database")+"</cf-title>\n" 
    +warning+ 
    "<table class='auto'>\n" 
 
    "  <tr>\n" 
    "    <td><b>"+_(415,"Action")+":</b></td>\n" 
    "    <td><default variable='form.what'>\n" 
    "      <select name='what'>\n" 
    "        <option value='copy'>"+_(416,"Copy the data to a new database")+"</option>\n" 
    "        <option value='move'>"+_(417,"Rename database")+"</option>\n" 
    "      </select></default>\n" 
    "    </td>\n" 
    "  </tr>\n" 
    "  <tr>\n" 
    "    <td><b>"+_(418,"New name")+":</b></td>\n" 
    "    <td><input name='name' value='&form.name;'/></td>\n" 
    "  </tr>\n" 
 
    "  <tr>\n" 
    "    <td valign=top colspan='2'>\n" 
    "      <i>"+_(530,"The new name of the database. To make it easy on " 
                  "your users, use all lowercaps characters, and avoid hard to type " 
                  "characters.")+"</i>\n" 
    "    </td>\n" 
    "  </tr>\n"+ 
 
    (internal?"": 
     " <tr>\n" 
     "   <td><nbsp><b>URL:</b></nbsp></td>\n" 
     "   <td colspan='3'><input name='url' size=50 value='&form.url;'/></td>\n" 
     " </tr>\n" 
     " <tr>" 
     "   <td colspan='4'><i>\n"+ 
     "     "+_(422,"This URL is only used for </i>External<i> databases, it is " 
               "totally ignored for databases defined internally in Roxen. ")+"\n</i>\n" 
     "   </td>" 
     " </tr>\n")+ 
 
    "</table>\n"+ 
    "<table width='100%'><tr><td>" 
    "<input type=hidden name=action value='&form.action;' />" 
    "<cf-cancel href='" + Roxen.html_encode_string(id->not_query) + 
    "?db=" + Roxen.html_encode_string(id->variables->db) + 
    "&amp;&usr.set-wiz-id;'/></td>\n" 
    "<td align=right>" 
    "<submit-gbutton2 name='ok' type='ok'>" + _(201,"OK") + "</submit-gbutton2>" 
    "</td>\n</table>\n"; 
} 
 
mixed delete_db( string db, RequestID id ) 
{ 
  string msg; 
  if( DBManager.is_internal( db ) ) 
    msg = (string)_(361, "Are you sure you want to delete the database %s " 
                    "and the data?"); 
  else 
    msg = (string)_(362,"Are you sure you want to delete the database %s?" 
                    " No data will be deleted from the remote database."); 
 
  VERIFY(msg); 
  report_notice( _(424,"The database %s was deleted by %s")+"\n", 
                 db, id->misc->authenticated_user->name() ); 
  DBManager.drop_db( db ); 
  return Roxen.http_redirect( "/dbs/", id ); 
} 
 
mixed clear_db( string db, RequestID id ) 
{ 
  VERIFY(_(425,"Are you sure you want to delete all tables in %s?")); 
 
  Sql.Sql sq = DBManager.get( db ); 
 
  // Note: Drop table may fail due to foreign key references 
  //       in some databases. Thus the outer loop and the catch. 
  int table_cnt; 
  array(string) remaining_tables = DBManager.db_tables( db ); 
  do { 
    foreach( remaining_tables, string r ) { 
      catch { sq->query( "DROP TABLE "+r ); }; 
    } 
    table_cnt = sizeof(remaining_tables); 
    remaining_tables = DBManager.db_tables( db ); 
  } while (sizeof(remaining_tables) && 
           (table_cnt > sizeof(remaining_tables))); 
  if (sizeof(remaining_tables)) { 
    // We've failed to drop some tables, try forcing an error. 
    sq->query( "DROP TABLE " + remaining_tables[0]); 
  } 
  return 0; 
} 
 
 
int image_id = time() ^ gethrtime(); 
 
string is_image( string x ) 
{ 
  if( !stringp(x) ) 
    return 0; 
  if( has_prefix( x, "GIF" ) ) 
    return "gif"; 
  if( has_value( x, "JFIF" ) ) 
    return "jpeg"; 
  if( has_prefix( x, "\x89PNG" ) ) 
    return "png"; 
} 
 
int is_deflated (string what) 
{ 
  // This detection is an estimate - it may give false positives. 
  if (!stringp (what) || sizeof (what) < 3) 
    return 0; 
  return ((what[0] & 0x0f) == 8) && !(((what[0] << 8) + what[1]) % 31); 
} 
 
int is_encode_value( string what ) 
{ 
  if( !stringp(what) ) 
    return 0; 
  return strlen(what) >= 5 && has_prefix (what, "¶ke"); 
} 
 
string format_decode_value( string what ) 
{ 
#if 0 
  string trim_comments( string what ) /* Needs work */ 
  { 
    string a, b; 
    while( sscanf( what, "%s/*%*s*/%s", a, b ) ) 
      what = a+b; 
    return what; 
  }; 
#endif 
 
  // Type is program or object? 
  if( (what[4] & 15) == 5 || (what[4] & 15) == 3 ) 
    return Roxen.html_encode_string( 
      sprintf("<"+_(233,"bytecode data")+" ("+ 
              _(505,"%d bytes")+")>", strlen(what))); 
 
  catch 
  { 
    return 
      "<pre>"+ 
      Roxen.html_encode_string( 
        String.trim_all_whites(sprintf("%O",decode_value(what))))+ 
      "</pre>"; 
  }; 
  return Roxen.html_encode_string( what ); 
} 
 
string store_image( string x ) 
{ 
  string id = (string)image_id++; 
 
  .State->images[ id ] = ([ 
    "type":"image/"+(is_image( x )||"unknown"), 
    "data":x, 
    "len":strlen(x), 
  ]); 
  return id; 
} 
 
string db_switcher( RequestID id ) 
{ 
  mapping q = DBManager.get_permission_map( ); 
  if ( !sizeof( q )) 
    return ""; 
 
  string res = #" 
  <script type='text/javascript'> 
  function switch_db( objSel ) { 
    if( objSel.selectedIndex == 0) { 
      return; 
    } 
    var selValue = objSel.options[objSel.selectedIndex].value; 
    if(selValue != '&form.db:js;') { 
      window.location.href = window.location.pathname + '?db=' + escape( selValue ) + '\x26&usr.set-wiz-id:js;'; 
    } 
  } 
  </script> 
  <select name='db' onchange='switch_db(this)'> 
    <option value=''>Switch to other DB</option>\n"; 
  foreach( sort(indices(q)), string d ) { 
    res += sprintf( "<option value='%s'%s>%s</option>\n", d, 
                    (d == id->variables->db)? "selected='selected'": "", d); 
  } 
  return res + "</select><noscript><input type='submit' value='Switch db'/></noscript>\n"; 
} 
 
string format_table_owner (mapping(string:string) mod_info, void|int skip_conf) 
{ 
  // Note: Code duplication in db_list.pike. 
 
  if ((<0, "">)[mod_info->conf]) return 0; 
 
  Configuration c = roxen.find_configuration( mod_info->conf ); 
  RoxenModule m = c && !(<0, "">)[mod_info->module] && 
    c->find_module( mod_info->module ); 
  ModuleInfo i = 
    !(<0, "">)[mod_info->module] && 
    roxen.find_module( (mod_info->module/"#")[0] ); 
  string mn; 
 
  if (!skip_conf) { 
    if (c) { 
      mn = "<a href='../sites/site.html/" + 
        Roxen.http_encode_url (mod_info->conf) + "/'>" + 
        Roxen.html_encode_string (c->query_name()) + "</a>"; 
    } 
    else 
      mn = Roxen.html_encode_string ( 
        sprintf ((string) _(542, "the deleted site %O"), mod_info->conf)); 
  } 
 
  if( m ) { 
    string module = "<a href='../sites/site.html/"+ 
      Roxen.http_encode_url(mod_info->conf)+"/n!n/"+ 
      replace(mod_info->module,"#","!")+"/"+ 
      "'>"+ Roxen.html_encode_string (i->get_name())+"</a>"; 
    if (mn) 
      mn = sprintf ((string) _(543, "%s in %s"), module, mn); 
    else 
      mn = module; 
  } 
  else if( i ) { 
    if (mn) 
      mn = sprintf ( 
        (string) _(544, "the deleted module %s in %s"), 
        Roxen.html_encode_string (sprintf ("%O", (string) i->get_name())), 
        mn); 
    else 
      mn = sprintf ( 
        (string) _(545, "the deleted module %s"), 
        Roxen.html_encode_string (sprintf ("%O", (string) i->get_name()))); 
  } 
 
  return mn; 
} 
 
mapping|string parse( RequestID id ) 
{ 
  if( id->variables->image ) 
    return m_delete( .State->images, id->variables->image ); 
 
  if( !id->variables->db || 
      !( CU_AUTH( "Edit Global Variables" ) ) ) 
    return Roxen.http_redirect( "/dbs/", id ); 
 
  string res = 
    "<set variable='var.form-anchor' value='#dbquery'/>" 
    "<use file='/template'/><tmpl>" 
    "<topmenu base='../' selected='dbs'/>" 
    "<content><cv-split><subtablist width='100%'><st-tabs>" 
    "<insert file='subtabs.pike'/></st-tabs><st-page>" 
    "<input type='hidden' name='sort' value='&form.sort:http;' />\n"; 
 
  if( id->variables->action && actions[ id->variables->action ]) 
  { 
    res += "<input type='hidden' name='db' value='&form.db:http;' />\n"; 
    mixed tmp = actions[ id->variables->action ][1]( id->variables->db, id ); 
    if( stringp( tmp ) ) 
      return res+tmp+"\n</st-page></content></tmpl>"; 
    if( tmp ) 
      return tmp; 
  } 
 
  string url = DBManager.db_url( id->variables->db ); 
 
  string charset = "unicode"; 
#if !constant (Mysql.mysql.HAVE_MYSQL_FIELD_CHARSETNR) 
  if (DBManager.is_mysql (url)) { 
      // Ugly kludge for broken mysql client lib. Works most of the 
      // time, at least.. 
      charset = "broken-unicode"; 
  } 
#endif 
 
  Sql.Sql db; 
  mixed db_connect_error = 
    catch (db = DBManager.get (id->variables->db, 0, 0, 0, charset)); 
  if (db_connect_error) 
    // Try again without charset. 
    db_connect_error = catch (db = DBManager.get (id->variables->db)); 
  if (!db && !db_connect_error) { 
    db_connect_error = ({ "Failed to connect to database.\n", ({}) }); 
  } 
 
  string qres=""; 
 
  class QueryHistory 
  { 
    inherit Variable.Variable; 
    constant type = "QueryHistory"; 
 
    void create() 
    { 
      ::create( ([]), 65535 ); 
    } 
  }; 
 
  object user = id->misc->config_user; 
  QueryHistory hs; 
  if( !(hs = user->settings->getvar( "db_history" ) ) ) 
  { 
    user->settings->defvar( "db_history", (hs = QueryHistory( )) ); 
    user->settings->restore(); 
  } 
  if( (!id->variables->query || id->variables["reset_q.x"]) ) 
  { 
    array sel_t_columns = ({}); 
    if( !(<0, "">)[id->variables->table] ) { 
      if (mixed err = catch { 
          sel_t_columns = DBManager.db_table_fields( id->variables->db, 
                                                     id->variables->table ) 
            ->name; 
        }) { 
        report_debug ("Error listing fields for table %s.%s: %s", 
                      id->variables->db, id->variables->table, 
                      describe_error (err)); 
      } 
    } 
 
    mapping h = hs->query(); 
 
    function(string:string) quote_name = lambda (string s) {return s;}; 
    if (DBManager.is_mysql (id->variables->db)) 
      // FIXME: Ought to be generalized and put into Sql.pmod. 
      quote_name = lambda (string s) { 
                     if (db && db->master_sql->is_keyword (s)) 
                       return "`" + s + "`"; 
                     return s; 
                   }; 
 
    if( !id->variables["reset_q.x"] && 
        h[id->variables->db+"."+id->variables->table] ) 
      id->variables->query = h[id->variables->db+"."+id->variables->table]; 
    else if( !(<0, "">)[id->variables->table] ) 
      id->variables->query = "SELECT "+ 
        (sizeof (sel_t_columns) ? 
         map (sel_t_columns, quote_name) *", " : "*") + 
        " FROM "+ quote_name (id->variables->table); 
    else if( DBManager.is_mysql( id->variables->db ) ) 
      id->variables->query = "SHOW TABLES"; 
    else 
      id->variables->query = ""; 
  } 
 
  if (id->variables["run_q.x"] || id->variables["reset_q.x"]) 
    hs->query()[ id->variables->db+"."+id->variables->table ] 
      = id->variables->query-"\r"; 
 
  if(db && id->variables["run_q.x"]) 
  { 
    user->settings->save(); 
 
    string query = ""; 
    // Normalize. 
    foreach( (id->variables->query-"\r")/"\n", string q ) 
    { 
      //q = (q/" "-({""}))*" "; 
      q = String.trim_all_whites (q); 
      if (q == "--") break; 
      if (q != "") 
        query = (query == "" ? q : query + "\n" + q); 
    } 
 
    foreach( (query/";\n")-({""}); int i; string q ) 
    { 
      Sql.sql_result big_q; 
 
      int h = gethrtime(); 
      if (mixed err = catch (big_q = db->big_query( q ))) { 
        qres += "<p><font color='&usr.warncolor;'>"+ 
          sprintf((string)_(1062,"Error running query %d: %s"), i + 1, 
                  replace (Roxen.html_encode_string ( 
                             String.trim_all_whites (describe_error(err))), 
                           "\n", "<br/>\n"))+ 
          "</font></p>\n"; 
        continue; 
      } 
      float qtime = (gethrtime()-h)/1000000.0; 
 
      if (!big_q) 
        // Query had no result or was empty/commented out. 
        continue; 
 
      do { 
        int qrows; 
        qres += "<p>\n" 
          "<table id='res' class='nice valign-top'><thead><tr>"; 
        // FIXME: Using id='res' above is wrong, as the tag 
        //        can be generated multiple times in the same 
        //        document. See also similar code further below. 
        multiset right_columns = (<>); 
        int column; 
 
        array(string) col_types = ({}); 
        array(string) col_names = ({}); 
 
        foreach( big_q->fetch_fields(), mapping field ) 
        { 
          switch( field->type  ) 
          { 
          case "char":  // Actually a TINYINT. 
          case "tiny integer": 
          case "short": 
          case "int": 
          case "integer": 
          case "long": 
          case "long integer": 
          case "int24": 
          case "longlong": 
            right_columns[column]=1; 
            qres += "<th class='num'>"; 
            col_types += ({"int"}); 
            break; 
          case "real": 
          case "float": 
          case "double": 
            right_columns[column]=1; 
            qres += "<th class='num'>"; 
            col_types += ({"float"}); 
            break; 
          case "decimal": 
          case "numeric": 
            qres += "<th class='num'>"; 
            col_types += ({"string"}); 
            break; 
          case "bit": 
          default: 
            qres += "<th>"; 
            col_types += ({"string"}); 
          } 
          qres += Roxen.html_encode_string (field->name) + "</th>\n"; 
          col_names += ({ field->name }); 
          column++; 
        } 
        qres += "</tr></thead>"; 
 
        mapping(string:string) mod_info = 
          DBManager.module_table_info (id->variables->db, ""); 
 
        Configuration c = !(<0, "">)[mod_info->conf] && 
          roxen.find_configuration (mod_info->conf); 
        RoxenModule m = c && !(<0, "">)[mod_info->module] && 
          c->find_module (mod_info->module); 
 
        // Find any column formatter callback in the DB's owner 
        // module. See function prototype in base_server/module.pike. 
        function(string,string,string,array(string),array(string),array(string), 
                 RequestID:string) format_col_cb = 
          m && m->format_db_browser_value; 
        array(int) formatted_total_size = allocate(sizeof(col_names), 0); 
        if (id->variables->exp_fields == "disabled") 
          format_col_cb = 0; 
 
        while( array q = big_q->fetch_row() ) 
        { 
          qrows++; 
          qres += "<tr>"; 
          for( int i = 0; i<sizeof(q); i++ ) { 
            qres += right_columns[i] ? "<td class='num'>" : "<td>"; 
            if( !q[i] ) 
              qres += "<i>NULL</i>"; 
            else if( intp( q[i] ) || col_types[i] == "int" ) 
              qres += (string) (int) q[i]; 
            else if( floatp( q[i] ) || col_types[i] == "float" ) 
              qres += (string) (float) q[i]; 
            else if( is_image( q[i] ) ) 
              qres += 
                "<img src='browser.pike?image=" + store_image( q[i] ) + 
                "&amp;&usr.set-wiz-id;' />"; 
            else { 
              mixed tmp = q[i]; 
              int got_result; 
              if (format_col_cb) { 
                //  Check for excessive amount of formatted data 
                if (formatted_total_size[i] > MAX_TOTAL_FORMATTED_SIZE) { 
                  qres += 
                    "<span class='warn_exp'>" + 
                    "Total formatted data length exceeded &ndash; limit your query." 
                    "</span>"; 
                  got_result = 1; 
                } else if (mixed formatted = 
                           format_col_cb (id->variables->db, 
                                          id->variables->table, 
                                          col_names[i], col_names, 
                                          col_types, q, id)) { 
                  int formatted_len = sizeof(formatted); 
                  if ((formatted_len >= MAX_FIELD_FORMATTED_SIZE) && 
                      (id->variables->exp_fields == "auto")) { 
                    //  This field alone is too big to display 
                    qres += 
                      "<span class='warn_exp'>" + 
                      "Skipping " + (formatted_len / 1024) + "K formatted data." 
                      "</span>"; 
                    got_result = 1; 
                  } else { 
                    formatted_total_size[i] += formatted_len; 
                    qres += formatted; 
                    got_result = 1; 
                  } 
                } 
              } 
 
              if (!got_result) { 
                if (is_deflated (tmp)) { 
                  // is_deflated _may_ give false positives, hence the catch. 
                  catch { 
                    tmp = Gz.inflate()->inflate (tmp); 
                  }; 
                } 
 
                if( is_encode_value( tmp ) ) 
                  qres += format_decode_value(tmp); 
                else if (String.width (tmp) > 8) { 
                  // Let wide chars skip past the %q quoting, because 
                  // it'll quote them to \u escapes otherwise. 
                  string q = ""; 
                  int s; 
                  foreach (tmp; int i; int c) 
                    if (c >= 256) { 
                      if (s < i) q += sprintf ("%q", tmp[s..i - 1])[1..<1]; 
                      q += sprintf ("%c", c); 
                      s = i + 1; 
                    } 
                  q += sprintf ("%q", tmp[s..])[1..<1]; 
                  qres += Roxen.html_encode_string (q); 
                } 
                else 
                  qres += Roxen.html_encode_string(sprintf("%q", tmp)[1..<1]); 
              } 
            } 
            qres += "</td>"; 
          } 
          qres += "</tr>\n"; 
        } 
 
        qres += "</table>"+ 
          sprintf( _(426,"Query took %[0].3fs, %[1]d rows in the reply")+ 
                   "\n</p>\n", qtime, qrows); 
 
        if (!big_q->next_result) break; 
        h = gethrtime(); 
        if (mixed err = catch (big_q = big_q->next_result())) { 
          qres += "<p><font color='&usr.warncolor;'>"+ 
            sprintf((string)_(1062,"Error running query %d: %s"), i + 1, 
                    replace (Roxen.html_encode_string ( 
                               String.trim_all_whites (describe_error(err))), 
                             "\n", "<br/>\n"))+ 
            "</font></p>\n"; 
          break; 
        } 
        qtime = (gethrtime()-h)/1000000.0; 
        if (!big_q) break; 
 
        // More results available. 
      } while(1); 
    } 
  } 
 
  if( !(<0, "">)[id->variables->table] ) 
    res += "<input type=hidden name='table' value='&form.table:http;' />\n"; 
 
  // DB switcher and title. 
 
  res += "" 
    "<table class='auto'><tr valign='center'>" 
    "<td><span class='icon db x-large'></span></td>" + 
    "<td>" + db_switcher( id ) + "</td></tr></table>\n" 
    "<hr class='section'>" 
    "<h3>Database <em>" + Roxen.html_encode_string (id->variables->db) + "</em></h3>\n"; 
 
  if (db_connect_error) 
    res += "<div class='notify error'>" + 
      _(1063, "Error connecting to database: ") + 
      Roxen.html_encode_string (describe_error (db_connect_error)) + 
      "</div>\n"; 
 
  // Bullet list with generic database info. 
 
  res += "<ul>\n"; 
 
#define ADD_LI(X,Y) lis += ({ ([ "value" : (X), "bold" : Y ]) }) 
 
  array(mapping) lis = ({}); 
 
  if (id->variables->db == "local") { 
    ADD_LI(_(546, "Internal data that cannot be shared between servers."), 0); 
    res += "<li>" + 
      _(546, "Internal data that cannot be shared between servers.") + "</li>\n"; 
  } 
  else if (id->variables->db == "shared") { 
    ADD_LI(_(547, "Internal data that may be shared between servers."), 0); 
    res += "<li>" + 
      _(547, "Internal data that may be shared between servers.") + "</li>\n"; 
  } 
  else if( !url ) { 
    ADD_LI("Internal database.", 0); 
    res += "<li>Internal database.</li>\n"; 
  } 
  else { 
    ADD_LI("Database URL: " + Roxen.html_encode_string(url), 0); 
    res += "<li>Database URL: " + Roxen.html_encode_string(url)+"</li>\n"; 
  } 
 
  mapping(string:string) db_info = 
    DBManager.module_table_info (id->variables->db, ""); 
  if (string owner = format_table_owner (db_info)) { 
    ADD_LI(sprintf((string)_(428,"Defined by %s."), owner), 0); 
    res += "<li>" + sprintf((string)_(428,"Defined by %s."), owner) + 
      "</li>\n"; 
  } 
  if (string c = db_info->comment) { 
    c = String.trim_all_whites (c); 
    if (c != "") { 
      ADD_LI(c, 0); 
      res += "<li>" + Roxen.html_encode_string (c) + "</li>\n"; 
    } 
  } 
 
  string default_charset = DBManager.get_db_default_charset(id->variables->db); 
  if (default_charset) { 
    ADD_LI(_(548,"Default charset:") + default_charset, 0); 
    res += "<li>" + _(548,"Default charset:") + 
      Roxen.html_encode_string(default_charset) + "</li>"; 
  } 
 
  ADD_LI(sprintf( 
      (string) 
      _(506,"Member of the %s database group."), 
      "<a href='edit_group.pike?group="+ 
      Roxen.http_encode_url(DBManager.db_group( id->variables->db ))+ 
      "&amp;&usr.set-wiz-id;'>" + 
      DBManager.get_group( DBManager.db_group( id->variables->db ) ) 
      ->lname + 
      "</a>" 
  ), 0); 
 
  res +="<li>" + 
    sprintf( (string) 
             _(506,"Member of the %s database group."), 
             "<a href='edit_group.pike?group="+ 
             Roxen.http_encode_url(DBManager.db_group( id->variables->db ))+ 
             "&amp;&usr.set-wiz-id;'>" + 
             DBManager.get_group( DBManager.db_group( id->variables->db ) ) 
             ->lname + 
             "</a>") 
    + "</li>"; 
 
  string schedule = DBManager.db_schedule(id->variables->db); 
  if (schedule) { 
    ADD_LI(sprintf( 
      (string)_(1114, "Backuped via the %s backup schedule."), 
      "<a href='schedules.html'>" + (schedule) +"</a>" 
    ), 0); 
    res += "<li>" + 
      sprintf( (string)_(1114, "Backuped via the %s backup schedule."), 
               "<a href='schedules.html'>" + 
               Roxen.html_encode_string(schedule) + 
               "</a>") + 
      "</li>"; 
  } else { 
    ADD_LI(_(1115, "Not a member of any backup schedule."), 1); 
    res += "<li><b>" + 
      _(1115, "Not a member of any backup schedule.") + 
      "</b></li>"; 
  } 
  res += "</ul>\n"; 
 
  // TRACE("lis: %O\n", lis); 
 
  if (db) { 
    // The database table list. 
 
    res += "<p><table id='tbls' class='nice db-list'>"; 
 
    array table_data = ({}); 
    int sort_ok, got_owner_column; 
 
    string deep_table_info( string table ) 
    { 
      array(mapping(string:mixed)) data = 
        DBManager.db_table_fields( id->variables->db, table ); 
      if( !data ) 
        return sprintf((string)_(507,"Cannot list fields in %s databases"), 
                       DBManager.db_driver(id->variables->db) ); 
 
      multiset(string) props = (<>); 
      foreach (data, mapping(string:mixed) r) 
        foreach (r; string prop;) 
          props[prop] = 1; 
      props->name = 0;          // Always listed first. 
      props->type = 0;          // Always listed second. 
      props->table = 0;         // Just our own table - ignored. 
      array(string) sort_props = sort (indices (props)); 
 
      string res = "<table id='tbl-details' class='tbl-details'>" 
        "<thead>" 
        "<tr>" 
        "<th>Column</th>" 
        "<th>Type</th>" + 
        map (sort_props, 
             lambda (string prop) { 
               return "<th>" + Roxen.html_encode_string ( 
                 String.capitalize (prop)) + "</th>"; 
             }) * "" + 
        "</tr>\n" 
        "</thead>" 
        "<tbody>"; 
 
      foreach( data, mapping(string:mixed) r ) 
      { 
        res += "<tr>" 
          "<td>" + Roxen.html_encode_string (r->name) + "</td>" 
          "<td>" + Roxen.html_encode_string (r->type) + "</td>"; 
 
        foreach (sort_props, string prop) { 
          mixed val = r[prop]; 
          if (zero_type (val)) 
            res += "<td></td>"; 
          else if (intp (val) || floatp (val)) 
            res += "<td class='num'>" + val + "</td>"; 
          else if (stringp (val)) 
            res += "<td>" + Roxen.html_encode_string (val) + "</td>"; 
          else if (arrayp (val) && 
                   !catch (val = (array(string)) val)) 
            res += "<td>" + Roxen.html_encode_string (val * ", ") + "</td>"; 
          else if (multisetp (val) && 
                   !catch (val = (array(string)) indices (val))) 
            res += "<td>" + Roxen.html_encode_string (val * ", ") + "</td>"; 
          else if (objectp (val)) 
            res += "<td>" + Roxen.html_encode_string (sprintf ("%O", val)) + 
              "</td>"; 
          else 
            res += "<td>" + Roxen.html_encode_string (sprintf ("<%t>", val)) + 
              "</td>"; 
        } 
 
        res += "</tr>\n"; 
      } 
 
      return res+ "</tbody></table>"; 
    }; 
 
    void add_table_info( string table, mapping tbi ) 
    { 
      mapping(string:string) tbl_info = 
        DBManager.module_table_info (id->variables->db, table); 
 
      int deep_info = id->variables->table == table; 
 
      string res = "<tr id='tbl-" + Roxen.http_encode_url(table) + "'" + 
        (deep_info ? " class='tbl-open'" : "") + 
        ">" 
        "<td class='nowrap'>" 
        "<a href='browser.pike?sort=&form.sort:http;&amp;" 
        "db=&form.db:http;&amp;&usr.set-wiz-id;" + 
        (deep_info ? "" : "&amp;table="+Roxen.http_encode_url(table)) + 
        "#tbl-" + Roxen.http_encode_url(table) + "'" 
        " class='icon table no-decoration'>"+ 
        table+"</a></td>" 
        "<td class='num'>"+ 
        (!tbi || zero_type (tbi->rows) ? "" : tbi->rows) + "</td>" 
        "<td class='num'>" + 
        (!tbi || zero_type (tbi->data_length) ? "" : 
         sprintf ("%d KiB", 
                  ((int)tbi->data_length+(int)tbi->index_length) / 1024)) + 
        "</td>"; 
 
      string owner; 
      if ((db_info->conf || "") != (tbl_info->conf || "")) 
        owner = format_table_owner (tbl_info, 0); 
      else if ((db_info->module || "") != (tbl_info->module || "")) 
        owner = format_table_owner (tbl_info, 1); 
      if (owner) { 
        res += "<td>" + String.capitalize (owner) + "</td>"; 
        got_owner_column = 1; 
      } 
      else res += "<td></td>"; 
 
      if (deep_info) { 
        res += "</tr>\n<tr class='tbl-details'><td colspan='5'>"; 
 
        if (tbl_info->comment) 
          sscanf( tbl_info->comment, "%s\0%s", 
                  tbl_info->tbl, tbl_info->comment ); 
 
        if( tbl_info->tbl && tbl_info->tbl != table) 
          if( tbl_info->tbl != (string)0 ) { 
            res += 
              "<div class='notify warn description'>" + 
              sprintf((string) _(429,"The table is known as %O " 
                                 "in the module."), tbl_info->tbl ) + "</div>\n"; 
          } 
          else { 
            res += 
              "<div class='notify warn description'>" + 
              sprintf((string) _(430,"The table is an anonymous table defined " 
                                 "by the module."), tbl_info->tbl ) + "</div>\n"; 
          } 
 
        if (string c = tbl_info->comment) { 
          c = String.trim_all_whites (c); 
          if (c != "" && c != "0") { 
            res += 
              "<div class='description'>" + 
              Roxen.html_encode_string (c) + 
              "</div>\n"; 
          } 
        } 
 
        res += deep_table_info (table) + "</td></tr>\n"; 
      } 
      else 
        res += "</tr>\n"; 
 
      if( tbi ) 
        sort_ok = 1; 
 
      table_data += ({({ 
                        table, 
                        (tbi ?(int)tbi->data_length+ (int)tbi->index_length:0), 
                        (tbi ?(int)tbi->rows:0), 
                        res 
                      })}); 
    }; 
 
    foreach( DBManager.db_tables( id->variables->db )-({0}), string tb ) 
      add_table_info(tb, 
                     DBManager.db_table_information(id->variables->db, tb)); 
 
    switch( id->variables->sort ) 
    { 
      default: 
        sort( column( table_data, 0 ), table_data ); 
        break; 
 
      case "rows": 
        sort( column( table_data, 2 ), table_data ); 
        table_data = reverse( table_data ); 
        break; 
 
      case "size": 
        sort( column( table_data, 1 ), table_data ); 
        table_data = reverse( table_data ); 
        break; 
    } 
#define SEL(X,Y)                                                        \ 
    ((id->variables->sort == X || (Y && !id->variables->sort)) ?        \ 
     "<span class='icon sort-desc'></span>" :                           \ 
     "<span class='icon sort'></span>") 
 
    if( sort_ok ) 
    { 
      if (!got_owner_column) 
        // Try to hide the owner column when it's empty. Doesn't work 
        // in firefox 2.0, though. 
        res += "<col span='3'/><col style='visiblity: collapse'/>\n"; 
      res += 
        "<thead><tr>" 
        "<th><a href='browser.pike?db=&form.db:http;&amp;table=&form.table:http;&amp;sort=name&amp;&usr.set-wiz-id;'>"+ 
        SEL("name", 1) + _(376,"Name")+ 
        "</a></th>\n" 
        "<th class='num'><a href='browser.pike?db=&form.db:http;&amp;table=&form.table:http;&amp;sort=rows&amp;&usr.set-wiz-id;'>"+ 
        SEL("rows",0)+String.capitalize(_(374,"rows"))+ 
        "</a></th>\n" 
        "<th class='num'><a href='browser.pike?db=&form.db:http;&amp;table=&form.table:http;&amp;sort=size&amp;&usr.set-wiz-id;'>"+ 
        SEL("size",0)+_(377,"Size")+ 
        "</a></th>\n" 
        "<th>Owner</th>\n" 
        "</tr></thead>\n"; 
    } 
 
    res += "<tbody>" + column( table_data, 3 )*"\n" + 
      "</tbody></table></p>\n"; 
 
    // Query widget. 
 
    string formatter_options = 
      "<span style='font-size: smaller;'>Smart field formatters: </span>" 
      "<default variable='form.exp_fields'>" 
      "<select name='exp_fields'>" 
      " <option value='auto'>Enabled for data &lt; " + (MAX_FIELD_FORMATTED_SIZE / 1024) + "K</option>" 
      " <option value='disabled'>Disabled</option>" 
      " <option value='force'>Force expansion of long fields</option>" 
      "</select>" 
      "</default>" 
      "<br />"; 
 
    int db_has_formatters = 0; 
    if (id->variables->db) { 
      mapping(string:string) mod_info = 
        DBManager.module_table_info (id->variables->db, ""); 
      Configuration c = !(<0, "">)[mod_info->conf] && 
        roxen.find_configuration (mod_info->conf); 
      RoxenModule m = c && !(<0, "">)[mod_info->module] && 
        c->find_module (mod_info->module); 
      db_has_formatters = m && m->format_db_browser_value; 
    } 
 
    res += 
      "<a name='dbquery'></a><p>" 
      "<textarea rows='12' cols='90' wrap='soft' name='query' " 
      " style='font-size: 90%'>" + 
      Roxen.html_encode_string (id->variables->query) + "</textarea><br />" + 
      (db_has_formatters ? formatter_options : "") + 
      "<table class='auto'><tr><td>" 
      "<submit-gbutton2 name=reset_q>"+_(378,"Reset query")+"</submit-gbutton2>" 
      "</td><td>" 
      "<submit-gbutton2 name=run_q>"+_(379,"Run query")+"</submit-gbutton2>" 
      "</td><td style='font-size: smaller; padding-left: 10px'>" + 
      _(1064, "Tip: Put '--' on a line to ignore everything below it.") + 
      "</td></tr></table></p>"; 
 
    // Query result. 
 
    res += qres; 
  } 
 
  // Actions. 
 
  res += "<p>"; 
 
  int flags = DBManager.is_internal(id->variables->db)*3; 
  if (!flags) { 
    mapping(string:mixed) url_info = 
      DBManager.get_db_url_info(id->variables->db); 
    if (url_info && has_prefix(url_info->path, "mysql://")) { 
      // Is external mysql. 
      flags = 2; 
    } 
  } 
 
#define ADD_ACTION(X) if(!actions[X][2] || (actions[X][2] & flags) )    \ 
   res += sprintf("<link-gbutton href='%s?db=%s&amp;action=%s&amp;&usr.set-wiz-id;'>%s</link-gbutton>\n",\ 
                  id->not_query, id->variables->db, X, actions[X][0] ) 
 
#define ADD_ACTION2(X)                                                           \ 
  do {                                                                           \ 
    res += sprintf(                                                              \ 
      "<option value='%s?db=%s&amp;action=%s&amp;&usr.set-wiz-id;'>%s</option>", \ 
      id->not_query, id->variables->db, X, actions[X][0]);                       \ 
  } while (0) 
 
  switch( id->variables->db ) 
  { 
    case "local": 
      res += "<hr><select data-goto=''><option>Select action...</option>"; 
 
      foreach( ({ "move","backup","optimize","repair","schedule" }), string x ) { 
        ADD_ACTION2( x ); 
      } 
 
      res += 
        "</select>" 
        "<link-gbutton href='./' type='cancel'>Cancel</link-gbutton>"; 
      break; 
 
    default: 
      res += "<hr><select data-goto=''><option>Select action...</option>"; 
 
      foreach( sort(indices( actions )), string x ) { 
        ADD_ACTION2( x ); 
      } 
 
      res += 
        "</select>" 
        "<link-gbutton href='./' type='cancel'>Cancel</link-gbutton>"; 
      break; 
  } 
  return res+"</p></st-page></subtablist></cv-split></content></tmpl>"; 
}