use drupal;

drop view poll_v;
drop view forum_v;
drop view blog_v;
drop view blog_post_v;
drop view forum_post_v;
drop view poll_post_v;
drop view aggregator_item_v;

SPARQL
prefix v: <http://www.openlinksw.com/schemas/drupal_v#>

drop quad map virtrdf:Drupal .

drop iri class v:user_iri .
drop iri class v:iri .
drop iri class v:feed_item_iri .
drop iri class v:space_iri .
drop iri class v:feed_iri .
drop iri class v:poll_iri .
drop iri class v:forum_iri .
drop iri class v:blog_iri .
drop iri class v:blog_post_iri .
drop iri class v:forum_post_iri .
drop iri class v:poll_post_iri .
drop iri class v:mbox .
;


use drupal;

create procedure ts2date (in x int)
{
  return dateadd ('second', x, stringdate ('1970-1-1'));
};

create view poll_v as select n.*, u.name from poll p, node n, users u where n.nid = p.nid and n.uid = u.uid;
create view forum_v as select n.*, u.name from forum f, node n, users u where n.nid = f.nid and n.uid = u.uid;
create view blog_v as select u.*, u.name||'\'s blog' as title_v from users u;
create view blog_post_v as select nr.*, u.name, drupal..ts2date (n.created) as created, drupal..ts2date (n.changed) as changed
        from node n, node_revisions nr, users u where
        nr.nid = n.nid and nr.vid = n.vid and n.uid = u.uid and n.type in ('blog', 'story');
create view forum_post_v as select nr.*, u.name, drupal..ts2date (n.created) as created, drupal..ts2date (n.changed) as changed
        from node n, node_revisions nr, users u where
        nr.nid = n.nid and nr.vid = n.vid and n.uid = u.uid and n.type = 'forum';
create view poll_post_v as select nr.*, u.name, drupal..ts2date (n.created) as created, drupal..ts2date (n.changed) as changed
        from node n, node_revisions nr, users u where
        nr.nid = n.nid and nr.vid = n.vid and n.uid = u.uid and n.type = 'poll';
create view aggregator_item_v as select *, drupal..ts2date(timestmp) as created from aggregator_item;

grant select on poll_v to "SPARQL";
grant select on forum_v to "SPARQL";
grant select on blog_v to "SPARQL";
grant select on blog_post_v to "SPARQL";
grant select on forum_post_v to "SPARQL";
grant select on poll_post_v to "SPARQL";
grant select on aggregator_item_v to "SPARQL";
grant execute on ts2date to "SPARQL";
grant select on drupal..users to "SPARQL";
grant select on drupal..aggregator_feed to "SPARQL";
grant select on drupal..aggregator_item to "SPARQL";


SPARQL
prefix sioc: <http://rdfs.org/sioc/ns#>
prefix sioct: <http://rdfs.org/sioc/types#>
prefix atom: <http://atomowl.org/ontologies/atomrdf#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
prefix dc: <http://purl.org/dc/elements/1.1/>
prefix dct: <http://purl.org/dc/terms/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
prefix wikiont: <http://sw.deri.org/2005/04/wikipedia/wikiont.owl#>
prefix aowl: <http://atomowl.org/ontologies/atomrdf#>
prefix v: <http://www.openlinksw.com/schemas/drupal_v#>

create iri class v:user_iri "http://^{URIQADefaultHost}^/drupal/user/%U#this" (in user_name varchar not null) .
create iri class v:iri "http://^{URIQADefaultHost}^/drupal/proxy/%s#this" (in uri varchar not null) .
create iri class v:feed_item_iri "http://^{URIQADefaultHost}^/drupal/feed/item/%d#this" (in iid integer not null) .
create iri class v:space_iri  "http://^{URIQADefaultHost}^/drupal/space/%s#this" (in uri varchar not null) .
create iri class v:feed_iri "http://^{URIQADefaultHost}^/drupal/feed/%d#this" (in fid integer not null) .
create iri class v:poll_iri "http://^{URIQADefaultHost}^/drupal/poll/%d#this" (in nid integer not null) .
create iri class v:forum_iri "http://^{URIQADefaultHost}^/drupal/forum/%d#this" (in nid integer not null) .
create iri class v:blog_iri "http://^{URIQADefaultHost}^/drupal/blog/%d#this" (in uid integer not null) .
create iri class v:blog_post_iri "http://^{URIQADefaultHost}^/drupal/blog/%d/%d#this" (in uid integer not null, in nid integer not null) .
create iri class v:forum_post_iri "http://^{URIQADefaultHost}^/drupal/forum/%d/%d#this" (in uid integer not null, in nid integer not null) .
create iri class v:poll_post_iri "http://^{URIQADefaultHost}^/drupal/poll/%d/%d#this" (in uid integer not null, in nid integer not null) .
create iri class v:mbox "mailto:%s" (in mail varchar not null) .

