if not val then
hasvalue = false -- rank doesn't match
elseif qualsonly and qualID then
-- suppress value returned: only qualifiers are requested
else
out[#out+1], mlt[#out+1] = val, lcode
end
end  
-- See if qualifiers are to be returned:
local snak = v.mainsnak or v
if hasvalue and v.qualifiers and qualID ~= "" and snak.snaktype~="novalue" then
-- collect all wanted qualifier values returned in qlist, indexed by propertyID
local qlist = {}
local timestart, timeend = "", ""
-- loop through qualifiers
for k1, v1 in pairs(v.qualifiers) do
if allflag or qwanted[k1] then
if k1 == "P1326" then
local ts = v1[1].datavalue.value.time
local dp = v1[1].datavalue.value.precision
qlist[k1] = dateFormat(ts, dp, args.qdf, args.bc, pd, "", lang, "before")
elseif k1 == "P1319" then
local ts = v1[1].datavalue.value.time
local dp = v1[1].datavalue.value.precision
qlist[k1] = dateFormat(ts, dp, args.qdf, args.bc, pd, "", lang, "after")
elseif k1 == "P580" then
timestart = propertyvalueandquals(v1, qargs)[1] or "" -- treat only one start time as valid
elseif k1 == "P582" then
timeend = propertyvalueandquals(v1, qargs)[1] or "" -- treat only one end time as valid
else
local q = assembleoutput(propertyvalueandquals(v1, qargs), qargs)
-- we already deal with circa via 'sourcing circumstances' if the datatype was time
-- circa may be either linked or unlinked *** internationalise later ***
if datatype ~= "time" or q ~= "circa" and not (type(q) == "string" and q:find("circa]]")) then
qlist[k1] = q
end
end
end -- of test for wanted
end -- of loop through qualifiers
-- set date separator
local t = timestart .. timeend
-- *** internationalise date separators later ***
local dsep = "&ndash;"
if t:find("%s") or t:find("&nbsp;") then dsep = " &ndash; " end
-- set the order for the list of qualifiers returned; start time and end time go last
if next(qlist) then
local qlistout = {}
if allflag then
for k2, v2 in pairs(qlist) do
qlistout[#qlistout+1] = v2
end
else
for i2, v2 in ipairs(qorder) do
qlistout[#qlistout+1] = qlist[v2]
end
end
if t ~= "" then
qlistout[#qlistout+1] = timestart .. dsep .. timeend
end
local qstr = assembleoutput(qlistout, qargs)
if qualsonly then
out[#out+1] = qstr
else
out[#out] = out[#out] .. " (" .. qstr .. ")"
end
elseif t ~= "" then
if qualsonly then
if timestart == "" then
out[#out+1] = timeend
elseif timeend == "" then
out[#out+1] = timestart
else
out[#out+1] = timestart .. dsep .. timeend
end
else
out[#out] = out[#out] .. " (" .. timestart .. dsep .. timeend .. ")"
end
end
end -- of test for qualifiers wanted  
if maxvals > 0 and #out >= maxvals then break end
end -- of for each value loop  
-- we need to pick one value to return if the datatype was "monolingualtext"
-- if there's only one value, use that
-- otherwise look through the fallback languages for a match
if datatype == "monolingualtext" and #out >1 then
lang = mw.text.split( lang, '-', true )[1]
local fbtbl = mw.language.getFallbacksFor( lang )
table.insert( fbtbl, 1, lang )
local bestval = ""
local found = false
for idx1, lang1 in ipairs(fbtbl) do
for idx2, lang2 in ipairs(mlt) do
if (lang1 == lang2) and not found then
bestval = out[idx2]
found = true
break
end
end -- loop through values of property
end -- loop through fallback languages
if found then
-- replace output table with a table containing the best value
out = { bestval }
else
-- more than one value and of them on the list of fallback languages
-- sod it, just give them the first one
out = { out[1] }
end
end
return out
end  
---
-- Common code for p.getValueByQual and p.getValueByLang
---
-- Dependencies: parseParam; setRanks; parseInput; sourced; assembleoutput;
---
local _getvaluebyqual = function(, qualID, checkvalue)  
-- The property ID that will have a qualifier is the first unnamed parameter
local propertyID = mw.text.trim(.args[1] or "")
if propertyID == "" then return "no property supplied" end  
if qualID == "" then return "no qualifier supplied" end  
-- onlysourced is a boolean passed to return property values
-- only when property values are sourced to something other than Wikipedia
-- if nothing or an empty string is passed set it true
-- if "false" or "no" or 0 is passed set it false
local onlysrc = parseParam(.args.onlysourced or .args.osd, true)  
-- set the requested ranks flags
.args.reqranks = setRanks(.args.rank)  
-- set a language object and code in the .args table
.args.langobj = findLang(.args.lang)
.args.lang = .args.langobj.code  
local args = .args  
-- check for locally supplied parameter in second unnamed parameter
-- success means no local parameter and the property exists
local qid, props = parseInput(, args[2], propertyID)  
local linked = parseParam(args.linked, true)
local lpre = (args.linkprefix or args.lp or ""):gsub('"', '')
local lpost = (args.linkpostfix or ""):gsub('"', '')
local pre = (args.prefix or ""):gsub('"', '')
local post = (args.postfix or ""):gsub('"', '')
local uabbr = parseParam(args.unitabbr or args.uabbr, false)
local filter = (args.unit or ""):upper
local maxvals = tonumber(args.maxvals) or 0
if filter == "" then filter = nil end  
if qid then
local out = {}
-- Scan through the values of the property
-- we want something like property is "pronunciation audio (P443)" in propertyID
-- with a qualifier like "language of work or name (P407)" in qualID
-- whose value has the required ID, like "British English (Q7979)", in qval
for k1, v1 in ipairs(props) do
if v1.mainsnak.snaktype == "value" then
-- check if it has the qualifier
local v1q = v1.qualifiers
if v1q and v1q[qualID] then
if onlysrc == false or sourced(v1) then
-- if we've got this far, we have a (sourced) claim with qualifiers
-- so see if matches the required value
-- We'll only deal with wikibase-items and strings for now
if v1q[qualID][1].datatype == "wikibase-item" then
if checkvalue(v1q[qualID][1].datavalue.value.id) then
out[#out + 1] = rendersnak(v1, args, linked, lpre, lpost, pre, post, uabbr, filter)
end
elseif v1q[qualID][1].datatype == "string" then
if checkvalue(v1q[qualID][1].datavalue.value) then
out[#out + 1] = rendersnak(v1, args, linked, lpre, lpost, pre, post, uabbr, filter)
end
end
end -- of check for sourced
end -- of check for matching required value and has qualifiers
else
return nil
end -- of check for string
if maxvals > 0 and #out >= maxvals then break end
end -- of loop through values of propertyID
return assembleoutput(out, .args, qid, propertyID)
else
return props -- either local parameter or nothing
end -- of test for success
return nil
end  
---
-- _location takes Q-id and follows P276 (location)
-- or P131 (located in the administrative territorial entity) or P706 (located on terrain feature)
-- from the initial item to higher level territories/locations until it reaches the highest.
-- An optional boolean, 'first', determines whether the first item is returned (default: false).
-- An optional boolean 'skip' toggles the display to skip to the last item (default: false).
-- It returns a table containing the locations - linked where possible, except for the highest.
---
-- Dependencies: findLang; labelOrId; linkedItem
---
local _location = function(qid, first, skip)
first = parseParam(first, false)
skip = parseParam(skip, false)
local locs = {"P276", "P131", "P706"}
local out = {}
local langcode = findLang:getCode
local finished = false
local count = 0
local prevqid = "Q0"
repeat
local prop
for i1, v1 in ipairs(locs) do
local proptbl = mw.wikibase.getBestStatements(qid, v1)
if #proptbl > 1 then
-- there is more than one higher location
local prevP131, prevP131id
if prevqid ~= "Q0" then
prevP131 = mw.wikibase.getBestStatements(prevqid, "P131")[1]
prevP131id = prevP131
and prevP131.mainsnak.datavalue
and prevP131.mainsnak.datavalue.value.id
end
for i2, v2 in ipairs(proptbl) do
local parttbl = v2.qualifiers and v2.qualifiers.P518
if parttbl then
-- this higher location has qualifier 'applies to part' (P518)
for i3, v3 in ipairs(parttbl) do