Modul:Wikidata
Hente formaterte data fra Wikidata
Bruk:
{{#invoke:Wikidata|formatStatements|property=p17}}
Parametere
property = Påkrevd parameter som beskriver hvilken egenskap som skal hentes.
entityId = ID-nummeret til den artikkelen du vil hente property for. (fungerer ikke siden funksjonen mw.wikibase.getEntity( id ) ikke er aktivert ennå)
enbarten = ?Når bare én verdi skal hentes, for eksempel for å hente et bilde av flagger og ikke alle de andre objektene. enbarten=0 gir det første objektet, men andre verdier er mulige så lenge tilsvarend objekt fins på Wikidata.
separator = For å bruke et annet skilletegn enn "," for eksempel separator=<br/>
label = For å angi forskjellig lenketekst. For eksempel: [[Blekinge läns våpen|våpen]] fra label=våpen
Hente nettstedlenke
For å vise tittelen på artikkelen på f.eks. russisk Wikipedia:
{{#invoke:Wikidata|sitelink|ruwiki}}
Commons-kategori
For å vise tittelen på lenket Commons-kategori (P373) eller Commons-nettstedlenke (sitelink):
{{#invoke:Wikidata|commonscat}}
Kan også formateres med en mal:
{{#invoke:Wikidata|commonscat|template=Mal:MinMal}}
Brukes av Mal:Commonscat
local i18n = { ["errors"] = { ["property-param-not-provided"] = "Parameter for egenskap mangler.", ["entity-not-found"] = "Objektet blr ikke funnet.", ["unknown-claim-type"] = "Ukjent claim-type.", ["unknown-snak-type"] = "Ukjent snak-type.", ["unknown-datavalue-type"] = "Ukjent type dataverdi.", ["unknown-entity-type"] = "Ukjent objekt-type.", ["unknown-value-module"] = "Du må justere både verdi-parameterne og verdien i funksjonsmodulen.", ["value-module-not-found"] = "Modulen som verdi-modulen pekte på ble ikke funnet.", ["value-function-not-found"] = "Funksjonen som verdi-funksjonen pekte på ble ikke funnet." }, ["somevalue"] = "''ukjent verdi''", ["novalue"] = "''uten verdi''"} function getEntityFromId( id ) if id then return mw.wikibase.getEntityObject( id ) end return mw.wikibase.getEntityObject()end function getEntityIdFromValue( value ) local prefix = '' if value['entity-type'] == 'item' then prefix = 'Q' elseif value['entity-type'] == 'property' then prefix = 'P' else return formatError( 'unknown-entity-type' ) end return prefix .. value['numeric-id']end function formatError( key ) return '<span class="error">' .. i18n.errors[key] .. '</span>'end function formatStatements( options )local default = options.default or '' if options.property == 'precisión' or options.property == 'breddegrad' or options.property == 'lengdegrad' then propiedad = 'P625' else propiedad = options.property end if not propiedad then return formatError( 'property-param-not-provided' ) end --Get entity local entity = getEntityFromId( options.entityId ) if not entity then return default end if (entity.claims == nil) or (not entity.claims[propiedad]) then return default end --Declaración de formato y concatenado limpio local propiedad = {} if options.property == 'breddegrad' or options.property == 'lengdegrad' or options.property == 'precisión' then propiedad = 'P625' else propiedad = options.property end local cadena = entity.claims[propiedad] local s = {} -- Esto es para que el índice 0 pase a ser 1. Necesario para que todo quede en el mismo orden que en ítem for k, v in pairs(cadena) do s[k+1] = v end local formattedStatements = {} for i, statement in pairs(s) do table.insert( formattedStatements, formatStatement( statement, options ) ) end return mw.text.listToText( formattedStatements, options.separator, options.conjunction )end function formatStatement( statement, options ) if not statement.type or statement.type ~= 'statement' then return formatError( 'unknown-claim-type' ) end return formatSnak( statement.mainsnak, options ) --TODO reference and qualifiersend function formatSnak( snak, options ) if snak.snaktype == 'somevalue' then return i18n['somevalue'] elseif snak.snaktype == 'novalue' then return i18n['novalue'] elseif snak.snaktype == 'value' then return formatDatavalue( snak.datavalue, options ) else return formatError( 'unknown-snak-type' ) endend function formatDatavalue( datavalue, options ) --Use the customize handler if provided if options['value-module'] or options['value-function'] then if not options['value-module'] or not options['value-function'] then return formatError( 'unknown-value-module' ) end local formatter = require ('Module:' .. options['value-module']) if formatter == nil then return formatError( 'value-module-not-found' ) end local fun = formatter[options['value-function']] if fun == nil then return formatError( 'value-function-not-found' ) end return fun( datavalue.value, options ) end --Default formatters if options.property == 'breddegrad' then --Para las coordenadas return datavalue.value['breddegrad'] elseif options.property == 'lengdegrad' then return datavalue.value['lengdegrad'] elseif options.property == 'precisión' then return datavalue.value['precision'] elseif datavalue.type == 'wikibase-entityid' then return formatEntityId( getEntityIdFromValue( datavalue.value ), options ) elseif datavalue.type == 'string' then return datavalue.value --TODO ids + media elseif datavalue.value['breddegrad'] and datavalue.value['lengdegrad'] then return marco:preprocess('{{coord|' .. datavalue.value['breddegrad'] .. '|' .. datavalue.value['lengdegrad'] .. '|type:' .. options.tipo .. '|display=' .. options.display ..'|format=' .. options.formato..'}}') else return formatError( 'unknown-datavalue-type' ) endend function formatEntityId( entityId, options ) local label = mw.wikibase.label( entityId ) local link = mw.wikibase.sitelink( entityId ) if link and options['link'] ~= 'no' then if label then return '[[' .. link .. '|' .. label .. ']]' else return '[[' .. link .. ']]' end elseif label then return label --TODO what if no links and label + fallback language? else return '' endend local p = {} function p.formatStatements( frame ) marco = frame local args = frame.args local valorwikidata = formatStatements( frame.args ) if args.prioridad == 'sí' and valorwikidata ~= '' then return formatStatements( frame.args ) elseif args.value and args.value ~= '' then return args.value else return formatStatements( frame.args ) end end-- Returns the page title of a linked page in another Wikimedia site as a string-- If no wikidata page exists, or if no link exists to the given project, -- the function returns nilfunction p.sitelink(frame)-- First argument is the site code ('enwiki', 'commonswiki', ...)local siteId = frame.args[1]-- Get Wikibase item connected to the current pagelocal entity = mw.wikibase.getEntityObject()if not entity thenreturn nil -- no wikidata page for current itemend-- Get title the item is linked with in the given sitereturn entity:getSitelink(siteId)end-- Returns Commons category, if anyfunction p.commonscat(frame)templateArgs = {}-- if a category name is specified explicitly, we use that valueif frame.args[1] and frame.args[1] ~= '' thentemplateArgs[1] = frame.args[1]if frame.args[2] and frame.args[2] ~= '' thentemplateArgs[2] = frame.args[2]end-- otherwise, we fetch the category name from Wikidataelse-- Get Wikibase item connected to the current pagelocal entity = mw.wikibase.getEntityObject()if not entity thenreturn '[[Kategori:Sider med Commonscat som mangler Wikidata-side]]'end -- no wikidata page for current item-- Get formatted valuelocal value = entity:formatPropertyValues( 'P373' )['value']if value == '' then -- property not set-- Commons categories may also be stored as sitelinksvalue = p.sitelink({args = {'commonswiki'}})if value and mw.ustring.sub(value, 0, 9) == 'Category:' thenvalue = mw.ustring.sub(value, 10) -- remove namespaceelsevalue = '' -- not a category, probably a gallery pageendendif value == '' then -- neither property nor sitelink availablereturn '[[Kategori:Sider med Commonscat der Wikidata-siden mangler Commons-lenke]]'endtemplateArgs[1] = valueend-- Return formatted value using a templateif frame.args['template'] thenreturn frame:expandTemplate{title = frame.args['template'],args = templateArgs}end-- or just return plain category namereturn templateArgs[1]end-- This is used to get a value, or a comma separated list of them if multiple values existp.hentVerdi = 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" thenlocal entity = mw.wikibase.getEntityObject()local claimsif entity and entity.claims thenclaims = entity.claims[propertyID]endif claims then-- if wiki-linked value output as link if possibleif (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") thenlocal out = {}for k, v in pairs(claims) dolocal 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"] endif sitelink thenout[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"elseout[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<abbr title='Article is not yet available in this wiki'>[*]</abbr>"endendreturn table.concat(out, ", ")else-- just return best vakuesreturn entity:formatPropertyValues(propertyID).valueendelsereturn ""endelsereturn input_parmendendp.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" thenlocal entity = mw.wikibase.getEntityObject()if entity.claims[propertyID] ~= nil thenlocal out = {}for k, v in pairs(entity.claims[propertyID]) dofor k2, v2 in pairs(v.qualifiers[qualifierID]) doif v2.snaktype == 'value' thenif (mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"])) thenout[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"]) .. "]]"elseout[#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>"endendendendreturn table.concat(out, ", ")elsereturn ""endelsereturn input_parmendend-- This is used to get a value like 'male' (for property p21) which won't be linked and numbers without the thousand separatorsp.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" thenlocal entity = mw.wikibase.getEntityObject()local claimsif entity then claims = entity.claims[propertyID] endif claims thenlocal result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value-- if number type: remove thousand separatorsif (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") thenresult = mw.ustring.gsub(result, "(%d),(%d)", "%1%2")endreturn resultelsereturn ""endelsereturn input_parmendendp.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" thenlocal entity = mw.wikibase.getEntityObject()if entity.claims[propertyID] ~= nil thenlocal out = {}for k, v in pairs(entity.claims[propertyID]) dofor k2, v2 in pairs(v.qualifiers[qualifierID]) doif v2.snaktype == 'value' thenif v2.datavalue.value["numeric-id"] thenout[#out + 1] = mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"])elseout[#out + 1] = v2.datavalue.valueendendendendlocal ret = table.concat(out, ", ")return string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2)elsereturn ""endelsereturn input_parmendendp.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" thenlocal entity = mw.wikibase.getEntityObject()if entity.claims[propertyID] ~= nil thenlocal out = {}for k, v in pairs(entity.claims[propertyID]) dofor k2, v2 in pairs(v.qualifiers[qualifierID]) doif v2.snaktype == 'value' thenif (mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"])) thenout[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"]) .. "]]"elseout[#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>"endendendendreturn table.concat(out, ", ")elsereturn ""endelsereturn input_parmendend-- This is used to get a value like 'male' (for property p21) which won't be linked and numbers without the thousand separatorsp.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" thenlocal entity = mw.wikibase.getEntityObject()local claimsif entity then claims = entity.claims[propertyID] endif claims thenlocal result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value-- if number type: remove thousand separatorsif (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") thenresult = mw.ustring.gsub(result, "(%d),(%d)", "%1%2")endreturn resultelsereturn ""endelsereturn input_parmendendp.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" thenlocal entity = mw.wikibase.getEntityObject()if entity.claims[propertyID] ~= nil thenlocal out = {}for k, v in pairs(entity.claims[propertyID]) dofor k2, v2 in pairs(v.qualifiers[qualifierID]) doif v2.snaktype == 'value' thenif v2.datavalue.value["numeric-id"] thenout[#out + 1] = mw.wikibase.label("Q" .. v2.datavalue.value["numeric-id"])elseout[#out + 1] = v2.datavalue.valueendendendendlocal ret = table.concat(out, ", ")return string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2)elsereturn ""endelsereturn input_parmendendreturn p