Module:Sandbox/Masoud.h1368/Wikidata.Ca explained

-- 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 if type(t1[k] or false)

"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 local cframe = mw.getCurrentFrame local pframe = cframe:getParent langcode = pframe and pframe.args.lang 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 )

false then langcode = wiki.langcode end end end loadI18n(langcode) local languages = mw.language.getFallbacksFor(langcode) table.insert(languages, 1, langcode) if langcode

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

nil or (type(var)

'string' and mw.text.trim(var)

))end

-- Set local case to a labellocal function case(localcase, label, ...) if not isSet(label) then return label end if type(localcase)

"function" then return localcase(label) elseif 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

wiki.langcode then -- using getLabelWithLang when possible instead of getLabelByLang, do not solve redirects pending phab:T157868 label, l = mw.wikibase.getLabelWithLang(id) else label = mw.wikibase.getLabelByLang(id, l) end if label then lang = l break end end return label, langend

-- 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

"Q6581072" or gender_id

"Q1052281" or gender_id

"Q43445" then -- female, transgender female, female organism return true end return falseend

-- 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,)

lang then return feminine_claim.mainsnak.datavalue.value.text end endend

-- 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

nil or lang ~= uselang) then local namespace = if string.sub(label_id, 1, 1)

'P' then namespace = 'Property:' end ret_icon = " " untranslated = true end if isSet(i18n.categorylabels) and lang ~= uselang and uselang

wiki.langcode then ret_icon = ret_icon .. '' .. i18n.categorylabels .. (lang and '' or '/Q]]') end end return ret_lang .. ret_iconend

-- editicon values: true/false (no=false), right, void defaults to i18n.addpencil-- labelicon only by parameterlocal function setIcons(arg, parg) local val = arg

nil and parg or arg local edit_icon, label_icon if not isSet(val) then edit_icon, label_icon = i18n.addpencil, true elseif val

false or val

"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

"right" and ' style="float: right;"' or ret = '

' .. "" .. "" if isSet(i18n.categoryprop) then ret = ret .. "" .. string.gsub(i18n.categoryprop, '$1', parameters.property) .. "" end end return retend-- add edit icon to the last element of a tablelocal function addEditIconTable(thetable, parameters) if #thetable

0 or 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 or formatting

nil then return text end -- only expand braces if provided in argument, not included in value as in Q1164668 if mw.ustring.find(formatting, '