Module:Query

local query = {}

local functional = require("Module:Luafun") local ids = require("Module:IDs") local sparql = require("Module:SPARQL") local properties = require("Module:Properties")

Functions to generate SPARQL variable names (moved to Module:SPARQL)

query.is_var = sparql.is_var query.prefix_var = sparql.prefix_var query.create_var = sparql.create_var

-- function that creates a string of the form "?plop" or wd:Qxx to serve as values in a SPARQL triple patterns of the from "  " -- If the "val" is a table with only one element, and a string of the form "Q[1-9]+" then the value will be wd:Qxx..., -- if the "val" is any other kind of table, the value will be a sparql variable made of the concatenation of all the values of the table function query.create_value(val) if #val > 1 or sparql.is_var(val[1]) then return functional.reduce(function(pref, var) return query.prefix_var(var, pref) end, "", val) elseif (ids.is_validID(val[1])) then return "wd:" .. ids.id_normalize(val[1]) else return sparql.prefix_var(val[1], '') end end

-- same as above except it creates a value of the form "?plop" or wdt:Pxx (variables that can occur on in "   " ). function query.create_property_value(val, prefix) if #val > 1 or sparql.is_var(val[1]) then return functional.reduce(function(pref, var) return query.prefix_var(var, pref) end, "", val) elseif (properties.normalize(val[1])) then return (prefix or "wdt:") .. properties.normalize(val[1]) else return sparql.prefix_var(val[1], '') end end

local function shallowcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table' then copy = {} for orig_key, orig_value in pairs(orig) do           copy[orig_key] = orig_value end else -- number, string, boolean, etc copy = orig end return copy end

-- --

--functions used to manipulate "partial queries" (cf. )

-- this functions takes a list of values for a parameter of a wikitemplate (in form of a coma separated string) -- and expand the template for each of the values

function query.multiexpand(template, multiarglist, multiargname, args, separator, frame ) local result = '' multiargname = multiargname or 1 -- defaults to 1 local expand = function(val) local finalargs = shallowcopy(args) finalargs[multiargname] = val return frame:expandTemplate{title=template, args=finalargs} end local expansions = functional.map(expand, multiarglist):totable return table.concat(expansions, separator or " ") end

function query.expandParam(frame) local args = frame.args local arglist = args["arglist"] local argname = args["argname"] local sep = args["separator"]          -- separator between the outputs of the templates local templatename = args["name"] local inputsep = args["inputseparator"] -- separator between the inputs of the templates (ignored if "newlinesep" is set) local newlineinputsep = args["newlineinputsep"] -- separator between the inputs of the templates (ignored if "newlinesep" is set) local args2 = {} -- the arguments other than our named separator are passed to the expanded template for idx, val in pairs(args) do		if idx ~= "arglist" and idx ~= "argname" and idx ~= "separator" and idx ~= "inputseparator" and idx ~= argname then if tostring(idx):sub(1,7)=="escape_" then idx = string(idx):sub(8) end args2[idx]=val end end inputsep = (newlineinputsep and "\n") or inputsep or "," return query.multiexpand(templatename, mw.text.split(arglist, inputsep), argname, args2, sep, frame) end

-- -- interface --

function query.valid_var(frame) return sparql.create_var(frame.args[1]) end

function query.prefixed_var(frame) return query.prefix_var(frame.args[1], frame.args[2]) end

-- called from Template:Sparql var function query.sparql_value(frame) return query.create_value(frame:getParent.args) end

-- called from Template:Sparql property var function query.sparql_property_value(frame) local templateargs = frame:getParent.args local prefix = templateargs["namespace"] return query.create_property_value(frame:getParent.args, prefix) end

function query.qualifier_predicate(frame) return "pq:" .. properties.normalize(frame.args[1]) end

return query