-- version 20210304 from master @cawiki
local p =
-- Initialization of variables --------------------
local i18n =
local cases = -- functions for local grammatical cases defined at
local required = ... -- variadic arguments from require functionlocal wiki =
local untranslated -- used in infobox modules: nil or truelocal _ -- variable for unused returned values, avoiding globals
-- Module local functions --------------------------------------------
-- Credit to http://stackoverflow.com/a/1283608/2644759, cc-by-sa 3.0local function tableMerge(t1, t2) for k, v in pairs(t2) do if type(v)
"table" then tableMerge(t1[k] or, t2[k] or) else t1[k] = v end else t1[k] = v end end return t1end
local function loadI18n(lang) local exist, res = pcall(require, wiki.module_title .. "/i18n") if exist and next(res) ~= nil then tableMerge(i18n, res.i18n) cases = res.cases end if lang ~= wiki.langcode then exist, res = pcall(require, wiki.module_title .. "/i18n/" .. lang) if exist and next(res) ~= nil then tableMerge(i18n, res.i18n) tableMerge(cases, res.cases) end endend
-- Table of language codes: requested or default and its fallbackslocal function findLang(langcode) if mw.language.isKnownLanguageTag(langcode or )
false then if not mw.title.getCurrentTitle.isContentPage then langcode = cframe:preprocess('') end if mw.language.isKnownLanguageTag(langcode or )
wiki.langcode then for _, l in ipairs(i18n.addfallback) do table.insert(languages, l) end end return languagesend
-- Argument is 'set' when it exists (not nil) or when it is not an empty string.local function isSet(var) return not (var
'string' and mw.text.trim(var)
-- Set local case to a labellocal function case(localcase, label, ...) if not isSet(label) then return label end if type(localcase)
"smallcaps" then return '
' .. label .. '' elseif cases[localcase] then return cases[localcase](label, ...) end return labelend-- get safely a serialized snaklocal function getSnak(statement, snaks) local ret = statement for i, v in ipairs(snaks) do if not ret then return end ret = ret[v] end return retend
-- mw.wikibase.getLabelWithLang or getLabelByLang with a table of languageslocal function getLabelByLangs(id, languages) local label local lang for _, l in ipairs(languages) do if l
-- getBestStatements if bestrank=true, else getAllStatements with no deprecatedlocal function getStatements(entityId, property, bestrank) local claims = if not (entityId and mw.ustring.match(property, "^P%d+$")) then return claims end if bestrank then claims = mw.wikibase.getBestStatements(entityId, property) else local allclaims = mw.wikibase.getAllStatements(entityId, property) for _, c in ipairs(allclaims) do if c.rank ~= "deprecated" then table.insert(claims, c) end end end return claimsend
-- Is gender femenine? true or falselocal function feminineGender(id) local claims = mw.wikibase.getBestStatements(id or mw.wikibase.getEntityIdForCurrentPage,'P21') local gender_id = getSnak(claims,) if gender_id
"Q1052281" or gender_id
-- Fetch female form of labellocal function feminineForm(id, lang) local feminine_claims = getStatements(id, 'P2521') for _, feminine_claim in ipairs(feminine_claims) do if getSnak(feminine_claim,)
-- Add an icon for no label in requested languagelocal function addLabelIcon(label_id, lang, uselang, icon) local ret_lang, ret_icon = , if icon then if lang and lang ~= uselang then ret_lang = " (" .. lang .. ")" end if label_id and (lang
'P' then namespace = 'Property:' end ret_icon = " " untranslated = true end if isSet(i18n.categorylabels) and lang ~= uselang and uselang
-- editicon values: true/false (no=false), right, void defaults to i18n.addpencil-- labelicon only by parameterlocal function setIcons(arg, parg) local val = arg
false or val
"no" then edit_icon, label_icon = false, false else edit_icon, label_icon = val, true end return edit_icon, label_iconend
-- Add an icon for editing a statement with requirements for Wikidata Bridgelocal function addEditIcon(parameters) local ret = if parameters.editicon and parameters.id and parameters.property then local icon_style = parameters.editicon
false then return thetable end local last_element = thetable[#thetable] local the_icon = addEditIcon(parameters) -- add it before last html closing tags local tags = local rev_element = string.reverse(last_element) for tag in string.gmatch(rev_element, '(>%l+/<)') do if string.match(rev_element, '^' .. tags .. tag) then tags = tags .. tag else break end end local last_tags = string.reverse(tags) local offset = string.find(last_element, last_tags .. '$') if offset then thetable[#thetable] = string.sub(last_element, 1, offset - 1) .. the_icon .. last_tags else thetable[#thetable] = last_element .. the_icon end return thetableend
-- Escape Lua captureslocal function captureEscapes(text) return mw.ustring.gsub(text, "(%%%d)", "%%%1")end
-- expandTemplate or callParserFunctionlocal function expandBraces(text, formatting) if text
nil then return text end -- only expand braces if provided in argument, not included in value as in Q1164668 if mw.ustring.find(formatting, '