Module:WikidataIB/sandbox1: Difference between revisions

Content deleted Content added
Sync sandbox
Prune code duplication
Line 1,061:
return nil
end
 
 
-------------------------------------------------------------------------------
-- getValueByQual gets the value of a property which has a qualifier with a given entity value
Line 1,071 ⟶ 1,069:
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-------------------------------------------------------------------------------
-- Dependencies: _getvaluebyqual; parseParam; setRanks; parseInput; sourced;
-- assembleoutput;
-------------------------------------------------------------------------------
p.getValueByQual = function(frame)
local qualID = frame.args[2]
 
-- The property ID that will have a qualifier is the first unnamed parameter
local propertyID = mw.text.trim(frame.args[1] or "")
if propertyID == "" then return "no property supplied" end
 
-- The property ID of the qualifier with a particular value is in named parameter |qualID=
local qualID = frame.args.qualID or ""
if qualID == "" then return "no qualifier supplied" end
 
-- The Q-id of the value for the qualifier we want to match is in named parameter |qvalue=
local qval = frame.args.qvalue or ""
if qval == "" then return "no qualifier value supplied" end
local function checkQID(id)
 
return id == qval
-- onlysourced is a boolean passed to return property values
end
-- only when property values are sourced to something other than Wikipedia
return _getvaluebyqual(frame, qualID, checkQID)
-- if nothing or an empty string is passed set it true
-- if "false" or "no" or 0 is passed set it false
local onlysrc = parseParam(frame.args.onlysourced or frame.args.osd, true)
 
-- set the requested ranks flags
frame.args.reqranks = setRanks(frame.args.rank)
 
-- check for locally supplied parameter in second unnamed parameter
-- success means no local parameter and the property exists
local qid, props = parseInput(frame, frame.args[2], propertyID)
 
if qid then
local out = {}
-- Scan through the values of the property
-- we want something like property is "pronunciation audio (P443)" in propertyID
-- with a qualifier like "language of work or name (P407)" in qualID
-- whose value has the required ID, like "British English (Q7979)", in qval
for k1, v1 in pairs(props) do
if v1.mainsnak.snaktype == "value" and v1.mainsnak.datavalue.type == "string" then
-- We'll only deal with returning strings for now
-- so check if it has the right qualifier
local v1q = v1.qualifiers
if v1q and v1q[qualID] then
if onlysrc == false or sourced(v1) then
-- if we've got this far, we have a (sourced) claim with qualifiers
-- so see if matches the required value
-- We'll only deal with wikibase-items for now
if v1q[qualID][1].datatype == "wikibase-item" then
if v1q[qualID][1].datavalue.value.id == qval then
out[#out + 1] = v1.mainsnak.datavalue.value
end
end
end -- of check for sourced
end -- of check for matching required value and has qualifiers
else
return "not string"
end -- of check for string
end -- of loop through values of propertyID
return assembleoutput(out, frame.args, qid, propertyID)
else
return props -- either local parameter or nothing
end -- of test for success
return nil
end
 
 
-------------------------------------------------------------------------------
-- getValueByLang gets the value of a property which has a qualifier P407
Line 1,144 ⟶ 1,090:
-- (if no code is supplied, it uses the default language)
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-------------------------------------------------------------------------------
-- Dependencies: _getvaluebyqual; parseParam; setRanks; parseInput; sourced; assembleoutput;
-------------------------------------------------------------------------------
p.getValueByLang = function(frame)
-- The language code for the qualifier we want to match is in named parameter |lang=
local langcode = frame.args.lang or ""
if langcode == "" then
langcode = frame:callParserFunction{ name = "int", args = "lang" }
end
function checkLanguage(id)
-- id should represent a language like "British English (Q7979)"
-- it should have string property "Wikimedia language code (P424)"
-- qlcode will be a table:
local qlcode = mw.wikibase.getBestStatements(id, "P424")
if (#qlcode > 0) and (qlcode[1].mainsnak.datavalue.value == langcode) then
return true
end
end
return _getvaluebyqual(frame, "P407", checkLanguage)
end
-------------------------------------------------------------------------------
-- Common code for p.getValueByQual and p.getValueByLang
-------------------------------------------------------------------------------
-- Dependencies: parseParam; setRanks; parseInput; sourced; assembleoutput;
-------------------------------------------------------------------------------
p.getValueByLang = function _getvaluebyqual(frame, qualID, checkvalue)
 
-- The property ID that will have a qualifier is the first unnamed parameter
Line 1,153 ⟶ 1,122:
if propertyID == "" then return "no property supplied" end
 
-- The property ID of the qualifier "languagewith ofa workparticular orvalue name"is in named parameter |qualID=
local qualID = frame.args.qualID or "P407"
if qualID == "" then return "no qualifier supplied" end
 
-- The language code for the qualifier we want to match is in named parameter |lang=
local langcode = frame.args.lang or ""
if langcode == "" then
langcode = frame:callParserFunction{ name = "int", args = "lang" }
end
 
-- onlysourced is a boolean passed to return property values
Line 1,179 ⟶ 1,143:
-- Scan through the values of the property
-- we want something like property is "pronunciation audio (P443)" in propertyID
-- with a qualifier like "language of work or name (P407)" in qualID
-- whose value has the required ID, matchinglike the"British English (Q7979)", languagein codeqval
-- like "British English (Q7979)" matches "en-gb"
for k1, v1 in pairs(props) do
if v1.mainsnak.snaktype == "value" and v1.mainsnak.datavalue.type == "string" then
Line 1,193 ⟶ 1,156:
-- We'll only deal with wikibase-items for now
if v1q[qualID][1].datatype == "wikibase-item" then
local qlid =if checkvalue(v1q[qualID][1].datavalue.value.id == qval) then
-- qlid should represent a language like "British English (Q7979)"
-- it should have string property "Wikimedia language code (P424)"
-- qlcode will be a table:
local qlcode = mw.wikibase.getBestStatements(qlid, "P424")
if (#qlcode > 0) and (qlcode[1].mainsnak.datavalue.value == langcode) then
out[#out + 1] = v1.mainsnak.datavalue.value
end
end
end -- of check for sourced
end -- of check for matching required value and has qualifiers
else
return "not string"
Line 1,214 ⟶ 1,172:
return nil
end
 
 
-------------------------------------------------------------------------------