Module:Sparql

From solab
Jump to navigation Jump to search

--require("json") local bodge = require "Module:gtable" local p = {}

function my_query(res, par, filter, limit)

if par == nil then par = "dcterms:subject" end
if filter == nil then filter = "[^a-z A-Z 0-9]*" end
if limit == nil then limit = "" end
local r = [[
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dpb: <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT distinct  ?subject  ?name ?dbppropname ?rdfslabel ?page
WHERE { 
        ?subject  ]]..par..[[   <http://dbpedia.org/resource/]]..res..[[>.
        OPTIONAL { ?subject rdfs:label ?rdfslabel . 
                  FILTER (LANG(?rdfslabel) = 'en' || LANG(?rdfslabel) = 'it' ) .} 
        OPTIONAL { ?subject dbpprop:name ?dbppropname . }
        OPTIONAL { ?subject foaf:name ?name . }
        OPTIONAL { ?subject foaf:isPrimaryTopicOf  ?page . }
      } ]]..limit 
return r

end

function my_query_sub(res, par, filter, limit)

if par == nil then par = "dcterms:subject" end
if filter == nil then filter = "[^a-z A-Z 0-9]*" end
if limit == nil then limit = "" end
local r = [[
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dpb: <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT distinct *
WHERE { 
        OPTIONAL { dbpedia:]] .. res .. [[ owl:sameAs ?resource .}
        OPTIONAL { dbpedia:]] .. res .. [[ rdfs:label ?rdfslabel . 
                  FILTER (LANG(?rdfslabel) = 'en' || LANG(?rdfslabel) = 'it' ) .} 
        OPTIONAL { dbpedia:]] .. res .. [[ dbpprop:name ?dbppropname . }
        OPTIONAL { dbpedia:]] .. res .. [[ foaf:name ?name . }
        OPTIONAL { dbpedia:]] .. res .. [[ foaf:isPrimaryTopicOf  ?page . }
      } ]]..limit 
return r

end -- ]].. pipe..pipe [[ LANG(?label_en) = 'it' ) --local simb = frame:preprocess( "" )

function p.table(frame)

   local ep = frame.args.endpoint
   if ep == nil then 
      ep = "http://dbpedia.org/sparql"
   end
   local filter = frame.args.filter
   if  filter == nil then  filter = "[^a-z A-Z 0-9]*" end
   local limit =  frame.args.limit
   if  limit == nil then limit =  "limit 100" end
   local resource  = frame.args.resource
   if  resource   == nil then  resource =  nil end
   local par = frame.args.par 
   if  par == nil then  
       par = "dcterms:subject"
   end
    
   local debug = frame.args.debug
   if debug == nil then
       notrace  = "yes" 
   else notrace = "no"
   end


   local format = "WIKITABLE"
   local cat = frame.args.cat
    local query = 
   
  if cat == nil then
   query = my_query(resource, par,filter,limit)
   else
   query = my_query_sub(resource, par,filter,limit)
   end
   local res = frame:preprocess("{{#gu_gen: sparql|"..ep.."||"..query.."|"..notrace.."|"..format.."||}}")
   res = "res " .. res 
   return res 

end


function my_sparql_query(text , limit)

if limit == nil then limit = "" end local r = text..limit return r end


function p.opendata(frame)

   local ep = frame.args.endpoint
   if  ep  == nil then  ep =  "http://dbpedia.org/sparql" end
   local filter = frame.args.filter
   if  filter  == nil then  filter =  "[^a-z A-Z 0-9]*" end
   local limit =  frame.args.limit
   if  limit   == nil then limit =  "limit 100" end
   local resource  = frame.args.resource
   if  resource   == nil then  resource =  nil end
   local par = frame.args.par 
   if  par   == nil then  par =  "dcterms:subject" end
    
   local debug = frame.args.debug
   if  debug   == nil then  notrace  = "yes" else notrace  = "no" end
   
   local format = "WIKITABLE"
   local query =  frame.args.query

   local res=frame:preprocess("{{#gu_gen: sparql|"..ep.."| |"..query.."|"..notrace.."|"..format.."||}}")
   return res 

end

function p.query(frame)

   local t={}
   for n,v in frame:argumentPairs() do table.insert (t,{n,v}) end
   table.sort (t,comp)
   r = '{|class="wikitable sortable"\n!name!!value\n'
   for i,nv in ipairs(t) do r = r..'|-\n|'..nv[1]..'||'..nv[2]..'\n' end
   r=r..'|}'
   r =  [[
        PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
        PREFIX dcterms: <http://purl.org/dc/terms/>
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
        SELECT distinct  ?label_en ?page
        WHERE { ?subject  ]]..  frame.args.par ..  [[ <http://dbpedia.org/resource/]]..frame.args.res.. [[>.
        FILTER (REGEX(?subject, ']] .. frame.args.filter .. [[')) . 
        <http://dbpedia.org/resource/]].. frame.args.res .. [[>  rdfs:label ?label  . 
        FILTER (LANG(?label) = 'en') .
        ?subject rdfs:label ?label_en .
        FILTER (LANG(?label_en) = 'en' || LANG(?label_en) = 'it' ) .
        OPTIONAL {
        ?subject  foaf:isPrimaryTopicOf  ?page . }} ]] ..frame.args.limit
   return r

end

return p