File: o100.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 (992 lines) | stat: -rw-r--r-- 33,881 bytes parent folder | download | duplicates (2)
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
--  
--  $Id: o100.sql,v 1.4 2007/01/22 22:01:37 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
--  
--  
--set MACRO_SUBSTITUTION off;
--set IGNORE_PARAMS on;

create procedure init_xq_s_4 ()
{
  declare stat, msg any;
  stat := '00000';
  exec ('create table O100_CACHED_PAGES (O_URL varchar primary key, O_CNT LONG VARCHAR, O_TITLE varchar, O_FORUM varchar, O_HOME varchar)', stat, msg);
  if (stat <> '00000')
    rollback work;
  exec ('alter table O100_CACHED_PAGES add O_HOME varchar', stat, msg);
  if (stat <> '00000')
    rollback work;
  exec ('alter table O100_CACHED_PAGES add O_IP varchar', stat, msg);
  if (stat <> '00000')
    rollback work;
  exec ('alter table O100_CACHED_PAGES add O_COUNTRY varchar', stat, msg);
  if (stat <> '00000')
    rollback work;
  exec ('alter table O100_CACHED_PAGES add O_CITY varchar', stat, msg);
  if (stat <> '00000')
    rollback work;
  exec ('alter table O100_CACHED_PAGES add O_LAT float', stat, msg);
  if (stat <> '00000')
    rollback work;
  exec ('alter table O100_CACHED_PAGES add O_LNG float', stat, msg);
  if (stat <> '00000')
    rollback work;
  exec ('create table attendees_sources (name varchar primary key, title varchar, query long varchar)', stat, msg);
  DAV_COL_CREATE_INT ('/DAV/home/tutorial_demo/feeds/', '110100100R', 'tutorial_demo', 'administrators', null, null, 0, 0, 0, null, null);
  DAV_COL_CREATE_INT ('/DAV/home/tutorial_demo/feeds/opml/', '110100100R', 'tutorial_demo', 'administrators', null, null, 0, 0, 0, null, null);
  DAV_COL_CREATE_INT ('/DAV/home/tutorial_demo/feeds/rss/', '110100100R', 'tutorial_demo', 'administrators', null, null, 0, 0, 0, null, null);
  DAV_COL_CREATE_INT ('/DAV/home/tutorial_demo/feeds/foaf/', '110100100R', 'tutorial_demo', 'administrators', null, null, 0, 0, 0, null, null);
  commit work;
};

init_xq_s_4 ();

create procedure
DB.DBA.GET_BLOG_URL (in url varchar)
{
    declare content varchar;
    declare olduri varchar;
    declare hdr any;


    if (url is null)
      signal ('22023', 'Invalid URL string');

    olduri := url;
    again:
    hdr := null;
    commit work;
    dbg_obj_print ('url=', url);
    registry_set ('xq_s_4_stat', url);
    registry_set ('xq_s_4_stat_dt', datestring (now()));
    if (url like 'http://%')
      content := http_get (url, hdr);
    else
      content := http_client_ext (url, null, null, 'GET', null, null, null, null, hdr);
    dbg_obj_print ('done.');

    if (hdr[0] not like 'HTTP/1._ 200 %')
      {
       if (hdr[0] like 'HTTP/1._ 30_ %')
	{
	  url := http_request_header (hdr, 'Location');
	  if (isstring (url))
	    {
	      url := WS.WS.EXPAND_URL (olduri, url);
	      --dbg_obj_print ('loc', url);
	      goto again;
	    }
	}
        signal ('22023', trim(hdr[0], '\r\n'), '22023');
        return NULL;
      }
    url := olduri;
    return content;
};


