end
if checklangcode and checklangcode ~= "" then
-- this is a P-value for language-code, so we'll check qualifiers for languagedata
-- first get any qualifiers
local qualifiers = claim.qualifiers or {}
for _, qualid in ipairs( langqvalorder ) do
-- if the claim has this qualifier
if qualifiers[qualid] then
-- it's here, let's check it out!
local items = {}
-- traverse all snaks in this qualifier
for _, qualsnak in ipairs( qualifiers[qualid] ) do
if qualsnak and pval[qualid] then
-- mw.log("qualsnak = " .. dump(qualsnak))
-- check if the snak is of the correct snaktype and datatype
local valid = qualsnak.snaktype == pval[qualid].types.snaktype
and qualsnak.datatype == pval[qualid].types.datatype
if valid then
-- now get the actual data
langcode = table.concat(getLanguageData('P305', 'Q'..qualsnak.datavalue.value['numeric-id']), '')
end
end
end
end
-- mw.log("langcode is now="..dump(langcode))
end
if string.find(langcode, "([pP]%d+)") then
-- we still don't have any langcode, so we default to "en"
langcode = nil
end
end
local stillvalid = true
-- we should check a couple of other qualifiers as well
-- first get any qualifiers
local qualifiers = claim.qualifiers or {}
for _, qualid in ipairs( otherqvalorder ) do
-- if the claim has this qualifier
if qualifiers[qualid] then
-- it's here, let's check it out!
local items = {}
-- traverse all snaks in this qualifier
for _, qualsnak in ipairs( qualifiers[qualid] ) do
if qualsnak and pval[qualid] then
-- mw.log("qualsnak = " .. dump(qualsnak))
-- check if the snak is of the correct snaktype and datatype
local valid = qualsnak.snaktype == pval[qualid].types.snaktype
and qualsnak.datatype == pval[qualid].types.datatype
if not valid then
-- sorry, this is not correct
mw.log("qualsnak = INCORRECT")
stillvalid = false
end
end
end
end
-- mw.log("langcode is now="..dump(langcode))
end
if stillvalid then
if preferred then
head[#head+1] = { value=stringFormatter(mainsnak.datavalue) }
if langcode and langcode ~= '' then
head[#head]['langcode'] = langcode
end
else
tail[#tail+1] = { value=stringFormatter(mainsnak.datavalue) }
if langcode and langcode ~= '' then
tail[#tail]['langcode'] = langcode
end
end
end
end
end
-- mw.log("getValuesFromWikidata returning head="..dump(head).." tail="..dump(tail))
return #head>0 and head or tail
end  
local function findMainLinksOnWikidata(props, pagetitle, short_links)
local output = {}
local pid = nil
-- get the entity we are checking
local entity = mw.wikibase.getEntityObject
-- to avoid deep tests
if not entity then
return nil
end
local values = getValuesFromWikidata(props)
for _, value in ipairs( values ) do
local verified_value = nil
if props.regex then
-- we have a local defined regex, so this will have to pass first
-- maybe we'll have to convert the regex to Lua-style
local regex = regexConverter(props.regex)
local test = string.match( value.value, '^'..regex..'$' )
--mw.log("testing with "..regex.. " and test="..dump(test).." and value="..id)
if test then
-- it matched, this is correct and overrides any other.
verified_value = value.value
end
else
verified_value = value.value
end
if verified_value then
local url = ''
output[#output+1] = {}
output[#output].langcode = value.langcode
output[#output].category = {}
if props.url_f then
-- we have a local defined url-formatter function, use it as first priority
url = props.url_f(verified_value)
if props.track and not string.find(props.langcode, "([pP]%d+)") then
output[#output].category[#output[#output].category+1] = mw.message.newRawMessage(cmodule:getMessage(contLang:getCode, 'track-cat-local-wd'), props.prop):plain
elseif props.track then
output[#output].category[#output[#output].category+1] = mw.message.newRawMessage(cmodule:getMessage(contLang:getCode, 'track-cat-wd-wd'), props.prop):plain
end
elseif props.url then
-- we have a local defined url-formatter string, use it as second priority
url = mw.message.newRawMessage(props.url, verified_value):plain
if props.track and not string.find(props.langcode, "([pP]%d+)") then
output[#output].category[#output[#output].category+1] = mw.message.newRawMessage(cmodule:getMessage(contLang:getCode, 'track-cat-local-wd'), props.prop):plain
elseif props.track then
output[#output].category[#output[#output].category+1] = mw.message.newRawMessage(cmodule:getMessage(contLang:getCode, 'track-cat-wd-wd'), props.prop):plain
end
else
-- get the formatvalue from the property, if it exists
local formatterUrl = getFormatterUrl(props.prop, verified_value)
if formatterUrl ~= '' then
url = mw.message.newRawMessage(formatterUrl, verified_value):plain
if props.track then
output[#output].category[#output[#output].category+1] = mw.message.newRawMessage(cmodule:getMessage(contLang:getCode, 'track-cat-wd-wd'), props.prop):plain
end
end
end
if url ~= '' then
local this_wiki = mw.getContentLanguage
local this_wiki_code = this_wiki:getCode
local langlink = (value.langcode and value.langcode ~= '' and value.langcode ~= this_wiki_code) and mw.message.newRawMessage(conf:g('msg-langcode'), value.langcode, mw.language.fetchLanguageName(value.langcode, this_wiki_code)) or ""
if short_links and props.short then
output[#output].text =
mw.message.newRawMessage(props.short,
getLabel(entity, props.genitive, pagetitle),
url,
langlink,
verified_value,
mw.uri.encode(verified_value, 'PATH'))
:plain
else
output[#output].text =
mw.message.newRawMessage(props.message,
getLabel(entity, props.genitive, pagetitle),
url,
langlink,
verified_value,
mw.uri.encode(verified_value, 'PATH'))
:plain
end
end
end
end
--mw.log("findMainLinksOnWikidata returning="..dump(output))
return output
end  
local function getSitelinksFromWikidata(props, entity)
local output = {}
--mw.log("getSitelinksFromWikidata, props="..dump(props))
-- to avoid deep tests
if not entity then
entity = mw.wikibase.getEntityObject
if not entity then
--mw.log("getSitelinksFromWikidata no entity")
return nil
end
end
local requested_sitelink = string.match(props.prop, "SL(%l+)")
local sitelinks = entity:getSitelink(requested_sitelink)
if sitelinks and sitelinks ~= '' then
output[#output+1] = { value = sitelinks }
end
--mw.log("getSitelinksFromWikidata returning output="..dump(output))
return output
end  
local function findSiteLinksOnWikidata(props, pagetitle, short_links)
local output = {}
local pid = nil
-- get the entity we are checking
local entity = mw.wikibase.getEntityObject
-- to avoid deep tests
if not entity then
return nil
end
local values = getSitelinksFromWikidata(props)
for _, value in ipairs( values ) do
local verified_value = nil
if props.regex then
-- we have a local defined regex, so this will have to pass first
-- maybe we'll have to convert the regex to Lua-style
local regex = regexConverter(props.regex)
local test = string.match( value.value, '^'..regex..'$' )
--mw.log("testing with "..regex.. " and test="..dump(test).." and value="..id)
if test then
-- it matched, this is correct and overrides any other.
verified_value = value.value
end
else
verified_value = value.value
end
if verified_value then
--mw.log("it's verified..")
local url = ''
output[#output+1] = {}
output[#output].langcode = value.langcode
output[#output].category = {}
if props.url_f then
-- we have a local defined url-formatter function, use it as first priority
url = props.url_f(verified_value)
if props.track and not string.find(props.langcode, "(SL%l+)") then
output[#output].category[#output[#output].category+1] = mw.message.newRawMessage(cmodule:getMessage(contLang:getCode, 'track-cat-local-wd'), props.prop):plain
elseif props.track then
output[#output].category[#output[#output].category+1] = mw.message.newRawMessage(cmodule:getMessage(contLang:getCode, 'track-cat-wd-wd'), props.prop):plain
end
elseif props.url then
-- we have a local defined url-formatter string, use it as second priority
url = mw.message.newRawMessage(props.url, verified_value):plain
if props.track and not string.find(props.langcode, "(SL%l+)") then