Module:Wikidata: Difference between revisions
Jump to navigation
Jump to search
>Tobias1984 (insert function claim) |
>Tobias1984 (fixed typo in comment) |
||
Line 227: | Line 227: | ||
end | end | ||
-- returns the page id (Q...) of | -- returns the page id (Q...) of the current page or nothing of the page is not connected to Wikidata | ||
function p.pageId(frame) | function p.pageId(frame) | ||
local entity = mw.wikibase.getEntityObject() | local entity = mw.wikibase.getEntityObject() |
Revision as of 06:32, 26 October 2014
Documentation for this module may be created at Module:Wikidata/doc
local p = {} -- This is used to get a value, or a comma separated list of them if multiple values exist p.getValue = function(frame) local propertyID = mw.text.trim(frame.args[1] or "") local input_parm = mw.text.trim(frame.args[2] or "") if input_parm == "FETCH_WIKIDATA" then local entity = mw.wikibase.getEntityObject() local claims = entity.claims[propertyID] if claims then -- if wiki-linked value output as link if possible if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then local out = {} for k, v in pairs(claims) do local sitelink = mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) local label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end if sitelink then out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]" else out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<abbr title='Article is not yet available in this wiki'>[*]</abbr>" end end return table.concat(out, ", ") else return entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value end else return "" end else return input_parm end end p.getQualifierValue = function(frame) local propertyID = mw.text.trim(frame.args[1] or "") local qualifierID = mw.text.trim(frame.args[2] or "") local input_parm = mw.text.trim(frame.args[3] or "") if input_parm == "FETCH_WIKIDATA" then local entity = mw.wikibase.getEntity() if entity.claims[propertyID] ~= nil then local out = {} for k, v in pairs(entity.claims[propertyID]) do for k2, v2 in pairs(v.qualifiers[qualifierID]) do if v2.snaktype == 'value' then if (mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"])) then out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"]) .. "]]" else out[#out + 1] = "[[:d:Q" .. v2.datavalue.value["numeric-id"] .. "|" .. mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"]) .. "]]<abbr title='Article is not yet available in this wiki'>[*]</abbr>" end end end end return table.concat(out, ", ") else return "" end else return input_parm end end -- This is used to get a value like 'male' (for property p21) which won't be linked and numbers without the thousand separators p.getRawValue = function(frame) local propertyID = mw.text.trim(frame.args[1] or "") local input_parm = mw.text.trim(frame.args[2] or "") if input_parm == "FETCH_WIKIDATA" then local entity = mw.wikibase.getEntityObject() local claims = entity.claims[propertyID] if claims then local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value -- if number type: remove thousand separators if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") then result = mw.ustring.gsub(result, "(%d),(%d)", "%1%2") end return result else return "" end else return input_parm end end p.getRawQualifierValue = function(frame) local propertyID = mw.text.trim(frame.args[1] or "") local qualifierID = mw.text.trim(frame.args[2] or "") local input_parm = mw.text.trim(frame.args[3] or "") if input_parm == "FETCH_WIKIDATA" then local entity = mw.wikibase.getEntity() if entity.claims[propertyID] ~= nil then local out = {} for k, v in pairs(entity.claims[propertyID]) do for k2, v2 in pairs(v.qualifiers[qualifierID]) do if v2.snaktype == 'value' then if v2.datavalue.value["numeric-id"] then out[#out + 1] = mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"]) else out[#out + 1] = v2.datavalue.value end end end end local ret = table.concat(out, ", ") return string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2) else return "" end else return input_parm end end -- This is used to get a date value for date_of_birth (p569), etc. which won't be linked -- consolidate by testing if entity.claims[propertyID].mainsnak.datavalue.type is "time" -- Dates are stored as 28 characters if the year >99 -- e.g. +00000002014-01-01T00:00:00Z for 2014 -- Dates are stored as 26 characters if the year =<99 -- e.g. +000000050-01-01T00:00:00Z for 50 CE p.getDateValue = function(frame) local propertyID = mw.text.trim(frame.args[1] or "") local input_parm = mw.text.trim(frame.args[2] or "") local date_format = mw.text.trim(frame.args[3] or "dmy") if input_parm == "FETCH_WIKIDATA" then local entity = mw.wikibase.getEntity() if entity.claims[propertyID] ~= nil then local out = {} local dt = {} for k, v in pairs(entity.claims[propertyID]) do if v.mainsnak.snaktype == 'value' then local d = v.mainsnak.datavalue.value.time if #d > 26 then dt.year = string.sub(d, 9, 12) dt.month = string.sub(d, 14, 15) dt.day = string.sub(d, 17, 18) else dt.year = string.sub(d, 9, 10) dt.month = string.sub(d, 12, 13) dt.day = string.sub(d, 15, 16) end if date_format == "mdy" then out[#out + 1] = os.date("%B %e, %Y", os.time(dt)) elseif date_format == "my" then out[#out + 1] = os.date("%B %Y", os.time(dt)) elseif date_format == "y" then out[#out + 1] = os.date("%Y", os.time(dt)) else out[#out + 1] = os.date("%e %B %Y", os.time(dt)) end end end return table.concat(out, ", ") else return "" end else return input_parm end end p.getQualifierDateValue = function(frame) local propertyID = mw.text.trim(frame.args[1] or "") local qualifierID = mw.text.trim(frame.args[2] or "") local input_parm = mw.text.trim(frame.args[3] or "") local date_format = mw.text.trim(frame.args[4] or "dmy") if input_parm == "FETCH_WIKIDATA" then local entity = mw.wikibase.getEntity() if entity.claims[propertyID] ~= nil then local out = {} local dt = {} for k, v in pairs(entity.claims[propertyID]) do for k2, v2 in pairs(v.qualifiers[qualifierID]) do if v2.snaktype == 'value' then local d = v2.datavalue.value.time if #d > 26 then dt.year = string.sub(d, 9, 12) dt.month = string.sub(d, 14, 15) dt.day = string.sub(d, 17, 18) else dt.year = string.sub(d, 9, 10) dt.month = string.sub(d, 12, 13) dt.day = string.sub(d, 15, 16) end if date_format == "mdy" then out[#out + 1] = os.date("%B %e, %Y", os.time(dt)) elseif date_format == "my" then out[#out + 1] = os.date("%B %Y", os.time(dt)) elseif date_format == "y" then out[#out + 1] = os.date("%Y", os.time(dt)) else out[#out + 1] = os.date("%e %B %Y", os.time(dt)) end end end end return table.concat(out, ", ") else return "" end else return input_parm end end -- This is used to get the TA98 (Terminologia Anatomica first edition 1998) values like 'A01.1.00.005' (property P1323) -- which are then linked to http://www.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/01.1.00.005%20Entity%20TA98%20EN.htm -- uses the newer mw.wikibase calls instead of directly using the snaks -- formatPropertyValues returns a table with the P1323 values concatenated with ", " so we have to split them out into a table in order to construct the return string p.getTAValue = function(frame) local ent = mw.wikibase.getEntityObject() local props = ent:formatPropertyValues('P1323') local out = {} local t = {} for k, v in pairs(props) do if k == 'value' then t = mw.text.split( v, ", ") for k2, v2 in pairs(t) do out[#out + 1] = "[http://www.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/" .. string.sub(v2, 2) .. "%20Entity%20TA98%20EN.htm " .. v2 .. "]" end end end ret = table.concat(out, "<br> ") if #ret == 0 then ret = "Invalid TA" end return ret end -- returns the page id (Q...) of the current page or nothing of the page is not connected to Wikidata function p.pageId(frame) local entity = mw.wikibase.getEntityObject() if not entity then return nil else return entity.id end end -- the "qualifiers" and "snaks" field have a respective "qualifiers-order" and "snaks-order" field -- use these as the second parameter and this function instead of the built-in "pairs" function -- to iterate over all qualifiers and snaks in the intended order. local function orderedpairs(array, order) if not order then return pairs(array) end -- return iterator function local i = 0 return function() i = i + 1 if order[i] then return order[i], array[order[i]] end end end function getReferences(claim) local result = "" -- traverse through all references for ref in pairs(claim.references or {}) do local refparts -- traverse through all parts of the current reference for snakkey, snakval in orderedpairs(claim.references[ref].snaks or {}, claim.references[ref]["snaks-order"]) do if refparts then refparts = refparts .. ", " else refparts = "" end -- output the label of the property of the reference part, e.g. "imported from" for P143 refparts = refparts .. tostring(mw.wikibase.label(snakkey)) .. ": " -- output all values of this reference part, e.g. "German Wikipedia" and "English Wikipedia" if the referenced claim was imported from both sites for snakidx = 1, #snakval do if snakidx > 1 then refparts = refparts .. ", " end refparts = refparts .. getSnakValue(snakval[snakidx]) end end if refparts then result = result .. "<ref>" .. refparts .. "</ref>" end end return result end function p.claim(frame) local property = frame.args[1] or "" local id = frame.args["id"] -- "id" must be nil, as access to other Wikidata objects is disabled in Mediawiki configuration local qualifierId = frame.args["qualifier"] local parameter = frame.args["parameter"] local list = frame.args["list"] local references = frame.args["references"] local showerrors = frame.args["showerrors"] local default = frame.args["default"] if default then showerrors = nil end -- get wikidata entity local entity = mw.wikibase.getEntityObject(id) if not entity then if showerrors then return printError("entity-not-found") else return default end end -- fetch the first claim of satisfying the given property local claims = findClaims(entity, property) if not claims or not claims[1] then if showerrors then return printError("property-not-found") else return default end 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 result local error if list then local value -- iterate over all elements and return their value (if existing) result = {} for idx in pairs(claims) do local claim = claims[sortindices[idx]] value, error = getValueOfClaim(claim, qualifierId, parameter) if not value and showerrors then value = error end if value and references then value = value .. getReferences(claim) end result[#result + 1] = value end result = table.concat(result, list) else -- return first element local claim = claims[sortindices[1]] result, error = getValueOfClaim(claim, qualifierId, parameter) if result and references then result = result .. getReferences(claim) end end if result then return result else if showerrors then return error else return default end end end return p