Modulu:Wikidata: berrikuspenen arteko aldeak

Ezabatutako edukia Gehitutako edukia
update from ca.wiki: alternative values of property, new format musical-notation
formatting sound for musical-notation, access to all functions via require, fixes for raw novalue/somevalue
1. lerroa:
-- version 2019111620191128 from master @cawiki
 
local p = {}
9. lerroa:
["property-not-found"] = "Property not found.",
["qualifier-not-found"] = "Qualifier not found.",
['not-from-content-page'] = "Do not invoke from content page. Use a template or use a module subpage like /sandbox for testing."
},
["datetime"] =
34 ⟶ 33 lerroa:
},
["monolingualtext"] = '<span lang="%language">%text</span>',
 
["cite"] = { -- Cite web parameters
["url"] = "url",
59 ⟶ 58 lerroa:
{
langcode = mw.language.getContentLanguage().code,
module_title = mw.wikibase.getSitelink('Q12069631Module:Wikidata'), -- title of this module for access to its /subpages, change if necessary
}
 
88 ⟶ 87 lerroa:
 
loadI18n()
 
-- 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 var == '')
end
 
local function case(localcase, label, ...)
if label == nil ornot isSet(label == '') or cases[localcase] == nil then
return label
end
98 ⟶ 102 lerroa:
 
local function findLang(langcode)
if langcode == nil or langcode == "" or mw.language.isKnownLanguageTag(langcode or '') == false then
local myframecframe = mw.getCurrentFrame()
local pframe = cframe:getParent()
langcode = myframe.args.lang
if langcode == nilpframe orand langcode == "" orpframe.args.lang
if mw.language.isKnownLanguageTag(langcode or '') == false then
if not mw.title.getCurrentTitle().isContentPage then
langcode = myframecframe:getParentpreprocess().args.'{{int:lang}}')
if langcode == nil or langcode == "" or mw.language.isKnownLanguageTag(langcode) == false then
end
if not mw.title.getCurrentTitle().isContentPage then
if langcode == nil or langcode == "" or mw.language.isKnownLanguageTag(langcode or '') == false then
langcode = myframe:preprocess( '{{int:lang}}' )
langcode = myframewiki.args.langlangcode
end
if langcode == nil or langcode == "" or mw.language.isKnownLanguageTag(langcode) == false then
langcode = wiki.langcode
end
end
end
120 ⟶ 122 lerroa:
-- Helper function to obtain the default language code
function p.lang(frame)
returnlocal lang = frame and findLang(frame.args[1])[1] -- nil via require
return findLang(lang)[1]
end
 
265 ⟶ 268 lerroa:
end
 
local function printDatatypeMusicalNot(data, formatting)
local attr = {}
return mw.getCurrentFrame():callParserFunction('#tag:score', data)
if formatting == 'sound' then
attr.sound = 1
end
return mw.getCurrentFrame():callParserFunctionextensionTag('#tag:score', data, attr)
end
 
273 ⟶ 280 lerroa:
return '[' .. data .. ' ' .. mw.text.split(data, '//' )[2] .. ']'
elseif mw.ustring.find((parameters.formatting or ''), '$1', 1, true) then -- formatting = a pattern
return expandBraces(mw.ustring.gsub(parameters.formatting, '$1', {['$1']=data}), parameters.formatting)
elseif parameters.case then
return case(parameters.case, data, parameters.lang[1])
281 ⟶ 288 lerroa:
end
 
local function printDatavalueUrlprintDatatypeUrl(data, parameters)
return printDatavalueString(urlEscapes(data), parameters)
end
 