create procedure getFeed (in url varchar, in cache int)
{
  declare content, hdr, xt, rss, atom, ret, ins, title, home, isfeed any;
  declare arr, arr2 any;

  hdr := null;
  declare exit handler for sqlstate '*' {
    rollback work;
    --dbg_obj_print (__SQL_MESSAGE);
    return '';
  };
  ins := 0; isfeed := 0;
  content := (select O_CNT from O100_CACHED_PAGES where O_URL = url);
  if (content is null and cache)
    return '';
  if (content is null)
    {
      --dbg_obj_print ('URL', url);
      content := DB.DBA.GET_BLOG_URL (url);
      insert replacing O100_CACHED_PAGES (O_URL, O_CNT, O_FORUM) values (url, content, connection_get ('xq_s_4'));
      ins := 1;
    }
  xt := xtree_doc (content, 2, url);
  title := xpath_eval ('/html/head/title/text()', xt, 1);
  home := url;

  -- there is a case where feed can be here

  if (title is null)
    {
      title := xpath_eval ('/rss/channel/title/text()', xt, 1);
      home := xpath_eval ('/rss/channel/link/text()', xt, 1);
      if (title is null)
	{
	  title := xpath_eval ('/feed/title/text()', xt, 1);
	  home := xpath_eval ('/feed/link[@rel="alternate"]/@href', xt, 1);
	}
      if (title is null)
	{
	  title := xpath_eval ('/rdf/channel/title/text()', xt, 1);
	  home := xpath_eval ('/rdf/channel/@about', xt, 1);
	}
      if (title is not null)
	isfeed := 1;
    }

  if (ins)
    {
      update O100_CACHED_PAGES set O_TITLE = title, O_HOME = home where O_URL = url;
      commit work;
    }

  if (isfeed)
    {
      ret := url;
      goto retu;
    }

  --rss := xpath_eval ('/html/head/link[@rel="alternate" and @type="application/rss+xml"]/@href', xt, 1);
  --atom := xpath_eval ('/html/head/link[@rel="alternate" and @type="application/atom+xml"]/@href', xt, 1);
  --ret := WS.WS.EXPAND_URL (url, coalesce (rss, atom, ''));

  arr := xpath_eval ('/html/head/link[@rel="alternate" and @type="application/rss+xml"]/@href', xt, 0);
  arr2 := xpath_eval ('/html/head/link[@rel="alternate" and @type="application/atom+xml"]/@href', xt, 0);
  ret := '';
  if (length(arr) = 1)
    {
      ret := WS.WS.EXPAND_URL (url, arr[0]);
    }
  else if (length(arr2) = 1)
    {
      ret := WS.WS.EXPAND_URL (url, arr2[0]);
    }
  else
    {
      foreach (varchar u in arr) do
	{
	  u := cast (WS.WS.EXPAND_URL (url, u) as varchar);

	  if (u like url||'%' and u <> url)
	    {
	      ret := u;
	      goto retn;
	    }
	}
      foreach (varchar u in arr2) do
	{
	  u := cast (WS.WS.EXPAND_URL (url, u) as varchar);

	  if (u like url||'%' and u <> url)
	    {
	      ret := u;
	      goto retn;
	    }
	}
    }

retn:
  ret := case when ret = url then '' else cast (ret as varchar) end;

retu:

  return ret;
};

create procedure getTitle (in att varchar, in url varchar, in cache int)
{
  getFeed (url, cache);
  return coalesce ((select O_TITLE from O100_CACHED_PAGES where O_URL = url), att);
};

create procedure getHome (in url varchar, in cache int)
{
  getFeed (url, cache);
  return coalesce ((select O_HOME from O100_CACHED_PAGES where O_URL = url), url);
};


create procedure getExpandedUrl (in base varchar, in url varchar)
{
  return WS.WS.EXPAND_URL (base, url);
};

create procedure xq_s_4_get_country (in url varchar, in url2 varchar)
{
  return coalesce (
      (select O_COUNTRY from O100_CACHED_PAGES where O_URL = url),
      (select O_COUNTRY from O100_CACHED_PAGES where O_URL = url2),
      NULL);
};

create procedure xq_s_4_get_city (in url varchar, in url2 varchar)
{
  return coalesce (
      (select O_CITY from O100_CACHED_PAGES where O_URL = url),
      (select O_CITY from O100_CACHED_PAGES where O_URL = url2),
      NULL);
};

create procedure xq_s_4_get_ip (in url varchar, in url2 varchar)
{
  return coalesce (
      (select O_IP from O100_CACHED_PAGES where O_URL = url),
      (select O_IP from O100_CACHED_PAGES where O_URL = url2),
      NULL);
};

create procedure xq_s_4_get_lat (in url varchar, in url2 varchar)
{
  declare ret any;
  ret := (select O_LAT from O100_CACHED_PAGES where O_URL = url);
  if (ret is null)
    ret := (select O_LAT from O100_CACHED_PAGES where O_URL = url2);
  if (ret is not null)
    ret := sprintf ('%.06f', ret);
  return ret;
};

create procedure xq_s_4_get_lng (in url varchar, in url2 varchar)
{
  declare ret any;
  ret := (select O_LNG from O100_CACHED_PAGES where O_URL = url);
  if (ret is null)
    ret := (select O_LNG from O100_CACHED_PAGES where O_URL = url2);
  if (ret is not null)
    ret := sprintf ('%.06f', ret);
  return ret;
};

