File: wa_integration.sql

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (1882 lines) | stat: -rw-r--r-- 61,570 bytes parent folder | download
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
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
--
--  $Id: wa_integration.sql,v 1.12.2.3 2010/10/07 13:24:15 source Exp $
--
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--
--  Copyright (C) 1998-2006 OpenLink Software
--
--  This project is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; only version 2 of the License, dated June 1991.
--
--  This program is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along
--  with this program; if not, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--

use DB
;

create procedure blog2_exec_no_error(in expr varchar) {
  declare state, message, meta, result any;
  exec(expr, state, message, vector(), 0, meta, result);
}
;

-- eliminate old 'blog' application from WA
delete from DB.DBA.WA_TYPES where WAT_NAME = 'WEBLOG'
;

delete from DB.DBA.WA_MEMBER_TYPE where WMT_APP = 'db.dba.wa_blog'
;

delete from DB.DBA.WA_INSTANCE where WAI_NAME = 'db.dba.wa_blog'
;

-- create new blog2 application in WA
blog2_exec_no_error('
  create type wa_blog2 under web_app as (
    blogid varchar,
    owner  int
  )
  constructor method wa_blog2(stream any),
  overriding method wa_id_string() returns any,
  overriding method wa_new_inst(login varchar) returns any,
  overriding method wa_join_request(login varchar) returns any,
  overriding method wa_leave_notify(login varchar) returns any,
  overriding method wa_membership_edit_form (stream any) returns any,
  overriding method wa_front_page (stream any) returns any,
  overriding method wa_state_edit_form(stream any) returns any,
  overriding method wa_state_posted(post any, stream any) returns any,
  overriding method wa_home_url() returns varchar,
  overriding method wa_periodic_activity() returns any,
  overriding method wa_drop_instance() returns any,
  overriding method wa_private_url() returns any,
  overriding method wa_join_approve(login varchar) returns any,
  overriding method wa_member_data (u_id int, stream any) returns any,
  overriding method wa_member_data_edit_form (u_id int, stream any) returns any,
  overriding method wa_class_details () returns varchar,
  overriding method wa_https_supported () returns int,
  overriding method wa_dashboard () returns any,
  overriding method wa_addition_urls () returns any,
  overriding method wa_addition_instance_urls () returns any,
  method wa_vhost_options () returns any,
  method wa_new_inst_from_old_blog(login varchar, blogid varchar) returns any,
  method wa_dashboard_last_item () returns any
  '
)
;

wa_exec_no_error('alter type wa_blog2 add overriding method wa_front_page_as_user(inout stream any, in user_name varchar) returns any');

wa_exec_no_error('alter type wa_blog2 add overriding method wa_size() returns int');
wa_exec_no_error('alter type wa_blog2 add method wa_vhost_options () returns any');
wa_exec_no_error('alter type wa_blog2 add method wa_dashboard_last_item () returns any');
wa_exec_no_error('alter type wa_blog2 add overriding method wa_rdf_url (in vhost varchar, in lhost varchar) returns varchar');
wa_exec_no_error('alter type wa_blog2 add overriding method wa_post_url (in vhost varchar, in lhost varchar, in inst_name varchar, in post any) returns varchar');


insert replacing DB.DBA.WA_TYPES(WAT_NAME, WAT_DESCRIPTION, WAT_TYPE, WAT_REALM) values ('WEBLOG2', 'Blog', 'db.dba.wa_blog2', 'wa')
;

insert soft DB.DBA.WA_MEMBER_TYPE (WMT_APP, WMT_NAME, WMT_ID, WMT_IS_DEFAULT) values ('WEBLOG2', 'author', 2, 0)
;

insert soft DB.DBA.WA_MEMBER_TYPE (WMT_APP, WMT_NAME, WMT_ID, WMT_IS_DEFAULT) values ('WEBLOG2', 'reader', 3, 0)
;

-- upgrade from previous versions
delete from DB.DBA.WA_MEMBER_TYPE where WMT_ID < 2
;



create method wa_id_string () for wa_blog2 {
  return 'weblog2_' || self.blogid || '_' || cast (self.owner as varchar);
}
;

create constructor method wa_blog2 (inout stream any) for wa_blog2 {
  ;
}
;

create method wa_member_data (in u_id int, inout stream any) for wa_blog2 {
  return 'N/A';
}
;

create method wa_member_data_edit_form (in u_id int, inout stream any) for wa_blog2 {
  declare home, sid varchar;
  select BI_HOME into home from BLOG.DBA.SYS_BLOG_INFO, "DB"."DBA"."SYS_USERS"
  where BI_BLOG_ID = self.blogid and BI_OWNER = self.owner;

  sid := connection_get ('wa_sid');

  if (sid is null)
    {
      sid := md5 (concat (datestring (now ()), http_client_ip (), http_path ()));
      insert into DB.DBA.VSPX_SESSION (VS_REALM, VS_SID, VS_UID, VS_STATE, VS_EXPIRY)
      values ('wa', sid, connection_get ('vspx_user'),
      serialize (
	vector (
	  'vspx_user', connection_get ('vspx_user'),
	  'blogid' , self.blogid,
	  'uid', connection_get ('vspx_user')
	  )
	), now());
    }
  http_request_status ('HTTP/1.1 302 Found');
  http_header(sprintf('Location: %sindex.vspx?page=member_data&member_id=%d&sid=%s&realm=wa\r\n', home, u_id, sid));
  return;
}
;

create method wa_drop_instance () for wa_blog2 {
  declare blog_home, blog_path any;
  blog_home := (select BI_HOME from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = self.blogid);
  blog_path := (select BI_P_HOME from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = self.blogid);
  blog_home := subseq(blog_home, 0, length(blog_home) - 1);
  for select BI_P_HOME from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = self.blogid do {
    delete from WS.WS.SYS_DAV_RES where RES_FULL_PATH like BI_P_HOME||'%';
    delete from WS.WS.SYS_DAV_COL where WS.WS.COL_PATH (COL_ID) like BI_P_HOME||'%';
  }
  delete from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = self.blogid;
  (self as web_app).wa_drop_instance();
  DB.DBA.VHOST_REMOVE(lpath => blog_home);
  DB.DBA.VHOST_REMOVE(lpath => blog_home || '/templates');
  DB.DBA.VHOST_REMOVE(lpath => blog_home || '/gems');
  DB.DBA.VHOST_REMOVE(lpath => blog_home || '/images');
  DB.DBA.VHOST_REMOVE(lpath => blog_home || '/audio');
  for select HP_HOST, HP_LISTEN_HOST, HP_LPATH from DB.DBA.HTTP_PATH where
      HP_PPATH = blog_path and
      HP_HOST not like '%ini%'and HP_HOST not like '*sslini*'
  do
  {
    DB.DBA.VHOST_REMOVE(vhost=>HP_HOST, lhost=>HP_LISTEN_HOST, lpath=>HP_LPATH);
  }
  -- call parent method to make wa level application and membership management action
}
;

create method wa_join_request (in login varchar) for wa_blog2 {
  -- empty one
  ;
}
;

create method wa_join_approve (in login varchar) for wa_blog2 {
  -- empty one
  ;
}
;

create procedure blog_ensure_domain (inout inst wa_blog2, in vhost any, in lhost any)
{
  declare dirs any;
  dirs := inst.wa_addition_urls ();
  foreach (any dir in dirs) do
    {
      if (not exists (select 1 from DB.DBA.HTTP_PATH where HP_HOST = vhost and HP_LISTEN_HOST = lhost and HP_LPATH = dir[2]))
  {
    VHOST_DEFINE(
        vhost=>vhost,
        lhost=>lhost,
        lpath=>dir[2],
        ppath=>dir[3],
        is_dav=>dir[4],
        is_brws=>dir[5],
        def_page=>dir[6],
        auth_fn=>dir[7],
        realm=>dir[8],
        ppr_fn=>dir[9],
        vsp_user=>dir[10],
        soap_user=>dir[11],
        sec=>dir[12],
        ses_vars=>dir[13],
        soap_opts=>dir[14],
        auth_opts=>dir[15],
        opts=>dir[16],
        is_default_host=>dir[17]);
  }
    }
}
;

-- owner makes a new blog
create method wa_new_inst (in login varchar) for wa_blog2 {
  declare uid, id, num, _mem_model int;
  declare inst_name, descr, folder, home, blogid, path varchar;
  declare inst wa_blog;

  home := connection_get('blog2_home');
  uid := (select U_ID from DB.DBA.SYS_USERS where U_NAME = login);

  num := (select count(*) from BLOG.DBA.SYS_BLOG_INFO where BI_OWNER = uid);

again:
  blogid := sprintf ('%s-blog-%d', login, num);
  if (exists (select 1 from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = blogid))
    {
      num := num + 1;
      goto again;
    }

  folder := sprintf ('/DAV/home/%s/%s/', login, blogid);

  path := sprintf ('/DAV/home/%s/Blogs/', login);
  DB.DBA.DAV_MAKE_DIR (path, uid, null, '110100000N');
  update WS.WS.SYS_DAV_COL set COL_DET = 'Blog' where COL_ID = DAV_SEARCH_ID (path, 'C');

  BLOG.DBA.BLOG2_HOME_CREATE (uid, blogid, folder, home);

  self.blogid := blogid;
  self.owner := uid;

  if(self.wa_name is null or length(self.wa_name) = 0) {
    signal('WA002', 'self.wa_name can not be empty');
  }

  select BI_TITLE, BI_WAI_MEMBER_MODEL into descr, _mem_model from BLOG.DBA.SYS_BLOG_INFO
  where BI_BLOG_ID = self.blogid;
  if (_mem_model is null)
    _mem_model := 0;
  update BLOG.DBA.SYS_BLOG_INFO set BI_WAI_NAME=self.wa_name, BI_WAI_MEMBER_MODEL=_mem_model
  where  BI_BLOG_ID = self.blogid;

  insert into DB.DBA.WA_INSTANCE (WAI_NAME, WAI_TYPE_NAME, WAI_INST, WAI_DESCRIPTION, WAI_MEMBER_MODEL)
    values (self.wa_name, 'WEBLOG2', self, descr, _mem_model);

  -- call parent method to make wa level membership management action
  return (self as web_app).wa_new_inst(login);
}
;

