Modulo:Wikidata/b3
Daytoy a modulo ket agala kadagiti napormat a datos manipud iti Wikidata.
Dagiti annong[urnosen ti taudan]
Daytoy a modulo ket addaan kadagiti dua nga annong.
formatStatements[urnosen ti taudan]
Mangpalubos a mangpormat kadagiti insasao para iti naisangayan a tagikua (property) iti banag a mainaig iti agdama a panid.
Dagiti kompleto a parametro ti formatStatements ket dagiti sumaganad nga eskritu dita baba.
{{#invoke:WD3|formatStatements|property= |rank= |value= |formatting= |separator= |conjunction= }}
formatQualifiers[urnosen ti taudan]
Mangpalubos a mangpormat kadagiti insasao para iti naisangayan a panangikari (qualifier) iti banag a mainaig iti agdama a panid.
Dagiti kompleto a parametro ti formatQualifiers ket dagiti sumaganad nga eskritu dita baba.
{{#invoke:WD3|formatQualifiers|qualifierProperty= |property= |rank= |value= |formatting= |separator= |conjunction= }}
Dagiti parametro[urnosen ti taudan]
|property=
(nasken) ti id ti tagikua a kas iti "p18". Maipalubos pay ti dakkel a letra a kas ti "P18".
|qualifierProperty=
(nasken) panangikari ti tagikua nga inaramid babaen ti "P" ken sarunuen babaen ti bilang. Kitaen ti napno a listaan dagiti tagikua.
|value=
(pagpilian) pateg manipud ti Wikidata. Mabalin a paglilian laeng a parametro a kas parametro ti plantilia, isunga mangala ti pateg ti parametro manipud iti plantilia imbes nga iti pateg ti Wikidata.
|rank=
(pagpilian) no saan nga iyasentar daytoy a parametro, iparangna amin dagiti pateg dagiti amin nga insasao manipud kadagiti tagikua.|rank=one
mangiparuar laeng ti maysa nga insasao. Ti umuna nga insasao a mabirukan nga adaan ti kangatuan a ranggo ti mausar.|rank=all
mangiparuar kadagiti amin a pateg.|rank=normal
mangiparuar laeng kadagiti insasao iti ranggo a "normal" wenno "preferred".|rank=preferred
mangiparuar kadagiti insasao nga addaan iti ranggo iti "preferred"
|formatting=
(pagpilian) ti nasaysayaat a pormat. Ti kasisigud a pormat ket naipipa a silpo. Dagiti mabalin a pateg ket makita dita baba:|formatting=raw
ID ti banag.|formatting=label
etiketa ti banag.|formatting=sitelink
silpo iti banag ti Wikipedia.|formatting=weblink
pormat ti akin-ruar a silpo [http://example.com example.com]|formatting=pattern
segun ti alagaden a pormat nga agus-usar ti $1 a kas ti parametro a masukatan|formatting=latitude
latitud ti nagsasabtan iti insasao iti pormat a desimal|formatting=longitude
longitud ti nagsasabtan iti insasao iti pormat a desimal|formatting=dimension
pateg ti dimension ti nagsasabtan a kapada ti #dim:D|formatting=year
pormat dagiti petsa a naipakpakita iti "tttt" (2021)- No pateg a kita ti datos, ti kasisigud a pormat ket "aa bbb tttt" (16 Enero 2021)
|separator=
(pagpilian) no adda dagiti nadumaduma a pateg para iti isu met laeng a tagikua, daytoy ket mausar a mangisina kadagiti pateg. Ti kasisigud a pateg ket koma (a kas naipakita iti MediaWiki:Comma-separator) . Para kadagiti bertikal a listaan mabalin nga usaren ti <br /> wenno <li>
|conjunction=
(pagpilian) ti conjunction ket isu ti maudi a panangisina. Ti kasisigud a pateg ket "ken" (a kas naipakita iti MediaWiki:And)
Nota[urnosen ti taudan]
Nasken a saan nga usaren ti modulo a kasla iti daytoy [[commons:Category:{{#invoke:WD3|formatStatements|property=P373|formatting=}}]] gapu ta awan ti pannakaikeddeng nga addaan laeng iti maymaysa nga insasao iti Wikidata para iti naited a tagikua. Imbes ket nga usaren ti {{#invoke:WD3|formatStatements|property=P373|formatting=[[commons:Category:$1|$1]]}} (wenno {{#invoke:WD3|formatStatements|property=P373|formatting=[[commons:Category:$1|$1]]|rank=one}} no kayat laeng ti maysa a silpo).
Dagiti pagarigan[urnosen ti taudan]
Ti artikulo ni Corazon Aquino - d:q1480
- Nalaka a panagusar
- Tagikua ti P19 - lugar a nakaipasngayan: {{#invoke:WD3|formatStatements|property=P19}} mangiparuar ti Paniqui
- Tagikua ti P569 - petsa a naipasngay: {{#invoke:WD3 | formatStatements | property=P569}} mangiparuar ti 25 Enero 1933
- Tagikua ti P20 - lugar a pimmusayan: {{#invoke:WD3|formatStatements|property=P20}} mangiparuar ti Makati
- Tagikua ti P70 - petsa nga ipupusay: {{#invoke:WD3 | formatStatements | property=P70}} mangiparuar ti 1 Agosto 2009
- Tagikua ti P26 - asawa: {{#invoke:WD3|formatStatements|property=P26}} mangiparuar ti Benigno S. Aquino
- Tagikua ti P40 - anak/annak : {{#invoke:WD3|formatStatements|property=P40}} mangiparuar ti Benigno Aquino III ken Kris Aquino
- Tagikua ti P373 - kategoria ti commons: {{#invoke:WD3|formatStatements|property=P373|formatting=[[commons:Category:$1|$1]]}} mangiparuar ti Corazon Aquino
- Tagikua ti P214 - VIAF a panangilasin: {{#invoke:WD3|formatStatements|property=P214|formatting=[http://viaf.org/viaf/$1 $1]}} mangiparuar ti 72191166
Dagiti silpo[urnosen ti taudan]
local p = {}
-- module local variables
local wiki =
{
langcode = mw.language.getContentLanguage().code
}
local res = mw.loadData("Modulo:Wikidata/i18n")
function getClaims(args) -- returns a table of the claims (TODO: matching some conditions given in args)
if not args.property then
return formatError( 'property-param-not-provided' )
end
--Get entity
local itemID = args.item
local entity = nil
local property = string.upper(args.property)
if args.entity and type( args.entity ) == "table" then
entity = args.entity
else
entity = mw.wikibase.getEntityObject(itemID)
end
if not entity or not entity.claims or not entity.claims[property] then
return nil
end
return entity.claims[property]
end
function numOfClaims( claims )
if type(claims) ~= "table" then
return 0
elseif claims == {} then
return 0
else
return #claims
end
end
function getDatavalue(snak, args)
if snak.snaktype ~= 'value' then
return nil
end
local formatting = args.formatting
local datatype = snak.datavalue.type
if datatype == 'wikibase-entityid' then
local entityId = "Q" .. tostring(snak.datavalue.value['numeric-id'])
local label = mw.wikibase.label( entityId )
local sitelink = mw.wikibase.sitelink( entityId )
if formatting == 'raw' then
return entityId
elseif formatting == 'label' then
return ( label or entityId )
elseif formatting == 'sitelink' then
return ( sitelink or 'wikidata:' .. entityId )
else
return formatEntityId( entityId )
end
elseif datatype == 'string' then
local astring = snak.datavalue.value
if formatting == 'weblink' then
return '[' .. astring .. ' ' .. mw.text.split( astring, '//' )[2] .. ']'
elseif mw.ustring.find( (formatting or ''), '$1', 1, true ) then -- formatting = a pattern
return mw.ustring.gsub( formatting, '$1', astring ) .. '' --Hack to get only the first result of the function
else
return astring
end
elseif datatype == 'quantity' then
local entity = mw.wikibase.getEntityObject(args.item)
local result = entity:formatPropertyValues(string.upper(args.property)).value
-- remove thousand separators, bounds and units
--result = mw.ustring.gsub(result, "(%d)%.(%d)", "%1%2")
if formatting == "unit" then
result = mw.ustring.gsub(result, "(%d)±%A+", "%1 ")
else
result = mw.ustring.gsub(result, "(%d)±.*", "%1")
end
result = mw.ustring.gsub(result, "%+", "")
return mw.text.trim(result)
elseif datatype == 'time' then
-- Dates and times are stored in ISO 8601 format
-- check for negative date, ex. "-0027-01-16T00:00:00Z"
local suffix = ""
local timestamp = snak.datavalue.value.time
if string.sub(timestamp, 1, 1) == '-' then
timestamp = '+' .. string.sub(timestamp, 2)
suffix = " B.C."
elseif string.sub(timestamp, 2, 3) == '00' then
suffix = " A.D."
end
local function d(f)
return mw.language.new(wiki.langcode):formatDate(f, timestamp) .. suffix
end
local precision = snak.datavalue.value.precision or 11
local any = tonumber(mw.ustring.match(timestamp, "^\+?%d+"))
local ret = ""
-- precision is 10000 years or more
if precision <= 5 then
local factor = 10 ^ ((5 - precision) + 4)
local y2 = math.ceil(math.abs(any) / factor)
local relative = mw.ustring.gsub(res.i18n.datetime[precision], "$1", tostring(y2))
if suffix == " BC" then
ret = mw.ustring.gsub(res.i18n.datetime.beforenow, "$1", relative)
else
ret = mw.ustring.gsub(res.i18n.datetime.afternow, "$1", relative)
end
-- precision is decades, centuries or millennia
elseif precision == 6 then
ret = mw.ustring.gsub(res.i18n.datetime[6], "$1", tostring(math.floor((math.abs(any) - 1) / 1000) + 1)) .. suffix
elseif precision == 7 then -- segles
ret = mw.ustring.gsub(res.i18n.datetime[7], "$1", tostring(math.floor((math.abs(any) - 1) / 100) + 1)) .. suffix
elseif precision == 8 then
ret = mw.ustring.gsub(res.i18n.datetime[8], "$1", tostring(math.floor(math.abs(any) / 10) * 10)) .. suffix
-- precision is year
elseif formatting == 'year' or precision == 9 then
ret = tostring(any) .. suffix
-- precision is month
elseif precision == 10 then
ret, _ = string.gsub(d("F Y"), " 0+", " ")
else
ret, _ = string.gsub(d("j F Y"), " 0+", " ")
end
return ret
elseif datatype == 'globecoordinate' then
local coord = snak.datavalue.value
local globetable = { Q2 = 'earth', Q111 = 'mars', Q405 = 'moon'}
if formatting == 'latitude' then
return coord.latitude
elseif formatting == 'longitude' then
return coord.longitude
elseif formatting == 'dimension' then
return coord.dimension
else
if coord.globe == '' or coord.globe == nil then
return 'earth'
else
local globenum = mw.text.split( snak.datavalue.value.globe, 'entity/' )[2] -- http://www.wikidata.org/wiki/Q2
if globetable[globenum] then
return globetable[globenum]
else
return globenum
end
end
end
else
return formatError( 'unknown-datavalue-type' )
end
end
function getEntityIdFromValue( value )
if value['entity-type'] == 'item' then
return 'q' .. value['numeric-id']
elseif value['entity-type'] == 'property' then
return 'p' .. value['numeric-id']
else
return formatError( 'unknown-entity-type' )
end
end
function formatError( key )
return '<span class="error">' .. res.i18n.errors[key] .. '</span>'
end
function formatStatement(statement, args)
if not statement.type or statement.type ~= 'statement' then
return formatError( 'unknown-claim-type' )
end
return formatSnak(statement.mainsnak, args)
--TODO reference and qualifiers
end
function formatQualifier ( qualifiers, qualifierProperty, options )
if not qualifiers[qualifierProperty] then
return nil
end
return formatSnak( qualifiers[qualifierProperty][1], options )
end
function formatSnak(snak, args)
if snak.snaktype == 'somevalue' then
return res.i18n['somevalue']
elseif snak.snaktype == 'novalue' then
return res.i18n['novalue']
elseif snak.snaktype == 'value' then
return getDatavalue(snak, args)
else
return formatError( 'unknown-snak-type' )
end
end
function formatEntityId( entityId )
local label = mw.wikibase.label( entityId )
local link = mw.wikibase.sitelink( entityId )
if link then
if label then
return '[[' .. link .. '|' .. label .. ']]'
else
return '[[' .. link .. ']]'
end
else
return '[[wikidata:' .. entityId .. '|' .. (label or entityId) .. ']]'
end
end
-- Return the site link (for the current site) for a given data item.
function p.getSiteLink( frame )
if frame.args[1] == nil then
entity = mw.wikibase.getEntityObject()
if not entity then
return nil
end
id = entity.id
else
id = frame.args[1]
end
return mw.wikibase.sitelink( id )
end
-- Return the statements with a format
function p.formatStatements(frame) -- Format statement and concat them cleanly
local args = frame.args
--If a value is already set, use it
if args.value and args.value ~= '' then
return args.value
end
local rawStatements = getClaims( args )
if not rawStatements or numOfClaims(rawStatements) == 0 then
return nil
end
--Format statement and concat them cleanly
local formattedStatements = {}
for i, statement in pairs( rawStatements ) do
if args.rank == 'one' then
return formatStatement( statement, args ) --Output only one value
elseif args.rank == 'preferred' then --This should be set as the default case
if #rawStatements == 1 then
return formatStatement( statement, args ) --Output the only value
else
if statement.rank == 'preferred' then
table.insert( formattedStatements, formatStatement( statement, args ) ) --Output only the preferred values
end
end
else
table.insert( formattedStatements, formatStatement( statement, args ) )
end
end
return mw.text.listToText( formattedStatements, args.separator, args.conjunction )
end
--Return the qualifiers with a format
function p.formatQualifiers(frame)
local args = frame.args
--If a value is already set, use it
if args.value and args.value ~= '' then
return args.value
end
--Required parameters
local property = string.upper(args.property)
local qualifierProperty = string.upper(args.qualifierProperty)
if not property then
return formatError( 'property-param-not-provided' )
end
if not qualifierProperty then
return formatError( 'qualifier-property-param-not-provided' )
end
--Get statements
local rawStatements = getClaims( args )
if not rawStatements or numOfClaims(rawStatements) == 0 then
return nil
end
--Format statement and concat them cleanly
local formattedQualifiers = {}
for i, statement in pairs( rawStatements ) do
if not statement.qualifiers or not statement.qualifiers[qualifierProperty] then
table.insert(formattedQualifiers, "")
elseif args.rank == 'one' then
return formatQualifier( statement.qualifiers, qualifierProperty, args )
elseif args.rank == 'preferred' then --This should be set as the default case
if #rawStatements == 1 then
return formatQualifier( statement.qualifiers, qualifierProperty, args ) --Output the only value
else
if statement.rank == 'preferred' then
table.insert( formattedQualifiers, formatQualifier( statement.qualifiers, qualifierProperty, args ) ) --Output only the preferred values
end
end
else
table.insert( formattedQualifiers, formatQualifier( statement.qualifiers, qualifierProperty, args ) )
end
end
return mw.text.listToText( formattedQualifiers, args.separator, args.conjunction )
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
local ret = table.concat(out, "<br> ")
if #ret == 0 then
ret = "Invalid TA"
end
return ret
end
-- look into entity object
function p.ViewSomething(frame)
local data = mw.wikibase.getEntityObject()
if not data then
return nil
end
local f = frame.args[1] and frame or frame:getParent()
local i = 1
while true do
local index = f.args[i]
if not index then
if type(data) == "table" then
return mw.text.jsonEncode(data, mw.text.JSON_PRESERVE_KEYS + mw.text.JSON_PRETTY)
else
return tostring(data)
end
end
data = data[index] or data[tonumber(index)]
if not data then
return
end
i = i + 1
end
end
-- Dump data tree structure
-- From pl:Module:Wikidane, by User:Paweł Ziemian
function p.Dump(frame)
local data = mw.wikibase.getEntityObject()
if not data then
return res.i18n.warnDump
end
local f = frame.args[1] and frame or frame:getParent()
local i = 1
while true do
local index = f.args[i]
if not index then
return "<pre>" .. mw.dumpObject(data) .. "</pre>" .. res.i18n.warnDump
end
data = data[index] or data[tonumber(index)]
if not data then
return res.i18n.warnDump
end
i = i + 1
end
end
-- Look into entity object
-- From pl:Module:Wikidane, function V, by User:Paweł Ziemian
function p.getEntityFromTree(frame)
local data = mw.wikibase.getEntityObject()
if not data then
return nil
end
local f = frame.args[1] and frame or frame:getParent()
local i = 1
while true do
local index = f.args[i]
if not index then
return tostring(data)
end
data = data[index] or data[tonumber(index)]
if not data then
return
end
i = i + 1
end
end
return p