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 2018080920180819 from master @cawiki
 
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 .. "Module:Wikidata/i18n")
if exist and next(res) ~= nil then
tableMerge(i18n, res.i18n)
Línia 95 ⟶ 101:
loadI18n()
 
local function case(word, localcase, lang)
local wiki =
if word == nil or word == '' or cases[localcase] == nil then
{
return word
langcode = mw.language.getContentLanguage().code
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.newgetCurrentTitle(myframe:getParent():getTitle()).isContentPage then
langcode = myframe:preprocess( '{{int:lang}}' )
end
Línia 119 ⟶ 128:
table.insert(languages, 1, langcode)
return languages
end
 
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
 
Línia 166 ⟶ 167:
if feminine_claims then
for _, feminine_claim in ipairs(feminine_claims) do
local feminine_value = getValueOfClaim(feminine_claim, nil, {["formattinglist"]="lang", ["lang"]={lang}})
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, wikiparameters.langcodelang[1])
else
return data
Línia 262 ⟶ 284:
amount = mw.ustring.gsub(amount, "%+", "")
local sortkey = string.format("%09d", amount)
amount = mw.language.new(wikiparameters.langcodelang[1]):formatNum(tonumber(amount))
-- 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 = mw.wikibase.labelgetLabelByLangs(unitID, parameters.lang)
local unit_symbol
if pcall(require, "Module:Wikidata/Units") then
suffix = " " .. require("Module:Wikidata/Units").getUnit(amount, unit_label, unitID,if parameters.formatting == "unitcode") then
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_add = ""
local calendar_id = mw.text.split(data.calendarmodel, 'entity/')[2]
local calendar_add = ""
if (timestamp < "+1582-10-15T00:00:00Z" and (calendar_id == "Q12138" or calendar_id == "Q1985727"))
or if (timestamp >< "+1582-10-04T0015T00:00:00Z" and (calendar_id == "Q11184" or calendar_model[calendar_id] == "Q1985786gregorian"))
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>"
local calendar_model = {["Q12138"] = "gregorian", ["Q1985727"] = "gregorian", ["Q11184"] = "julian", ["Q1985786"] = "julian"}
calendar_add = " <sup>(" .. mw.message.new('Wikibase-time-calendar-' .. calendar_model[calendar_id]):inLanguage(wiki.langcode):plain() .. ")</sup>"
end
Línia 307 ⟶ 335:
addon = addon .. calendar_add
local function d(f, t)
return mw.language.new(wikiparameters.langcodelang[1]):formatDate(f, t or timestamp) .. addon
end
local precision = data.precision or 11
Línia 358 ⟶ 386:
 
local function printDatavalueEntity(data, parameters)
local entityId = "Q" .. tostring(data['numeric-id'])
local entityIdPreffix = data['entity-type'] == 'property' and "Property:" .. entityId or entityId
if parameters.formatting == 'raw' then
return entityId, entityId
end
local label, lang = mw.wikibase.getLabelWithLanggetLabelByLangs(entityId, parameters.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:' .. entityIdentityIdPreffix)
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:' .. entityIdentityIdPreffix .. '|' .. (labelcase or entityId) .. ']]'
ret2 = labelcase or entityId
end
end
return ret1 .. addEditIcon(entityIdPreffix, lang, parameters.lang[1], parameters.editicon), ret2
return ret1, ret2
end
 
local function printDatavalueMonolingualText(data, parameters)
-- data fields: language [string], text [string]
local result = nil
-- temporary track for migration to list=lang
if parameters.formatting == "language" or parameters.formatting == "text" then
result local oldformatting = data[parameters.formatting] and
elseif not (parameters.formatting then== "language" or parameters.formatting == "text") and
not mw.ustring.find(parameters.formatting, '$', 1, true)
if data["language"] == wiki.langcode then
if oldformatting then
result = data["text"]
require('Mòdul:utilitats').rastreig("Wikidata/monolingualtext")
end
end
else
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
--if iteratequalifier[1].datatype over== "monolingualtext" qualifiers to get local languagethen
-- iterate over monolingualtext qualifiers to get local language
for idx in pairs(qualifier) do
for idx in pairs(qualifier) do
if qualifier[idx].datavalue and qualifier[idx].datavalue.value and qualifier[idx].datavalue.value.language then
if qualifier[idx].datavalue.value and qualifier[idx].datavalue.value.language == wikiparameters.langcodelang[1] then
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[".item"] or (pargs and pargs[".item"]); if id == "" then id = nil end
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[".item"] or frame:getParent().args[".item"]; if id == "" then id = nil end
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 label, lang
if languages[1] == wiki.langcode and args.fixed then
label = require("Module:Wikidata/labels").infoboxLabelsFromId[id] -- exceptions or labels fixed
end
local pencil = ''
iflocal label, == nil thenlang
if args.label then
label, lang = getLabelByLangs(id, languages)
if label then= args.label
else
if args.itemgender and feminineGender(args.itemgender) then
local languages = findLang()
label = feminineForm(id, lang) or label
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
label = mw.language.new(lang):ucfirst(mw.text.nowiki(label)) -- sanitize
end
pencil = addEditIcon(id, lang, languages[1], editicon)
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