create method wa_new_inst_from_old_blog(in login varchar, in old_blogid varchar) for wa_blog2 {
  declare uid, id, blogid, num int;
  declare inst_name, blog_name, descr, folder varchar;
  declare inst wa_blog;

  uid := (select U_ID from DB.DBA.SYS_USERS where U_NAME = login);

  if (not exists (select 1 from BLOG.DBA.SYS_BLOG_INFO where BI_OWNER = uid)) {
    num := 0;
  }
  else {
    num := (select count(*) from BLOG.DBA.SYS_BLOG_INFO where BI_OWNER = uid);
  }

  -- determine old blog physical path in DAV
  declare old_p_path any;
  old_p_path := (select BI_P_HOME from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = old_blogid);

  -- determine old photo path in DAV
  declare _photo, descrip, oldopts any;
  whenever not found goto nfb;
  select BI_PHOTO, BI_ABOUT, BI_TITLE, deserialize (blob_to_string (BI_OPTIONS))
      into _photo, descrip, descr, oldopts from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = old_blogid;
  nfb:

  folder := sprintf ('/DAV/home/%s/%s/', login, old_blogid);
  self.blogid := old_blogid;

  BLOG.DBA.BLOG2_HOME_CREATE(uid, self.blogid, folder);
  BLOG..BLOG2_SET_OPTION ('WelcomeMessage', oldopts, descrip);

  if(self.wa_name is null or length(self.wa_name) = 0) {
    signal('WA002', 'self.wa_name can not be empty');
  }

  self.wa_member_model := 0;
  self.owner := uid;

  insert into DB.DBA.WA_INSTANCE (WAI_NAME, WAI_TYPE_NAME, WAI_INST, WAI_DESCRIPTION)
    values (self.wa_name, 'WEBLOG2', self, descr);

  -- determine new blog physical path in DAV
  declare new_p_path, vhosts any;
  new_p_path := (select BI_P_HOME from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = self.blogid);

  -- keep an analogue of old one to preserve foreign references
  DB.DBA.VHOST_DEFINE (lpath=>sprintf ('/blog/%s/blog', login), ppath=>new_p_path, vsp_user=>'dba', is_dav=>1,
      def_page => 'index.vspx', is_brws=>0, ppr_fn=>'BLOG.DBA.BLOG2_RSS2WML_PP', ses_vars=>1);

  -- remove old VDs
  for select p.HP_HOST as HOST, p.HP_LISTEN_HOST as LISTEN_HOST, p.HP_LPATH as LPATH
    from DB.DBA.HTTP_PATH p where p.HP_PPATH = old_p_path do
    {
      http_map_del (LPATH, HOST, LISTEN_HOST);
      -- remove descendant gems directory
      if (exists (select 1 from DB.DBA.HTTP_PATH p1 where p1.HP_PPATH = concat (old_p_path, 'gems/')
      and p1.HP_LISTEN_HOST = LISTEN_HOST and p1.HP_HOST = HOST and p1.HP_LPATH = concat (LPATH, '/gems')))
        VHOST_REMOVE (HOST, LISTEN_HOST, concat (LPATH,'/gems'));
    }

  vhosts := vector ();
  for select distinct HP_HOST, HP_LISTEN_HOST from DB.DBA.HTTP_PATH where HP_PPATH = old_p_path do
    {
      vhosts := vector_concat (vhosts, vector (vector (HP_HOST, HP_LISTEN_HOST)));
    }

  -- update the old VDs with new physical location
  update DB.DBA.HTTP_PATH set HP_PPATH = new_p_path, HP_POSTPROCESS_FUNC = 'BLOG.DBA.BLOG2_RSS2WML_PP'
  where HP_PPATH = old_p_path;

  -- put the new VDs in the set
  for select HP_LPATH, HP_PPATH, HP_HOST, HP_LISTEN_HOST, HP_STORE_AS_DAV, HP_DIR_BROWSEABLE,
           HP_DEFAULT, HP_SECURITY, HP_REALM, HP_AUTH_FUNC, HP_POSTPROCESS_FUNC, HP_RUN_VSP_AS,
             HP_RUN_SOAP_AS, HP_PERSIST_SES_VARS, HP_SOAP_OPTIONS, HP_AUTH_OPTIONS, HP_OPTIONS, HP_IS_DEFAULT_HOST
       from DB.DBA.HTTP_PATH where HP_PPATH = new_p_path do
    {
      http_map_table (HP_LPATH, HP_PPATH, HP_HOST, HP_LISTEN_HOST, HP_STORE_AS_DAV,
    HP_DIR_BROWSEABLE, HP_DEFAULT, HP_SECURITY, HP_REALM, HP_AUTH_FUNC, HP_POSTPROCESS_FUNC, HP_RUN_VSP_AS,
    HP_RUN_SOAP_AS, HP_PERSIST_SES_VARS,
    deserialize (HP_SOAP_OPTIONS), deserialize (HP_AUTH_OPTIONS), deserialize (HP_OPTIONS), HP_IS_DEFAULT_HOST);
    }

  foreach (any vd in vhosts) do
    {
      blog_ensure_domain (self, vd[0], vd[1]);
    }

  -- change old blog's resources path in DAV to new one
  declare _resources any;
  _resources := vector();

  for select RES_FULL_PATH, RES_PERMS, RES_OWNER, RES_GROUP
    from WS.WS.SYS_DAV_RES where RES_FULL_PATH like concat(old_p_path, '%')  do
      {
        _resources := vector_concat(_resources, vector(vector (RES_FULL_PATH, RES_PERMS, RES_OWNER, RES_GROUP)));
      }

  declare _idx, _old_res, _new_res, _dav_name, _dav_uid, _dav_pwd any;
  _dav_uid := http_dav_uid();
  _dav_name := (select U_NAME from WS.WS.SYS_DAV_USER where U_ID = _dav_uid);
  _dav_pwd := (select pwd_magic_calc (U_NAME, U_PWD, 1) from WS.WS.SYS_DAV_USER where U_ID = _dav_uid);
  _idx := 0;
  while(_idx < length(_resources)) {
    declare res_mime any;
    _old_res := _resources[_idx][0];
    res_mime := http_mime_type (_old_res);
    if (res_mime like 'image/%' or res_mime like 'audio/%' or res_mime like 'video/%' or lower (_old_res) like '%.wma')
      {
        if(_old_res not like concat(old_p_path, '%/%.%')) {
        -- additional directory level doesn't exists
        _new_res := replace(_old_res, old_p_path, concat(new_p_path, 'images/'));
      }
      else {
        -- additional directory level exists and equal 'images'
        if(_old_res like concat(old_p_path, 'images/%.%')) {
          _new_res := replace(_old_res, old_p_path, new_p_path);
        }
        else {
          -- skip unknown resource
          goto skip;
        }
      }
      DAV_COPY (_old_res, _new_res, 0, _resources[_idx][1], _resources[_idx][2], _resources[_idx][3], _dav_name, _dav_pwd);
    }
    skip:
    _idx := _idx + 1;
  }
  -- update personal photo position
  if(_photo is not null and length(_photo) > 0)
    {
      _photo := concat('images/', _photo);
    }
  update BLOG.DBA.SYS_BLOG_INFO
    set BI_PHOTO = _photo, BI_OPTIONS = serialize (oldopts) where BI_BLOG_ID = self.blogid;

  -- call parent method to make wa level membership management action
  return (self as web_app).wa_new_inst(login);
}
;

-- owner makes a new blog
create method wa_class_details () for wa_blog2
{
  declare info varchar;
  info := 'The Virtuoso Weblog Application allows you to run an online diary system. Like a diary it can be a private system, however in the spirit of weblog these are often public for outsides to pass comment.  Weblog supports community based operation to keep groups of weblogs together for collaboration of friends or fellow members of an organization department.';
  return info;
}
;

-- owner makes a new blog
create method wa_https_supported () for wa_blog2
{
  return 0;
}
;

create method wa_membership_edit_form (inout stream any) for wa_blog2
{
  declare home, sid varchar;
  select BI_HOME into home from BLOG.DBA.SYS_BLOG_INFO, DB.DBA.SYS_USERS
  where BI_BLOG_ID = self.blogid and BI_OWNER = self.owner;
  sid := connection_get ('wa_sid');

  if (sid is null)
    {
      sid := md5 (concat (datestring (now ()), http_client_ip (), http_path ()));
      insert into DB.DBA.VSPX_SESSION (VS_REALM, VS_SID, VS_UID, VS_STATE, VS_EXPIRY)
	  values ('wa', sid, connection_get ('vspx_user'),
	      serialize (
		vector (
		  'vspx_user', connection_get ('vspx_user'),
		  'blogid' , self.blogid,
		  'uid', connection_get ('vspx_user')
		  )
		), now());
    }
  http_request_status ('HTTP/1.1 302 Found');
  http_header(sprintf('Location: %sindex.vspx?page=membership&sid=%s&realm=wa\r\n', home, sid));
  return;
}
;

