Saltar para o conteúdo

Módulo:WikidataIB: diferenças entre revisões

Origem: Wikipédia, a enciclopédia livre.
Conteúdo apagado Conteúdo adicionado
Consertando erro na devolução do rótulo feminino
adicionando categorização para monitorar os artigos em que o Wikidata está sendo usado
Linha 594: Linha 594:
end
end
end
end

-- if there's anything to return, then return a list
-- if there's anything to return, then return a list
-- comma-separated by default, but may be specified by the sep parameter
-- comma-separated by default, but may be specified by the sep parameter
-- optionally specify a hlist or ubl
-- optionally specify a hlist or ubl
if #out > 0 then
if #out > 0 then
local resp
if sorted then table.sort(out) end
if sorted then table.sort(out) end
if list == "hlist" then
if list == "hlist" then
return frame:expandTemplate{title = 'Hlist', args = out}
resp = frame:expandTemplate{title = 'Hlist', args = out}
elseif list == "ubl" then
elseif list == "ubl" then
return frame:expandTemplate{title = 'Unbulleted list', args = out}
resp = frame:expandTemplate{title = 'Unbulleted list', args = out}
else
else
if noic then
if noic then
return table.concat(out, separator)
resp = table.concat(out, separator)
else
else
return table.concat(out, separator) .. icon
resp = table.concat(out, separator) .. icon
end
end
end
end
-- adicionar categoria para saber os artigos onde o Wikidata está sendo usado
return resp .. '[[Categoria:!Artigos que usam dados do Wikidata em infobox]]'
else
else
return nil -- no items had valid reference
return nil -- no items had valid reference

Revisão das 19h20min de 6 de maio de 2018

Documentação do módulo[ver] [editar] [histórico] [purgar]

Este módulo foi projetado especificamente para implementar um mecanismo que move o controle de se os valores do Wikidata são usados em uma infobox do programador da predefinição no nível de design da infobox para o editor no nível de artigo. Ele só deve ser usado dentro de uma infobox, ou de dentro de uma predefinição.

Uso

O módulo provê essas chamadas especificamente para o uso em infoboxes no presente:

  1. getValue
  2. getPreferredValue chamada principal, usada para obter o valor(es) de uma dada propriedade
  3. getQualifierValue - dado: (1) uma propriedade; (2) seu valor; (3) o IDpropriedade do qualificador, retorna valores que igualam
  4. getValueByQual obtém o valor de uma propriedade que tem um qualificador com um dado valor de entidade
  5. getValueByLang obtém o valor de uma propriedade que tem um qualificador P407 ("linguagem de trabalho ou nome") cujo valor tem o código de linguagem dado
  6. getValueByRefSource obtém o valor de uma propriedade que tem uma referência "indicada em" (P248) cujo valor tem o ID-entidade indicado
  7. getPropOfProp se o(S) Valor (s) do prop1 são do tipo "wikibase-item" então ele retorna o(S) Valor (s) do prp2 de cada um desses itens wikibase
  8. getAwardCat se o item tem valores de P166 (prêmio recebido), em seguida, ele examina cada um dos prêmios para P2517 (categoria de destinatários do prêmio) e retorna a categoria correspondente, com o item P734 (nome de família) como chave de ordenação, ou nenhum tipo de chave se não existir nenhum nome de família
  9. getIntersectCat para cada valor do prop1 ele obtém a categoria principal do valor e então cada valor do prop2, então ele retorna todas as categorias que representam a interseção dessas propriedades
  10. getSumOfParts verifica a propriedade 'tem parte' (P527) para valores correspondentes a uma lista, se os valores correspondentes tiverem um qualificador 'quantidade' (P1114), essas quantidades são somadas e devolvidas (mas o zero devolve zero)
  11. getCoords - obtém as coordenadas e passa-as através do modelo:Coord

A chamada obsoleta getSourcedValue foi removida, pois agora é redundante para getValue que pode fazer o mesmo trabalho usando o parâmetro |onlysourced=true (que está definido por padrão). A chamada obsoleta getPreferredValue ainda é mantida, mas deve substituída por getValue|rank=best.

Existem também essas chamadas úteis:

  1. getLink se houver um link do site para um artigo no Wiki local, ele retorna um link para o artigo com a etiqueta Wikidata como o texto apresentado. Se não houver nenhum link do site, ele retorna o rótulo como texto simples. Se não houver nenhuma etiqueta na língua local, devolve o ID da entidade
  2. getAT (Article Title) Se houver um link do site para um artigo no Wiki local, ele retorna o link do site como texto simples, caso contrário nada
  3. getSiteLink obtém o link de texto simples para um artigo num determinado wiki
  4. getLabel devolve a etiqueta Wikidata para a língua local como texto simples. Se não houver nenhuma etiqueta na língua local, devolve o ID da entidade
  5. getAllLabels obtém o conjunto de legendas e formata-o para mostrar como wikitext
  6. labelorid devolve a legenda com toda o wikitexto removido, ou o ID da entidade se não houver legenda
  7. getDescription devolve a descrição do artigo para a entidade Wikidata se o parâmetro local for "Wikidata".
  8. getAllDescriptions obtém o conjunto de descrições e formata-o para mostrar como wikitext
  9. getAliases devolve os pseudónimos da entidade na língua actual ou dada
  10. getAllAliases obtém o conjunto de nomes alternativos e formata-o para mostrar como wikitext
  11. pageId devolve o id da página Wikidata ligada (ID de entidade, número Q) da página actual
  12. formatDate leva uma datetime do formato usual de mw.wikibase.entidade: formatpropertytyvalues e formata-o de acordo com os parâmetros df (formato de data) e a.C.
  13. formatNumber formata um número de acordo com o código da língua fornecida
  14. checkBlacklist devolve true se o campo "não" estiver na lista negra (isto é, permitido))
  15. emptyor devolve zero se o parâmetro for apenas pontuação, espaços em branco ou marcas de html, caso contrário devolve o argumento inalterado
  16. getLang devolve o código da língua MediaWiki ou o nome completo da língua do conteúdo actual
  17. getItemLangCode procura por país (P17), depois pela língua oficial desse país (P37), e devolve o código da língua (P424)
  18. findLanguage devolve (1) a língua indicada se for válida; ou (2) a língua definida do utilizador; ou (3) a língua do wiki actual
  19. getQid devolve (1) o ID da entidade, se for fornecido; ou (2) O ID da entidade do "tópico principal da categoria (P 301)"; ou (3) O ID da entidade associado à página actual; ou (4) nada
  20. followQid dada uma lista de propriedades, procura por cada propriedade por sua vez e devolve o ID da entidade do primeiro valor correspondente (opcionalmente, devolve todos os IDs da entidade que correspondem)
  21. getGlobe devolve o ID de entidade do globo usado em P625 (localização das coordenadas), ou zero se não existir um
  22. getCommonsLink retorna um dos seguintes, por ordem de preferência: o link do site Commons do item linked Wikidata; o sitelink Commons da principal categoria do tópico linked Wikidata item;
  23. siteID retorna a raiz do globalSiteID, por exemplo "en" para "enwiki", "enwikisource", "en-gb", etc.
  24. projID mesmo que siteID
  25. location pesquisa a partir da localização atual para cima ao longo da cadeia de locais de nível superior, retornando cada um até chegar a um país
  26. examine devolve um dump formatado da propriedade dada
  27. url2 toma um parâmetro que é um url apropriado e o formata para uso em um infobox; Aceita a sua própria saída como entrada
  28. getWebsite obtém o sítio Web Oficial (P856) e formata-o para utilização numa caixa de infocomment
  29. checkvalue procura através de uma propriedade para um dado ID de entidade como o seu valor e devolve esse ID de entidade, se for encontrado; caso contrário, nada
  30. checkValidity devolve se o primeiro parâmetro sem nome representa um ID de entidade válido
  31. showNoLinks mostra os títulos dos artigos que não devem ser ligados

