# Module:Wikidata Infobox


local p = {}  
local WikidataIB = require("Module:WikidataIB")  

-- Code from 'Module:No globals'  
local mt = getmetatable(_G) or {}  
function mt.__index (t, k)  
if k ~= 'arg' then  
error('Tried to read nil global ' .. tostring(k), 2)  
end  
return nil  
end  
function mt.__newindex(t, k, v)  
if k ~= 'arg' then  
error('Tried to write global ' .. tostring(k), 2)  
end  
rawset(t, k, v)  
end  
setmetatable(_G, mt)  
-- End of code from 'Module:No globals'  

function p.ifThenShow  
if mw.text.trim(.args[1] or *) ~= * then  
return (.args[3] or *) .. (.args[1] or *) .. (.args[4] or '')  
else  
return (.args[2] or '')  
end  
end  

-- Given an input area, return a map zoom level to use with mw:Extension:Kartographer in . Defaults to mapzoom=15.  
function p.autoMapZoom  
local size = tonumber(.args[1]) or 0  
local LUT = { 5000000, 1000000, 100000, 50000, 10000, 2000, 150, 50, 19, 14, 5, 1, 0.5 }  
for zoom, scale in ipairs(LUT) do  
if size > scale then  
return zoom+1  
end  
end  
return 15  
end  

function p.formatLine  
local part2 = mw.text.trim(.args[2] or '')  
local returnstr = ''  
if part2 ~= '' then  
returnstr = '<tr '  
if (.args.mobile or 'n') == 'y' then  
returnstr = returnstr .. ''  
end  
local newframe = {}  
newframe.args = {}  
newframe.args.qid = .args[1]  
returnstr = returnstr .. '><th>' .. mw.getContentLanguage:ucfirst(WikidataIB.getLabel(newframe))  
returnstr = returnstr .. '</th><td '  
if (.args.wrap or 'n') == 'y' then  
returnstr = returnstr .. ''  
end  
returnstr = returnstr .. '>' .. part2 .. '</td></tr>'  
end  
return returnstr  
end  

function p.hasValue (tab, val)  
for index, value in ipairs(tab) do  
if value == val then  
return true  
end  
end  

return false  
end  

-- baseLang is a utility function that returns the base language in use  
-- so for example, both English (en) and British English (en-gb) return 'en'  
-- from https://commons.wikimedia.org/wiki/Module:Wikidata2  
function p.baseLang  
local txtlang = :callParserFunction( "int", "lang" ) or ""  
-- This deals with specific exceptions: be-tarask -> be_x_old  
if txtlang == "be-tarask" then  
return "be_x_old"  
end  
local pos = txtlang:find("-")  
local ret = ""  
if pos then  
ret = txtlang:sub(1, pos-1)  
else  
ret = txtlang  
end  
return ret  
end  

function p.langDirection  
local lang = mw.text.trim(.args[1] or '')  
if (not mw.language.isSupportedLanguage(lang)) then  
lang = :callParserFunction( "int", "lang" ) -- get user's chosen language  
end  
return mw.getLanguage(lang):getDir  
end  

---
convertChar returns the non-diacritic version of the supplied character.  
stripDiacrits replaces words with diacritical characters with their non-diacritic equivalent.  
strip_diacrits is available for export to other modules.  
stringIsLike tests two words, returning true if they only differ in diacritics, false otherwise.  
stringIs_like is available for export to other modules.  
---

local function characterMap  
-- table with characters with diacrits and their equivalent basic latin characters  
local charMap_from, charMap_to  
charMap_from = 'ÁÀÂÄǍĂĀÃÅĄƏĆĊĈČÇĎĐḌÐÉÈĖÊËĚĔĒẼĘẸĠĜĞĢĤĦḤİÍÌÎÏǏĬĪĨĮỊĴĶĹĿĽĻŁḶḸṂŃŇÑŅṆŊÓÒÔÖǑŎŌÕǪỌŐØŔŘŖṚṜŚŜŠŞȘṢŤŢȚṬÚÙÛÜǓŬŪŨŮŲỤŰǗǛǙǕŴÝŶŸỸȲŹŻŽ'..  
'áàâäǎăāãåąəćċĉčçďđḍðéèėêëěĕēẽęẹġĝğģĥħḥıíìîïǐĭīĩįịĵķĺŀľļłḷḹṃńňñņṇŋóòôöǒŏōõǫọőøŕřŗṛṝśŝšşșṣťţțṭúùûüǔŭūũůųụűǘǜǚǖŵýŷÿỹȳźżž'  
charMap_to = 'AAAAAAAAAAACCCCCDDDDEEEEEEEEEEEGGGGHHHIIIIIIIIIIIJKLLLLLLLMNNNNNNOOOOOOOOOOOORRRRRSSSSSSTTTTUUUUUUUUUUUUUUUUWYYYYYZZZ'..  
'aaaaaaaaaaacccccddddeeeeeeeeeeegggghhhiiiiiiiiiiijklllllllmnnnnnnoooooooooooorrrrrssssssttttuuuuuuuuuuuuuuuuwyyyyyzzz'  
local charMap = {}  
for i = 1,mw.ustring.len(charMap_from) do  
charMap[mw.ustring.sub(charMap_from, i, i)] = mw.ustring.sub(charMap_to, i, i)  
end  
charMap['ß'] = 'ss'  
return charMap  
end  

function p.convertChar  
local ch = .args.char or mw.text.trim(.args[1]) or ""  
local charMap = characterMap  
return charMap[ch] or ch  
end  

function p.strip_diacrits(wrd)  
if wrd then  
local charMap = characterMap  
wrd = string.gsub(wrd, "[^\128-\191][\128-\191]*", charMap )  
end  
return wrd  
end  

function p.stripDiacrits  
return p.strip_diacrits(.args.word or mw.text.trim(.args[1]))  
end  

function p.stringIs_like(wrd1, wrd2)  
return p.strip_diacrits(wrd1) == p.strip_diacrits(wrd2)  
end  

function p.stringIsLike  
local wrd1 = .args.word1 or .args[1]  
local wrd2 = .args.word2 or .args[2]  
if p.strip_diacrits(wrd1) == p.strip_diacrits(wrd2) then  
return true  
else  
return nil  
end  
end  

return p