create method wa_front_page (inout stream any) for wa_blog2
{
  declare home, sid, phome varchar;
  select BI_HOME, BI_P_HOME into home, phome from BLOG.DBA.SYS_BLOG_INFO
  where BI_BLOG_ID = self.blogid and BI_OWNER = self.owner;

  if (is_http_ctx ())
    {
      declare vh, lh any;
      vh := http_map_get ('vhost');
      lh := http_map_get ('lhost');
      declare exit handler for not found
        {
          signal ('NOPAT', 'You do not have any virtual directory defined within current domain. Please define one.');
        };
      select HP_LPATH into home from DB.DBA.HTTP_PATH where HP_HOST = vh and HP_LISTEN_HOST = lh and HP_PPATH = phome;
      if (home not like '%/' and home <> '/')
        home := home || '/';
    }

  sid := connection_get ('wa_sid');

  if (sid is null)
    {
      sid := md5 (concat (datestring (now ()), http_client_ip (), http_path ()));
      insert into DB.DBA.VSPX_SESSION (VS_REALM, VS_SID, VS_UID, VS_STATE, VS_EXPIRY)
      values ('wa', sid, connection_get ('vspx_user'),
      serialize (
	vector (
	  'vspx_user', connection_get ('vspx_user'),
	  'blogid' , self.blogid,
	  'uid', connection_get ('vspx_user'),
	  'go_to_wa', 'yes'
	  )
	), now());
    }
  http_request_status ('HTTP/1.1 302 Found');
  http_header (sprintf('Location: %sindex.vspx?page=index&sid=%s&realm=wa\r\n', home, sid));
  return;
}
;

create method wa_front_page_as_user (inout stream any, in user_name varchar) for wa_blog2
{
  declare home, sid, phome, owner_name varchar;
  select BI_HOME, BI_P_HOME, U_NAME into home, phome, owner_name from BLOG.DBA.SYS_BLOG_INFO, "DB"."DBA"."SYS_USERS"
  where BI_BLOG_ID = self.blogid and BI_OWNER = self.owner and U_ID = BI_OWNER;


  if (is_http_ctx ())
    {
      declare vh, lh any;
      vh := http_map_get ('vhost');
      lh := http_map_get ('lhost');
      declare exit handler for not found
        {
          signal ('NOPAT', 'You do not have any virtual directory defined within current domain. Please define one.');
        };
      select HP_LPATH into home from DB.DBA.HTTP_PATH where HP_HOST = vh and HP_LISTEN_HOST = lh and HP_PPATH = phome;
      if (home not like '%/' and home <> '/')
        home := home || '/';
    }

  sid := connection_get ('wa_sid');

  if (sid is null)
    {
      sid := md5 (concat (datestring (now ()), http_client_ip (), http_path ()));
      insert into DB.DBA.VSPX_SESSION (VS_REALM, VS_SID, VS_UID, VS_STATE, VS_EXPIRY)
      values ('wa', sid, owner_name,
      serialize (
	vector (
	  'vspx_user', user_name,
	  'blogid' , self.blogid,
	  'uid', user_name
	  )
	), now());
    }
  http_request_status ('HTTP/1.1 302 Found');
  http_header (sprintf('Location: %sindex.vspx?page=index&sid=%s&realm=wa\r\n', home, sid));
  return;
}
;

create method wa_state_edit_form (inout stream any) for wa_blog2 {
  declare home, sid, phome varchar;

  select BI_HOME, BI_P_HOME into home, phome from BLOG.DBA.SYS_BLOG_INFO
  where BI_BLOG_ID = self.blogid and BI_OWNER = self.owner;

  if (is_http_ctx ())
    {
      declare vh, lh any;
      vh := http_map_get ('vhost');
      lh := http_map_get ('lhost');
      declare exit handler for not found
        {
          signal ('NOPAT', 'You do not have any virtual directory defined within current domain. Please define one.');
        };
      select HP_LPATH into home from DB.DBA.HTTP_PATH where HP_HOST = vh and HP_LISTEN_HOST = lh and HP_PPATH = phome;
      if (home not like '%/' and home <> '/')
        home := home || '/';
    }

  sid := connection_get ('wa_sid');

  if (sid is null)
    {
      sid := md5 (concat (datestring (now ()), http_client_ip (), http_path ()));
      insert into DB.DBA.VSPX_SESSION (VS_REALM, VS_SID, VS_UID, VS_STATE, VS_EXPIRY)
      values ('wa', sid, connection_get ('vspx_user'),
      serialize (
	vector (
	  'vspx_user', connection_get ('vspx_user'),
	  'blogid' , self.blogid,
	  'uid', connection_get ('vspx_user')
	  )
	), now());
    }
  http_request_status ('HTTP/1.1 302 Found');
  http_header (sprintf('Location: %sindex.vspx?page=ping&sid=%s&realm=wa\r\n', home, sid));
  return;
}
;

create method wa_state_posted (in post any, inout stream any) for wa_blog2 {
  -- empty one
  ;
}
;

create method wa_home_url () for wa_blog2 {
  declare uri varchar;
  uri := null;
  whenever not found goto endf;
  select BI_HOME into uri from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = self.blogid;
  endf:
  return uri;
}
;


create method wa_rdf_url (in vhost varchar, in lhost varchar) for wa_blog2
{
  declare p_path_str, l_path_str, full_path varchar;
  p_path_str := (select BI_P_HOME from BLOG..SYS_BLOG_INFO where BI_BLOG_ID = self.blogid);
  if (p_path_str is null)
    signal ('22023', 'No such blog');
  l_path_str := (select top 1 HP_LPATH from DB.DBA.HTTP_PATH where HP_PPATH = p_path_str and HP_HOST = vhost and HP_LISTEN_HOST = lhost);
  if (l_path_str is null)
    signal ('22023', 'No virtual directory found.');
  full_path := concat (l_path_str, '/gems/index.rdf');
  return full_path;
}
;

create method wa_post_url (in vhost varchar, in lhost varchar, in inst_name varchar, in post any) for wa_blog2
{
  declare p_path_str, l_path_str, full_path varchar;
  p_path_str := (select BI_P_HOME from BLOG..SYS_BLOG_INFO where BI_BLOG_ID = self.blogid);
  if (p_path_str is null)
    signal ('22023', 'No such blog');
  l_path_str := (select top 1 HP_LPATH from DB.DBA.HTTP_PATH where HP_PPATH = p_path_str and HP_HOST = vhost and HP_LISTEN_HOST = lhost);
  if (l_path_str is null)
    signal ('22023', 'No virtual directory found.');
  full_path := concat (rtrim (l_path_str, '/'), '/?id=', post);
  return full_path;
};

create method wa_private_url () for wa_blog2 {
  declare uri varchar;
  uri := null;
  whenever not found goto endf;
  select BI_HOME into uri from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = self.blogid;
  endf:
  return uri;
}
;

create method wa_periodic_activity () for wa_blog2 {
  return;
}
;

create procedure DB.DBA.BLOG2_GET_USER_ACCESS(in blogid varchar, in usr varchar, in pwd varchar default null)
{
  declare _is_public, _role, _status any;
  _is_public := 0;
  _role := 0;
  _status := 0;
  -- check if blog instance exists
  declare _wai_name any;
  _wai_name := (select BI_WAI_NAME from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = blogid);
  if(_wai_name is null) signal('WA001', 'Application instance not found.');
  -- check if blog instance is public
  _is_public := (select WAI_IS_PUBLIC from DB.DBA.WA_INSTANCE where WAI_NAME = _wai_name);
  if(_is_public is null) _is_public := 0;
  if(usr is not null)
  {
    if(exists(select 1 from DB.DBA.SYS_USERS where U_NAME = usr and U_DAV_ENABLE = 1 and U_IS_ROLE = 0)) {
      declare _user_id any;
      _user_id := (select U_ID from DB.DBA.SYS_USERS where U_NAME = usr);
      -- validate password if necessary
      if(pwd is not null) {
        declare _real_pwd any;
        _real_pwd := (select pwd_magic_calc(U_NAME, U_PASSWORD, 1) from DB.DBA.SYS_USERS where U_ID = _user_id);
        if(pwd <> _real_pwd) return 0;
      }
      -- if it's registered user - check his role against current blog
      -- if several roles assigned to one user - use the best one (minimum value)
      _role := (select min(WAM_MEMBER_TYPE) from DB.DBA.WA_MEMBER
  where WAM_STATUS <= 2 and WAM_USER = _user_id and WAM_INST = _wai_name
    and (WAM_MEMBER_SINCE < now() or WAM_MEMBER_SINCE is null) and (WAM_EXPIRES > now() or WAM_EXPIRES is null));
      _status := (select WAM_STATUS from DB.DBA.WA_MEMBER where WAM_USER = _user_id
  and WAM_INST = _wai_name and (WAM_MEMBER_SINCE < now() or WAM_MEMBER_SINCE is null)
     and (WAM_EXPIRES > now() or WAM_EXPIRES is null));
      if(_status = 1) _role := 1;  -- owner
      if(_role is null) _role := 0;
    }
  }
  commit work;
  -- return 0 in no access, 1 if owner, 2 if author, 3 if can read (reader or blog is public);
  if(_role in (1, 2)) return _role;
  if(_is_public) return 3;
  return _role;
}
;