Exemplo de chamadas:

{{#invoke:WikidataIB |getValue |<PropertyID> |name=<fieldname> |suppressfields=<list of fields which will never display> |fetchwikidata=<list of fields to fetch values from Wikidata> |onlysourced=<yes/no> |noicon=<yes/no> |df=<dmy/mdy/y> |bc=<BC/BCE> |qual=<ALL/DATES/P999> |list=<ubl/hlist/prose> |linked=<yes/no> |<local parameter>}}
{{#invoke:WikidataIB |getCoords |name=<fieldname> |suppressfields=<list of fields which will never display> |fetchwikidata=<list of fields to fetch values from Wikidata> |<local parameter>}}
{{#invoke:WikidataIB |getQualifierValue |<PropertyID> |pval=<ID of target value for the property> |qual=<qualifier ID for that target value> |name=<fieldname> |suppressfields=<list of fields which will never display> |fetchwikidata=<list of fields to fetch values from Wikidata> |onlysourced=<yes/no>}}
{{#invoke:WikidataIB |getValueByQual |<PropertyID> |qualID=<qualifier property ID to match> |qvalue=<QID of target value for the qualifier property> |name=<fieldname> |suppressfields=<list of fields which will never display> |fetchwikidata=<list of fields to fetch values from Wikidata> |onlysourced=<yes/no>}}
{{#invoke:WikidataIB |getValueByLang |<PropertyID> |lang=<language code to match> |name=<fieldname> |suppressfields=<list of fields which will never display> |fetchwikidata=<list of fields to fetch values from Wikidata> |onlysourced=<yes/no>}}


Função getValue

Parameters to getValue

Nome Alias Função Padrão
(primeiro sem nome) 1 O ID da propriedade cujos valores são devolvidos. Necessário.
(segundo sem nome) 2 Um valor fornecido localmente que, se não estiver vazio, será devolvido em preferência ao valor no Wikidata. vazio
qid O número Q (ID da entidade) da entidade a que a propriedade pertence. Se não for fornecido ou vazio, é por omissão o item associado Wikidata da página actual-usamw.wikibase.getEntityIdForCurrentPage(). Id do item para a página atual
eid Uma alternativa para qid. Excepto para eid, todos os parâmetros para o getValue tratam o zero e o texto vazio o mesmo. Então, ajuste |x= dá o mesmo resultado que omitir o parâmetro. No entanto, para fornecer alguma compatibilidade com outros módulos, utilizando
 Nota: Para eid, veja [[]].
devolve sempre um texto vazio, omitindo eid permite qid funcione normalmente.
Id do item para a página atual
rank [Insensível à capitalização] quando definido como best, devolve os valores preferidos se estiverem presentes, caso contrário devolve os valores normais. Quando definido como best devolve os valores preferidos. Quando configurado para normal, devolve os valores normais. Quando configurado para deprecated devolve valores desactualizados. Qualquer valor de parâmetro começando com "b" é "best"; começando com "p" é "prefered"; começando com "n" é "normal"; começando com "d" é obsoleto. Vários valores são permitidos: "p n d" retornaria todas as fileiras. O "best" sobrepõe-se às outras bandeiras. Outros valores são ignorados e se não for solicitada nenhuma classificação, preferidos e normais são devolvidos. preferred e normal
qual A lista separada de pontuação de IDs de propriedades de qualificadores que tem que ser devolvidos em parênteses depois da propriedade. Configuracao qual=ALL devolve todos os qualificadores. Configuração qual=DATES devolve data inicial (P580) e data final (P582) com separador de data. none
qualsonly qo Um booleano que permite a exibição de apenas o(s) qualificador (es), sem o valor da propriedade ou parênteses. Valores no, false e 0 são todos falsos; qualquer outra é true. false
qualsonly qo Um booleano que permite a exibição de apenas o(s) qualificador (es), sem o valor da propriedade ou parênteses. Valores no, false e 0 são todos falsos; qualquer outra é true. false
fetchwikidata fwd Lista de campos para buscar valores de. ALL buscas todos os campos. Um valor de NONE ou vazio ou omitir o parâmetro não busca campos. none
suppressfields spf Lista de campos que nunca irão aparecer. Isso mesmo forçará um valor local no campo a não exibir. none
name Nome do campo. Quando informa a info caixa, este é o nome que fetchwikidata e suppressfields vai reconhecer. Requirido se fetchwikidata ou suppressfields é específicado (excepto quando fetchwikidata=ALL). nil
onlysourced osd Um booleano que irá filtrar os valores do Wikidata que não são obtidos ou que só são obtidos na Wikipédia. Valores no, false e 0 são todos false; qualquer outro é true. true
df Formato de data: pode ser dma (dia antes de mês) ou mda (mês antes de dia) ou a (apenas ano). dmy
qdf Qualificadores de formato de data. Se omitido, revere para parâmetro padrão df, ou "y" ou "a" se df também é omitido. valor de df ou "y" ou "a"
bc Formato de sufixos a.C. ou d.C. (BC/BCE) para datas. BCE
plaindate pd Texto para modificar a formatação das datas. A configuração "true"/"yes"/"1" desactiva a adição de "circunstâncias de fontes" (P 1480) e quaisquer ligações. Definir "adj" faz o mesmo, mas usa a forma adjetiva da data. false
linked Um booleano que permite o link para uma página local através de seu link do site no Wikidata. Valores no, false e 0 são todos false; qualquer outro é true. true
displaytext dt Um texto que se sobrepõe ao texto apresentado de um item ligado, se não estiver vazio. empty
shortname sn Um booleano que permite o uso de Nome curto (p 1813) em vez de etiqueta para um item ligado. Valores no, false e 0 são todos false; quaquer outro é true. false
uselabel uselbl Um booleano que força a exibição do rótulo em vez do link do site desambiguado para um item ligado. As etiquetas são muito mais propensas ao vandalismo que os links do site. Valores no, false e 0 são todos false; qualquer outro é true. false
wdlinks wdl Um booleano que permite a exibição de links para Wikidata quando nenhum artigo local existe. Valores no, false e 0 são todos false; qualquer outro é true. false
unitabbr uabbr Um booleano que permite a exibição de abreviações de unidade para unidades comuns. Valores no, false e 0 são todos false; qualquer outro é true. false
convert conv Um booleano que permite a passagem de quantidades para Predefinição:Cvt. Valores no, false e 0são todos false; qualquer outro é true. false
showunits su Um booleano que permite a exibição de unidades para quantidades. Valores no, false e 0são todos false; qualquer outro é true. true
scale Uma cadeia de texto que configura a escala de formato das quantidades. Valores são: "a"=automático; "0"=sem escala; "3"=milhar; "6"=milhão; "9"=bilhãi; "12"=trilhão. 0
  • getValue pode também pegar um parâmetro |qid= que é o ID do Wikidata para o artigo. Isto não será normalmente usado, mas está disponível para teste, embora faça a chamada cara.
  • A propriedade a ser retornada é passada na primeira propriedade não nomeada.
  • O nome do campo que a função é chamada é passada no parâmetro |name=, que é primeiramente checado em uma blacklist de campos que não devem ser mostradas nunca, (i.e. a chamada retorna nil em todas as circunstâncias). Se o campo não está na blacklist, é então checado contra uma whitelist. Se o nome do campo concorda, a chamada retornará qualquer valor fornecido localmente se é fornecido como segundo parâmetro não nomeado, ou o valor do Wikidata caso contrário.
  • O nome é compulsório quando a blacklist ou a whitelist é usada, então o módulo retorna nil se não é fornecido.
  • A blacklist é passada no parâmetro |suppressfields=
  • A whitelist é passada no parâmetro |fetchwikidata=

A função getValue aceitará um parâmetro booleano |onlysourced= que irá suprimir retornos dos valores do Wikidata que estão sem fontes ou somente referenciados a Wikipédia. A ausência do parâmetro, um parâmetro |onlysourced vazio e uma string vazia ("") têm como default para verdadeiro (i.e. somente os valores referenciados são retornados). Os valores no, false e 0 são tratados como falsos (i.e. todos os valores são retornados); qualquer outro valor é verdadeiro (embora |onlysourced=yes seja recomendado para legibilidade).

A função getValue aceitará um parâmetro booleano noicon que suprimirá o ícone "edite no Wikidata" para quando o valor retornado for processado futuramente pela infobox (e.g. uma url). A ausência do parâmetro ou um parâmetro |noicon vazio default para falso (i.e. o parâmetro é adicionado). A string vazia ("") e os valores no, false e 0 são tratados como falsos; qualquer outro valor é verdadeiro (embora |noicon=true seja recomendado para legibilidade).

Em ordem de lidar com o requerimento para datas em my, dmy ou y, getValue aceita um parâmetro |df= que pode pegar valores "dmy", "my" ou "y" - default é "dmy".

Se um artigo exigir o sufixo a.C., getValue retornará a data com esse sufixo;

Em uma invocação do módulo para extrair datas, getValue aceita um parâmetro |idioma= que pode receber o valor "pt" para formatar a data de acordo com o português europeu (única diferença se dá nas datas envolvendo o primeiro dia do mês. Por exemplo: 1º de maio de 2017 (em português brasileiro) e 1 de maio de 2017 (em português europeu)).

  • A propriedade a ser retornada é passada na primeira propriedade não nomeada.

A função getValue aceitará um especificador de uso de unidade de medida (ao invés da unidade por extenso) na terceira propriedade não nomeada. A unidade de medida será exibida somente se o valor passado for unidade

Função getPreferredValue

A função getPreferredValue trabalha exatamente como getValue, tomando os mesmos parâmetros, mas se qualquer um dos valores para a propriedade tem um rank set preferido, ela retornará esses valores.

Exemplo

Obtendo o(s) nome(s) da criador (P170) de Corisca e Sátiro (Q29016906):

Função getSourcedValue

  • getSourcedValue trabalha exatamente como getValue, mas somente retorna valores que tenham referência a algo mais do que a Wikipedia. Não há garantias de fontes confiáveis, mas ajuda separar o joio do trigo.

Exemplo de getSourcedValue

De Richard Burton (Q151973):

Burton correntemente tem quatro valores para ocupação (P106) no Wikidata:

Usando getValue em Richard Burton:

Usando getSourcedValue em Richard Burton:

  • {{#invoke:WikidataIB |getSourcedValue |P106 |name=occupation |fetchwikidata=ALL}} ->

Função getCoords

  • getCoords pode também pegar um parâmetro |qid= que é o ID do Wikidata para o artigo. Isto não será normalmente usado, mas está disponível para testes, embora faça a chamada cara.
  • As coordenadas do Wikidata são passadas a Predefinição:Coord que retorna o display como se fosse chamado manualmente.
  • O nome do campo que a função é chamada é passada no parâmetro |name, que é primeiramente checado em uma blacklist de campos que não devem ser mostradas nunca, (i.e. a chamada retorna nil em todas as circunstâncias). Se o campo não está na blacklist, é então checado contra uma whitelist. Se o nome do campo concorda, a chamada retornará qualquer valor fornecido localmente se é fornecido como segundo parâmetro não nomeado, ou o valor do Wikidata caso contrário.
  • O nome é compulsório quando a blacklist ou a whitelist é usada, então o módulo retorna nil se não é fornecido.
  • A blacklist é passada no parâmetro |suppressfields=
  • A whitelist é passada no parâmetro |fetchwikidata=

Função getQualifierValue

O getQualifierValue é para usar quando queremos concordar o valor de um qualificador. Nós precisamos saber a propriedade e o valor da propriedade que a qualifica. Os parâmetros são:

  • A propriedade ID passada no parâmetro não nomeado (ou |1)
  • O valor alvo para esta propriedade em |pval
  • O ID qualificador para o valor alvo em |qual
  • O nome do campo onde é chamada para implementar "whitelisting" e "blacklisting" da propriedade nele
  • A lista de campos para concordância ("whitelist") em |fetchwikidata= - aceita |fetchwikidata=ALL para concordar todos os campos
  • Lista opcional de campos para não serem exibidos ("blacklist") em |suppressfields=
  • booleano opcional para especificar se apenas valores referenciados da propriedade são retornados (default para "no") em |onlysourced=
  • Item ID opcional para acesso arbitrário (chamada carísssima!) em |qid=

Exemplo de getQualifierValue

Em Telescópio do Polo Sul (Q1513315) existe uma propriedade evento notável (P793), que tem um valor construção civil (Q385378). Tem dois qualificadores, data inicial (P580) e data final (P582). Para obter a data de início:

  • {{#invoke:WikidataIB |getQualifierValue |P793 |pval=Q385378 |qual=P580 |name=xyz |fetchwikidata=ALL }}

Em Telescópio do Polo Sul retorna:

  • novembro 2006

Função getLink

getLink retorna o rótulo para um Qid ligado a wiki para o artigo local (se o artigo existe).

Função getLabel

getLabel retorna o rótulo para uma Qid. Se o rótulo não existe, retorna o Qid. Note que este é o rótulo dado para a entrada no Wikidata na mesma linguagem da corrende Wikipédia, se o rótulo existe.

  • {{#invoke:WikidataIB |getLabel |Q29016906}} → Corisca e Sátiro
  • {{#invoke:WikidataIB |getLabel |Q3621491}} → arqueólogo

Função getAT

getAT retorna o título do artigo para uma Qid. Se o título do artigo não existe, não retorna nada. Note que este é o título do artigo na corrente Wikipedia, se o interwiki existe na entrada Wikidata.

  • {{#invoke:WikidataIB |getAT |Q29016906}}
  • {{#invoke:WikidataIB |getAT |Q3621491}}

Função formatDate*

formatDate aceita uma data no formato usual de mw.wikibase.entity:formatPropertyValues, como "1 de agosto de 30 a.C." como parâmetro 1 e o formata de acordo com os parâmetros df e bc.

  • {{#invoke:WikidataIB |formatDate | 1 de agosto de 30 | bc=BC |df=dmy}}1
  • {{#invoke:WikidataIB |formatDate | 1 de agosto de 20 | bc=BCE |df=mdy}}1
  • df = "dmy" / "mdy" / "y" - default é "dmy"
  • bc = "BC" / "BCE" - default é "BCE"

* Não está funcionando normalmente

Adendo sobre o idioma de retorno

Por padrão, os rótulos retornados pelo módulo serão retornados por default na variante brasileira do português. Caso um editor deseje que o rótulo seja retornado em português europeu (essas são, infelizmente, as duas únicas variantes implementadas no Wikidata), ao utilizar qualquer uma das funções apresentadas, basta adicionar |idioma=pt ou |idioma=pt-ao (neste caso, os rótulos serão retornados em português europeu, e as datas serão retornadas no formato angolano) nos parâmetros da função.

Ver também

-- Módulo para experimentar o uso de uma lista de campos proibidos e uma lista de campos permitidos
-- Pode pegar um parâmetro nomeado |qid que é o identificador do artigo no Wikidata. Em geral, seu uso deve ser restrito a páginas de teste e documentações.
-- Os campos colocados na lista de proibições não serão exibidos de forma alguma, ou seja, o módulo deverá retornar nul para esses campos
-- Os campos colocados na lista de permissões terão valores devolvidos sempre que existirem localmente
-- O nome do campo em que esta função é chamada é passado através do parâmetro nomeado |nome
-- O nome é obrigatório somente quando a lista de proibições e/ou a lista de permissões são usadas. O módulo retornará nul caso ele não seja passado
-- A lista de proibições é passada através do parâmetro nomeado |supressfields
-- A lista de permissões é passada através do parâmetro nomeado |fetchwikidata

local p = {}

local wiki =
{
    langcode = mw.language.getContentLanguage().code
}

local i18n =
{
    ["errors"] =
    {
        ["property-not-found"] = "Propriedade não encontrada.",
        ["entity-not-found"] = "Entidade Wikidata desconhecida.",
        ["unknown-claim-type"] = "Tipo claim com valor desconhecido.",
        ["unknown-entity-type"] = "Tipo entity com valor desconhecido.",
        ["qualifier-not-found"] = "Qualificador não encontrado.",
        ["site-not-found"] = "Projeto Wikimedia não encontrado.",
        ["unknown-datetime-format"] = "Formato datatempo desconhecido.",
        ["local-article-not-found"] = "Artigo ainda não se econtra disponível nesta wiki."
    },
    ["editenowikidata"] = "Edite no Wikidata",
    ["filespace"] = "Ficheiro",
    ["months"] =
    {
    	"janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"
    },
    ["ac"] = "a.C.",
    ["century"] = "[[Século $1]]",
    ["centurysemlink"] = "século $1"
}


-------------------------------------------------------------------------------
-- Private functions
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- formatDate pega uma data no formato usual de mw.wikibase.entity:formatPropertyValues
-- como "1 de agosto de 30 aC" como primeiro parâmetro e o formata de acordo com os parâmetros dateformat (formato de data), bc (indicador de era), idioma e datasemlink
-- df = <"dmy"/"mdy"/"y">. O valor padrão é "dmy"
-- bc = <"a.C.">. O valor padrão será "a.C."
-- idioma = <"pt"/"pt-ao"/"">. O valor padrão é "", e significa que a data será exibida no formato português brasileiro. "pt" representa o formato português europeu e "pt-ao" o formato do português angolano.
-- datasemlink = <"sim"/"">. O valor padrão é "", e significa que a data será devolvida em formato wiki.
-- primeiro a versão local
local format_Date = function(frame,datetime, dateformat, bc, idioma, datasemlink)
	local datetime = datetime or "14 de fevereiro 1 August 30 BCE" -- in case of nil value
	-- chop off multiple vales and/or any hours, mins, etc.
	-- keep anything before punctuation - we just want a single date:
	local dateval = string.match( datetime, "[%w ç]+")
	local dateformat = string.lower(dateformat or "dmy") -- default to dmy
	local datasemlink = datasemlink
	
	local c1
	local c2
	if datasemlink == "sim" then
		c1 = ""
		c2 = ""
	else
		c1 = "[["
		c2 = "]]"
	end
	
	local bc = string.upper(bc or "") -- can't use nil for bc
	-- we only want to accept two possibilities: BC or default to BCE
	if bc=="BCE" then
		bc = "&nbsp;" .. i18n["ac"] -- prepend the space. **internationalise later**
	else
		bc = "&nbsp;" .. i18n["ac"]
	end
	local postchrist = true -- start by assuming no BCE
	local dateparts = {}
	for word in string.gmatch(dateval,"([%w-ç]+)") do
		if word == "BC" or word=="BCE" then -- **internationalise later**
			postchrist = false
		else
			-- we'll keep the parts that are not 'BCE' in a table
			dateparts[#dateparts + 1] =  word
		end
	end
	if postchrist then bc = "" end -- set AD dates to no suffstring.("$1","$1",**internationalise later**
	local sep = "&nbsp;de&nbsp;" -- separator is nbsp
	local fdate = table.concat(dateparts, "&nbsp;de&nbsp;") -- formatted date defaults to same order as input
	-- if we have day month year, check dateformat
	if #dateparts == 3 then
		--por padrão dmy
		--se é dia primeiro
		if idioma == "pt" then
			fdate = c1..dateparts[1]..sep..dateparts[2]..c2..sep..c1..dateparts[3]..bc..c2
		elseif idioma =="pt-ao" then
			fdate = c1..dateparts[1]..sep..dateparts[2]:gsub("^%l", string.upper)..c2..sep..c1..dateparts[3]..bc..c2
		else
			if dateparts[1]==tostring(1) then
				fdate = c1..dateparts[1].."º"..sep..dateparts[2]..c2..sep..c1..dateparts[3]..bc..c2
			else
				fdate = c1..dateparts[1]..sep..dateparts[2]..c2..sep..c1..dateparts[3]..bc..c2
			end
		end
		if dateformat == "y" then
			fdate = c1..dateparts[3]..bc..c2
		elseif dateformat == "my" then
			if idioma =="pt-ao" then
				fdate = c1..dateparts[2]:gsub("^%l", string.upper) ..c2.. sep .. c1 .. dateparts[3] ..bc.. c2
			else
				fdate = c1..dateparts[2]:gsub("^%l", string.upper) ..c2.. sep .. c1 .. dateparts[3] ..bc.. c2
			end
		end
	elseif #dateparts == 2 then
		if dateformat == "y" then
			fdate = c1..dateparts[2]..bc..c2
		else
			if idioma == "pt-ao" then
				fdate = c1..dateparts[1]:gsub("^%l", string.upper)..c2..sep..c1..dateparts[2]..bc..c2
			else
				fdate = c1..dateparts[1]..c2..sep..c1..dateparts[2]..bc..c2
			end
		end
	else
		fdate = c1..dateparts[1]..bc..c2
	end
	
	return fdate
end

local format_Date_template = function(frame,datetime, dateformat, bc, idioma)
	local datetime = datetime or "14 de fevereiro"
	local dateval = string.match( datetime, "[%w ç]+")
	local dateformat = string.lower(dateformat or "dmy")
	
	local bc = string.upper(bc or "")
	bc = "-"
	
	local function _numes(mes)
		local result = mes
		for k, v in ipairs(i18n["months"]) do
			if mes == v then
				result = tostring(k)
			end
		end
		return result
	end
	
	local postchrist = true -- start by assuming no BCE
	local dateparts = {}
	for word in string.gmatch(dateval,"([%w-ç]+)") do
		if word == "BC" or word=="BCE" then -- **internationalise later**
			postchrist = false
		else
			-- we'll keep the parts that are not 'BCE' in a table
			dateparts[#dateparts + 1] =  _numes(word)
		end
	end
	if postchrist then bc = " " end
	local sep = "|"
	local fdate = table.concat(dateparts, sep)
	if #dateparts == 3 then
		if dateformat == "y" then
			fdate = sep..sep..mw.text.trim(bc)..dateparts[3]
		elseif dateformat == "my" then
			fdate = sep..dateparts[2]..sep..mw.text.trim(bc)..dateparts[3]
		else
			fdate = dateparts[1]..sep..dateparts[2]..sep..mw.text.trim(bc)..dateparts[3]
		end
	elseif #dateparts == 2 then
		if dateformat == "y" then
			fdate = sep..sep..mw.text.trim(bc)..dateparts[2]
		else
			fdate = sep..dateparts[1]..sep..mw.text.trim(bc)..dateparts[2]
		end
	else
		fdate = sep..sep..mw.text.trim(bc)..dateparts[1]
	end
	
	return fdate
end

-------------------------------------------------------------------------------
-- parseParam takes a (string) parameter, e.g. from the list of frame arguments,
-- and makes "false", "no", and "0" into the (boolean) false
-- it makes the empty string and nil into the (boolean) value passed as default
-- allowing the parameter to be true or false by default.
local parseParam = function(param, default)
	if param and (#param > 0) then
		param = param:lower()
		if (param == "false") or (param == "no") or (param == "não") or (param == "0") then
			return false
		else
			return true
		end
	else
		return default
	end
end

-------------------------------------------------------------------------------
-- The label in a Wikidata item is subject to vulnerabilities 
-- that an attacker might try to exploit.
-- It needs to be 'sanitised' by removing any wikitext before use.
-- If it doesn't exist, just return the id for the item
local labelOrId = function (id)
	local label = mw.wikibase.label(id)
	if label then
		return mw.text.nowiki(label)
	else
		return id
	end
end

-------------------------------------------------------------------------------
-- sourced takes a table representing a statement that may or may not have references
-- it counts how many references are sourced to something not contianing the word "wikipedia"
-- the reference string "ref" is available for debugging
-- it returns a boolean = true if there are any sourced references.
local sourced = function(claim)
	local refs = 0
	if claim.references then
		for kr, vr in pairs(claim.references) do
			local ref = mw.wikibase.renderSnaks(vr.snaks)
			if not ref:find("Wikipedia") then refs = refs + 1 end
		end
	end
	return refs > 0
end

-------------------------------------------------------------------------------
-- parseInput processes the Q-id , the blacklist and the whitelist
-- if an input parameter is supplied, it returns that and ends the call.
-- it returns a boolean indicating whether or not the call should continue
-- and an object containing all of the Wikidata for the Qid supplied or the current page
local parseInput = function(frame, input_parm, property_id)
	-- can take a named parameter |qid which is the Wikidata ID for the article.
	-- This will not normally be used because it's an expensive call.
	local qid = frame.args.qid
	if qid and (#qid == 0) then qid = nil end
	
	-- The blacklist is passed in named parameter |suppressfields
	local blacklist = frame.args.suppressfields
	
	-- The whitelist is passed in named parameter |fetchwikidata
	local whitelist = frame.args.fetchwikidata
	
	-- The name of the field that this function is called from is passed in named parameter |name
	local fieldname = frame.args.name
	if blacklist then
		-- The name is compulsory when blacklist is used, so return nil if it is not supplied
		if not fieldname or (#fieldname == 0) then return false, nil, nil end
		-- If this field is on the blacklist, then return nil
		if blacklist:find(fieldname) then return false, nil, nil end
	end
	
	-- If we got this far then we're not on the blacklist
	-- The blacklist overrides any locally supplied parameter as well
	-- If a non-blank input parameter was supplied return it
	if input_parm then return false, input_parm, nil end
	
	-- Otherwise see if this field is on the whitelist:
	if not (whitelist and (whitelist == 'ALL' or whitelist:find(fieldname))) then
		-- not on the whitelist so just return what should be a nil input parameter
		return false, input_parm, nil
	end
	
	-- See what's on Wikidata:
	local entity = mw.wikibase.getEntityObject(qid)
	if entity and entity.claims then
		local props = entity.claims[property_id]
		if props and props[1] then 
			return true, entity, props
		end
	end
	-- no property on Wikidata
	return false, input_parm, nil
end

-------------------------------------------------------------------------------
local function _getvalue(frame, entity, props, filter, propertyID, unit) 
	-- onlysourced is a boolean passed to return only values sourced to other than Wikipedia
	-- if nothing or an empty string is passed set it true
	-- if "false" or "no" or "0" is passed set it false
	local onlysrc = parseParam(frame.args.onlysourced, true)
	
	-- noicon is a boolean passed to suppress the trailing "edit at Wikidata" icon
	-- for use when the value is processed further by the infobox
	-- if nothing or an empty string is passed set it false
	-- if "false" or "no" or "0" is passed set it false
	local noic = parseParam(frame.args.noicon, false)
	
	-- wdlinks is a boolean passed to enable links to Wikidata when no article exists
	-- if nothing or an empty string is passed set it false
	-- if "false" or "no" or "0" is passed set it false
	local wdl = parseParam(frame.args.wdlinks, false)
	
	-- sorted is a boolean passed to enable sorting of the values returned
	-- if nothing or an empty string is passed set it false
	-- if "false" or "no" or "0" is passed set it false
	local sorted = parseParam(frame.args.sorted, false)
	
	-- separator is a string that is used to separate multiple returned values
	-- if nothing or an empty string is passed set it to the default
	-- any double-quotes " are stripped out, so that spaces may be passed
	-- e.g. |sep=" - "
	local sepdefault = ", " -- **internationalise later**
	local separator = frame.args.sep or ""
	if separator=="nenhum" then
		separator = ""
	else
		separator = string.gsub(separator, '"', '')
		if #separator == 0 then
			separator = sepdefault
			end
	end
	
	-- list is a string that may be "", "hlist" or "ubl"
	-- this controls whether multiple values are output as comma-separated
	-- as a horizontal list (hlist) or unbulleted list (ubl)
	local list = frame.args.list or ""
	if list~="hlist" and list~="ubl" then list = "" end
	
	-- prefix is a string that may be nil, empty (""), or a string of characters
	-- this is prefixed to each value
	-- useful when when multiple values are returned
	-- any double-quotes " are stripped out, so that spaces may be passed
	local prefix = frame.args.prefix or ""
	prefix = string.gsub(prefix, '"', '')
	
	-- postfix is a string that may be nil, empty (""), or a string of characters
	-- this is postfixed to each value
	-- useful when when multiple values are returned
	-- any double-quotes " are stripped out, so that spaces may be passed
	local postfix = frame.args.postfix or ""
	postfix = string.gsub(postfix, '"', '')
	
	-- linkprefix is a string that may be nil, empty (""), or a string of characters
	-- this creates a link and is then prefixed to each value
	-- useful when when multiple values are returned and indirect links are needed
	-- any double-quotes " are stripped out, so that spaces may be passed
	local lprefix = frame.args.linkprefix or ""
	lprefix = string.gsub(lprefix, '"', '')
	
	-- linkpostfix is a string that may be nil, empty (""), or a string of characters
	-- this is postfixed to each value when linking is enabled with lprefix
	-- useful when when multiple values are returned
	-- any double-quotes " are stripped out, so that spaces may be passed
	local lpostfix = frame.args.linkpostfix or ""
	lpostfix = string.gsub(lpostfix, '"', '')
	
	-- maxvals is a string that may be nil, empty (""), or a number
	-- this determines how many items may be returned when multiple values are available
	-- the behaviour is customisable for each different data type
	local maxvals = tonumber(frame.args.maxvals) or -1
	
	-- So now we have something to return: deal with:
	-- (1) ["datatype"] = "wikibase-item";
	-- (2) ["datatype"] = "time";
	-- (3) ["datatype"] = "commonsMedia",
	--     ["datatype"] = "external-id",
	--     ["datatype"] = "string",
	--     ["datatype"] = "url"
	-- (4) ["datatype"] = "quantity"
	-- (5) ["datatype"] = "globe-coordinate", or anything else
	
	
	local lang = mw.language.getContentLanguage().code
	local thisQid = entity.id
	-- table 'out' is going to to store the return value(s):
	local out = {}
	local semlink = frame.args.semlink or "não"
	local icon = " [[" .. i18n["filespace"] .. ":Blue pencil.svg |frameless |text-top |10px |alt=" .. i18n["editenowikidata"] .. "|link=https://www.wikidata.org/wiki/" .. thisQid .. "?uselang=" .. lang .. "#" .. propertyID .. "|" .. i18n["editenowikidata"] .. "]]"
	local dni = frame.args.dni or "não"
	
	-- data type is a wikibase item:
	if props[1].mainsnak.datatype == "wikibase-item" then
		-- it's wiki-linked value, so output as link if possible
		for k, v in pairs(props) do
			if filter(v) and ((onlysrc == false) or sourced(v)) then -- has valid refs or all values required
				if v.mainsnak.snaktype == "somevalue" then -- check for value is unknown
					out[#out + 1] = "Desconhecido"
				elseif v.mainsnak.snaktype == "novalue" then -- check for value is none
					-- out[#out + 1] = "No value" -- don't return a value for this
				else
					local qnumber = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
					local sitelink = mw.wikibase.sitelink(qnumber)
					local label = labelOrId(qnumber)
					
					local gender = frame.args.gender or ""
					if gender ~= "" and entity.claims["P21"] then
						local genderID = entity.claims["P21"][1].mainsnak.datavalue.value["id"]
						if genderID == "F" or genderID == "Q6581072" or genderID == "Q1052281" or genderID == "Q43445" then
							local genderEntity = mw.wikibase.getEntityObject(qnumber)
							if genderEntity.claims["P2521"] then
								for k2,v2 in pairs(genderEntity.claims["P2521"]) do
									if filter(v2) and (v2.mainsnak.datavalue.value["language"] == "pt-br" or v2.mainsnak.datavalue.value["language"] == "pt") then
										label = v2.mainsnak.datavalue.value["text"]
									end
								end
							end
						end
					end
					
					if sitelink then
						if semlink == "sim" then
							out[#out + 1] = label
						else
							out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
						end
					else
						-- no sitelink, so check first for a redirect with that label
						local artitle = mw.title.new(label, 0)
						if artitle.id > 0 then
							if artitle.isRedirect then
								-- no sitelink, but there's a redirect with the same title as the label
								-- let's link to that
								out[#out + 1] = label
							else
								-- no sitelink and not a redirect but an article exists with the same title as the label
								-- that's probably a dab page, so output the plain label
								out[#out + 1] = label
							end
						else
							-- no article or redirect with the same title as the label
							if wdl then
								-- show that there's a Wikidata entry available
								out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]&nbsp;<span title='" .. i18n["errors"]["local-article-not-found"] .. "'>[[File:Wikidata-logo.svg|16px|alt=|link=]]</span>"
							else
								-- no wikidata links required, so don't show nothing
								--out[#out + 1] = label
							end
						end
					end
					-- If the property has a qualifier of latest date, add that in all cases:
					if v.qualifiers then
						local quals = v.qualifiers["P1326"] -- latest date qualifier
						if quals and #out>0 then
							out[#out] = out[#out] .. " (" ..mw.wikibase.renderSnaks(quals) .. ")"
						end
					end
				end -- check for unknown value
			end -- check for sourced
			if maxvals > 0 and #out >= maxvals then break end
		end -- loop through k-v pairs
	
	-- data type is time:
	elseif props[1].mainsnak.datatype == "time" then
		local datamorte = ""
		-- it's a date value, so output according to formatting preferences
		for k, v in pairs(props) do
			-- check for references, and count valid references
			if filter(v) and ((onlysrc == false) or sourced(v)) then
				if v.mainsnak.snaktype == "somevalue" then -- check for value is unknown
					out[#out + 1] = "Desconhecido"
				elseif v.mainsnak.snaktype == "novalue" then -- check for value is none
					-- out[#out + 1] = "No value" -- don't return a value for this
				else
					local timestamp = v.mainsnak.datavalue.value.time
					-- A year can be stored like this: "+1872-00-00T00:00:00Z",
					-- which is processed here as if it were the day before "+1872-01-01T00:00:00Z",
					-- and that's the last day of 1871, so the year is wrong.
					-- So fix the month 0, day 0 timestamp to become 1 January instead:
					timestamp = timestamp:gsub("%-00%-00T", "-01-01T")
					local dateprecision = v.mainsnak.datavalue.value.precision
					local fpvdate = tonumber(timestamp:sub(2, 5))
					local fdate
					if dateprecision >= 9 then -- 9 is year precision
						local dateformat = "y"
						if dateprecision >= 10 then -- prepend month
							if dni ~= "não" then
								fpvdate = tonumber(timestamp:sub(7, 8)) .. " " .. fpvdate
							else
								fpvdate = i18n.months[tonumber(timestamp:sub(7, 8))] .. " " .. fpvdate
							end
							dateformat = frame.args.df
							if dateprecision >= 11 then -- prepend day
									fpvdate = tonumber(timestamp:sub(10, 11)) .. " " .. fpvdate
							end
							if timestamp:sub(1, 1) == "-" then
								fpvdate = fpvdate .. " BCE"
							end
						else
							if timestamp:sub(1, 1) == "-" then
								fpvdate = fpvdate.." BCE"
							end
						end
						local idioma = frame.args.idioma or "pt-br"
						local datasemlink = frame.args.datasemlink or semlink
						if dni ~= "não" then
							if entity.claims["P570"] then
								datamorte = "existe"
							end
						end
						if frame.args.display=="predefinição" then
							fdate  = format_Date_template(frame,fpvdate,dateformat,frame.args.bc,frame.args.idioma)
						else
							fdate  = format_Date(frame,fpvdate, dateformat, frame.args.bc,frame.args.idioma,frame.args.datasemlink,datamorte,dni)
						end
					elseif dateprecision == 7 then -- century
						local datasemlink = frame.args.datasemlink or semlink
						local century = require("Módulo:Romano").main({math.floor((fpvdate - 1) / 100) + 1})
						if timestamp:sub(1, 1) == "-" then -- date is BC
							if datasemlink == "sim" then
								fdate = mw.ustring.gsub(i18n["centurysemlink"], "$1", century.."&nbsp;"..i18n["ac"])
							else
								fdate = mw.ustring.gsub(i18n["century"], "$1", century.."&nbsp;"..i18n["ac"])
							end
						else
							if datasemlink == "sim" then
								fdate = mw.ustring.gsub(i18n["centurysemlink"], "$1", century)
							else
								fdate = mw.ustring.gsub(i18n["century"], "$1", century)
							end
						end
					else
						-- date precisions 0 to 6 (billion years to millenium) TODO:
						--
					end
						out[#out+1] = fdate
				end -- check for unknown value
			end -- check for sourced
			if maxvals > 0 and #out >= maxvals then break end
		end -- loop through k-v pairs
	
	-- data types which are strings:
	elseif props[1].mainsnak.datatype == "commonsMedia" or props[1].mainsnak.datatype == "external-id" or props[1].mainsnak.datatype == "string" or props[1].mainsnak.datatype == "url" then
		-- commonsMedia or external-id or string or url
		-- all have mainsnak.datavalue.value as string
		for k, v in pairs(props) do
			if filter(v) and ((onlysrc == false) or sourced(v)) then -- has valid refs or all values required
				if v.mainsnak.snaktype == "somevalue" then -- check for value is unknown
					out[#out + 1] = "Desconhecido"
				elseif v.mainsnak.snaktype == "novalue" then -- check for value is none
					-- out[#out + 1] = "No value" -- don't return a value for this
				else
					if lprefix > "" then
						out[#out+1] = "[[" .. lprefix .. v.mainsnak.datavalue.value .. lpostfix .. "|" .. prefix .. v.mainsnak.datavalue.value .. postfix .. "]]"
					else
						if postfix == "ref" then
							out[#out+1] = "<ref name =" .. string.match(v.mainsnak.datavalue.value, "%.(.-)%.") .. "-" .. string.match(v.mainsnak.datavalue.value, "^.+/(.-)$") .. ">" .. v.mainsnak.datavalue.value .. "</ref>"
						else
							out[#out+1] = prefix .. v.mainsnak.datavalue.value .. postfix
						end
					end -- check for link requested
				end -- check for unknown value
			end -- check for sourced
			if maxvals > 0 and #out >= maxvals then break end
		end -- loop through k-v pairspairs
	
	-- data types which are quantities:
	elseif props[1].mainsnak.datatype == "quantity" then
		for k, v in pairs(props) do
			if filter(v) and ((onlysrc == false) or sourced(v)) then -- has valid refs or all values required
				if v.mainsnak.snaktype == "somevalue" then -- check for value is unknown
					out[#out + 1] = "Desconhecido"
				elseif v.mainsnak.snaktype == "novalue" then -- check for value is none
					-- out[#out + 1] = "No value" -- don't return a value for this
				else
					local amount = v.mainsnak.datavalue.value.amount
					amount = mw.ustring.gsub(amount, "%+", "")
					local sortkey = string.format("%09d", amount)
					local lang = mw.language.new(wiki.langcode)
					amount = lang:formatNum(tonumber(amount))
					-- This is used to get the unit name for a numeric value
					local suffix = ""
					local unitID = v.mainsnak.datavalue.value.unit
					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("Módulo:Wikidata/Unidades").getUnit(amount, unit_label, unitID, unit == "unidade")
					end
					out[#out+1] = amount .. suffix .. postfix
				end
			end	
			if maxvals > 0 and #out >= maxvals then break end
		end
		
	-- some other data type:
	else
		-- some other data type
		-- e.g. globe-coordinate where mainsnak.datavalue.value is a table
		-- either write a specific handler
		-- or we can use formatPropertyValues() as a temporary measure. This won't work with multiple valid values.
		for k, v in pairs(props) do
			if filter(v) and ((onlysrc == false) or sourced(v)) then -- has valid refs or all values required
				local propertyValue = entity:formatPropertyValues(propertyID).value
				out[#out+1] = propertyValue
				break
			end -- check for sourced
			if maxvals > 0 and #out >= maxvals then break end
		end
	end

	-- if there's anything to return, then return a list
	-- comma-separated by default, but may be specified by the sep parameter
	-- optionally specify a hlist or ubl
	if #out > 0 then
		local resp
		if sorted then table.sort(out) end
		if list == "hlist" then
			resp = frame:expandTemplate{title = 'Hlist', args = out}
		elseif list == "ubl" then
			resp = frame:expandTemplate{title = 'Unbulleted list', args = out}
		else
			if noic then
				resp = table.concat(out, separator)
			else
				resp = table.concat(out, separator) .. icon
			end
		end
		-- adicionar categoria para saber os artigos onde o Wikidata está sendo usado
		return resp .. '[[Categoria:!Artigos que usam dados do Wikidata em infobox]]'
	else
		return nil -- no items had valid reference
	end
end

-------------------------------------------------------------------------------
-- Public functions
-------------------------------------------------------------------------------
-- getValue 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 "")
	
	-- There may be a local parameter supplied, if it's blank, set it to nil
	local input_parm =  mw.text.trim(frame.args[2] or "")
	local unit = mw.text.trim(frame.args[3] or "")
	if input_parm and (#input_parm == 0) then input_parm = nil end
	local success, errorOrEntity, props = parseInput(frame, input_parm, propertyID)
	if not success then
		return errorOrEntity
	else
		entity = errorOrEntity
	end
	local function filter(claim)
		return true
	end
	return _getvalue(frame, entity, props, filter, propertyID, unit)
end
-------------------------------------------------------------------------------
-- getPreferredValue is used to get a value, or a comma separated list of them if multiple values exist
-- If preferred ranks are set, it will return those values, otherwise values with normal ranks
--
p.getPreferredValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	
	-- There may be a local parameter supplied, if it's blank, set it to nil
	local input_parm =  mw.text.trim(frame.args[2] or "")
	local unit =  mw.text.trim(frame.args[3] or "")
	if input_parm and (#input_parm == 0) then input_parm = nil end
	local success, errorOrEntity, props = parseInput(frame, input_parm, propertyID)
	if not success then
		return errorOrEntity
	else
		entity = errorOrEntity
	end
	local prefflag = false
	for k, v in pairs(props) do
		if v.rank == "preferred" then 
			prefflag = true 
			break
		end
	end
	local function filter(claim)
		return claim.rank == "preferred" or prefflag == false
	end
	return _getvalue(frame, entity, props, filter, propertyID, unit)
end

-------------------------------------------------------------------------------
-- getSourcedValue is used to get a value, or a comma separated list of them if multiple values exist
-- but only values that are sourced are returned
-- redundant to getValue with onlysourced=true but kept for backwards compatibility
-- now defined via getValue
--
p.getSourcedValue = function(frame)
	frame.args.onlysourced = "yes"
	return p.getValue(frame)
end


-------------------------------------------------------------------------------
-- getCoords is used to get coordinates for display in an infobox
-- whitelist and blacklist are implemented
-- optional 'display' parameter is allowed, defaults to "inline, title"
--
p.getCoords = function(frame)
	local propertyID = "P625"
	local input_parm =  mw.text.trim(frame.args[1] or "")
	if input_parm and (#input_parm == 0) then input_parm = nil end
	
	-- if there is a 'display' parameter supplied, use it
	-- otherwise default to "inline, title"
	local disp = frame.args.display
	if (not disp) or (#disp == 0) then
		disp = "inline, title"
	end
	
	local success, errorOrEntity = parseInput(frame, input_parm, property_id)
	if not success then
		return errorOrEntity
	else
		local entity = errorOrEntity
		local lat_long = {}
		local coords = entity:formatPropertyValues(propertyID).value
		-- the latitude and longitude are returned like this: nn°nn&#39;nn.n&#34;
		-- using html entities with hex values really screws up parsing the numbers - thanks devs
		local lat = mw.ustring.match(coords, "^[^,]*")  -- everything from the start to before the comma
		local long = mw.ustring.match(coords, "[^ ]*$") -- everything from after the space to the end
		lat = lat:gsub("&#%d%d;", ":")                  -- clean out the html entities
		long = long:gsub("&#%d%d;", ":")                -- clean out the html entities
		-- read the latitude numbers into a table
		for num in mw.ustring.gmatch(lat, "%d+%.?%d*") do
  			lat_long[#lat_long + 1] = num
		end
		-- add the N/S
		lat_long[#lat_long + 1] = lat:sub(-1)
		-- read the longitude numbers into a table
		for num in mw.ustring.gmatch(long, "%d+%.?%d*") do
			lat_long[#lat_long + 1] = num
		end
		-- add E/W for long
		lat_long[#lat_long + 1] = long:sub(-1)
		-- add named parameter for display
		lat_long["display"] = disp
		-- invoke template Coor dms with the values stored in the table
		return frame:expandTemplate{title = 'Coor dms', args = lat_long}
	end
end


-------------------------------------------------------------------------------
-- getQualifierValue is used to get a formatted value of a qualifier
-- 
-- The call needs:	a property (the unnamed parameter or 1=)
-- 					a target value for that property (pval=)
--					a qualifier for that target value (qual=)
-- The usual whitelisting and blacklisting of the property is implemented
-- The boolean onlysourced= parameter can be set to return nothing
-- when the property is unsourced (or only sourced to Wikipedia)
-- 
p.getQualifierValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")

	-- The PropertyID of the target value of the property
	-- whose qualifier is to be returned is passed in named parameter |pval=
	local propvalue = frame.args.pval
	
	-- The PropertyID of the qualifier
	-- whose value is to be returned is passed in named parameter |qual=
	local qualifierID = frame.args.qual

	-- onlysourced is a boolean passed to return qualifiers
	-- only when property values are sourced to something other than Wikipedia
	-- if nothing or an empty string is passed set it false
	-- if "false" or "no" or 0 is passed set it false
	local onlysrc = parseParam(frame.args.onlysourced,false)
	
	local success, errorOrEntity, props = parseInput(frame,nil, propertyID)
	if not success then
		return errorOrEntity
	else
		local entity = errorOrEntity
		-- Scan through the values of the property
		-- we want something like property is P793, significant event (in propertyID)
		-- whose value is something like Q385378, construction (in propvalue)
		-- then we can return the value(s) of a qualifier such as P580, start time (in qualifierID)
		for k1, v1 in pairs(props) do
			if v1.mainsnak.snaktype == "value" and v1.mainsnak.datavalue.type == "wikibase-entityid" then
				-- It's a wiki-linked value, so check if it's the target (in propvalue)
				-- and if it has qualifiers
				if v1.mainsnak.datavalue.value.id == propvalue and v1.qualifiers then
					if (onlysrc == true) and not sourced(v1) then
						return
					end
					-- if we've got this far, we have a (sourced) claim with qualifiers
					-- which matches the target, so find the value(s) of the qualifier we want
					local quals = v1.qualifiers[qualifierID]
					local out = {}
					if quals then
						if quals[1].datatype == "wikibase-item" then
							for k2, v2 in pairs(quals) do
								local valueID = v2.datavalue.value.id
								local sitelink = mw.wikibase.sitelink(valueID)
								local label = labelOrId(valueID)
								if sitelink then
									out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
								else
									out[#out + 1] = "[[:d:" .. valueID .. "|" .. label .. "]]&nbsp;<span title='" .. i18n["errors"]["local-article-not-found"] .. "'>[[Ficheiro:Wikidata-logo.svg|16px|alt=|link=]]</span>"
								end
							end
							return table.concat(out, ", ")
						else
							return mw.wikibase.renderSnaks(quals)
						end
					end
				end
			end -- of loop through values of propertyID
		end
	end
	return nil
end

-------------------------------------------------------------------------------
-- getLink returns the label for a Qid wiki-linked to the local article (if the article exists)
-- if label doesn't exist, it returns the Qid wiki-linked to the local article (if the article exists)
--
p.getLink = function(frame)
	local itemID = mw.text.trim(frame.args[1] or "")
	if itemID == "" then return end
	local sitelink = mw.wikibase.sitelink(itemID)
	local label = labelOrId(itemID)
	if sitelink then
		return "[[" .. sitelink .. "|" .. label .. "]]"
	else
		return label
	end
end


-------------------------------------------------------------------------------
-- getLabel returns the label for a Qid
-- if label doesn't exist, it returns the Qid
--
p.getLabel = function(frame)
	local itemID = mw.text.trim(frame.args[1] or "")
	if itemID == "" then return end
	return labelOrId(itemID)
end


-------------------------------------------------------------------------------
-- getAT returns the article title for a Qid
-- or nothing if it doesn't exist
--
p.getAT = function(frame)
	local itemID = mw.text.trim(frame.args[1] or "")
	if itemID == "" then return end
	return mw.wikibase.sitelink(itemID)
end

-------------------------------------------------------------------------------
-- getSitelink returns the article title for a Qid in a globalSiteId wiki (like "enwiki" or "ptwiki")
-- or nothing if it doesn't exist
--
p.getSitelink = function(frame)
	local itemID = mw.text.trim(frame.args[1] or "")
	local globalSiteId = mw.text.trim(frame.args[2] or "")
	return mw.wikibase.sitelink(itemID,globalSiteId)
end

-------------------------------------------------------------------------------
-- formatDate is a wrapper to export the private function format_Date
--
p.formatDate = function(frame)
	return format_Date(frame,frame.args[1], frame.args.df, frame.args.bc,frame.args.idioma, frame.args.datasemlink)
end

-------------------------------------------------------------------------------
-- checkBlacklist allows a test to check whether a named field is suppressed
-- {{#if:{{#invoke:WikidataIB |checkBlacklist |name=Joe |suppressfields=Dave; Joe; Fred}} | not blacklisted | blacklisted}}
-- displays "blacklisted"
-- {{#if:{{#invoke:WikidataIB |checkBlacklist |name=Jim |suppressfields=Dave; Joe; Fred}} | not blacklisted | blacklisted}}
-- displays "not blacklisted"
--
p.checkBlacklist = function(frame)
	local blacklist = frame.args.suppressfields
	local fieldname = frame.args.name
	if blacklist and fieldname then
		if blacklist:find(fieldname) then return nil end
		return true
	end
end

p.predMorte = function(frame)
	local result1 = {}
	local result2 = {}
	local idioma
	
	for word in string.gmatch(frame.args[1],"[^|]+") do
		result1[#result1+1] = word
	end
	for word2 in string.gmatch(frame.args[2],"[^|]+") do
		result2[#result2+1] = word2
	end
	if #result1 == 2 then
		result1[3] = result1[2]
		result1[2] = result1[1]
		result1[1] = ""
	elseif #result1 == 1 then
		result1[3] = result1[1]
		result1[2] = ""
		result1[1] = ""
	end
	if #result2 == 2 then
		result2[3] = result2[2]
		result2[2] = result2[1]
		result2[1] = ""
	elseif #result2 == 1 then
		result2[3] = result2[1]
		result2[2] = ""
		result2[1] = ""
	end

	if frame.args.idioma == "pt-ao" then
		idioma = "ao"
	else
		idioma = "br"
	end
	
	if (result1[1]=="Desconhecido") or (result1[2]== "Desconhecido") or (result1[3]== "Desconhecido") or (result2[1]=="Desconhecido") or (result2[2]== "Desconhecido") or (result2[3]== "Desconhecido") then
		return "Desconhecido"
	end
	
	return frame:expandTemplate{ title = 'Morte', args = {result1[1],result1[2],result1[3],result2[1],result2[2],result2[3],lang = idioma}}
end

p.predDni = function(frame)
	local result = {}
	local idioma
	local comidade = frame.args[2] or ""
	for word in string.gmatch(frame.args[1],"[^|]+") do
		result[#result+1] = word
	end
	if #result == 2 then
		result[3] = result[2]
		result[2] = result[1]
		result[1] = ""
	elseif #result == 1 then
		result[3] = result[1]
		result[2] = ""
		result[1] = ""
	elseif #result == 0 then
		result[3] = ""
		result[2] = ""
		result[1] = ""
	end
	
	if frame.args.idioma == "pt-ao" then
		idioma = "ao"
	else
		idioma = "br"
	end
	
	if string.match(result[3], "-") then
		result[3] = string.gsub(result[3], "-", "")
		result[3] = result[3].." a.C."
	end
	
	if (result[1]=="Desconhecido") or (result[2]== "Desconhecido") or (result[3]== "Desconhecido") then
		return "Desconhecido"
	end

	if comidade then
		return frame:expandTemplate{ title = 'Dni', args = {result[1],result[2],result[3],comidade,lang = idioma}}
	else
		return frame:expandTemplate{ title = 'Dni', args = {result[1],result[2],result[3],lang = idioma}}
	end
end


return p