Module:WikidataIB: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>RexxS
(start function derived from Module:Wikidata getValue)
 
meta>RexxS
(setup qid and fieldname)
Line 1: Line 1:
-- Module to try out use of a blacklist and whitelist for infobox fields
-- Module to try out use of a blacklist and whitelist for infobox fields
-- can take a named parameter |qid which is the Wikidata ID for the article. This will not normally be used
-- Fields in blacklist are never to be displayed, i.e. module must return nil
-- Fields in blacklist are never to be displayed, i.e. module must return nil
-- Fields in whitelist return local value if it exists or the Wikidata value otherwise
-- Fields in whitelist return local value if it exists or the Wikidata value otherwise
-- The name of the field this is called from is passed in parameter |name
-- The name of the field that this function is called from is passed in named parameter |name
-- blacklist is passed in parameter |suppressfields
-- The name is compulsory, so the module returns nil if it is not supplied
-- whitelist is passed in parameter |fetchwikidata
-- blacklist is passed in named parameter |suppressfields
-- whitelist is passed in named parameter |fetchwikidata


-- This is used to get a value, or a comma separated list of them if multiple values exist
-- This is used to get a value, or a comma separated list of them if multiple values exist
Line 10: Line 12:
local propertyID = mw.text.trim(frame.args[1] or "")
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
-- can take a named parameter |qid which is the Wikidata ID for the article. This will not normally be used.
local qid = frame.args.qid
if qid and (#qid == 0) then qid = nil end
-- The name of the field that this function is called from is passed in named parameter |name
-- The name is compulsory, so the module returns nil if it is not supplied
local fieldname =frame.args.name
if not fieldname or (#fieldname == 0) then return nil end
if input_parm then
if input_parm then
local entity = mw.wikibase.getEntityObject()
local entity = mw.wikibase.getEntityObject()

Revision as of 18:04, 16 May 2016

Documentation for this module may be created at Module:WikidataIB/doc

-- Module to try out use of a blacklist and whitelist for infobox fields
-- can take a named parameter |qid which is the Wikidata ID for the article. This will not normally be used
-- Fields in blacklist are never to be displayed, i.e. module must return nil
-- Fields in whitelist return local value if it exists or the Wikidata value otherwise
-- The name of the field that this function is called from is passed in named parameter |name
-- The name is compulsory, so the module returns nil if it is not supplied
-- blacklist is passed in named parameter |suppressfields
-- whitelist is passed in named parameter |fetchwikidata

-- This is used to get a value, or a comma separated list of them if multiple values exist
p.getValue = function(frame)
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	
	-- can take a named parameter |qid which is the Wikidata ID for the article. This will not normally be used.
	local qid = frame.args.qid
	if qid and (#qid == 0) then qid = nil end
	
	-- The name of the field that this function is called from is passed in named parameter |name
	-- The name is compulsory, so the module returns nil if it is not supplied
	local fieldname =frame.args.name
	if not fieldname or (#fieldname == 0) then return nil end
	
	if input_parm then
		local entity = mw.wikibase.getEntityObject()
		local claims
		if entity and entity.claims then
			claims = entity.claims[propertyID]
		end
		if claims then
			-- if wiki-linked value output as link if possible
			if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
				local out = {}
				for k, v in pairs(claims) do
					local sitelink = mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					local label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
					if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end
							
					if sitelink then
						out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
					else
						out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>"
					end
				end
				return table.concat(out, ", ")
			else
				-- just return best vakues
				return entity:formatPropertyValues(propertyID).value
			end
		else
			return ""
		end
	else
		return input_parm
	end
end