create procedure DB.DBA.BLOG2_CREATE_SID() {
  return md5(concat(datestring(now()), http_client_ip (), http_path ()));
}
;

create procedure DB.DBA.BLOG2_GET_USER_BY_SESSION(in sid varchar, in realm varchar, in minutes integer default 30) {
  declare _last_ip, _cookie_use, _opts, _date_exp, _usr any;
  _usr := NULL;
  _cookie_use := 0;
  whenever not found goto not_found;
  select
    VS_UID,
    VS_EXPIRY,
    deserialize(blob_to_string(VS_STATE))
  into
    _usr,
    _date_exp,
    _opts
  from
    DB.DBA.VSPX_SESSION
  where
    VS_REALM = realm and
    VS_SID = sid;
  if(_opts is not null) {
    _cookie_use := get_keyword('cookie_use', _opts, 0);
    _last_ip := get_keyword('last_ip', _opts, '');
  }
  if(_cookie_use = 0) {
    -- take into account session expiration
    if(datediff('minute', _date_exp, now()) > minutes) {
      _usr := NULL;
    }
  }
  else {
    -- take into account initial ip
    if(_last_ip <> http_client_ip()) {
      _usr := NULL;
    }
  }
not_found:
  return _usr;
}
;

create procedure DB.DBA.BLOG2_GET_ACCESS(in blogid varchar, inout sid varchar, in realm varchar, in minutes integer default 30) {
  declare _usr, _rights, _new_sid any;
  _usr := DB.DBA.BLOG2_GET_USER_BY_SESSION(sid, realm, minutes);
  _rights := DB.DBA.BLOG2_GET_USER_ACCESS(blogid, _usr);
  if(_usr is not null and _rights > 0)
  {
    -- change sid to new one at each access if it necessary
    declare _opts, _sid_mutation any;
    _sid_mutation := 0;
    _opts := (select
                deserialize (blob_to_string (BI_OPTIONS))
              from
                BLOG.DBA.SYS_BLOG_INFO
              where
                BI_BLOG_ID = blogid);
    if(_opts is not null)
    {
      _sid_mutation := get_keyword('EnableSIDMutation', _opts, 0);
    }
    if(_sid_mutation > 0)
    {
      _new_sid := DB.DBA.BLOG2_CREATE_SID();
      update DB.DBA.VSPX_SESSION set VS_SID = _new_sid where VS_SID = sid and VS_REALM = realm and VS_UID = _usr;
      sid := _new_sid;
    }
  }
  else if (length (sid))
  {
    delete from DB.DBA.VSPX_SESSION where VS_SID = sid and VS_REALM = realm;
    sid := null;
  }
  return _rights;
}
;

create procedure DB.DBA.BLOG2_GEMS_AUTH(in realm varchar) {
  declare hdr, lines, auth, usr, pwd, domain, blogid any;
  blogid := (select
               BI_BLOG_ID
             from
               BLOG.DBA.SYS_BLOG_INFO
             where
               BI_HOME = subseq(http_path(), 0, strrchr(http_path(), '/gems/') - 4));
  --check if blog is public
  if(DB.DBA.BLOG2_GET_USER_ACCESS(blogid, '', '') > 0) {
    return 1;
  }
  -- check if auth data are received
  lines := http_request_header();
  domain := http_path();
  domain := subseq(domain, 0, strstr(domain, '/gems/') + 6);
  auth := vsp_auth_vec(lines);
  if(0 <> auth) {
    usr := get_keyword ('username', auth, '');
    pwd := get_keyword ('pass', auth, '');
    if(DB.DBA.BLOG2_GET_USER_ACCESS(blogid, usr, pwd) > 0) {
      return 1;
    }
  }
  vsp_auth_get('Weblog authentication',
               domain,
               md5(datestring (now ())),
               md5('IfYouWantSomethingDoneDoItYourself'),
               'false',
               lines,
               1);
  http('<HTML><HEAD><TITLE>Unauthorized</TITLE></HEAD><BODY><H2>XML Resource</H2>You are not authorized</BODY></HTML>');
  return 0;
}
;

create method wa_dashboard () for wa_blog2 {
  declare url varchar;
  url := sioc..forum_iri ('weblog', self.wa_name);
  return ( select
              XMLAGG(XMLELEMENT('dash-row',
                         XMLATTRIBUTES('normal' as "class", BLOG.DBA.BLOG2_DATE_FOR_HUMANS(B_TS) as "time", self.wa_name as "application"),
                         XMLELEMENT('dash-data',
	    XMLATTRIBUTES(

	      concat ( N'<a href="' , cast (url as nvarchar),
	       	       N'/', cast (B_POST_ID as nvarchar), N'">',
		       charset_recode (BLOG.DBA.BLOG2_GET_TITLE (B_META, B_CONTENT), 'UTF-8', '_WIDE_'), N'</a>')

	      "content", B_COMMENTS_NO "comments")
                         )
                    )
              )
            from
      (select top 10 * from BLOG.DBA.SYS_BLOGS where B_STATE = 2 and B_BLOG_ID = self.blogid order by B_TS desc) T);
}
;

create method wa_size () for wa_blog2
{
  declare tot, tot2 int;
  declare path varchar;
  tot := 0;
  path := '';
  whenever not found goto endf;
  select sum (length (B_CONTENT)) into tot from BLOG.DBA.SYS_BLOGS where B_BLOG_ID = self.blogid;
  select BI_P_HOME into path from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = self.blogid;
  tot2 := coalesce ((select sum (length (RES_CONTENT)) from WS.WS.SYS_DAV_RES where RES_FULL_PATH like path || '%'), 0);
  tot := (tot + tot2)/1024;
  endf:
  return tot;
}
;

create method wa_dashboard_last_item () for wa_blog2
{
  declare ret any;
  whenever not found goto nf;
  ret := null;
  select BI_DASHBOARD into ret from BLOG..SYS_BLOG_INFO where BI_BLOG_ID = self.blogid;
  nf:;
  return ret;
};

create method wa_addition_urls () for wa_blog2 {
  return vector(
    vector(null, null, '/weblog/public', registry_get('_blog2_path_') || 'public', 1, 0, null, null, null, null, 'dba', null, null, 1, null, null, null, 0),
    vector(null, null, '/weblog/templates', registry_get('_blog2_path_') || 'templates', 1, 0, null, null, null, null, 'dba', null, null, 1, null, null, null, 0),
    vector(null, null, '/RPC2', '/SOAP/', 0, 0, null, null, null, null, null, 'MT', null, 1, vector('XML-RPC', 'yes'), null, null, 0),
    vector(null, null, '/Atom', '/SOAP/Http/gdata', 0, 0, null, null, null, null, null, 'MT', null, 1, vector ('atom-pub', 1), null, null, 0),
    vector(null, null, '/mt-tb', '/SOAP/', 0, 0, null, null, null, null, null, 'MT', null, 1, vector('XML-RPC', 'yes'), null, null, 0),

    vector(null, null, '/BlogAPI', '/SOAP/', 0, 0, null, null, null, null, null, 'BLOG_API', null, 1,
      vector (
      'Namespace','http://www.openlinksw.com/weblog/api', 'SchemaNS', 'http://www.openlinksw.com/weblog/api',
      'MethodInSoapAction','only',
      'ServiceName', 'BlogAPI', 'elementFormDefault', 'qualified', 'Use', 'literal'
      ),
      null, null, 0)
  );
}
;

create method wa_vhost_options () for wa_blog2
{
  declare p_home any;
  whenever not found goto nfb;
  select BI_P_HOME into p_home from BLOG..SYS_BLOG_INFO where BI_BLOG_ID = self.blogid;

  return
      vector
       (
	 p_home,        		-- physical home
	 'index.vspx',  		-- default page
	 'dba',         		-- user for execution
	 0,             		-- directory browsing enabled
	 1,				-- WebDAV repository
	 vector (),			-- virtual directory options
	 'BLOG.DBA.BLOG2_RSS2WML_PP',	-- post-processing function
	 null				-- pre-processing (authentication) function
       );

  nfb:
  return null;
};

create method wa_addition_instance_urls () for wa_blog2 {
  if (0)
    {
      -- disabled
      declare p_home any;
      whenever not found goto nfb;
      select BI_P_HOME into p_home from BLOG..SYS_BLOG_INFO where BI_BLOG_ID = self.blogid;
      return vector (
        vector(null, null, '/tag/'||self.blogid, p_home || 'index.vspx', 1, 0, null, null, null, null,
          'dba', null, null, 1, null, null, vector ('noinherit', 'yes'), 0)
          );
      nfb:;
    }
  return null;
}
;

