Module:Vernacular
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules
English: Code behind {{Vernacular name}}, a template to be used on taxon (plant or animal) gallery or category to provide all the taxon's Common names or vernacular names in as many languages as possible.
Alternative to Module:Wikidata4Bio aiming at much less resource use.
Code
--[[
__ __ _ _ __ __ _
| \/ | ___ __| |_ _| | ___ \ \ / /__ _ __ _ __ __ _ ___ _ _| | __ _ _ __
| |\/| |/ _ \ / _` | | | | |/ _ (_) \ / / _ \ '__| '_ \ / _` |/ __| | | | |/ _` | '__|
| | | | (_) | (_| | |_| | | __/_ \ V / __/ | | | | | (_| | (__| |_| | | (_| | |
|_| |_|\___/ \__,_|\__,_|_|\___(_) \_/ \___|_| |_| |_|\__,_|\___|\__,_|_|\__,_|_|
Authors and maintainers:
* User:Jarekt
]]
local core = require('Module:Core')
-- local function to help normalize input arguments
local function normalize_input_args(input_args, output_args)
for name, value in pairs( input_args ) do
if value ~= '' then -- nuke empty strings
if type(name)=='string' then name=string.lower(name) end -- convert to lower case
output_args[name] = value
end
end
return output_args
end
--------------------------------------------------------------------------------
local function langWrapper(text, textLang)
-- code equivalent to https://commons.wikimedia.org/wiki/Template:Description
local language = mw.language.new( textLang )
local dir = language:getDir()
local Lang = language:ucfirst(mw.language.fetchLanguageName( textLang, textLang))
local str = mw.ustring.format('<span class="language %s"><b>%s:</b> %s</span>', textLang, Lang, text)
return str
end
-- initialize object to be returned
local p = {}
--------------------------------------------------------------------------------
function p.get_vernacular_names1(item)
-- get a list of taxon common name (P1843), one for each language
local vn_dict = {}
for _, statement in ipairs(mw.wikibase.getBestStatements( item, 'P1843' ) or {}) do
if (statement.mainsnak.snaktype == "value") and (statement.rank ~= 'deprecated') then
local val = statement.mainsnak.datavalue.value
local lang = val.language
local text = val.text
local sitelink = mw.wikibase.getSitelink(item, lang .. 'wiki')
if (sitelink) then
text = '[[:' .. lang .. ':' .. sitelink .. '|' .. text .. ']]'
end
vn_dict[lang] = langWrapper(text, lang)
end
end
return vn_dict
end
--------------------------------------------------------------------------------
function p.get_vernacular_names2(item)
-- get a list of taxon common name (P1843), one for each language
local entity = mw.wikibase.getEntity( item )
local statements = entity:getBestStatements( 'P1843' )
local taxon_name = core.parseStatements(entity:getBestStatements( 'P225' ), nil)[1]
local vn_dict = {}
for _, statement in ipairs(statements or {}) do
if (statement.mainsnak.snaktype == "value") and (statement.rank ~= 'deprecated') then
local val = statement.mainsnak.datavalue.value
local lang = val.language
local text = val.text
local sitelink = mw.wikibase.getSitelink(item, lang .. 'wiki')
if (sitelink) then
text = '[[:' .. lang .. ':' .. sitelink .. '|' .. text .. ']]'
end
vn_dict[lang] = langWrapper(text, lang)
end
end
for lang, value in pairs( entity.labels or {} ) do
local label = value.value
if (not vn_dict[lang]) and (label~=taxon_name) then
local text = label
local sitelink = mw.wikibase.getSitelink(item, lang .. 'wiki')
if (sitelink) then
text = '[[:' .. lang .. ':' .. sitelink .. '|' .. text .. ']]'
end
vn_dict[lang] = langWrapper(text, lang)
end
end
return vn_dict
end
--------------------------------------------------------------------------------
local function add_local_names(vn_dict, args, item)
-- add names provided to the template
for lang, text in pairs( args ) do
if type(lang)=='string' and mw.language.isSupportedLanguage(lang) and (not vn_dict[lang]) then -- lang has to be a valid language
vn_dict[lang] = langWrapper(text, lang)
end
end
return vn_dict
end
--------------------------------------------------------------------------------
local function render_QS_URL(vn_dict, args, item)
local today = '+' .. os.date('!%F') .. 'T00:00:00Z/11' -- today's date in QS format
local url = mw.uri.decode(mw.title.getCurrentTitle():canonicalUrl()) -- URL to current page
local ref = '|S143|Q565|S813|' .. today .. '|S4656|"' .. mw.uri.decode(url) .. '"'
local icon = 'File:Commons_to_Wikidata_QuickStatements.svg'
local exclude = { no=1 }
qsTable = {}
local str = '%s|P1843|%s:"%s"|S143|Q565|S813|%s|S4656|"%s"'
local locNames = False
for lang, text in pairs( args ) do
if type(lang)=='string' and mw.language.isSupportedLanguage(lang) and (not exclude[lang]) then
locNames = True
if (not vn_dict[lang]) then
table.insert(qsTable, string.format(str, item, lang, text, today, url))
end
end
end
if #qsTable==0 and locNames then
return ' <span style="color:red;font-weight:bold">(Redundant local names)</span>'
elseif #qsTable==0 then
return ''
end
local qsURL = table.concat(qsTable, '||')
local hoverMsg = 'Add properties to Wikidata item based on this file'
qsURL = mw.uri.encode(qsURL, "PATH")
qsURL = 'https://quickstatements.toolforge.org/#/v1=' .. qsURL -- create full URL link
qsURL = ' [[' .. icon .. '|15px|link=' .. qsURL .. '|' .. hoverMsg ..']]'
return qsURL
end
--------------------------------------------------------------------------------
local function sort_names(vn_dict)
local keys = {}
for key in pairs(vn_dict) do
table.insert(keys, key)
end
table.sort(keys)
local vn_lines = {}
for _, key in ipairs(keys) do
table.insert(vn_lines, '\n*'..vn_dict[key])
end
return table.concat(vn_lines, '')
end
--------------------------------------------------------------------------------
function p.vernacular_name(frame)
-- switch to lowercase parameters to make them case independent
local args = {}
args = normalize_input_args(frame:getParent().args, args)
args = normalize_input_args(frame.args, args)
local item = args[1] or args.wikidata or args.item or args.usewikidata
local extended = core.yesno(args.extended, false)
-- get the content of the infobox: a list of common names
local qsURL = ''
local qs_cat = ''
local vn_dict = {}
if (item) then
if (extended) then
vn_dict = p.get_vernacular_names2(item)
else
vn_dict = p.get_vernacular_names1(item)
end
qsURL = render_QS_URL(vn_dict, args, item)
if #qsURL>0 then
qs_cat = '\n[[Category:Vernacular_names template with quick statements]]'
end
end
vn_dict = add_local_names(vn_dict, args, item)
vn = sort_names(vn_dict)
if #vn==0 then
return ''
end
local style0 = 'display:table;margin:2px 0;border:1px solid #CCC;padding:1px;'
local style1 = 'background:var(--background-color-success-subtle,#F1FCF1); color:var(--color-base,#202122);padding:0 .25em'
local style2 = 'font-size:95%;background:var(--background-color-interactive-subtle,#FDFDFD); color:var(--color-base,#202122); padding:0 4px'
-- Get the header of the infobox
local title = frame:expandTemplate{ title = 'VN/title' }
local logo = string.format("[[File:Wikidata-logo.svg|20px|wikidata:%s|link=wikidata:%s]]", item, item)
local edit = core.editAtWikidata(item, '', args.lang)
title = string.format('<div style="%s">%s\n%s%s%s</div>', style1, title, logo, edit, qsURL)
local body = string.format('<div class="hlist" style="%s">%s\n</div>', style2, vn)
res = string.format('<div class="biota" style="%s">%s\n%s</div>', style0, title, body)
return res .. qs_cat
end
return p