create procedure xq_s_4_get_blog (in url varchar, in cache int)
{
  declare qry, stat, msg, dta, mdta any;
  dbg_obj_print (url);
  qry := sprintf ('sparql
  	define get:soft "soft" define input:default-graph-uri "%s"
  	prefix foaf: <http://xmlns.com/foaf/0.1/>
  	prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?blog WHERE { ?p rdf:type foaf:Person ; foaf:weblog ?blog }
	limit 1', url);
  stat := '00000';
  exec (qry, stat, msg, vector (), 0, mdta, dta);
  if (stat = '00000' and isarray (dta) and length (dta))
    {
      declare blog any;
      blog := dta[0][0];
      return getFeed (blog, cache);
    }
  return '';
};

grant execute on DB.DBA.getExpandedUrl to public;
grant execute on DB.DBA.getFeed to public;
grant execute on DB.DBA.getTitle to public;
grant execute on DB.DBA.getHome to public;
grant execute on DB.DBA.xq_s_4_get_country to public;
grant execute on DB.DBA.xq_s_4_get_city to public;
grant execute on DB.DBA.xq_s_4_get_ip to public;
grant execute on DB.DBA.xq_s_4_get_lat to public;
grant execute on DB.DBA.xq_s_4_get_lng to public;
grant execute on DB.DBA.xq_s_4_get_blog to public;

xpf_extension ('http://www.openlinksw.com/demo/:getFeed', 'DB.DBA.getFeed');
xpf_extension ('http://www.openlinksw.com/demo/:getTitle', 'DB.DBA.getTitle');
xpf_extension ('http://www.openlinksw.com/demo/:getHome', 'DB.DBA.getHome');
xpf_extension ('http://www.openlinksw.com/demo/:getExpandedUrl', 'DB.DBA.getExpandedUrl');
xpf_extension ('http://www.openlinksw.com/demo/:getBlog', 'DB.DBA.xq_s_4_get_blog');

xpf_extension ('http://www.openlinksw.com/demo/:getCountry', 'DB.DBA.xq_s_4_get_country');
xpf_extension ('http://www.openlinksw.com/demo/:getCity', 'DB.DBA.xq_s_4_get_city');
xpf_extension ('http://www.openlinksw.com/demo/:getIP', 'DB.DBA.xq_s_4_get_ip');
xpf_extension ('http://www.openlinksw.com/demo/:getLat', 'DB.DBA.xq_s_4_get_lat');
xpf_extension ('http://www.openlinksw.com/demo/:getLng', 'DB.DBA.xq_s_4_get_lng');


create procedure GET_OPML_RES (in id any)
{
	-- if you use xtree_doc mode 1 the generated output is not a valid OPML - attributes @xmlUrl and @htmlUrl should NOT be lowercase -  @xmlurl
  return coalesce ((select xtree_doc(RES_CONTENT, 0, '', 'UTF-8') from WS.WS.SYS_DAV_RES where RES_FULL_PATH = '/DAV/home/tutorial_demo/feeds/opml/'||id||'.opml'), null);
}
;

create procedure GET_FOAF_RES (in id any)
{
	-- if you use xtree_doc mode 1 the generated output is not a valid OPML - attributes @xmlUrl and @htmlUrl should NOT be lowercase -  @xmlurl
  return coalesce ((select xtree_doc(RES_CONTENT, 0, '', 'UTF-8') from WS.WS.SYS_DAV_RES where RES_FULL_PATH = '/DAV/home/tutorial_demo/feeds/foaf/'||id||'.xml'), null);
}
;

create procedure GET_RSS_RES (in id any)
{
  return coalesce ((select RES_CONTENT from WS.WS.SYS_DAV_RES where
	RES_FULL_PATH = '/DAV/home/tutorial_demo/feeds/rss/'||SYS_ALFANUM_NAME (id)||'.xml'), null);
}
;

create procedure STORE_OPML_RES (in id any, in content any)
{
  DAV_RES_UPLOAD_STRSES_INT ('/DAV/home/tutorial_demo/feeds/opml/'||id||'.opml', content, '', '110100100RM', 'tutorial_demo', 'administrators', 'dav', null, 0);
}
;

create procedure STORE_RSS_RES (in id any, in content any)
{
  DAV_RES_UPLOAD_STRSES_INT ('/DAV/home/tutorial_demo/feeds/rss/'||SYS_ALFANUM_NAME (id)||'.xml', content, '', '110100100RM', 'tutorial_demo', 'administrators', 'dav', null, 0);
}
;

create procedure STORE_FOAF_RES (in id any, in content any)
{
  DAV_RES_UPLOAD_STRSES_INT ('/DAV/home/tutorial_demo/feeds/foaf/'||id||'.xml', content, '', '110100100RM', 'tutorial_demo', 'administrators', 'dav', null, 0);
}
;


insert replacing attendees_sources values
(
  'techcrunch', 'TechCrunch Meetup',

  'declare namespace ns="http://www.openlinksw.com/demo/";
   declare namespace h="http://www.w3.org/1999/xhtml";
  <opml version="1.1"><head><title>TechCrunch Meetup</title></head><body>{
    let $div := document("http://wiki.techcrunch.com/third_meetup", "", 2)//h:div[@class="level1"][preceding-sibling::*[normalize-space(.)="Attendees"]]
    for $row in $div//h:div[@class="li"][h:a[@class="urlextern"]]
    return <outline title="{string($row)}" htmlUrl="{$row/h:a/@href}" xmlUrl="{ns:getFeed ($row/h:a/@href, <CACHE>)}" text="{normalize-space($row)}"/>
    }</body></opml>'
 );

insert replacing attendees_sources values
(
  'blog100', 'Blog 100 Stream',

 'declare namespace ns="http://www.openlinksw.com/demo/";
      <opml version="1.1"><head><title>Blog 100 Stream</title></head><body>{
      for $nod in document("http://news.com.com/html/ne/blogs/CNETNewsBlog100.opml", "", 2)//outline[@url]
	    return <outline title="{$nod/@text}" htmlUrl="{$nod/@url}" xmlUrl="{$nod/@xmlurl}" text="{$nod/@text}"/>
     }</body></opml>'
);

insert replacing attendees_sources values
(
  'osc2003', 'Open Source Convention 2003',

 'declare namespace ns="http://www.openlinksw.com/demo/";
      <opml version="1.1"><head><title>Open Source Convention 2003</title></head><body>{
      for $nod in document("http://conferences.oreillynet.com/pub/w/23/speakers.html", "", 2)//font[@size="3"]
	    return <outline title="{string ($nod)}" htmlUrl="http://conferences.oreillynet.com{$nod//a/@href}" xmlUrl="" text="{string ($nod)}"/>
     }</body></opml>'
);

insert replacing attendees_sources values
(
  'osc2005', 'Open Source Convention 2005',

 'declare namespace ns="http://www.openlinksw.com/demo/";
      <opml version="1.1"><head><title>Open Source Convention 2005</title></head><body>{
      for $nod in document("http://conferences.oreillynet.com/pub/w/38/speakers.html", "", 2)//font[@size="3"]
	    return <outline title="{string ($nod)}" htmlUrl="http://conferences.oreillynet.com{$nod//a/@href}" xmlUrl="" text="{string ($nod)}"/>
     }</body></opml>'
);

insert replacing attendees_sources values
(
  'o100', 'Open Media 100 List',
  'declare namespace ns="http://www.openlinksw.com/demo/";
      <opml version="1.1"><head><title>Open Media 100 List</title></head><body>{
      let $tab := document("http://www.alwayson-network.com/comments.php?id=10852_0_11_0_C", "", 2)//table[caption[contains(., "Open Media 100 List")]]
      for $td in $tab//td[a[@href]]
         for $nod in $td/node()
	    let $att := $nod/preceding-sibling::node()[normalize-space(.)!=""]
            where ($nod[@href] and local-name($nod) = "a")
	      return <outline title="{string($nod)}" htmlUrl="{$nod/@href}" xmlUrl="{ns:getFeed ($nod/@href, <CACHE>)}" text="{normalize-space($att)}"/>
     }</body></opml>'
);

insert replacing attendees_sources values
(
  'gnomedexers', 'Gnomedexers 2005',

  'declare namespace ns="http://www.openlinksw.com/demo/";
      <opml version="1.1"><head><title>Gnomedexers 2005</title></head><body>{
      for $nod in document("http://www.gnomedex.com/holdings/br_2005%20Gnomedexers.opml", "", 2)//outline
	    let $att := $nod/@text
	    return <outline title="{ns:getTitle($att, $nod/@url, <CACHE>)}" htmlUrl="{$nod/@url}" xmlUrl="{ns:getFeed ($nod/@url, <CACHE>)}" text="{normalize-space($att)}"/>
     }</body></opml>'
);

insert replacing attendees_sources values
(
  'web2005', 'Web 2005 speakers',
  'declare namespace ns="http://www.openlinksw.com/demo/";
      	    <opml version="1.1"><head><title>Web 2005 speakers</title></head><body>{
            for $nod in document("http://www.web2con.com/pub/w/40/speakers.html", "", 2)//a[string(.)="Click here"]
	    let $nfo := document (ns:getExpandedUrl ("http://www.web2con.com/pub/w/40/speakers.html", $nod/@href), "", 2)
	    let $url := $nfo//font[@size="-1"]/a/@href
	    return <outline title="{$nfo//font[@size="-1"]/text()}" htmlUrl="{string($url)}"
	      	xmlUrl="{ns:getFeed ($url, <CACHE>)}" text="{string ($nfo//font[@size="3"])}"/>
     }</body></opml>'
);

insert replacing attendees_sources values
(
  'ceo', 'CEO Blogs List',
   'declare namespace ns="http://www.openlinksw.com/demo/";
      	<opml version="1.1"><head><title>CEO Blogs List</title></head><body>{
            for $nod in document ("http://www.thenewpr.com/wiki/pmwiki.php?pagename=Resources.CEOBlogsList", "", 2, "UTF-8")//div[@id="wikitext"]//ul/li/a[@class="urllink"][1]
	    where ($nod/ancestor::ul[preceding-sibling::h2[. = "The list"] and not preceding-sibling::h2[. = "Backlinks"]])
	      return <outline text="{$nod/text()}" htmlUrl="{string($nod/@href)}" title="{ns:getTitle($nod/text(), $nod/@href, <CACHE>)}" xmlUrl="{ns:getFeed ($nod/@href, <CACHE>)}" />
     }</body></opml>'
);

insert replacing attendees_sources values
(
  'nigerian_bloggers', 'Nigerian Bloggers',
  'declare namespace ns="http://www.openlinksw.com/demo/";
      	<opml version="1.1"><head><title>Nigerian Bloggers</title></head><body>{
            for $nod in document ("http://nwr.cowblock.net/index.php?action=list", "", 2, "UTF-8")//table[tr/td/strong[.="All Entries"]]//a[@href]
	      return <outline text="{$nod/text()}" htmlUrl="{string($nod/@href)}" title="{ns:getTitle($nod/text(), $nod/@href, <CACHE>)}" xmlUrl="{ns:getFeed ($nod/@href, <CACHE>)}" />
     }</body></opml>'
);
insert replacing attendees_sources values
(
  'african_blogs', 'African Bloggers',
  'declare namespace ns="http://www.openlinksw.com/demo/";
      	      declare namespace n0="http://www.w3.org/1999/xhtml";
      	<opml version="1.1"><head><title>African Bloggers</title></head><body>{
            for $nod in document ("http://okrasoup.typepad.com/black_looks/2005/05/naija_blogs.html", "", 2, "UTF-8")//n0:div[n0:h2[. = "African Blogs"]]//n0:li/n0:a[@href]
	      return <outline text="{$nod/text()}" htmlUrl="{string($nod/@href)}" title="{ns:getTitle($nod/text(), $nod/@href, <CACHE>)}" xmlUrl="{ns:getFeed ($nod/@href, <CACHE>)}" />
     }</body></opml>'
);
insert replacing attendees_sources values
(
  'blogafrica', 'Blogafrica',
  'declare namespace ns="http://www.openlinksw.com/demo/";
      <opml version="1.1"><head><title>Blogafrica</title></head><body>{
      for $nod in document("http://allafrica.com/afdb/blogs/blogafrica.opml", "", 2, "UTF-8")//outline
	    let $att := $nod/@text
	    return <outline title="{ns:getTitle($att, $nod/@link, <CACHE>)}" htmlUrl="{ns:getHome ($nod/@link, <CACHE>)}" xmlUrl="{ns:getFeed ($nod/@link, <CACHE>)}" text="{normalize-space($att)}"/>
     }</body></opml>'
);


insert replacing attendees_sources values
(
  'vloggercon', 'Vloggercon',
'declare namespace ns="http://www.openlinksw.com/demo/";
declare namespace n0="http://www.w3.org/1999/xhtml";
<opml version="1.1"><head><title>Vloggercon</title></head><body>
{
  for $nod in document ("http://wiki.vloggercon.com/index.php?title=Attendees", "", 2, "UTF-8")//n0:ol[preceding-sibling::n0:a[@name="YES"]]/n0:li
     return <outline text="{$nod/text()}" htmlUrl="{string($nod/n0:a[1][@href and @rel="nofollow"]/@href)}" title="{ns:getTitle($nod/text(), $nod/@href, <CACHE>)}"
          xmlUrl="{ns:getFeed ($nod/n0:a[1][@href and @rel="nofollow"]/@href, <CACHE>)}" />
}
</body></opml>');

insert replacing attendees_sources values
(
  'sem_blogs', 'Semantic Weblogs',
'declare namespace n0="http://www.w3.org/1999/xhtml";
 declare namespace ns="http://www.openlinksw.com/demo/";
<opml version="1.1"><head><title>Semantic Weblogs</title></head><body>{
  for $nod in document("http://journal.dajobe.org/journal/2003/07/semblogs/", "", 2)//n0:ul[1]/n0:li
  let $nfo := $nod/n0:a[1]/@title
  let $html := $nod/n0:a[1]/@href
  let $rss := $nod/n0:a[2]/@href

  return <outline title="{string($nfo)}" htmlUrl="{string($html)}"
  xmlUrl="{ns:getFeed ($rss, <CACHE>)}" text="{string ($nfo)}"/>
}</body></opml>');


insert replacing attendees_sources values
(
  'mapufacture', 'Mapufacture',
'declare namespace ns="http://www.openlinksw.com/demo/";
declare namespace n0="http://www.w3.org/1999/xhtml";
<opml version="1.1"><head><title>Semantic Weblogs</title></head><body>{
  for $nod in document("http://mapufacture.com/georss/feed/list", "", 2)//n0:ul[1]/n0:li[n0:span[@class="FeedTitle"]]
  let $nfo := $nod/n0:span[@class="FeedTitle"]/n0:a
  let $html := $nod/n0:span[@class="FeedTitle"]/n0:a/@href
  let $rss := $nod/n0:span[@class="FeedUrl"]

  return <outline title="{string($nfo)}" htmlUrl="{string($html)}"
  xmlUrl="{ns:getFeed (string($rss), <CACHE>)}" text="{string ($nfo)}"/>
}</body></opml>');


insert replacing attendees_sources values
(
'foafmap', 'foafmap.net',
'declare namespace ns="http://www.openlinksw.com/demo/";
declare namespace rss="http://purl.org/rss/1.0/";
declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
<opml version="1.1"><head><title>foafmap.net</title></head><body>{
  for $nod in document("http://foafmap.net/rss.php", "", 0)//rss:item
  let $nfo := $nod/rss:title
  let $html := $nod/rss:link
  let $foaf := $nod/rdfs:seeAlso/@rdf:resource

  return <outline title="{string($nfo)}" htmlUrl="{string($html)}"
    xmlUrl="{ns:getBlog(string($foaf), <CACHE>)}" text="{string ($nfo)}"/>
}</body></opml>');

insert replacing attendees_sources values
(
'technologyvoices', 'Meet The Bloggers',
'declare namespace ns="http://www.openlinksw.com/demo/";
declare namespace rss="http://purl.org/rss/1.0/";
declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace x="http://www.w3.org/1999/xhtml";
<opml version="1.1"><head><title>Meet The Bloggers</title></head><body>{
  for $nod in document("http://www.technologyvoices.com/bloggers", "", 2)//x:div[@id = "newblogs"]/x:div[@class="picture"]
  let $nfo := $nod/following-sibling::x:dl/x:dd[1]
  let $html := $nod/x:a/@href


  return <outline title="{string($nfo)}" htmlUrl="http://www.technologyvoices.com/{string($html)}"
  xmlUrl="" text="{string ($nfo)}"/>
}</body></opml>');



insert replacing attendees_sources values
(
'technorati_pop', 'Technorati, Most popular',
'declare namespace ns="http://www.openlinksw.com/demo/";
declare namespace rss="http://purl.org/rss/1.0/";
declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace x="http://www.w3.org/1999/xhtml";
<opml version="1.1"><head><title></title></head><body>{
  for $nod in document("http://www.technorati.com/pop/blogs/", "", 2)//x:ol[@class="whatsup latest"]/x:li
  let $nfo := $nod//x:h3
  let $html := $nod//x:a[@class = "url"]/@href
  return <outline title="{string($nfo)}" htmlUrl="{string($html)}"
  xmlUrl="{ns:getFeed ($html, <CACHE>)}" text="{string ($nfo)}"/>
}</body></opml>');

insert replacing attendees_sources values
(
'technorati_fav', 'Technorati, Most Favorited',
'declare namespace ns="http://www.openlinksw.com/demo/";
declare namespace rss="http://purl.org/rss/1.0/";
declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace x="http://www.w3.org/1999/xhtml";
<opml version="1.1"><head><title></title></head><body>{
  for $nod in document("http://www.technorati.com/pop/blogs/?faves=1", "", 2)//x:ol[@class="whatsup latest"]/x:li
  let $nfo := $nod//x:h3
  let $html := $nod//x:a[@class = "url"]/@href
  return <outline title="{string($nfo)}" htmlUrl="{string($html)}"
  xmlUrl="{ns:getFeed ($html, <CACHE>)}" text="{string ($nfo)}"/>
}</body></opml>');

insert replacing attendees_sources values
(
'planetrdf', 'Planet RDF',
'declare namespace ns="http://www.openlinksw.com/demo/";
declare namespace rss="http://purl.org/rss/1.0/";
declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace x="http://www.w3.org/1999/xhtml";
<opml version="1.1"><head><title>Planet RDF</title></head><body>{
  for $nod in document("http://planetrdf.com/", "", 2)//x:h2[. = "Bloggers"]/following-sibling::x:ul/x:li
  let $nfo := $nod/x:a[1]
  let $html := $nod/x:a[1]/@href
  let $rss := $nod/x:a[2]/@href
  return <outline title="{string($nfo)}" htmlUrl="{string($html)}"
  xmlUrl="{ns:getFeed ($rss, <CACHE>)}" text="{string ($nfo)}"/>
}</body></opml>');

create procedure gen_opml (in attendee_list varchar, in cache int := 1)
{
  declare src any;
  declare xt, xp, res, q, t any;

  commit work;
  declare exit handler for sqlstate '*'
    {
      rollback work;
      return xtree_doc ('<opml/>');
    };

  if (cache)
    {
      res := GET_OPML_RES (attendee_list);
      if (res is not null)
        return res;
    }
  connection_set ('xq_s_4', attendee_list);

  xt := xtree_doc ('<dummy/>');

  q := (select blob_to_string (query) from attendees_sources where name = attendee_list);
  q := replace (q, '<CACHE>', cast (cache as varchar));

  xp := xquery_eval (q, xt);

  STORE_OPML_RES (attendee_list, serialize_to_UTF8_xml (xp));

  return (xp);
};

create procedure xq_s_4_get_location (in url varchar)
{
  declare ret, ip, cnt, hf any;

  hf := WS.WS.PARSE_URI (url);
  ip := tcpip_gethostbyname (hf[1]);
  cnt := http_get ('http://api.hostip.info/get_html.php?ip='||ip||'&position=true');
  ret := sprintf_inverse (cnt, 'Country:%s\nCity:%s\nLatitude:%s\nLongitude:%s', 0);
  return vector_concat (vector (ip), ret);
};

create trigger O100_CACHED_PAGES_I after insert on O100_CACHED_PAGES referencing new as N
{
  declare info any;
  declare lat, lng any;

  declare exit handler for sqlstate '*'
    {
      return;
    };

 info := xq_s_4_get_location (N.O_URL);

 lat := trim(info[3]);
 lng := trim(info[4]);

 if (length (lat))
   lat := atof (lat);
 else
   lat := null;

 if (length (lng))
   lng := atof (lng);
 else
   lng := null;

 update O100_CACHED_PAGES set O_IP = info[0], O_COUNTRY = trim(info[1]), O_CITY = trim (info[2]), O_LAT = lat, O_LNG = lng
     where O_URL = N.O_URL;

};

create procedure gen_foaf (in cache int := 1)
{
  declare src any;
  declare xt, xp, res any;

  if (cache)
    {
      res := GET_FOAF_RES ('foaf');
      if (res is not null)
        return res;
    }

  xt := xtree_doc ('<dummy/>');
  xp := xquery_eval (
    sprintf ('declare namespace ns="http://www.openlinksw.com/demo/";
      	      declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
      	      declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
      	      declare namespace foaf="http://xmlns.com/foaf/0.1/";
      	      <rdf:RDF>{
               for $doc in collection ("http://local.virt/DAV/home/tutorial_demo/feeds/opml/")//opml
	       return
  	      <foaf:Group>
                <foaf:name>{ string ($doc/head/title) }</foaf:name>{
      for $nod in $doc//outline
	    let $att := $nod/@text
	    return <foaf:member>
	             <foaf:Person>
		        <foaf:name>{ normalize-space($att) }</foaf:name>
			<foaf:homepage rdf:resource="{$nod/@htmlUrl}"/>
			<rdfs:seeAlso rdf:resource="{$nod/@xmlUrl}"/>
  		     </foaf:Person>
	           </foaf:member>

     }</foaf:Group>
    }</rdf:RDF>
     '),
    xt
  );

  STORE_FOAF_RES ('foaf', serialize_to_UTF8_xml (xp));

  return (xp);
}
;

create procedure gen_foaf_one (in which varchar, in cache int := 1)
{
  declare src any;
  declare xt, xp, res any;

  declare exit handler for sqlstate '*'
    {
      rollback work;
      return xtree_doc ('<rdf:RDF rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>');
    };

  if (cache)
    {
      res := GET_FOAF_RES (which||'_foaf');
      if (res is not null)
        return res;
    }

  xt := xtree_doc ('<dummy/>');
  xp := xquery_eval (
    sprintf ('declare namespace ns="http://www.openlinksw.com/demo/";
      	      declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
      	      declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
      	      declare namespace foaf="http://xmlns.com/foaf/0.1/";
	      declare namespace geo="http://www.w3.org/2003/01/geo/wgs84_pos#";
	      declare namespace vcard="http://www.w3.org/2001/vcard-rdf/3.0#";
      	      <rdf:RDF>{
               for $doc in document ("http://local.virt/DAV/home/tutorial_demo/feeds/opml/%s.opml")//opml
	       return
  	      <foaf:Group>
                <foaf:name>{ string ($doc/head/title) }</foaf:name>{
      for $nod in $doc//outline
	    let $att := $nod/@text
	    let $url := replace ($nod/@htmlUrl, "}", "")
	    return <foaf:member>
	             <foaf:Person rdf:about="{$url}#{ urlify (normalize-space($att)) }">
		        <foaf:name>{ normalize-space($att) }</foaf:name>
			<foaf:homepage rdf:resource="{$url}"/>
			<rdfs:seeAlso rdf:resource="{$nod/@xmlUrl}"/>
			<foaf:based_near>
			  <geo:Point geo:lat="{ns:getLat($url, $nod/@xmlUrl)}" geo:long="{ns:getLng($url, $nod/@xmlUrl)}" />
			</foaf:based_near>
			<vcard:ADR rdf:parseType="Resource">
			  <vcard:Country>{ns:getCountry($url, $nod/@xmlUrl)}</vcard:Country>
			  <vcard:Locality>{ns:getCity($url, $nod/@xmlUrl)}</vcard:Locality>
			</vcard:ADR>
  		     </foaf:Person>
	           </foaf:member>

     }</foaf:Group>
    }</rdf:RDF>
     ', which),
    xt
  );

  STORE_FOAF_RES (which||'_foaf', serialize_to_UTF8_xml (xp));

  return (xp);
}
;

create user "xq_s_4";

VHOST_REMOVE (lpath=>'/xq_s_4');

VHOST_DEFINE (lpath=>'/xq_s_4', ppath=>'/SOAP/', soap_user=>'xq_s_4',
    soap_opts => vector (
      'Namespace','http://temp.org/', 'SchemaNS', 'http://temp.org/','MethodInSoapAction','yes',
      'ServiceName', 'MyXMLService', 'elementFormDefault', 'qualified', 'Use', 'literal'
      )
    )
;


create procedure "GetAttendees" (in EventURI varchar, in ResultFormat int) returns xmltype
{
  declare x xmltype;
  declare xd any;
  declare page, ht varchar;

  -- some operations needs a dba privileges
  set_user_id ('dba');
  set http_charset='utf-8';

  EventURI := trim (EventURI);
  if (EventURI = 'http://www.alwayson-network.com/comments.php?id=10852_0_11_0_C')
    {
      if (ResultFormat = 2)
	{
	  xd := gen_foaf_one ('o100');
	}
      else if (ResultFormat = 3 or ResultFormat = 1)
	{
	  xd := gen_opml ('o100');
	}
      page := 'o100';
    }
  else if (EventURI = 'http://www.gnomedex.com/holdings/br_2005%%20Gnomedexers.opml')
    {
      if (ResultFormat = 2)
	{
	  xd := gen_foaf_one ('gnomedexers');
	}
      else if (ResultFormat = 3 or ResultFormat = 1)
	{
	  xd := gen_opml ('gnomedexers');
	}
      page := 'gnomedexers';
    }
  else if (EventURI = 'http://www.web2con.com/pub/w/40/speakers.html')
    {
      if (ResultFormat = 2)
	{
	  xd := gen_foaf_one ('web2005');
	}
      else if (ResultFormat = 3 or ResultFormat = 1)
	{
	  xd := gen_opml ('web2005');
	}
      page := 'web2005';
    }
  else if (EventURI = 'http://www.thenewpr.com/wiki/pmwiki.php/Resources/CEOBlogsList' or EventURI = 'http://www.thenewpr.com/wiki/pmwiki.php?pagename=Resources.CEOBlogsList')
    {
      if (ResultFormat = 2)
	{
	  xd := gen_foaf_one ('ceo');
	}
      else if (ResultFormat = 3 or ResultFormat = 1)
	{
	  xd := gen_opml ('ceo');
	}
      page := 'ceo';
    }
  else if (EventURI = 'http://nwr.cowblock.net/index.php?action=list')
    {
      if (ResultFormat = 2)
	{
	  xd := gen_foaf_one ('nigerian');
	}
      else if (ResultFormat = 3 or ResultFormat = 1)
	{
	  xd := gen_opml ('nigerian_bloggers');
	}
      page := 'nigerian_bloggers';
    }
  else if (EventURI = 'http://okrasoup.typepad.com/black_looks/2005/05/naija_blogs.html')
    {
      if (ResultFormat = 2)
	{
	  xd := gen_foaf_one ('african');
	}
      else if (ResultFormat = 3 or ResultFormat = 1)
	{
	  xd := gen_opml ('african_blogs');
	}
      page := 'african_blogs';
    }
  else if (EventURI = 'http://allafrica.com/afdb/blogs/blogafrica.opml')
    {
      if (ResultFormat = 2)
	{
	  xd := gen_foaf_one ('blogafrica');
	}
      else if (ResultFormat = 3 or ResultFormat = 1)
	{
	  xd := gen_opml ('blogafrica');
	}
      page := 'blogafrica';
    }
  else
    {
      signal ('22023', 'No such URL in the Database');
    }

  if (ResultFormat not in (1,2,3))
    signal ('22023', 'Wrong ResultFormat, must be 1, 2 or 3');

  xml_tree_doc_encoding (xd, 'UTF-8');
  x := new xmltype (xd);
  if (ResultFormat = 1)
    {
      x.xt_ent := xslt (TUTORIAL_XSL_DIR () || '/tutorial/xml/xq_s_4/xq_s_4svc.xsl', x.xt_ent,
      vector ('base', 'http://'|| HTTP_GET_HOST () || '/tutorial/xml/xq_s_4/'||page||'.vsp'));
    }
  return x;
}
;

grant execute on "GetAttendees" to "xq_s_4";

--inital loading

create procedure init_xq_s_4_feeds ()
{
  connection_set ('HTTP_CLI_TIMEOUT', 5);
  commit work;
  dbg_obj_print ('init_feeds', is_http_ctx ());
  if (is_http_ctx ())
    {
      declare arr, cnt any;
      arr := http_pending_req ();
      cnt := 0;
      foreach (any l in arr) do
	{
	  if (l[1] = '/tutorial/xml/xq_s_4/xq_s_4.vsp')
	    cnt := cnt + 1;
	}
      dbg_obj_print ('cnt=',cnt);
      http_request_status ('HTTP/1.1 302 Found');
      http_header ('Location: xq_s_4.vsp\r\n');
      if (cnt <= 1)
	{
	  http_flush ();
        }
      else
	{
	  return;
	}
    }

  declare ex, src any;
  declare i, l, dedl int;

  ex := vector ('o100', 'vloggercon', 'gnomedexers', 'web2005', 'ceo', 'nigerian_bloggers', 'african_blogs', 'blogafrica', 'techcrunch', 'blog100', 'osc2003', 'osc2005', 'sem_blogs', 'mapufacture', 'foafmap', 'technologyvoices', 'technorati_pop', 'technorati_fav', 'planetrdf');

  --ex := vector ('planetrdf');

  commit work;
  dedl := 5;
  l := length (ex);
  i := 0;

  declare exit handler for sqlstate '40001' {
    rollback work;
    dedl := dedl - 1;
    if (dedl > 0)
      goto again;
    registry_set ('xq_s_4_stat', 'error');
    resignal;
  };

  again:;
  while (i < l)
    {
      src := ex[i];
      gen_opml (src, 0);
      gen_foaf_one (src, 0);
      commit work;
      i := i + 1;
    }
  if (l > 0)
  gen_foaf (0);
  registry_set ('xq_s_4_stat', 'done');
};

commit work;
dbg_obj_print ('before init_feeds');
init_xq_s_4_feeds ();