create procedure BLOG2_UPGRADE_FROM_BLOG ()
{
  -- collect all existing blog's ids and owner's ids
  declare _blogs any;

  if (registry_get ('__BLOG2_UPGRADE_FROM_BLOG1') = 'done')
    return 0;

  if (exists (select 1 from DB.DBA.WA_INSTANCE where WAI_TYPE_NAME = 'WEBLOG2'))
    {
      registry_set ('__BLOG2_UPGRADE_FROM_BLOG1', 'done');
      return 0;
    }
  _blogs := vector();
  for select BI_BLOG_ID, BI_OWNER from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID <> '*weblog-root*' do
    {
      _blogs := vector_concat(_blogs, vector(BI_BLOG_ID, BI_OWNER));
    }
  -- return if no previous version blogs exists
  if (length (_blogs) = 0)
    return 0;
  -- create new wa instances for each old blog
  declare _idx, _blog_id, _blog_owner, _blog_owner_name any;
  declare _inst wa_blog2;
  _idx := 0;
  while(_idx <= length(_blogs) - 2) {
    _blog_id := _blogs[_idx];
    _blog_owner := _blogs[_idx + 1];
    _idx := _idx + 2;
    -- create new class instance
    _inst := new wa_blog2();
    -- generate unique instance name
    _blog_owner_name := (select U_NAME from DB.DBA.SYS_USERS where U_ID = _blog_owner);
    _inst.wa_name := _blog_owner_name || '\'s Weblog [' || _blog_id || ']';
    -- Closed member model as default
    _inst.wa_member_model := 1;
    _inst.blogid := _blog_id;
    -- Final touch
    declare _id any;
    _id := _inst.wa_new_inst_from_old_blog(_blog_owner_name, _blog_id);
    update
      DB.DBA.WA_INSTANCE
    set
      WAI_IS_PUBLIC = 1,
      WAI_DESCRIPTION = _inst.wa_name
    where
      WAI_ID = _id;
  }
  -- delete old blog's home
  -- DB.DBA.VHOST_REMOVE(lpath=>'/blog');
  update BLOG.DBA.SYS_BLOG_INFO set BI_INCLUSION = 1;
  registry_set ('__BLOG2_UPGRADE_FROM_BLOG1', 'done');
  return _idx;
}
;


-- called via vad_install
create procedure BLOG2_UPGRADE_FROM_BLOG2 ()
{
  declare home, blog_home, exec_path, fpath, content, dav_pwd, ver any;

  -- XXX: change this when gems etc are changed
  ver := '1.10';

  if (registry_get ('__BLOG2_UPGRADE_FROM_BLOG2_ver') = ver)
    return;

  dav_pwd := (select pwd_magic_calc (U_NAME, U_PWD, 1) from WS.WS.SYS_DAV_USER where U_ID = http_dav_uid ());

  for select BI_HOME, BI_P_HOME, BI_OWNER, BI_BLOG_ID, BI_OPTIONS from BLOG.DBA.SYS_BLOG_INFO
    where BI_BLOG_ID <> '*weblog-root*'
    do
      {
    home := subseq(BI_HOME, 0, length(BI_HOME) - 1);
    exec_path := BI_P_HOME;
    DAV_MAKE_DIR (exec_path, http_dav_uid(), http_dav_uid()+1, '110110100N');
    fpath := exec_path || 'index.vspx';
    content := (select RES_CONTENT from WS.WS.SYS_DAV_RES where RES_FULL_PATH = registry_get('_blog2_path_') || 'index.vspx');
    DB.DBA.DAV_RES_UPLOAD(fpath, content, 'text/html', '111101101N', 'dav', null, 'dav', dav_pwd);

    /* upgrade the gems with latest fixes */

    BLOG..BLOG2_UPGRADE_BLOG2_GEMS (BI_P_HOME, BI_BLOG_ID, dav_pwd, deserialize(blob_to_string (BI_OPTIONS)), BI_OWNER);

    /* the rest is upgrade from an alfa code of blog2 */
    if (registry_get ('__BLOG2_UPGRADE_FROM_BLOG2_ALFA_upgrade') = 'done')
      goto end_upg;


    DB.DBA.VHOST_REMOVE(lpath=>home);
    DB.DBA.VHOST_DEFINE(ses_vars=>1,
                 is_dav=>1,
                 lpath=>home,
                 ppath=>BI_P_HOME,
                 vsp_user=>'dba',
                 is_brws=>0,
                 def_page=>'index.vspx',
                 ppr_fn=>'BLOG.DBA.BLOG2_RSS2WML_PP'
                );

    -- some special VDs that not needed
    DB.DBA.VHOST_REMOVE(lpath=>home || '/gems');
    DB.DBA.VHOST_REMOVE(lpath=>home || '/images');
    DB.DBA.VHOST_REMOVE(lpath=>home || '/audio');
    DB.DBA.VHOST_REMOVE(lpath=>home || '/templates');

    -- the old home directory
    blog_home := subseq (BI_P_HOME, 0, length(BI_P_HOME) - 1) || '_exec/';
    for select HP_HOST, HP_LISTEN_HOST, HP_LPATH from DB.DBA.HTTP_PATH where
      HP_PPATH = blog_home and
      HP_HOST not like '%ini%'and HP_HOST not like '*sslini*'
    do
    {
      DB.DBA.VHOST_REMOVE(vhost=>HP_HOST, lhost=>HP_LISTEN_HOST, lpath=>HP_LPATH);
      DB.DBA.VHOST_DEFINE(vhost=>HP_HOST,
                   lhost=>HP_LISTEN_HOST,
                   ses_vars=>1,
                   is_dav=>1,
                   lpath=>HP_LPATH,
                   ppath=>BI_P_HOME,
                   vsp_user=>'dba',
                   is_brws=>0,
                   def_page=>'index.vspx',
                   ppr_fn=>'BLOG.DBA.BLOG2_RSS2WML_PP'
                  );
    }

    for select HP_HOST, HP_LISTEN_HOST, HP_LPATH from DB.DBA.HTTP_PATH where
      (HP_PPATH = BI_P_HOME || 'gems/' or
       HP_PPATH = BI_P_HOME || 'images/' or
       HP_PPATH = BI_P_HOME || 'audio/' or
       HP_PPATH = BI_P_HOME || 'templates/')
       and
       HP_HOST not like '%ini%'and HP_HOST not like '*sslini*'
      do
    {
      DB.DBA.VHOST_REMOVE(vhost=>HP_HOST, lhost=>HP_LISTEN_HOST, lpath=>HP_LPATH);
    }
      end_upg:;
  }
  registry_set ('__BLOG2_UPGRADE_FROM_BLOG2_ALFA_upgrade', 'done');
  registry_set ('__BLOG2_UPGRADE_FROM_BLOG2_ver', ver);
}
;

create procedure BLOG2_ATTACHED_ROOT(in path varchar) {
  declare root any;
  root := vector();
  for select
    BI_BLOG_ID
  from
    BLOG.DBA.SYS_BLOG_INFO,
    BLOG.DBA.SYS_BLOG_ATTACHES
  where
    BI_BLOG_ID = BA_C_BLOG_ID and
    BA_M_BLOG_ID = path
  do {
    if ((select WAI_IS_PUBLIC from DB.DBA.WA_INSTANCE
  where WAI_TYPE_NAME = 'WEBLOG2' and (WAI_INST as wa_blog2).blogid = BI_BLOG_ID) > 0)
      root := vector_concat(root, vector(BI_BLOG_ID));
  }
  return root;
}
;

create procedure BLOG2_ATTACHED_CHILD(in path varchar, in node any) {
  declare childs any;
  childs := vector();
  for select
    BI_BLOG_ID
  from
    BLOG.DBA.SYS_BLOG_INFO,
    BLOG.DBA.SYS_BLOG_ATTACHES
  where
    BI_BLOG_ID = BA_C_BLOG_ID and
    BA_M_BLOG_ID = node
  do {
    if ((select WAI_IS_PUBLIC from DB.DBA.WA_INSTANCE
  where WAI_TYPE_NAME = 'WEBLOG2' and (WAI_INST as wa_blog2).blogid = BI_BLOG_ID) > 0)
       childs := vector_concat(childs, vector(BI_BLOG_ID));
  }
  return childs;
}
;

create procedure BLOG2_INCLUDE_ROOT(in path varchar) {
  declare root any;
  root := vector();
  for select
    BI_BLOG_ID
  from
    BLOG.DBA.SYS_BLOG_INFO,
    BLOG.DBA.SYS_BLOG_ATTACHES
  where
    BI_BLOG_ID = BA_M_BLOG_ID and
    BA_C_BLOG_ID = path
  do
  {
    if ((select WAI_IS_PUBLIC from DB.DBA.WA_INSTANCE
  where WAI_TYPE_NAME = 'WEBLOG2' and (WAI_INST as wa_blog2).blogid = BI_BLOG_ID) > 0)
      root := vector_concat(root, vector(BI_BLOG_ID));
  }
  return root;
}
;

create procedure BLOG2_INCLUDE_CHILD(in path varchar, in node any) {
  declare childs any;
  childs := vector();
  for select
    BI_BLOG_ID
  from
    BLOG.DBA.SYS_BLOG_INFO,
    BLOG.DBA.SYS_BLOG_ATTACHES
  where
    BI_BLOG_ID = BA_M_BLOG_ID and
    BA_C_BLOG_ID = node
  do
  {
    if ((select WAI_IS_PUBLIC from DB.DBA.WA_INSTANCE
  where WAI_TYPE_NAME = 'WEBLOG2' and (WAI_INST as wa_blog2).blogid = BI_BLOG_ID) > 0)
    childs := vector_concat(childs, vector(BI_BLOG_ID));
  }
  return childs;
}
;