local function printDatavalueCoordinate(data, parameter)
local globes = {['Q3134']='callisto',['Q596']='ceres',['Q15040']='dione',['Q2']='earth',['Q3303']='enceladus',
294 ⟶ 301 lerroa:
if mw.ustring.find(parameter, '$globe', 1, true) then
local myglobe = 'earth'
if isSet(data.globe and data.globe ~= '') then
local globenum = mw.text.split(data.globe, 'entity/')[2] -- http://www.wikidata.org/wiki/Q2
myglobe = globes[globenum] or 'earth'
523 ⟶ 530 lerroa:
else
if parameter == "ucfirst" or parameter == "ucinternallink" then
if labelcase =and lang then
labelcase and= mw.language.new(lang):ucfirst(labelcase)
end
-- only first of a list, reset formatting for next ones
if parameter == "ucinterlanllink" then
608 ⟶ 617 lerroa:
return printDatatypeMath(snak.datavalue.value)
elseif snak.datatype == 'musical-notation' then
return printDatatypeMusicalNot(snak.datavalue.value, parameters.formatting)
elseif snak.datatype == "url" then
return printDatavalueUrlprintDatatypeUrl(snak.datavalue.value, parameters)
elseif snak.datavalue.type == "string" then
return printDatavalueString(snak.datavalue.value, parameters)
625 ⟶ 634 lerroa:
end
elseif snak.snaktype == 'novalue' then
if parameters.formatting == 'raw' then return end
return mw.message.new('Wikibase-snakview-snaktypeselector-novalue'):inLanguage(parameters.lang[1]):plain()
elseif snak.snaktype == 'somevalue' then
if parameters.formatting == 'raw' then return end
return mw.message.new('Wikibase-snakview-snaktypeselector-somevalue'):inLanguage(parameters.lang[1]):plain()
end
776 ⟶ 787 lerroa:
require(wiki.module_title .. '/debug').track('getSiteLink')
local id = frame.args[1]
if id == nil ornot isSet(id == '') then
id = mw.wikibase.getEntityIdForCurrentPage()
if id == nil then
790 ⟶ 801 lerroa:
local list = {}
for i = 0, num_qual do
if isSet(args[type_list .. i] and args[type_list .. i] ~= '') then
listed = true
list[tostring(i)] = {}
813 ⟶ 824 lerroa:
end
 
function p.claim_main(frameargs)
-- function renamed, track it
if mw.title.new(frame:getParent():getTitle()).isContentPage then
require(wiki.module_title .. '/debug').track('main')
if not mw.title.new(frame:getTitle()).isSubpage then
return p.claim(args)
-- invoked from a content page and not invoking a module subpage
return printError("not-from-content-page")
end
end
return p._main(frame.args, frame:getParent().args)
end
 
-- Onon debug console use: =p._main(claim{item="Q...", lang="xx", property="P...", ...})
-- Entry point from other modules or debugging, with a list of arguments.
function p._mainclaim(args, pargsframe)
-- From other modules use: require("Module:Wikidata")._main({item="Q...", property="P...", ...})
local args = frame.args or frame -- via invoke or require
-- On debug console use: =p._main({item="Q...", lang="xx", property="P...", ...})
function p._main(args, pargs)
--If a value is already set, use it
if isSet(args.value and args.value ~= '') then
return args.value
end
-- arguments
returnlocal pargs = p._main(frame.args, and frame:getParent().args)
local id = args.item or (pargs and pargs.item)
if id == nil ornot isSet(id == "") then
id = mw.wikibase.getEntityIdForCurrentPage()
if id == nil then return end
end
local property = string.upper(args["property"] or "")
978 ⟶ 986 lerroa:
for idx in pairs(claims) do
local claim = claims[sortindices[idx]]
local reference = {}
if not whitelisted then rowlist[idx] = true end
if firstrank ~= '' and firstrank ~= claim.rank then
1.039 ⟶ 1.047 lerroa:
if not value and showerrors then value = error end
if value then
if references and claim.references then reference = claim.references end
refs[#refs]["col0"] = reference
values[#values]["col0"] = value
sortkeys[#sortkeys]["col0"] = sortkey or value
1.072 ⟶ 1.080 lerroa:
local valuerow = values[sortindices[idx]]
local reference = getReferences({["references"] = refs[sortindices[idx]]["col0"]})
 
value = valuerow["col0"]
if parameters.formatting == "table" then
1.302 ⟶ 1.310 lerroa:
-- Returns pairs of instance label and property value fetching a recursive tree
function p.getParentValues(frame)
local args = frame.args or frame -- via invoke or require
local idpargs = argsframe.itemargs orand frame:getParent().args.item
iflocal id == ""args.item or id ==(pargs niland thenpargs.item)
if not isSet(id) then
id = mw.wikibase.getEntityIdForCurrentPage()
if id == nil then return end
end
local languages = findLang(args.lang)
local propertySup = args["property"]; if not isSet(propertySup == nil or propertySup == "") then propertySup = "P131" end --administrative entity
local propertyLabel = args["label"]; if not isSet(propertyLabel == nil or propertyLabel == "") then propertyLabel = "P31" end --instance
local propertyLink = args["valuetext"]; if propertyLink == "" then propertyLink = nil end --internallink
local upto = args["upto"]; if upto == "" then upto = nil end
1.319 ⟶ 1.328 lerroa:
local case = args["case"]; if case == "" then case = nil end
if isSet(args.uptolabelid and args.uptolabelid ~= "") then
upto = mw.wikibase.getLabel(args.uptolabelid)
end
if isSet(args.showlabelid and args.showlabelid ~= "") then
local showLabelList = {} for substring in mw.text.gsplit(args.showlabelid, '[/%s]+') do table.insert(showLabelList, mw.wikibase.getLabel(substring)) end
if #showLabelList > 0 then
1.344 ⟶ 1.353 lerroa:
include_self)
local rowformat = args["rowformat"]; if not isSet(rowformat == nil or rowformat == "") then rowformat = "$0 = $1" end
local separator = args["separator"]; if not isSet(separator == nil or separator == "") then separator = "<br />" end
local sorting = args["sorting"]; if sorting == "" then sorting = nil end
local cascade = (args.cascade == "true" or args.cascade == "yes")
1.357 ⟶ 1.366 lerroa:
 
function p.linkWithParentLabel(frame)
local args = {}frame.args or frame -- via invoke or require
for k, v in pairs(frame.args) do -- metatable
args[k] = v
end
-- get internal link of property/qualifier
1.366 ⟶ 1.372 lerroa:
args.formatting = "internallink"
args.separator = "/·/"
local pargslink_list = frame:getParent()p.claim(args)
if not isSet(link_list) then
local link_list = p._main(args, pargs)
if link_list == nil or link_list == "" then
return
end
1.375 ⟶ 1.380 lerroa:
-- get id value of property/qualifier
args.formatting = "raw"
local items_list = p._mainclaim(args, pargs)
local items_table = mw.text.split(items_list, "/·/", true)
1.391 ⟶ 1.396 lerroa:
for i, v in ipairs(items_table) do
args.item = v
local link_label = p._mainclaim(args, pargs)
if isSet(link_label and link_label ~= '') then
link_table[i] = mw.ustring.gsub(link_table[i] or '', "%[%[(.*)%|.+%]%]", "[[%1|" .. link_label .. "]]")
end
1.406 ⟶ 1.411 lerroa:
 
function p.yearsOld(frame)
local args = frame.args or frame -- via invoke or require
local id = args.item
if id == '' ornot isSet(id == nil) then
id = mw.wikibase.getEntityIdForCurrentPage()
end
1.486 ⟶ 1.491 lerroa:
local args = frame.args or frame -- via invoke or require
local id = mw.text.trim(args[1] or "")
if not isSet(id or id == "") then return end
local editicon = not (args.editicon == "false" or args.editicon == "no")
1.515 ⟶ 1.520 lerroa:
local linked = args.linked
if isSet(linked and linked ~= "") and linked ~= "no" then
local article = mw.wikibase.getSitelink(id) or ("d:" .. id)
return "[[" .. article .. "|" .. (label or id) .. "]]" .. pencil