alter quad storage virtrdf:DefaultQuadStorage

from drupal.DBA.users as users
from drupal.DBA.aggregator_feed as feed
from drupal.DBA.aggregator_item_v as feed_item where (^{feed_item.}^.fid = ^{feed.}^.fid)
from drupal.DBA.node as node
from drupal.DBA.node_revisions as node_revisions
from drupal.DBA.poll_v as poll
from drupal.DBA.forum_v as forum
from drupal.DBA.blog_v as blog
from drupal.DBA.blog_post_v as blog_post
from drupal.DBA.forum_post_v as forum_post
from drupal.DBA.poll_post_v as poll_post

  {
    create virtrdf:Drupal as graph iri ("http://^{URIQADefaultHost}^/drupal_v") option (exclusive)
      {
        v:user_iri (users.name) a sioc:User as virtrdf:Drupal-User-cls ;
        sioc:name users.name as virtrdf:Drupal-User-name ;
        sioc:email v:mbox(users.mail) as virtrdf:Drupal-User-mail ;
        sioc:id users.uid as virtrdf:Drupal-User-sioc_id .

        v:feed_iri (feed.fid)
        a aowl:Feed as virtrdf:Drupal-Feed-cls ;
        aowl:title feed.title as virtrdf:Drupal-Feed-title ;
        aowl:link v:iri (feed.link) as virtrdf:Drupal-Feed-link ;
        dc:description feed.description as virtrdf:Drupal-Feed-desc .

        v:feed_iri (feed.fid)
        aowl:entry
        v:feed_item_iri (feed_item.iid) as virtrdf:Drupal-Feed-aowl-entry .

        v:feed_item_iri (feed_item.iid) a aowl:Entry as virtrdf:Drupal-FeedItem-cls ;
        aowl:title feed_item.title as virtrdf:Drupal-FeedItem-title ;
        aowl:link feed_item.link as virtrdf:Drupal-FeedItem-link ;
        sioc:id feed_item.iid as virtrdf:Drupal-FeedItem-sioc-id ;
        sioc:content feed_item.description as virtrdf:Drupal-FeedItem-sioc-content ;
        aowl:content feed_item.description as virtrdf:Drupal-FeedItem-aowl-content ;
        dct:created feed_item.created as virtrdf:Drupal-FeedItem-cr ;
        aowl:source
        v:feed_iri (feed.fid) as virtrdf:Drupal-FeedItem-aowl-src .

        v:poll_iri (poll.nid) a sioct:SurveyCollection as virtrdf:Drupal-Poll-cls ;
        sioc:id poll.nid as virtrdf:Drupal-Poll-sioc-id ;
        dc:title poll.title as virtrdf:Drupal-Poll-title ;
        dct:created poll.created as virtrdf:Drupal-Poll-cr ;
        dct:modified poll.changed as virtrdf:Drupal-Poll-mod ;
        sioc:has_owner v:user_iri (poll.name) as virtrdf:Drupal-Poll-owner.

        v:user_iri (poll.name) sioc:owner_of v:poll_iri (poll.nid) as virtrdf:Drupal-Poll-owner_of .

        v:blog_iri (blog.uid) a sioct:Weblog as virtrdf:Drupal-Blog-cls ;
        sioc:id blog.uid as virtrdf:Drupal-Blog-sioc-id ;
        dc:title blog.title_v as virtrdf:Drupal-Blog-title ;
        sioc:has_owner v:user_iri (blog.name) as virtrdf:Drupal-Blog-owner .

        v:user_iri (blog.name) sioc:owner_of v:blog_iri (blog.uid) as virtrdf:Drupal-Blog-owner_of .

        v:forum_iri (forum.nid) a sioc:Forum as virtrdf:Drupal-Forum-cls ;
        sioc:id forum.nid as virtrdf:Drupal-Forum-sioc-id ;
        dc:title forum.title as virtrdf:Drupal-Forum-title ;
        dct:created forum.created as virtrdf:Drupal-Forum-cr ;
        dct:modified forum.changed as virtrdf:Drupal-Forum-mod ;
        sioc:has_owner v:user_iri (forum.name) as virtrdf:Drupal-Forum-owner.

        v:user_iri (forum.name) sioc:owner_of v:forum_iri (forum.nid) as virtrdf:Drupal-Forum-owner_of .

        v:blog_post_iri (blog_post.uid, blog_post.nid) a sioct:BlogPost as virtrdf:Drupal-BlogPost-cls ;
        sioc:id blog_post.nid as virtrdf:Drupal-BlogPost-sioc-id ;
        dc:title blog_post.title as virtrdf:Drupal-BlogPost-dc-title ;
        sioc:content blog_post.body as virtrdf:Drupal-BlogPost-sioc-content ;
        dct:created blog_post.created as virtrdf:Drupal-BlogPost-cr ;
        dct:modified blog_post.changed as virtrdf:Drupal-BlogPost-mod ;
        sioc:has_container v:blog_iri (blog_post.uid) as virtrdf:Drupal-BlogPost-sioc-has_container ;
        sioc:has_creator v:user_iri (blog_post.name) as virtrdf:Drupal-BlogPost-owner .

        v:blog_iri (blog_post.uid) sioc:container_of v:blog_post_iri (blog_post.uid, blog_post.nid)
        as virtrdf:Drupal-BlogPost-container_of .
        v:user_iri (blog_post.name) sioc:creator_of v:blog_post_iri (blog_post.uid, blog_post.nid)
        as virtrdf:Drupal-BlogPost-creator_of .

        v:forum_post_iri (forum_post.uid, forum_post.nid) a sioc:Post as virtrdf:Drupal-ForumPost-cls ;
        sioc:id forum_post.nid as virtrdf:Drupal-ForumPost-sioc-id ;
        dc:title forum_post.title as virtrdf:Drupal-ForumPost-dc-title ;
        sioc:content forum_post.body as virtrdf:Drupal-ForumPost-sioc-content ;
        dct:created forum_post.created as virtrdf:Drupal-ForumPost-cr ;
        dct:modified forum_post.changed as virtrdf:Drupal-ForumPost-mod ;
        sioc:has_container v:forum_iri (forum_post.uid) as virtrdf:Drupal-ForumPost-sioc-has_container ;
        sioc:has_creator v:user_iri (forum_post.name) as virtrdf:Drupal-ForumPost-owner .

        v:forum_iri (forum_post.uid) sioc:container_of v:forum_post_iri (forum_post.uid, forum_post.nid)
        as virtrdf:Drupal-ForumPost-container_of .
        v:user_iri (forum_post.name) sioc:creator_of v:forum_post_iri (forum_post.uid, forum_post.nid)
        as virtrdf:Drupal-ForumPost-creator_of .

        v:poll_post_iri (poll_post.uid, poll_post.nid) a sioc:Poll as virtrdf:Drupal-PollPost-cls ;
        sioc:id poll_post.nid as virtrdf:Drupal-PollPost-sioc-id ;
        dc:title poll_post.title as virtrdf:Drupal-PollPost-dc-title ;
        sioc:content poll_post.body as virtrdf:Drupal-PollPost-sioc-content ;
        dct:created poll_post.created as virtrdf:Drupal-PollPost-cr ;
        dct:modified poll_post.changed as virtrdf:Drupal-PollPost-mod ;
        sioc:has_container v:poll_iri (poll_post.uid) as virtrdf:Drupal-PollPost-sioc-has_container ;
        sioc:has_creator v:user_iri (poll_post.name) as virtrdf:Drupal-PollPost-owner .

        v:poll_iri (poll_post.uid) sioc:container_of v:poll_post_iri (poll_post.uid, poll_post.nid)
        as virtrdf:Drupal-PollPost-container_of .
        v:user_iri (poll_post.name) sioc:creator_of v:poll_post_iri (poll_post.uid, poll_post.nid)
        as virtrdf:Drupal-PollPost-creator_of .

      }
  } .