create procedure BLOG2_MOBLOG_PROCESS_PARTS (in parts any, inout body varchar, inout amime any, out result any, in any_part int) {
  declare name1, mime1, name, mime, enc, content, charset varchar;
  declare i, l, i1, l1, is_allowed int;
  declare part any;

  if (not isarray (result))
    result := vector ();

  if (not isarray (parts) or not isarray (parts[0]))
    return 0;
  -- test if there is an moblog compliant image
  part := parts[0];
--  dbg_obj_print ('part=', part);

  name1 := get_keyword_ucase ('filename', part, '');
  if (name1 = '')
    name1 := get_keyword_ucase ('name', part, '');

  mime1 := get_keyword_ucase ('Content-Type', part, '');
  charset := get_keyword_ucase ('charset', part, '');

  if (mime1 = 'application/octet-stream' and name1 <> '') {
    mime1 := http_mime_type (name1);
  }

  is_allowed := 0;
  i1 := 0;
  l1 := length (amime);
  while (i1 < l1) {
    declare elm any;
    elm := trim(amime[i1]);
    if (mime1 like elm) {
      is_allowed := 1;
      i1 := l1;
    }
    i1 := i1 + 1;
  }

  declare _cnt_disp any;
  _cnt_disp := get_keyword_ucase('Content-Disposition', part, '');

  if(is_allowed and (any_part or (name1 <> '' and _cnt_disp in ('attachment', 'inline')))) {
    name := name1;
    mime := mime1;
    enc := get_keyword_ucase ('Content-Transfer-Encoding', part, '');
    content := subseq (body, parts[1][0], parts[1][1]);
    if(enc = 'base64') content := decode_base64 (content);
    result := vector_concat (result, vector (vector (name, mime, content, _cnt_disp, enc, charset)));
    return 1;
  }
  -- process the parts
  if(not isarray (parts[2]))
    return 0;
  i := 0;
  l := length (parts[2]);
  while (i < l) {
    BLOG2_MOBLOG_PROCESS_PARTS (parts[2][i], body, amime, result, any_part);
    i := i + 1;
  }
  return 0;
}
;

create procedure BLOG2_MOBBLOGGING_GET_MOB_MESSAGE(in _msg any, in opts any) {
  declare parsed_message, amime, res any;
  declare rc integer;

  parsed_message := mime_tree (_msg);
  if (not isarray (opts)) return;
  amime := split_and_decode(get_keyword ('MoblogMIMETypes', opts, ''), 0, '\0\0,');
  if(amime is null or not isarray (amime) or length(amime) = 0) return null;

  res := null;
  BLOG2_MOBLOG_PROCESS_PARTS(parsed_message, _msg, amime, res, 0);
  return res;
}
;

create procedure BLOG2_UPLOAD_IMAGES_TO_BLOG_HOME(in _owner_id integer, in _blog_id varchar, in _image_body varchar, in _image_name varchar, out thumb_path varchar)
{
  declare _blog_home, _folder, _path, thumb any;
  declare mime_type varchar;

  thumb_path := null;

  _blog_home := (select BI_P_HOME from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = _blog_id);
  _folder := _blog_home || 'media/';
  DB.DBA.DAV_MAKE_DIR(_folder, _owner_id, http_dav_uid()+1, '110110100N');
  DB.DBA.DAV_MAKE_DIR(_folder||'thumbnail/', _owner_id, http_dav_uid()+1, '110110100N');
  _path := _folder || _image_name;
  DB.DBA.DAV_RES_UPLOAD_STRSES_INT(_path, _image_body, '', '110100100N', _owner_id, null, null, null, 0);
  mime_type := http_mime_type (_image_name);
  if (__proc_exists ('IM ThumbnailImageBlob', 2) and mime_type like 'image/%')
    {
      declare path1 varchar;
      thumb := BLOG2_MAKE_THUMB (_image_body, mime_type, 200);
      path1 := _folder || 'thumbnail/' || _image_name;
      thumb_path := path1;
      DB.DBA.DAV_RES_UPLOAD_STRSES_INT(path1, thumb, '', '110100100N', _owner_id, null, null, null, 0);
    }
  return _path;
}
;

create procedure BLOG2_MAKE_THUMB (in _image_body any, in mime_type any, in width1 int, in flag int := 1)
{
  declare thumb any;
  thumb := null;
  if (__proc_exists ('IM ThumbnailImageBlob', 2) and mime_type like 'image/%')
    {
      declare width, height int;

      _image_body := blob_to_string (_image_body);

      width := "IM GetImageBlobWidth" (_image_body, length (_image_body));
      height := "IM GetImageBlobHeight" (_image_body, length (_image_body));

      if (width > width1)
	{
	  declare height1 int;
	  height1 := (width1 * height) / width;
	  thumb := "IM ThumbnailImageBlob" (_image_body, length (_image_body), width1, height1, 1);
        }
      else
	thumb := _image_body;
    }
  else if (flag)
    thumb := _image_body;
  return thumb;
}
;

create procedure BLOG_GET_MEDIA_URL (in _path varchar)
{
  declare _host, _intf, suff, chost varchar;
  declare _pos int;

  chost := BLOG.DBA.BLOG_GET_HOST ();
  _host := chost;
  _pos := strchr (_host, ':');

  if (_pos is not null)
    {
      _intf := subseq (_host, _pos, length (_host));
      _host := subseq (_host, 0, _pos);
    }
  else
    _intf := ':80';

  for select max (HP_LPATH) as lpath, max (HP_PPATH) as ppath
    from HTTP_PATH where HP_HOST = _host and HP_LISTEN_HOST like '%'||_intf and
	_path like HP_PPATH||'%' do
  {
    if (lpath is not null and ppath is not null)
      {
	suff := substring (_path, length (ppath), length (_path));
	return sprintf ('http://%s%s%s%s', _host, _intf, lpath, suff);
      }
  }
  return sprintf ('http://%s%s', chost, _path);
};


create procedure BLOG2_INSERT_MEDIA_MESSAGE (in _caller_name varchar, in _params any, in _blog_id varchar, in _path varchar, in _mime any, in thumb_path varchar, in _bi_owner int, in _bi_home varchar)
{
  -- determine next post_id number from sequence
  declare _post_id any;
  declare _content, _texts any;
  declare  iurl, turl varchar;

  _post_id := cast(sequence_next ('blogger.postid') as varchar);
  if (_post_id = '0')
    _post_id := cast (sequence_next ('blogger.postid') as varchar);

  -- create post object
  declare _res BLOG.DBA."MTWeblogPost";
  _res := new BLOG.DBA."MTWeblogPost" ();
  _res.postid := _post_id;
  _res.title := get_keyword('subj2', _params, '');
  _res.author := _caller_name;
  _res.userid := _res.author;
  _res.dateCreated := now ();
  _texts := get_keyword('description', _params, '');

  -- create message body
  iurl := BLOG_GET_MEDIA_URL (_path);
  if (thumb_path is not null)
    turl := BLOG_GET_MEDIA_URL (thumb_path);
  else
    turl := iurl;

  if(_mime like 'image/%') {
    _content := '<div><a href="' || iurl || '">' ||
                '<img src="' || turl ||
                '" width="200" border="0" /></a></div><div><pre>' ||
                get_keyword ('changed_name', _params, '') || ' </pre></div><div>' || _texts || '</div>';
  }
  else {
    _content := '<div><a href="' || iurl || '">' ||
                _res.title || '</a></div><div><pre>' ||
                get_keyword ('changed_name', _params, '') || ' </pre></div>';
  }
  insert into BLOG.DBA.SYS_BLOGS (B_APPKEY, B_BLOG_ID, B_CONTENT, B_POST_ID, B_USER_ID, B_TS, B_META, B_STATE, B_TITLE)
  values('', _blog_id, _content, _post_id, _bi_owner, now(), _res, 2, _res.title);
}
;

create procedure MOB_GET_BLOG_ID (in _msg any, in tag varchar := 'blogId')
{
  declare parsed_message, amime, res, tok any;
  declare rc integer;

  _msg := blob_to_string (_msg);
  parsed_message := mime_tree (_msg);
  res := null;
  MOB_PROCESS_PARTS (parsed_message, _msg, res);

  foreach (any elm in res) do
    {
      --dbg_obj_print_vars (elm);
      tok := regexp_match(sprintf ('@%s=[^@]+@', tag), elm);
      if (tok is not null)
  {
    goto extr;
  }
    }

  tok := regexp_match(sprintf ('@%s=[^@]+@', tag), _msg);
  extr:
  if (tok is not null)
    {
      tok := substring (tok, 9, length (tok));
      tok := rtrim (tok, '@');
    }

  return tok;
}
;

create procedure decode_uuencoded_attachement (in _data any, inout attaches any, inout opts any)
{
  declare data, outp, att any;
  declare line, nam, amime varchar;
  declare in_UU integer;

  if (not isarray (opts)) return;
  amime := split_and_decode(get_keyword ('MoblogMIMETypes', opts, ''), 0, '\0\0,');
  if(amime is null or not isarray (amime) or length(amime) = 0) return null;

  data := string_output (http_strses_memory_size ());
  http (_data, data);

  outp := string_output (http_strses_memory_size ());
  att := string_output (http_strses_memory_size ());

  if (attaches is null)
    attaches := vector ();

  in_UU := 0;
  while (1)
    {
      line := ses_read_line (data, 0);
      if (line is null or isstring (line) = 0)
  return;

      if (in_UU = 0 and subseq (line, 0, 6) = 'begin ' and length (line) > 6)
  {
          in_UU := 1;
    nam := split_and_decode (line, 0, '\0\0 ');
    if (length (nam) > 2)
      nam := nam[2];
    else
      nam := 'unknown';
    string_output_flush (att);
    http (line, att);
    http ('\r\n', att);
  }
      else if (in_UU = 1 and subseq (line, 0, 3) = 'end')
  {
    declare atta any;
          in_UU := 0;
    http (line, att);
    atta := uudecode (string_output_string (att), 1);
    foreach (any elm in amime) do
      {
        elm := trim(elm);
        if (http_mime_type (nam) like elm)
    {
            attaches := vector_concat (attaches, vector (vector (nam, http_mime_type (nam), atta)));
      goto donep;
    }
      }
    donep:;
  }
      else if (in_UU = 1)
  {
    http (line, att);
    http ('\r\n', att);
  }
      else if (in_UU = 0)
  {
    http (line, outp);
    http ('\n', outp);
  }
    }
  return;
}
;

