«Modulu:Wikidata» eta «Modulu:Wikidata/probak» orrien arteko aldeak

(Orrien arteko aldeak)
1. orrialdea
2. orrialdea
Ezabatutako edukia Gehitutako edukia
egunari -a mugatzailea jartzen ez badiogu (abenduaren 10a), beste hau da egitura zuzena (abenduak 10)
 
update from ca.wiki, new functions expandBraces and getInstanceValue, and some fixes
 
63. lerroa: 63. lerroa:
return word
return word
end

local function expandBraces(text)
if globalFrame == nil or text == nil then return text end
if type(text) ~= "string" then
text = tostring(text)
end
for braces in mw.ustring.gmatch(text, "{{(.-)}}") do
local parts = mw.text.split(braces, "|")
local title = parts[1]
local parameters = {}
for i = 2, #parts do
if mw.ustring.find(parts[i], "=") then
local subparts = mw.text.split(parts[i], "=")
parameters[subparts[1]] = subparts[2]
else
table.insert(parameters, parts[i])
end
end
local braces_expanded
if mw.ustring.find(title, ":") then
braces_expanded = globalFrame:callParserFunction{name=title, args=parameters}
else
braces_expanded = globalFrame:expandTemplate{title=title, args=parameters}
end
text = mw.ustring.gsub(text, "{{" .. title .. ".-}}", braces_expanded)
end
return text
end
end


69. lerroa: 100. lerroa:
return '[' .. data .. ' ' .. mw.text.split(data, '//' )[2] .. ']'
return '[' .. data .. ' ' .. mw.text.split(data, '//' )[2] .. ']'
elseif mw.ustring.find((parameter or ''), '$1', 1, true) then -- formatting = a pattern
elseif mw.ustring.find((parameter or ''), '$1', 1, true) then -- formatting = a pattern
return mw.ustring.gsub(parameter, '$1', data) .. '' -- hack to get only the first result of the function
return expandBraces(mw.ustring.gsub(parameter, '$1', data))
else
else
return data
return data
105. lerroa: 136. lerroa:
local lang = mw.language.new(wiki.langcode)
local lang = mw.language.new(wiki.langcode)
amount = lang:formatNum(tonumber(amount))
amount = lang:formatNum(tonumber(amount))
-- This is used to get the unit name for a numeric value
return amount, sortkey
local suffix = ""
if parameter == "unit" or parameter == "unitcode" then
-- get the url for the unit entry on Wikidata:
local unitID = data.unit
-- and just return the last bit from "Q" to the end (which is the QID):
unitID = mw.ustring.sub(unitID, mw.ustring.find(unitID, "Q"), -1)
if mw.ustring.sub(unitID, 1, 1) == "Q" then
local unit_label = mw.wikibase.label(unitID)
suffix = " " .. require("Module:Wikidata/Units").getUnit(amount, unit_label, unitID, parameter == "unitcode")
end
end
return amount .. suffix, sortkey
end
end