;

DB.DBA.URLREWRITE_CREATE_REGEX_RULE (
    'drupal_rule2',
    1,
    '/drupal/([^#]*)',
    vector('path'),
    1,
    '/sparql?query=DESCRIBE+%%3Chttp%%3A//^{URIQADefaultHost}^/drupal/%U%%23this%%3E+FROM+%%3Chttp%%3A//^{URIQADefaultHost}^/drupal_v%%3E&format=%U',
    vector('path', 'path', '*accept*'),
    null,
    '(text/rdf.n3)|(application/rdf.xml)',
    2,
    null
    );

DB.DBA.URLREWRITE_CREATE_REGEX_RULE (
    'drupal_rule3',
    1,
    '/drupal/(user|post|site|proxy|wiki|forum|poll|blog|feed|space)/([^#]*)',
    vector('obj', 'path'),
    1,
    '/rdfbrowser/index.html?uri=http%%3A//^{URIQADefaultHost}^/drupal/%U/%U%%23this',
    vector('obj', 'path'),
    null,
    '(text/html)|(\\*/\\*)',
    2,
    303
    );


DB.DBA.URLREWRITE_CREATE_RULELIST (
    'drupal_rule_list1',
    1,
    vector (
                'drupal_rule3',
                'drupal_rule2'
          ));

DB.DBA.VHOST_REMOVE (lpath=>'/drupal');
DB.DBA.VHOST_DEFINE (lpath=>'/drupal', ppath=>'/vad/vsp/drupal/', is_dav=>0, vsp_user=>'dba', is_brws=>1, def_page=>'index.php',
    opts=>vector ('url_rewrite', 'drupal_rule_list1'));