create procedure MOB_PROCESS_PARTS (in parts any, inout body varchar, out result any)
{
  declare name1, mime1, name, mime, enc, content varchar;
  declare i, l, i1, l1, is_allowed int;
  declare part any;

  if (not isarray (result))
    result := vector ();

  if (not isarray (parts) or not isarray (parts[0]))
    return 0;
  -- test if there is an moblog compliant image
  part := parts[0];

  mime1 := get_keyword_ucase ('Content-Type', part, '');

  declare _cnt_disp any;
  _cnt_disp := get_keyword_ucase('Content-Disposition', part, 'inline');

  if (mime1 like 'text/%' and _cnt_disp = 'inline' and parts[1][0] < parts[1][1])
    {
      mime := mime1;
      enc := get_keyword_ucase ('Content-Transfer-Encoding', part, '');
      content := subseq (body, parts[1][0], parts[1][1]);

      if (enc = 'base64')
  content := decode_base64 (content);
      else if (enc = 'quoted-printable')
        content := uudecode (content, 12);
      result := vector_concat (result, vector (content));
      --return 1;
    }

  -- process the parts
  if(not isarray (parts[2]))
    return 0;

  i := 0;
  l := length (parts[2]);
  while (i < l)
    {
      MOB_PROCESS_PARTS (parts[2][i], body, result);
      i := i + 1;
    }
  return 0;
}
;

create procedure DB.DBA.BLOG2_MOBLOG_PROCESS_MSG(in _caller_user_name varchar, in _ma_m_id int,
             in _ma_m_fld varchar, in _msg_body any, in is_mb any) {

  -- return if message was already processed
  if(is_mb is not null and length(is_mb) > 0) {
    return;
  }

  -- determine caller user id
  declare _caller_user_id, _state, _special_mail any;
  declare _post_id any;
  declare _blog_id1, _blog_id, _mbody any;
  declare _opts, _bi_home, _bi_owner, _u_name any;
  declare cr cursor for select BI_OPTIONS, BI_HOME, BI_OWNER, U_NAME, BI_BLOG_ID
      from BLOG.DBA.SYS_BLOG_INFO, DB.DBA.SYS_USERS
      where  (_blog_id1 is not null and BI_BLOG_ID = _blog_id1 and BI_OWNER = U_ID) or
      (_blog_id1 is null and BI_OWNER = U_ID and U_ID = _caller_user_id);

  _mbody := blob_to_string (_msg_body);
  _blog_id := null;
  _post_id := null;

  -- first case is to have special moblog account
  if (regexp_match ('^[A-Za-z0-9 _@\.]+-blog-[0-9]+\$', _caller_user_name) is not null)
    {
      _special_mail := 1;
      _blog_id1 := _caller_user_name;
      _caller_user_id := null;
    }
  else
    {
      _special_mail := 0;
      _caller_user_id := (select U_ID from DB.DBA.SYS_USERS where U_NAME = _caller_user_name);
      if (_caller_user_id is null)
      return;

      -- detect blogId from message body
      _blog_id1 := MOB_GET_BLOG_ID (_msg_body);
    }

  -- detect postId from message body
  _post_id := MOB_GET_BLOG_ID (_msg_body, 'postId');

  -- if no blogId founded - do nothing
  if (_post_id is not null and _blog_id1 is null) return;


  whenever not found goto set_and_exit;
  open cr (prefetch 1);

  while (1)
    {
      -- determine options and other parameters for called blog
      fetch cr into _opts, _bi_home, _bi_owner, _u_name, _blog_id;
      _opts := deserialize (blob_to_string(_opts));

      if (_special_mail)
        _caller_user_id := _bi_owner;

      -- determine caller's membership for the blog instance
      whenever not found goto nf;
      declare _role, _status any;
      select min(WAM_MEMBER_TYPE), min(WAM_STATUS)
      into _role, _status
      from DB.DBA.WA_MEMBER, DB.DBA.WA_INSTANCE
      where WAM_STATUS <= 2 and WAI_TYPE_NAME = 'WEBLOG2' and WAM_USER = _caller_user_id and
      (WAI_INST as wa_blog2).blogid = _blog_id and WAM_INST = WAI_NAME and
      (WAM_MEMBER_SINCE < now() or WAM_MEMBER_SINCE is null) and ( WAM_EXPIRES > now() or WAM_EXPIRES is null);

      if (_status = 1)
        _role := 1;
    nf:
      -- if caller is not blog owner or blog author do nothing
      if (_role is null or _role not in (1,2))
        return;

      -- blog processing starts

    blog_processing:

      -- if _post_id is determined - create new comment to message
      if(_post_id is not null)
        goto new_comment;
      -- else - new message creation (one message for each attachment)

    new_message:

      -- retrieve attachments with allowed mime types from mail
      declare _attaches, res, parsed_message, texts any;
      _attaches := BLOG2_MOBBLOGGING_GET_MOB_MESSAGE(_mbody, _opts);
      decode_uuencoded_attachement (_mbody, _attaches, _opts);

      parsed_message := mime_tree (_mbody);
      res := null;
      MOB_PROCESS_PARTS (parsed_message, _mbody, res);
      
      texts := '';
      foreach (any elm in res) do
	{
	  texts := texts || elm;
	}

      -- if attaches was not founded - skip next block
      if(_attaches is null or not isarray(_attaches) or length(_attaches) = 0) goto _endmark;

      -- if attaches with allowed mime type founded, place it into MAIL_ATTACHMENTS
      declare _i, _l, _subject, _media_path any;
      declare _a_name, _a_mime, _a_content, _publ, secret any;
      _i := 0;
      _l := length(_attaches);
      _subject := substring(mail_header(_mbody, 'Subject'), 1, 512);
      _subject := BLOG.DBA.BLOG2_DECODE_SUBJECT (_subject);
      _state := null;

      if (get_keyword('MoblogAutoPublish', _opts, 0) = 1)
	_state := 'mob-pub';
      else
	_state := 'mob-new';

      while (_i < _l)
	{
	  _a_name := _attaches[_i][0];
	  _a_mime := _attaches[_i][1];
	  _a_content := _attaches[_i][2];
--	  dbg_obj_print_vars (_a_name, _a_mime, _a_content);
	  _publ := 0;
	  if (_state = 'mob-pub')
	    {
	        -- if auto creation of blog is allowed (even if this mail contains comment to some post_id)
		-- place attached resources in corresponded blog physical home and create new blog
		secret := get_keyword ('MoblogAutoSecret', _opts, '');
		if (length (secret) = 0 or regexp_match ('([[:space:]]|^)'||secret||'([[:space:]]|\$)', subseq (_mbody, 0, 10000))
		    is not null)
		  {
		    -- matching secret
	            declare thumb_path varchar;
		    _media_path := BLOG2_UPLOAD_IMAGES_TO_BLOG_HOME(_caller_user_id, _blog_id, _a_content, _a_name, thumb_path);
		    BLOG2_INSERT_MEDIA_MESSAGE (_u_name, vector('subj2', _subject, 'description', texts), _blog_id, _media_path, _a_mime, thumb_path,
			_bi_owner, _bi_home);
		    _publ := 1;
		  }
	    }
	  insert soft DB.DBA.MAIL_ATTACHMENT (MA_M_ID, MA_M_OWN, MA_M_FLD, MA_NAME, MA_MIME, MA_CONTENT, MA_PUBLISHED, MA_BLOG_ID)
	      values (_ma_m_id, _caller_user_name, _ma_m_fld, _a_name, _a_mime, _a_content, _publ, _blog_id);
	  _i := _i + 1;
	}

    goto _endmark;
      -- new comment for message creation
    new_comment:

      declare _mime_tree, _content_type any;
      _mime_tree := mime_tree (_mbody);
      _content_type := substring (mail_header(_mbody, 'Content-Type'), 1, 512);
      if (isarray(_mime_tree[1]))
	{
	  declare _mm_body, _mm_mail_id, _mm_from, _mm_subject varchar;
	  _mm_body := subseq(_mbody, _mime_tree[1][0], _mime_tree[1][1]);
	  if (exists(select 1 from BLOG.DBA.SYS_BLOGS where B_STATE = 2 and B_POST_ID = _post_id and B_BLOG_ID = _blog_id))
	    {
	      _mm_from := substring(mail_header(_mbody, 'From'), 1, 512);
	      _mm_subject := substring(mail_header(_mbody, 'Subject'), 1, 512);
	      _mm_subject := BLOG.DBA.BLOG2_DECODE_SUBJECT (_mm_subject);
	      --_mm_body := replace(_mm_body, _blog_token, '');
	      --_mm_body := replace(_mm_body, _post_token, '');
	      if (_content_type <> 'text/html') _mm_body := '<pre>' || _mm_body || '</pre>';
	      insert into BLOG.DBA.BLOG_COMMENTS (
		  BM_BLOG_ID, BM_POST_ID, BM_COMMENT, BM_NAME, BM_E_MAIL, BM_HOME_PAGE, BM_ADDRESS, BM_TS)
		  values( _blog_id, _post_id, _mm_body, _mm_subject, _mm_from, '', '127.0.0.1', now());
	    }
	}
     _endmark:;
    }
  set_and_exit:
  close cr;
  set triggers off;
  update MAIL_MESSAGE set MM_MOBLOG = _state where MM_OWN = _caller_user_name and MM_ID = _ma_m_id;
  set triggers on;
  return;
}
;