151. lerroa: 194. lerroa:
timestamp = timestamp .. " + 1 day" -- formatDate yyyy-mm-00 returns the previous month
timestamp = timestamp .. " + 1 day" -- formatDate yyyy-mm-00 returns the previous month
ret, _ = string.gsub(d("F Y"), " 0+", " ") -- supress leading zeros in year
ret, _ = string.gsub(d("F Y"), " 0+", " ") -- supress leading zeros in year
elseif parameter and parameter ~= "table" then
elseif parameter then
ret, _ = string.gsub(d(parameter), " 0+", " ")
ret, _ = string.gsub(d(parameter), " 0+", " ")
else
else
164. lerroa: 207. lerroa:
local sitelink = mw.wikibase.sitelink(entityId)
local sitelink = mw.wikibase.sitelink(entityId)
local parameter = parameters.formatting
local parameter = parameters.formatting
local labelcase = label or sitelink
if parameters.case then
labelcase = case(labelcase, parameters.case)
end
if parameter == 'raw' then
if parameter == 'raw' then
return entityId
return entityId, entityId
elseif parameter == 'label' then
elseif parameter == 'label' then
return (label or entityId)
return (labelcase or entityId), (labelcase or entityId)
elseif parameter == 'sitelink' then
elseif parameter == 'sitelink' then
return (sitelink or 'wikidata:' .. entityId)
return (sitelink or 'wikidata:' .. entityId), (sitelink or entityId)
elseif mw.ustring.find((parameter or ''), '$1', 1, true) then -- formatting = a pattern
local ret = mw.ustring.gsub(parameter, '$1', labelcase or entityId)
ret = expandBraces(ret)
return ret, labelcase or entityId
else
else
local labelcase = label or sitelink
if parameters.case then
labelcase = case(labelcase, parameters.case)
end
if sitelink then
if sitelink then
return '[[' .. sitelink .. '|' .. labelcase .. ']]', labelcase
return '[[' .. sitelink .. '|' .. labelcase .. ']]', labelcase
192. lerroa: 239. lerroa:
elseif parameter then
elseif parameter then
if data["language"] == wiki.langcode then
if data["language"] == wiki.langcode then
result = data["text"]
if parameter then
result = printDatavalueString(data["text"], parameter)
else
result = data["text"]
end
end
end
else
else
306. lerroa: 349. lerroa:
end
end


-- A la consola de depuració useu: =p.proves({item="Q...", property="P...", ...})
-- A la consola de depuració useu: =p._debug({item="Q...", property="P...", ...})
function p.proves(args)
function p._debug(args)
globalFrame = nil
return _main(args)
return _main(args)
end
end


function p.claim(frame)
function p.claim(frame)
local args = frame.args
globalFrame = frame
return _main(args)
return _main(frame.args)
end
end


