Mòdul:Wikidata: diferència entre les revisions
Contingut suprimit Contingut afegit
actualització, correccions i millores menors |
actualització de ca.wiki, millores menors |
||
Línia 1:
-- version
local p = {}
Línia 64:
local cases = {} -- functions for local grammatical cases defined at [[Module:Wikidata/i18n]]
local wiki =
{
langcode = mw.language.getContentLanguage().code,
module_title = "Module:Wikidata"
}
----------------------------------------------------------------------------
Línia 86 ⟶ 92:
local function loadI18n()
local exist, res = pcall(require, wiki.module_title .. "
if exist and next(res) ~= nil then
tableMerge(i18n, res.i18n)
Línia 95 ⟶ 101:
loadI18n()
local function case(word, localcase, lang)
if word == nil or word == '' or cases[localcase] == nil then
return word
end
return cases[localcase](word, lang)
end
local function findLang(langcode)
Línia 107 ⟶ 116:
langcode = myframe:getParent().args.lang
if langcode == nil or langcode == "" or mw.language.isKnownLanguageTag(langcode) == false then
if not mw.title.
langcode = myframe:preprocess( '{{int:lang}}' )
end
Línia 119 ⟶ 128:
table.insert(languages, 1, langcode)
return languages
end
Línia 166 ⟶ 167:
if feminine_claims then
for _, feminine_claim in ipairs(feminine_claims) do
local feminine_value = getValueOfClaim(feminine_claim, nil, {["
if feminine_value then
return feminine_value
Línia 172 ⟶ 173:
end
end
end
-- Fetch unit symbol
local function unitSymbol(id, lang)
local claims = findClaims(mw.wikibase.getEntityObject(id), 'P5061')
local langclaims = {}
if claims then
for _, snak in ipairs(claims) do
if snak.mainsnak and snak.mainsnak.datavalue and snak.mainsnak.datavalue.value and
not langclaims[snak.mainsnak.datavalue.value.language] -- just the first one by language
then
langclaims[snak.mainsnak.datavalue.value.language] = snak.mainsnak.datavalue.value.text
end
end
for _, l in ipairs(lang) do
if langclaims[l] then
return langclaims[l]
end
end
end
return langclaims["mul"] -- last try
end
Línia 226 ⟶ 248:
return expandBraces(mw.ustring.gsub(parameters.formatting, '$1', escaped_data), parameters.formatting)
elseif parameters.case then
return case(data, parameters.case,
else
return data
Línia 262 ⟶ 284:
amount = mw.ustring.gsub(amount, "%+", "")
local sortkey = string.format("%09d", amount)
amount = mw.language.new(
-- This is used to get the unit name for a numeric value
local suffix = ""
Línia 271 ⟶ 293:
unitID = mw.ustring.sub(unitID, mw.ustring.find(unitID, "Q"), -1)
if mw.ustring.sub(unitID, 1, 1) == "Q" then
local unit_label, lang =
local unit_symbol
unit_symbol = unitSymbol(unitID, parameters.lang)
end
if lang == wiki.langcode and pcall(require, wiki.module_title .. "/Units") then
suffix = " " .. require(wiki.module_title .. "/Units").getUnit(amount, unit_label, unitID, parameters.formatting == "unitcode", unit_symbol)
elseif parameters.formatting == "unitcode" then
suffix = " " .. unit_symbol or unit_label or unitID
else
suffix = " " .. unit_label or unitID
Línia 289 ⟶ 317:
-- calendar model
local calendar_model = {["Q12138"] = "gregorian", ["Q1985727"] = "gregorian", ["Q11184"] = "julian", ["Q1985786"] = "julian"}
local calendar_id = mw.text.split(data.calendarmodel, 'entity/')[2]
local calendar_add = ""
or (timestamp > "+1582-10-04T00:00:00Z" and calendar_model[calendar_id] == "julian")
then
calendar_add = " <sup>(" .. mw.message.new('Wikibase-time-calendar-' .. calendar_model[calendar_id]):inLanguage(parameters.lang[1]):plain() .. ")</sup>"
end
Línia 307 ⟶ 335:
addon = addon .. calendar_add
local function d(f, t)
return mw.language.new(
end
local precision = data.precision or 11
Línia 358 ⟶ 386:
local function printDatavalueEntity(data, parameters)
local entityId =
local entityIdPreffix = data['entity-type'] == 'property' and "Property:" .. entityId or entityId
if parameters.formatting == 'raw' then
return entityId, entityId
end
local label, lang =
local sitelink = mw.wikibase.sitelink(entityId)
local parameter = parameters.formatting
Línia 374 ⟶ 403:
local ret1, ret2
if parameter == 'label' then
ret1 = (labelcase or entityId)
ret2 = labelcase or entityId
elseif parameter == 'sitelink' then
ret1 = (sitelink or 'wikidata:' ..
ret2 = sitelink or entityId
elseif mw.ustring.find((parameter or ''), '$1', 1, true) then -- formatting = a pattern
Línia 401 ⟶ 430:
ret2 = labelcase
else
ret1 = '[[wikidata:' ..
ret2 = labelcase or entityId
end
end
return ret1 .. addEditIcon(entityIdPreffix, lang, parameters.lang[1], parameters.editicon), ret2
end
local function printDatavalueMonolingualText(data, parameters)
-- data fields: language [string], text [string]
-- temporary track for migration to list=lang
not mw.ustring.find(parameters.formatting, '$', 1, true)
if oldformatting then
require('Mòdul:utilitats').rastreig("Wikidata/monolingualtext")
end
if (parameters.list == "lang" or oldformatting) and data["language"] ~= parameters.lang[1] then
return
elseif parameters.formatting == "language" or parameters.formatting == "text" then
return data[parameters.formatting]
end
local result = data["text"]
if data["language"] ~= wiki.langcode then
result = mw.ustring.gsub(mw.ustring.gsub(i18n.monolingualtext, "%%language", data["language"]), "%%text", data["text"])
end
if mw.ustring.find((parameters.formatting or ''), '$', 1, true) then
-- output format defined with $text, $language
result = mw.ustring.gsub(parameters.formatting, '$text', result)
result = mw.ustring.gsub(result, '$language', data["language"])
end
return result
Línia 478 ⟶ 520:
return printDatavalueMonolingualText(snak.datavalue.value, parameters)
end
elseif snak.snaktype == 'novalue' then
return mw.message.new('Wikibase-snakview-snaktypeselector-novalue'):inLanguage(parameters.lang[1]):plain()
elseif snak.snaktype == 'somevalue' then
return mw.message.new('Wikibase-snakview-snaktypeselector-somevalue'):inLanguage(parameters.lang[1]):plain()
end
return mw.wikibase.renderSnak(snak)
Línia 491 ⟶ 537:
local qualifier = claim.qualifiers[qualifierId]
if qualifier then
-- iterate over monolingualtext qualifiers to get local language
for idx in pairs(qualifier) do
if qualifier[idx].datavalue.value and qualifier[idx].datavalue.value.language ==
return qualifier[idx]
end
Línia 562 ⟶ 608:
for snakidx = 1, #snakval do
if snakidx > 1 then refparts = refparts .. ", " end
refparts = refparts or '' .. getSnakValue(snakval[snakidx], {lang="wiki.langcode"})
end
refs[snakkey] = refparts
Línia 639 ⟶ 685:
-- arguments
local id = args
local languages = findLang(args.lang)
local idgender = args["itemgender"]
if idgender and not string.match(idgender, "^Q%d+$") then -- id malformed, maybe "unknown value"
Línia 654 ⟶ 701:
local parameter = args["formatting"] or ''; if parameter == "" then parameter = nil end
local case = args.case
local list = args["list"] or true; if (list == "false" or list == "no") then list = false end
local sorting_col = args.tablesort
local sorting_up = (args.sorting or "") ~= "-1"
Línia 663 ⟶ 710:
local showerrors = args["showerrors"]
local default = args["default"]
local editicon = not (args.editicon == "false" or args.editicon == "no")
local parameters = {["formatting"] = parameter, ["list"] = list, ["case"] = case, ["lang"] = languages, ["editicon"] = editicon,
["separator"] = separator, ["conjunction"] = conjunction, ["qseparator"] = separator, ["qconjunction"] = conjunction}
local preformat = ""
Línia 738 ⟶ 786:
parameters.separator = parameters.separator == "LF" and "\010" or parameters.separator
parameters.conjunction = parameters.conjunction == "LF" and "\010" or parameters.conjunction
-- i18n separators
parameters.separator = parameters.separator or mw.message.new('Comma-separator'):inLanguage(parameters.lang[1]):plain()
parameters.conjunction = parameters.conjunction or (mw.message.new('And'):inLanguage(parameters.lang[1]):plain() .. mw.message.new('Word-separator'):inLanguage(parameters.lang[1]):plain())
-- iterate over all elements and return their value (if existing)
local value, valueq
Línia 955 ⟶ 1.006:
end
local function getPropertyValue(id, property, parameter, langs, editicon)
local entity = mw.wikibase.getEntityObject(id)
if not (entity and entity.claims) then return end
Línia 981 ⟶ 1.032:
if snak.datavalue then
entityId = "Q" .. tostring(snak.datavalue.value['numeric-id'])
result, _ = getSnakValue(snak, {formatting=parameter, lang=langs, editicon=editicon})
end
Línia 989 ⟶ 1.040:
function p.getParentValues(frame)
local args = frame.args
local id = args
local languages = findLang()
local propertySup = args["property"]; if (propertySup == nil or propertySup == "") then propertySup = "P131" end --administrative entity
local propertyLabel = args["label"]; if (propertyLabel == nil or propertyLabel == "") then propertyLabel = "P31" end --instance
Línia 998 ⟶ 1.050:
local separator = args["separator"]; if (separator == nil or separator == "") then separator = "<br />" end
local sorting = args["sorting"]; if sorting == "" then sorting = nil end
local editicon = not (args.editicon == "false" or args.editicon == "no")
local lastlabel = uc_first(upto or '')
Línia 1.014 ⟶ 1.067:
for iter = 1, maxloop do
local label, link
id, link = getPropertyValue(id, propertySup, "internallink", languages, editicon)
if id then
_, label = getPropertyValue(id, propertyLabel, "label", languages)
if label and link then
if propertyLink then
_, linktext = getPropertyValue(id, propertyLink, "label", languages)
if linktext then
link = mw.ustring.gsub(link, "%[%[(.*)%|.+%]%]", "[[%1|" .. linktext .. "]]")
end
end
label = case(label, "infoboxlabel", languages[1])
if labelShow == nil or labelFilter[label] then
result[#result + 1] = {label, link}
Línia 1.099 ⟶ 1.152:
local comparator = function(a, b)
local rankmap = { deprecated = 2, normal = 1, preferred = 0 }
local ranka = rankmap[claims[a].rank or "normal"] .. string.format("%08d", a)
local rankb = rankmap[claims[b].rank or "normal"] .. string.format("%08d", b)
return ranka < rankb
end
Línia 1.189 ⟶ 1.242:
local editicon = not (args.editicon == "false" or args.editicon == "no")
local languages = findLang()
local pencil = ''
if args.label then
else
local languages = findLang()
if languages[1] == wiki.langcode then
-- exceptions or labels fixed
local exist, labels = pcall(require, wiki.module_title .. "/labels")
if exist and next(labels.infoboxLabelsFromId) ~= nil then
label = labels.infoboxLabelsFromId[id]
end
end
if label == nil then
label, lang = getLabelByLangs(id, languages)
if label then
if args.itemgender and feminineGender(args.itemgender) then
label = feminineForm(id, lang) or label
end
label = mw.language.new(lang):ucfirst(mw.text.nowiki(label)) -- sanitize
end
pencil = addEditIcon(id, lang, languages[1], editicon)
end
end
Línia 1.214 ⟶ 1.276:
return (label or id) .. pencil
end
end
-- Return default language used
function p.lang(frame)
return findLang(frame.args[1])[1]
end
|