create procedure BLOG.DBA.BLOG2_DECODE_SUBJECT (in subject varchar)
{
  declare sub any;
  if (subject like '=?UTF-8?B?%?=')
    {
      sub := substring (subject, 11, length (subject) - 12);
      sub := decode_base64 (sub);
      return sub;
    }
  else
    return subject;
};

create procedure
BLOG.DBA.BLOG2_UPDATE_SYS_BLOG_INFO ()
{
  if (registry_get ('__BLOG2_UPDATE_SYS_BLOG_INFO') = 'done')
    return;

  if (exists (select 1 from BLOG.DBA.SYS_BLOG_INFO where BI_WAI_MEMBER_MODEL is NULL))
    {
      for (select WAI_NAME, WAI_MEMBER_MODEL, (WAI_INST as wa_blog2).blogid as temp_id
        from DB.DBA.WA_INSTANCE where WAI_TYPE_NAME = 'WEBLOG2') do
   {
      update BLOG.DBA.SYS_BLOG_INFO set BI_WAI_NAME = WAI_NAME,
        BI_WAI_MEMBER_MODEL = WAI_MEMBER_MODEL where BI_BLOG_ID = temp_id;
   }
    }

  update BLOG.DBA.SYS_BLOG_INFO set BI_HAVE_COMUNITY_BLOG = 0 where BI_HAVE_COMUNITY_BLOG is NULL;
  registry_set ('__BLOG2_UPDATE_SYS_BLOG_INFO', 'done');
}
;

BLOG.DBA.BLOG2_UPDATE_SYS_BLOG_INFO ()
;


create trigger SYS_USERS_BLOG_INFO_UP after update (U_E_MAIL) on DB.DBA.SYS_USERS referencing old as O, new as N
{
  set triggers off;
  update BLOG..SYS_BLOG_INFO set BI_E_MAIL = N.U_E_MAIL where BI_OWNER = N.U_ID;
  set triggers on;
};


create trigger BI_WAI_MEMBER_MODEL_UPD after update on DB.DBA.WA_INSTANCE referencing old as O, new as N
{
  declare inst wa_blog2;
  declare blogid any;

  if (udt_instance_of (O.WAI_INST, 'DB.DBA.wa_blog2'))
    {
      inst := N.WAI_INST;
      blogid := inst.blogid;

      update BLOG.DBA.SYS_BLOG_INFO set BI_WAI_NAME = N.WAI_NAME, BI_WAI_MEMBER_MODEL = N.WAI_MEMBER_MODEL
	where BI_BLOG_ID = blogid;
    }
}
;

-- XXX: put a check
-- called via vad_install
create procedure
BLOG2_UPDATE_SYS_BLOG_INFO_DEL ()
{
  if (registry_get ('__BLOG2_UPDATE_SYS_BLOG_INFO_DEL_done') = 'done')
    return;
  if (exists (select 1 from BLOG.DBA.SYS_BLOG_INFO where BI_WAI_MEMBER_MODEL is NULL))
    {
      for (select WAI_NAME, WAI_MEMBER_MODEL, (WAI_INST as DB.DBA.wa_blog2).blogid as temp_id
	  from DB.DBA.WA_INSTANCE where WAI_TYPE_NAME = 'WEBLOG2') do
	{
	  update BLOG.DBA.SYS_BLOG_INFO set BI_WAI_NAME = WAI_NAME,
		 BI_WAI_MEMBER_MODEL = WAI_MEMBER_MODEL where BI_BLOG_ID = temp_id;
	}
    }

  if (exists (select 1 from BLOG.DBA.SYS_BLOG_INFO where BI_HAVE_COMUNITY_BLOG is NULL))
    {
      for (select (WAI_INST as DB.DBA.wa_blog2).blogid as temp_id from DB.DBA.WA_INSTANCE
	  where WAI_TYPE_NAME = 'WEBLOG2') do
	{
	  update BLOG.DBA.SYS_BLOG_INFO set BI_HAVE_COMUNITY_BLOG = 0 where BI_BLOG_ID = temp_id and BI_HAVE_COMUNITY_BLOG is NULL;
	}
    }
  set triggers off;
  update BLOG..BLOG_COMMENTS set BM_IS_PUB = 1, BM_IS_SPAM = 0 where BM_IS_PUB is null;
  set triggers on;
  registry_set ('__BLOG2_UPDATE_SYS_BLOG_INFO_DEL_done', 'done');
}
;


USE "DB"
;

create procedure BLOG_DAV_CHECK (in rc int)
{
  declare msg any;
  if (rc >= 0)
    return;
  msg := DAV_PERROR (rc);
  signal ('42000', msg);
};

create procedure BLOG_MAKE_ALL_BLOGS_IN_ONE ()
{
  declare _inst wa_blog2;
  declare hosts, chome, default_community, ndone any;

  ndone := BLOG2_UPGRADE_FROM_BLOG();

  -- One initial blog is needed, the test suite otherwise will fail
  --if (ndone = 0)
  --  return;

  _inst := new wa_blog2();
  _inst.wa_name := 'Community blog';
  _inst.wa_member_model := 0;

  if (exists (select 1 from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = 'dav-blog-1'))
    return;

  if (exists (select 1 from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = 'dav-blog-0'))
    return;

  if (registry_get ('__BLOG_MAKE_ALL_BLOGS_IN_ONE') = 'done')
    return;

  DAV_MAKE_DIR ('/DAV/home/dav/', http_dav_uid (), null, '110100100R');
  _inst.wa_new_inst('dav');

  hosts := vector ();
  for select distinct HP_HOST, HP_LISTEN_HOST, HP_LPATH
    from DB.DBA.HTTP_PATH where HP_PPATH = '/DAV/' and HP_LPATH in ('/', '/blog') and
    HP_STORE_AS_DAV  = 1 and (HP_DEFAULT = 'index.vspx' or HP_DEFAULT like 'index.vspx;%') do
      {
        hosts := vector_concat (hosts, vector (vector (HP_HOST, HP_LISTEN_HOST, HP_LPATH)));
      }

  default_community := 0;
  if (length (hosts))
    {
      whenever not found goto nfdf;
      declare exit handler for sqlstate '*' {
	goto nfdf;
      };
      select blob_to_string (RES_CONTENT) into chome from WS.WS.SYS_DAV_RES where RES_FULL_PATH = '/DAV/index.vspx';
      chome := xtree_doc (chome);
      if (xpath_eval ('/page[@name="blog-home-page"]', chome) is not null)
	default_community := 1;
      nfdf:;
    }

  if (default_community)
    {
      foreach (any vd in hosts) do
	 {
	   DB.DBA.VHOST_REMOVE (vd[0], vd[1], vd[2]);
	   blog_ensure_domain (_inst, vd[0], vd[1]);
	   DB.DBA.VHOST_DEFINE(
	              vhost=>vd[0],
		      lhost=>vd[1],
	              ses_vars=>1,
	              is_dav=>1,
	              lpath=>vd[2],
	              ppath=>'/DAV/home/dav/dav-blog-1/',
	              vsp_user=>'dba',
	              is_brws=>0,
	              def_page=>'index.vspx',
	              ppr_fn=>'BLOG.DBA.BLOG2_RSS2WML_PP'
	             );
	 }
    }

  --DB.DBA.DAV_DELETE_INT ('/DAV/index.vspx', 0, null, null, 0);

  if (exists (select 1 from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID = 'dav-blog-1'))
    {
      update BLOG.DBA.SYS_BLOG_INFO set BI_HAVE_COMUNITY_BLOG = 1 where BI_BLOG_ID = 'dav-blog-1';

      for (select BI_HOME, BI_BLOG_ID from BLOG.DBA.SYS_BLOG_INFO where BI_BLOG_ID <> 'dav-blog-1') do
	BLOG..BLOG2_BLOG_ATTACH ('dav-blog-1', BI_BLOG_ID);
    }
  registry_set ('__BLOG_MAKE_ALL_BLOGS_IN_ONE', 'done');
}
;

create trigger WA_USER_INFO_GEO_U after update (WAUI_LAT) on WA_USER_INFO referencing new as N
{
  for select BI_WAI_NAME from BLOG.DBA.SYS_BLOG_INFO where BI_OWNER = N.WAUI_U_ID do
    {
      ODS..APP_PING (BI_WAI_NAME, null, null, 'GeoURL');
    }
  return;
};

BLOG..blog2_exec_no_error ('DB.DBA.BLOG_MAKE_ALL_BLOGS_IN_ONE ()')
;


create procedure wa_collect_blog_tags (in id int)
{
   for (select BT_TAGS from BLOG..BLOG_TAG) do
	wa_add_tag_to_count (BT_TAGS, id);
}
;

create procedure collect_blog_rel_tags ()
{
  declare tags any;

  for (select BT_TAGS from BLOG..BLOG_TAG) do
     {
	tags := split_and_decode (BT_TAGS, 0, '\0\0,');
	add_tag_to_rel_count (__vector_sort (tags));
     }

  return;
}
;