Moduldokumentasjon

Modulen «Wikidata2» inneholder:

  • en funksjon (getValue) som gjør det mulig for skriptet å hente lenkede fra Wikidata ved å angi en gitt ID som første parameter.
  • en funksjon (getRawValue) som gjør det mulig for skriptet å hente ikke-lenkede verdier fra Wikidata ved å angi en gitt ID som første parameter.
  • en funksjon (getDateValue) som gjør det mulig for skriptet å hente datoverdier fra Wikidata ved å angi en gitt ID som første parameter.
  • en funksjon (getValueShortName) som gjør det mulig for skriptet å hente kortnavn (P1813) fra Wikidata ved å angi en gitt ID som første parameter.
  • en funksjon (getFile_extension) som gjør det mulig å legge et punktum (".") foran hvert statement
  • en funksjon (getVersion) som gjør det mulig å hente visse statement(s) basert på en qualifier (avansert)

Parametere rediger

For alle tre funksjoner kan det angis tre ubenevnte parametere: {{#invoke:Wikidata2|funksjon|1|2|3}}.

  • Funksjon kan her være getValue, getRawValue eller getDateValue, avhengig av hva du ønsker å hente ut.
    • getValue returnerer i utgangspunktet en lenke til en artikkel her på norsk bokmålswikipedia. Teksten som vises i lenken, er den etiketten (label) som er angitt på Wikidata. Dersom artikkelen ikke finnes her på norsk bokmål, vil den returnere kun etiketten i ulenket form. Dersom heller ikke etiketten er angitt for norsk bokmål, vil denne funksjonen returnere en tom streng. Når så etikett og artikkel etter hvert blir tilgjengelig på norsk bokmål, vil disse verdiene komme automatisk til syne.
    • getRawValue returnerer i utgangspunktet etiketten i ulenket form. Dersom etiketten ikke er angitt for norsk bokmål, vil denne funksjonen returnere en tom streng. Når så etikett etter hvert blir tilgjengelig på norsk bokmål, vil denne verdien komme automatisk til syne.
    • getDateValue returnerer en datoverdi i ulenket form. Dersom det ikke er angitt noen dato på Wikidata, vil denne funksjonen returnere en tom streng. Når så datoen etter hvert blir lagt inn, vil denne verdien komme automatisk til syne.
  • 1 Her angis ID-en til egenskapen (property) som ønskes hentet fra Wikidata. For eksempel p19 for fødested, p21 for kjønn eller p569 for fødselsdato.
  • 2 Her angis hva som skal vises. Dersom man setter inn hent_wikidata, vil data fra Wikidata returneres og vises. Ellers vises det man skriver inn. Dette gjør det mulig å benytte lokale verdier, for eksempel i en infoboks, fremfor å hente data fra Wikidata.
  • 3 Her angis måten en liste skal deles opp. Setter man inn <br /> vil man få ett element på hver linje. Standard er «, » (komma+mellomrom).

For {{#invoke:Wikidata2|getVersion|pid=|qqid=|ppid=}} se følgende bilde:

Bruk rediger

  • {{#invoke:Wikidata2|getValue|p26|}} = returnerer ingenting
  • {{#invoke:Wikidata2|getValue|p26|hent_wikidata}} = returnerer den lenkede verdien til egenskap p26 (ektefelle) lagret i Wikidata for den korresponderende artikkelen (for eksempel vil den i artikkelen om Bill Clinton returnere Hillary Rodham Clinton)
  • {{#invoke:Wikidata2|getValue|p26|[[Hillary Clinton]]}} = returnerer Hillary Clinton
  • {{#invoke:Wikidata2|getValueShortName|P264|hent_wikidata}} = returnerer den lenkede verdien til egenskap platemerke (P264) lagret i Wikidata for den korresponderende artikkelen (for eksempel vil den i artikkelen om Astrid S returnere [[Universal Music Group|Universal]])
  • {{#invoke:Wikidata2|getFile_extension|Q28865}} = returnerer filnavn slik for Python (Q28865): .py, .pyc, .pyd, .pyo, .pyw, .pyz . Ved bruk i infoboks fjern Q-id.
  • {{#invoke:Wikidata2|GetVersion|Q28865|pid=P348|qqid=Q2804309|ppid=P548}} = returnerer for Python (Q28865): 3.6.8, 3.7.3, 2.7.16, 3.5.7 . Ved bruk i infoboks fjern Q-id som står alene.


local p = {}
require('strict')
--local WikidataDato = require( 'Module:WikidataDato' )

-- Wrapper for pcall which returns nil on failure.
local function quickPcall(func)
	local success, result = pcall(func)
	if success then
		return result
	end
end

-- Count words by ','
local function countpattern(x) 
	local _, count = x:gsub(", ","") 
	return count 
end

local function parseInput(frame)
	local qid = frame.args.qid
	if qid and (#qid == 0) then qid = nil end
	local propertyID = mw.text.trim(frame.args[1] or "")
	local input_parm = mw.text.trim(frame.args[2] or "")
	if input_parm ~= "hent_wikidata" then
		return false, input_parm, nil, nil
	end
	local entity = mw.wikibase.getEntityObject(qid)
	local claims
	if entity and entity.claims then
		claims = entity.claims[propertyID]
		if not claims then
			return false, "", nil, nil
		end
	else
		return false, "", nil, nil
	end
	return true, entity, claims, propertyID
end

local function isType(claims, type)
		return claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == type
end

local function getValue(entity, claims, propertyID, delim, labelHook) 
	if labelHook == nil then
		labelHook = function (qnumber)
			return nil;
		end
	end
	if isType(claims, "wikibase-entityid") then
		local out = {}
		for k, v in pairs(claims) do
			local qnumber = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
			local sitelink = mw.wikibase.sitelink(qnumber)
			local label = labelHook(qnumber) or mw.wikibase.label(qnumber) or qnumber
			if sitelink then
				out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
			else
				out[#out + 1] = label
			end
		end
		return table.concat(out, delim)
	else
		-- just return best values
		return entity:formatPropertyValues(propertyID).value
	end
end

-- This is used to get a normal wiki-linked value, or a comma separated list of them if multiple values exist
p.getValue = function(frame)
	if not mw.wikibase then
        return ""
    end
    local propertyID = mw.text.trim(frame.args[1] or "")
    local input_parm = mw.text.trim(frame.args[2] or "")
    local table_separator = mw.text.trim(frame.args[3] or ",")
    if input_parm == "hent_wikidata" then
        local entity = mw.wikibase.getEntityObject()
        if not entity then
        	return ""
    	end
        if entity.claims and entity.claims[propertyID] ~= nil then
            local out = {}
            for k, v in pairs(entity.claims[propertyID]) do
            	if v.mainsnak.datavalue then
            		local numericId = v.mainsnak.datavalue.value["numeric-id"]
            		if numericId then
                    	if (mw.wikibase.sitelink("Q" .. numericId)) and mw.wikibase.label("Q" .. numericId) then
                        	out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. numericId) .. "|" .. mw.wikibase.label("Q" .. numericId) .. "]]"
                    	elseif (mw.wikibase.sitelink("Q" .. numericId)) then
                            out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. numericId) .. "]]"
                        else
                            out[#out + 1] = mw.wikibase.label("Q" .. numericId)
                        end
                    end
                end
            end
            return table.concat(out, table_separator .. ' ')
        else
            return ""
        end
    else
        return input_parm
    end
end

-- This is used to get a value like 'male' (for property p21) which won't be linked
p.getRawValue = function(frame)
	if not mw.wikibase then
        return ""
    end
    local propertyID = mw.text.trim(frame.args[1] or "")
    local input_parm = mw.text.trim(frame.args[2] or "")
    local table_separator = mw.text.trim(frame.args[3] or ",")
    if input_parm == "hent_wikidata" then
        local entity = mw.wikibase.getEntityObject()
                if not entity then
        	return ""
    	end
        if entity.claims[propertyID] ~= nil then
            local out = {}
            for k, v in pairs(entity.claims[propertyID]) do
                out[#out + 1] = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
            end
            return table.concat(out, table_separator .. ' ')
        else
            return ""
        end
    else
        return input_parm
    end
end

p.getValueShortName = function(frame)
	local go, errorOrentity, claims, propertyID = parseInput(frame)
	if not go then
		return errorOrentity
	end
	local entity = errorOrentity
	-- if wiki-linked value output as link if possible
	local function labelHook (qnumber)
		local label
		local claimEntity = mw.wikibase.getEntity(qnumber)
		if claimEntity ~= nil then
			if claimEntity.claims.P1813 then
				for k2, v2 in pairs(claimEntity.claims.P1813) do
					if v2.mainsnak.datavalue.value.language == "nb" then
						label = v2.mainsnak.datavalue.value.text
					end
					if v2.mainsnak.datavalue.value.language == "en" then
						label = v2.mainsnak.datavalue.value.text
					end
				end
			end
		end
		if label == nil or label == "" then return nil end
		return label
	end
	return getValue(errorOrentity, claims, propertyID, ", ", labelHook);
end

-- This is used to get a date value for date_of_birth (p569), etc. which won't be linked -- consolidate by testing if entity.claims[propertyID].mainsnak.datavalue.type is "time"
p.getDateValue = function(frame)
	if not mw.wikibase then
        return ""
    end
    local propertyID = mw.text.trim(frame.args[1] or "")
    local input_parm = mw.text.trim(frame.args[2] or "")
    local table_separator = mw.text.trim(frame.args[3] or "")
    if input_parm == "hent_wikidata" then
        local entity = mw.wikibase.getEntityObject()
                if not entity then
        	return ""
    	end
        if entity.claims[propertyID] ~= nil then
        	local date_temp = 'ISOtilNorskdato'
            local out = {}
            local dt = {}
            for k, v in pairs(entity.claims[propertyID]) do
                local d = v.mainsnak.datavalue.value.time
                lyear = string.sub(d, 9, 12)
                lmonth = string.sub(d, 14, 15)
                lday = string.sub(d, 17, 18)
                out[#out + 1] = mw.getCurrentFrame():expandTemplate{ title = date_temp, args = { lyear .. "-" .. lmonth ..  "-" .. lday, 'n' } }
            end
            return table.concat(out, table_separator .. ' ')
        else
            return ""
        end
    else
        return input_parm
    end
end

-- Get the rank of statement
local function getRank(prop)
	local rank = prop.rank
	if rank == 'preferred' then
		return 1
	elseif rank == 'normal' then
		return 0
	elseif rank == 'deprecated' then
		return -1
	else
		return 0 -- No rank or undefined rank is treated as "normal".
	end
end

-- Get the Q-id from the site or arg.
local function getId(id)
    local artikkel
    if not mw.wikibase then
        return ''
    end
    if id then
    	artikkel = mw.wikibase.getEntity(id)
    else
    	artikkel = mw.wikibase.getEntity()
    end
    if not artikkel then
    	return ''
    end
    return artikkel
end

-- Gets all values and add a dot (".") to it.
p.getFile_extension = function(frame)
 	local exts = {}
	
    local artikkel = getId(frame.args[1])
	local fileExten = artikkel:getBestStatements('P1195')
	
	for i, ext in ipairs(fileExten) do
	    exts[#exts+1] = ("." .. ext.mainsnak.datavalue.value)
	end
	return mw.text.nowiki(table.concat(exts, ', '))
end

local function formatEntityId(entityId,i)
    local label
    local link = mw.wikibase.sitelink( entityId )
	if i == 1 then
    	label = mw.wikibase.label( entityId )
    	label = label:gsub("^%l", string.upper)
    else
    	label = mw.wikibase.label( entityId )
    end
    if link then
        if label then
            return '[[' .. link .. '|' .. label .. ']]'
        else
            return '[[' .. link .. ']]'
        end
    elseif label then
        return label --TODO what if no links and label + fallback language?
    else
        return ''
    end
end

-- Gets all values and if multip. add [[Multi paradigm]]: if more then two q-values...
p.paradigme = function(frame)
 	local paradigms = {}
	
    local artikkel = getId(frame.args[1])
    if artikkel == "" then
    	return ""
    end
	local paradigm = artikkel:getBestStatements('P3966')
	local count = 0
	for i, paradig in ipairs(paradigm) do
		if paradig.mainsnak.datavalue.value.id ~= 'Q4306983' then --multi-paradigm programming
			count = count + 1
	    	paradigms[#paradigms+1] = formatEntityId(paradig.mainsnak.datavalue.value.id,count)
	    end
	end

	if paradigms[2] then
		local values = table.concat(paradigms, ', ')
		local multi = '[[' .. mw.wikibase.sitelink('Q188267') .. '|' .. 'Multi-paradigme' .. ']]' .. ': '
		if count >= 5 then
			return string.format([[<div class="mw-collapsible mw-collapsed"><div class="sentrert">%s paradigmer</div> <div class="mw-collapsible-content">%s</div></div>]], 
			count, multi .. values)
    	else
    		return multi .. values
    	end
	elseif paradigms[1] then
    	return paradigms[1]
    elseif paradigm[1] then
    	return '[[' .. mw.wikibase.sitelink('Q188267') .. '|' .. 'Multi-paradigme' .. ']]'
    else
    	return ""
	end
end

-- Filter on rank and qqid. Then return with "," between statements.
local function findBestProperty(entity, pid, qqid, ppid)
    local props = {}
    local propsAlt = {}

    local prop = quickPcall(function ()
	return entity['claims'][pid]
    end)

    if not prop then
        return ''
    end
	
    for i, pro in ipairs(prop) do
        local rank = getRank(pro)
        if rank == 1 and pro.qualifiers and pro.qualifiers[ppid] and pro.qualifiers[ppid][1].datavalue.value.id == qqid  then
            	if pro.qualifiers.P577[1].datatype == 'time' then
            		props[#props+1] = pro.mainsnak.datavalue.value .. ' / ' .. mw.wikibase.renderSnak(pro.qualifiers.P577[1])
            	else
            		props[#props+1] = pro.mainsnak.datavalue.value
            	end
        elseif rank == 0 and pro.qualifiers and pro.qualifiers[ppid] and pro.qualifiers[ppid][1].datavalue.value.id == qqid then
        		propsAlt[#propsAlt+1] = pro.mainsnak.datavalue.value
        			
    	end
    end
	if props[1] then 
		return quickPcall(function () 
			return table.concat(props, ', ') 
			end) 
	elseif propsAlt[1] then 
		return quickPcall(function () 
			return table.concat(propsAlt, ', ') 
			end)
	else
		return ''
	end
end

-- Find the version that you are looking for. Adds it to a collapsed class if over 5.
p.getVersion = function(frame)
    local artikkel = getId(frame.args[1]) -- Args[1] is the Q-id (alt)
    local pid = frame.args.pid -- Property (Pxx) under Q-value (Qxxxx)
    local qQid = frame.args.qqid -- Secound Q-id
    local pPid = frame.args.ppid -- Secound P-id (Property)
    local alle = frame.args.alle -- if 'ja' and if findBestProperty() return nil, get all statements
	
    local priVersions = quickPcall(function () 
			return findBestProperty(artikkel,pid,qQid,pPid)
			end)
    if priVersions == '' and alle == 'ja' and artikkel ~= "" then
		local temp = artikkel:formatPropertyValues( pid, mw.wikibase.entity.claimRanks )
		priVersions = temp['value']
	elseif priVersions == '' or priVersions == nil then
		return ''
    end
    if countpattern(priVersions) > 5 then
		return string.format([[<div class="mw-collapsible mw-collapsed"><div class="sentrert">%s versjoner</div> <div class="mw-collapsible-content">%s</div></div>]], 
		countpattern(priVersions), priVersions)
    else
		return mw.text.nowiki(priVersions)
    end
end

return p