325. lerroa: 369. lerroa:
local property = args["property"] or ""
local property = args["property"] or ""
local id = args["item"]; if id == "" then id = nil end
local id = args["item"]; if id == "" then id = nil end
local idgender = args["itemgender"]; if idgender == "" then idgender = nil end
local qualifierId = {}
local qualifierId = {}
qualifierId[1] = args["qualifier"] or args["qualifierProperty"] -- compatibilitat de transició
qualifierId[1] = args["qualifier"] or args["qualifierProperty"] -- compatibilitat de transició
385. lerroa: 430. lerroa:
if not claims or not claims[1] then
if not claims or not claims[1] then
if showerrors then return printError("property-not-found") else return default end
if showerrors then return printError("property-not-found") else return default end
end
-- find feminine case if gender is requested
if parameters.case == "gender" or idgender then
local genderEntity = idgender and mw.wikibase.getEntityObject(idgender) or entity
local genderClaims = genderEntity.claims["P21"]
if genderClaims then
local genderId = getValueOfClaim(genderClaims[1], nil, {["formatting"]="raw"})
if genderId == "Q6581072" or genderId == "Q1052281" then -- female or transgender female
parameters.case = (parameters.case == "infoboxlabel") and "labelfeminine" or "feminine"
end
end
end
end
414. lerroa: 471. lerroa:
local claim = claims[sortindices[idx]]
local claim = claims[sortindices[idx]]
if parameters.formatting == "table" then
if parameters.formatting == "table" then
value, sortkey, error = getValueOfClaim(claim, nil, parameters)
value, sortkey, error = getValueOfClaim(claim, nil, {formatting=args["colformat0"]})
for i, qual in ipairs(qualifierId) do
for i, qual in ipairs(qualifierId) do
valueq, sortkeyq, _ = getValueOfClaim(claim, qual, parameters)
valueq, sortkeyq, _ = getValueOfClaim(claim, qual, {formatting=args["colformat" .. i]})
values[#values]["col" .. i] = valueq
values[#values]["col" .. i] = valueq
sortkeys[#sortkeys]["col" .. i] = sortkeyq or valueq
sortkeys[#sortkeys]["col" .. i] = sortkeyq or valueq
451. lerroa: 508. lerroa:
end
end
end
end
value = expandBraces(value)
result[#result + 1] = value
result[#result + 1] = value
end
end
461. lerroa: 518. lerroa:
end
end
if result then return result else
-- This is used to get the unit name for a numeric value
local suffix = ""
if parameters.formatting == "unit" or parameters.formatting == "unitcode" then
local result_unit = entity:formatPropertyValues(property, mw.wikibase.entity.claimRanks).value
if claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity" then
if mw.ustring.find(result_unit, " ") then
local unit_label = mw.ustring.sub(result_unit, mw.ustring.find(result_unit, " ")+1, -1)
-- get the url for the unit entry on Wikidata:
local unitID = claims[1].mainsnak.datavalue.value.unit
-- and just return the last bit from "Q" to the end (which is the QID):
unitID = mw.ustring.sub(unitID, mw.ustring.find(unitID, "Q"), -1)
suffix = " " .. require("Module:Wikidata/Units").getUnit(result, unit_label, unitID, parameters.formatting == "unitcode")
end
end
end

if result then return result .. suffix else
if showerrors then return error else return default end
if showerrors then return error else return default end
end
end
599. lerroa: 640. lerroa:
i = i + 1
i = i + 1
end
end
end

-- getInstanceValue: returns a property value with its instance label fetching a recursive tree

local function uc_first(word)
return mw.ustring.upper(mw.ustring.sub(word, 1, 1)) .. mw.ustring.sub(word, 2)
end

local function getPropertyValue(id, property, parameter)
local entity = mw.wikibase.getEntityObject(id)
if not (entity and entity.claims) then return end
local claims = entity.claims[property]
if not claims then return end
-- get initial sort indices
local sortindices = {}
for idx in pairs(claims) do
sortindices[#sortindices + 1] = idx
end
-- sort by claim rank
local comparator = function(a, b)
local rankmap = { deprecated = 2, normal = 1, preferred = 0 }
local ranka = rankmap[claims[a].rank or "normal"] .. string.format("%08d", a)
local rankb = rankmap[claims[b].rank or "normal"] .. string.format("%08d", b)
return ranka < rankb
end
table.sort(sortindices, comparator)
local snak = claims[sortindices[1]].mainsnak
local entityId = "Q" .. tostring(snak.datavalue.value['numeric-id'])
local result, _ = getSnakValue(snak, {formatting=parameter})
return entityId, result
end

function p.getInstanceValue(frame)
local args = frame.args
local id = args["item"]; if id == "" then id = nil end
local propertyFetch = args["property"]; if (propertyFetch == nil or propertyFetch == "") then propertyFetch = "P131" end
local upto = args["upto"]; if upto == "" then upto = nil end
local labelShow = args["labelshow"]; if labelShow == "" then labelShow = nil end
local rowformat = args["rowformat"]; if (rowformat == nil or rowformat == "") then rowformat = "$0 = $1" end
local separator = args["separator"]; if (separator == nil or separator == "") then separator = "<br />" end
local sorting = args["sorting"]; if sorting == "" then sorting = nil end
local maxloop = tonumber(upto) or 10
local lastlabel = uc_first(upto or '')
local labelFilter = {}
if labelShow then
for i, v in ipairs(mw.text.split(labelShow, "/")) do
labelFilter[uc_first(v)] = true
end
end
local propertyInstance = "P31"
local result = {}
local label, link
for iter = 1, maxloop do
local label, link
id, link = getPropertyValue(id, propertyFetch, "internallink")
_, label = getPropertyValue(id, propertyInstance, "label")
if label and link then
label = case(label, "infoboxlabel")
if labelShow == nil or labelFilter[label] then
result[#result + 1] = {label, link}
end
if label == lastlabel then
break
end
else
break
end
end
local ret = {}
local first = 1
local last = #result
local iter = 1
if sorting == "-1" then first = #result; last = 1; iter = -1 end

for i = first, last, iter do
ret[#ret +1] = mw.ustring.gsub(rowformat, "$[01]", {["$0"] = result[i][1], ["$1"] = result[i][2]})
end
return mw.text.listToText(ret, separator, separator)
end
end