Modul:External links

Fra Wikisida.no
Sideversjon per 2. apr. 2016 kl. 14:11 av nb>Stigmj (some changes for callables)
Hopp til navigering Hopp til søk

Modulen "External links" inneholder følgende metoder som er ment brukt via maler

getLinks
Henter eksterne lenker fra Wikidata i henhold til en konfigurasjonsfil
Funksjonen har noen få felles standard-parametre som kan benyttes
conf
Funksjonen kalles opp slik: {{#invoke:External links|getLinks|conf=...}}
Den vil da returnere en rekke formaterte lenker til eksterne sider i henhold til en konfigurasjon som ligger som undersider av denne modulen.
Følgende konfigurasjoner er tilgjengelige
  • Arter - Artslenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Arter}}. Se {{Artslenker}} for eksempel på bruk.
  • Astronomi - Astronomi, Kalles opp slik: {{#invoke:External links|getLinks|conf=Astronomi}}. Se {{Astronomilenker}} for eksempel på bruk.
  • Autoritetsdata - Autoritetsdata, Kalles opp slik: {{#invoke:External links|getLinks|conf=Autoritetsdata}}. Se {{Autoritetsdata}} for eksempel på bruk.
  • Bryggeri - Bryggerilenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Bryggeri}}. Se {{Bryggerilenker}} for eksempel på bruk.
  • Film - Filmlenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Film}}. Se {{Filmlenker}} for eksempel på bruk.
  • Filmperson - Filmpersonlenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Filmperson}}. Se {{Filmperson}} for eksempel på bruk.
  • Musikk - Musikklenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Musikk}}. Se {{Musikklenker}} for eksempel på bruk.
  • Offisielle lenker - Offisielle lenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Offisielle lenker}}. Se {{Offisielle lenker}} for eksempel på bruk.
  • Politiker - Politiker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Politiker}}. Se {{Politiker}} for eksempel på bruk.
  • Some - Sosiale medie-lenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Some}}. Se {{Somelenker}} for eksempel på bruk.
  • Spill - Spill-lenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Spill}}. Se {{Spill-lenker}} for eksempel på bruk.
  • Sport - Sportslenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Sport}}. Se {{Sportslenker}} for eksempel på bruk.
  • Skip - Skipslenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Skip}}. Se {{Skipslenker}} for eksempel på bruk.
  • Forsker - Forskerlenker, Kalles opp slik: {{#invoke:External links|getLinks|conf=Forsker}}. Se {{Forskerlenker}} for eksempel på bruk.
  • Test - For testing av nye typer og utvikling, Kalles opp slik: {{#invoke:External links|getLinks|conf=Test}}. Skal ikke benyttes i produksjon.
tittel
Funksjonen kalles opp slik: {{#invoke:External links|getLinks|conf=<konfigurasjon ihht. ovenfor>|tittel=...}}
Denne parameteren kan overstyre artikkelnavnet som benyttes videre i modulen. Hvis dette ikke er satt, vil den som standard bruke sidenavnet til artikkelen.
maxlink
Funksjonen kalles opp slik: {{#invoke:External links|getLinks|conf=<konfigurasjon ihht. ovenfor>|maxlink=...}}
Denne parameteren setter maksimum antall lenker som vises frem. Standardverdien er satt til 10.
prop
Funksjonen kalles opp slik: {{#invoke:External links|getLinks|conf=<konfigurasjon ihht. ovenfor>|prop=<egenskap>,<egenskap2>,...}}
Denne parameteren setter en begrensning på hvilke egenskaper som skal vises frem. De skilles av med et komma, mellomrom, semikolon eller kolon. Dersom egenskapen starter med et minus-tegn (-), f.eks. -P345, så vil alle egenskaper, unntatt den som er angitt forsøkes vist frem. Det er mulig å legge inn flere av disse.
kort
Funksjonen kalles opp slik: {{#invoke:External links|getLinks|conf=<konfigurasjon ihht. ovenfor>|kort=1}}
Denne parameteren bestemmer om kort-versjonen av lenkene skal benyttes.
inline
Funksjonen kalles opp slik: {{#invoke:External links|getLinks|conf=<konfigurasjon ihht. ovenfor>|inline=1}}
Denne parameteren bestemmer om lenkene skal legges på en linje med en separator i mellom seg. Linjen vil ikke prefikses med noen bullet eller lignenden. Se definisjonen av separatoren i Module:External links/conf under ['msg-inline-separator']. Denne funksjonaliteten er ment brukt i tilfeller hvor man ønsker å benytte lenkene i referanser eller lignende.
språk
Funksjonen kalles opp slik: {{#invoke:External links|getLinks|conf=<konfigurasjon ihht. ovenfor>|språk=<langcode1>,<langcode2>...}}
Denne parameteren setter en begrensning på hvilke språk som skal vises frem. De skilles av med et komma, mellomrom, semikolon eller kolon. Dersom språkkoden starter med et minus-tegn (-), f.eks. -fr, så vil alle språkkoder, unntatt den vises frem. Det er mulig å legge inn flere av disse. Spesial-verdien alle overstyrer og tillater alle språk.
track
Funksjonen kalles opp slik: {{#invoke:External links|getLinks|conf=<konfigurasjon ihht. ovenfor>|track=<egenskap1>,<egenskap2>...}}
Denne parameteren styrer individuelle sporingskategorier for hver egenskap. De skilles av med et komma, mellomrom, semikolon eller kolon. Dersom egenskapen starter med et minus-tegn (-), f.eks. -P345, så vil den ikke spores (i tilfelle den er konfigurert til å spores i felleskonfigurasjonen). Det er mulig å legge inn flere av disse, og spesial-koden alle vil slå på sporing på alle.
<egenskapsnavn>
Funksjonen kalles opp slik: {{#invoke:External links|getLinks|conf=<konfigurasjon ihht. ovenfor>|<egenskapsnavn>=<verdi>}}
Dette er en generell måte å sette verdien til en egenskap manuelt fra artikler/maler. Dette er ment brukt for tilfeller hvor man bruker en lokalt definert egenskap og trenger å gi den en verdi fra artikkelen. F.eks. bbfc=46443 vil gi ID-en til Blue Thunder i BBFc sin database, og den manuelle definisjonen av bbfc i Module:External links/conf/Film vil da benyttes sammen med den ID-en for å bygge den eksterne lenken.

Se også


require('Module:No globals')
local genitiv = require('Modul:Genitiv')._genitiv
local contLang = mw.language.getContentLanguage()

local cmodule = {}
local conf = require 'Module:External links/conf'(contLang:getCode())

local p = {}

local function getLabel(entity, use_genitiv, pagetitle)
	local label = (pagetitle and pagetitle ~= '') and pagetitle or nil
	if not label and not entity then
		label = mw.title.getCurrentTitle().text
	elseif not label then
		label = mw.wikibase.label(entity.id) or mw.title.getCurrentTitle().text
	end
	return use_genitiv and genitiv(label) or label
end

-- @todo cleanup, this is in production, use the console
local function dump(obj)
	return "<pre>" .. mw.dumpObject(obj) .. "</pre>"
end


local function stringFormatter( datavalue )
	if datavalue == nil or datavalue['type'] ~= 'string' then
		return nil
	end
	return datavalue.value
end

local qual = {}
qual.P1793 = {
	types = {
		snaktype = 'value',
		datatype = 'string',
	},
}


local qorder = {'P1793'}

-- This is a really makeshift crappy converter, but it'll do some basic
-- conversion from PCRE to Lua-style patterns (note that this only work
-- in very few cases)
local function regexConverter( regex )
	local output = regex
	output = string.gsub(output, "\\d{2}", "%%d%%d")
	output = string.gsub(output, "\\d{3}", "%%d%%d%%d")
	output = string.gsub(output, "\\d{4}", "%%d%%d%%d%%d")
	output = string.gsub(output, "\\d{5}", "%%d%%d%%d%%d%%d")
	output = string.gsub(output, "\\d{6}", "%%d%%d%%d%%d%%d%%d")
	output = string.gsub(output, "\\d{7}", "%%d%%d%%d%%d%%d%%d%%d")
	output = string.gsub(output, "\\d{8}", "%%d%%d%%d%%d%%d%%d%%d%%d")
	output = string.gsub(output, "\\d", "%%d")
	
	return output
end


local function getFormatterUrl( prop, value )
	local head = ""
	local tail = ""
	local entity = mw.wikibase.getEntity(prop)
	-- to avoid deep tests
	if not entity or not entity.claims then
		return head
	end
	-- get the claims for this entity
	local statements = entity.claims['P1630']
	-- to avoid deep tests
	if not statements then
		return head
	end
	local formatters = {}
	-- let's go through the claims
	for _, claim in ipairs( statements ) do
		-- to avoid deep tests
		if not claim then
			claim = {}
		end
		local valid = claim['type'] == 'statement'
			and claim['rank'] ~= 'deprecated'
		if valid then
			local mainsnak = claim.mainsnak or {}
			--mw.log("mainsnak = " .. dump(mainsnak))
			local preferred = claim['rank'] == 'preferred'
			-- get any qualifiers for this claim (we are interested in P1793 for
			-- indication of which claim is correct) 
			local qualifiers = claim.qualifiers or {}
			-- now go through the qualifiers we are interested in (defined in
			-- the array qorder above this function)
			for _, qualid in ipairs( qorder ) do
				-- if the claim has this qualifier
				if qualifiers[qualid] then
					-- it's here, let's check it out!
					local items = {}
					-- traverse all snaks in this qualifier
					for _, qualsnak in ipairs( qualifiers[qualid] ) do
						if qualsnak and qual[qualid] and qualid == 'P1793' then
							--mw.log("qualsnak = " .. dump(qualsnak))
							-- check if the snak is of the correct snaktype and datatype
							local valid = qualsnak.snaktype == qual[qualid].types.snaktype
								and qualsnak.datatype == qual[qualid].types.datatype
							if valid then
								-- it is, so let's check if it matches the url
								-- (P1793 = regex expression)
								--mw.log("qualsnak.datavalue="..dump(qualsnak.datavalue))
								-- we'll have to convert the regex to Lua-style
								local regex = regexConverter(qualsnak.datavalue.value)
								--mw.log("regex="..dump(regex) .. " and value=" .. dump(value))
								local test = string.match( value, '^'..regex..'$' )
								--mw.log("testing with "..regex.. " and test="..dump(test).." and value="..value)
								if test then
									-- it matched, this is correct and overrides any other.
									if preferred then
										head = mainsnak.datavalue.value
									else
										tail = mainsnak.datavalue.value
									end
								end
							end
						end
					end
				else
					-- we don't have any qualifier, is it preferred?
					if (head == '' and preferred) or (tail == '' and not preferred) then
						-- if we don't have any other, use this one
						if preferred and head == '' then
							head = mainsnak.datavalue.value
						elseif not preferred and tail == '' then
							--mw.log("mainsnak.datavalue="..dump(mainsnak.datavalue))
							tail = mainsnak.datavalue.value
						end
					end
				end
			end
		end
	end
	--mw.log("head="..head.." and tail="..tail)
	return head ~= '' and head or tail

end


local function findMainLinksOnWikidata(props, pagetitle, short_links)
	local output = {}
	-- get the entity we are checking
	local entity = mw.wikibase.getEntityObject()
	-- to avoid deep tests
	if not entity then
		return nil
	end
	if not entity.claims then
		return output
	end
	-- get the claims for this entity
	local statements = entity.claims[props.prop]
	-- to avoid deep tests
	if not statements then
		return output
	end
	-- let's go through the claims
	for _, claim in ipairs( statements ) do
		-- to avoid deep tests
		if not claim then
			claim = {}
		end
		local valid = claim['type'] == 'statement'
			and claim['rank'] ~= 'deprecated'
		if valid then
			local mainsnak = claim.mainsnak or {}
			local preferred = claim['rank'] == 'preferred'
			-- get the content of the claim (the identifier)
			local value = stringFormatter(mainsnak.datavalue)
			local verified_value = nil
			if props.regex then
				-- we have a local defined regex, so this will have to pass first
				-- maybe we'll have to convert the regex to Lua-style
				local regex = regexConverter(props.regex)
				local test = string.match( value, '^'..regex..'$' )
				--mw.log("testing with "..regex.. " and test="..dump(test).." and value="..id)
				if test then
					-- it matched, this is correct and overrides any other.
					--mw.log("verified ID")
					verified_value = value
				end
			else
				verified_value = value
			end
			--mw.log("value="..dump(value))
			if verified_value then
				local url = ''
				if props.url_f then
					-- we have a local defined url-formatter function, use it as first priority
					url = props.url_f(verified_value)
				elseif props.url then
					-- we have a local defined url-formatter string, use it as second priority
					url = mw.message.newRawMessage(props.url, verified_value):plain()
				else
					-- get the formatvalue from the property, if it exists
					local formatterUrl = getFormatterUrl(props.prop, verified_value)
					if formatterUrl ~= '' then
						url = mw.message.newRawMessage(formatterUrl, verified_value):plain()
					end
				end
				--mw.log("formatterUrl="..dump(formatterUrl))
				if url ~= '' then
					output[#output+1] = {}
					output[#output].langcode = props.langcode
					if short_links and props.short then
						output[#output].text =
							mw.message.newRawMessage(props.short,
								getLabel(entity, props.genitiv, pagetitle),
								url)
							:plain()
					else
						output[#output].text =
							mw.message.newRawMessage(props.message,
								getLabel(entity, props.genitiv, pagetitle),
								url)
							:plain()
					end
				end
			end
		end
	end
	--mw.log("returning output="..dump(output))
	return output
end


local function findMainLinksLocal(props, pagetitle, short_links, id)
	local output = {}
	-- to avoid deep tests
	if not props.prop then
		return nil
	end
	if not id then
		return output
	end
	-- mw.log("i'm in with props="..dump(props).. " and pagetitle="..dump(pagetitle).." and short_links="..dump(short_links).." and id="..dump(id))
	-- get the formatvalue from the property
	local verified_id = nil
	if props.regex then
		-- let's verify the id
		-- maybe we'll have to convert the regex to Lua-style
		local regex = regexConverter(props.regex)
		local test = string.match( id, '^'..regex..'$' )
		--mw.log("testing with "..regex.. " and test="..dump(test).." and value="..id)
		if test then
			-- it matched, this is correct
			--mw.log("verified ID")
			verified_id = id
		end
	end
	if not verified_id then
		return output
	end
	if (props.url and props.url ~= '') or (props.url_f) then
		--mw.log("entering output..")
		output[#output+1] = {}
		output[#output].langcode = props.langcode
		local url = ''
		if props.url_f then
			-- we have a local defined url-formatter function, use it as first priority
			url = props.url_f(id)
		elseif props.url then
			-- we have a local defined url-formatter string, use it as second priority
			url = mw.message.newRawMessage(props.url, id):plain()
		else
			-- no url-formatters defined, bail out
			return {}
		end
		if short_links and props.short then
			output[#output].text =
				mw.message.newRawMessage(props.short,
					getLabel(nil, props.genitiv, pagetitle),
					url)
				:plain()
		else
			output[#output].text =
				mw.message.newRawMessage(props.message,
					getLabel(nil, props.genitiv, pagetitle),
					url)
				:plain()
		end
	end
	-- mw.log("returning output="..dump(output))
	return output
end


local function addLinkback(str, property)
	local id = mw.wikibase.getEntityObject()
	if not id then
		return str
	end
	if type(id) == 'table' then
		id = id.id
	end
	
	local class = ''
	local url = ''
	if property then
		class = 'wd_' .. string.lower(property)
		url = mw.uri.fullUrl('d:' .. id .. '#' .. property)
		url.fragment = property
	else
		url = mw.uri.fullUrl('d:' .. id )
	end
	
	local title = conf:g('wikidata-linkback-edit')
	local icon = '[%s [[File:Blue pencil.svg|%s|10px|baseline|link=]] ]'
	url = tostring(url)
	local v = mw.html.create('span')
		:addClass(class)
		:wikitext(str)
		:tag('span')
			:addClass('noprint plainlinks wikidata-linkback')
			:css('padding-left', '.5em')
			:wikitext(icon:format(url, title))
		:allDone()
	return tostring(v)
end

local function getArgument(frame, argument)
	local args = frame.args
	if args[1] == nil then
		local pFrame = frame:getParent();
		args = pFrame.args;
		for k,v in pairs( frame.args ) do
			args[k] = v;
		end
	end
	if args[argument] then
		return args[argument]
	end
	return nil
end

function p.getLinks(frame)
	local configured_conf = getArgument(frame, conf:a('arg-conf'))
	if configured_conf then
		cmodule = require ('Module:External_links/conf/'..configured_conf)
	else
		error(mw.message.newRawMessage(conf:g('missing-conf'), configured_conf):plain())
	end
	local output = {}
	local conf_claims = cmodule:getConfiguredClaims(contLang:getCode())
	local limits = cmodule:getLimits()
	assert(limits, mw.message.newRawMessage(conf:g('missing-limits'), configured_conf):plain())
	local links_shown = getArgument(frame, conf:a('arg-maxlink'))
	local pagetitle = getArgument(frame, conf:a('arg-title'))
	-- get a list of "approved" properties from the article itself
	local requested_properties = getArgument(frame, conf:a('arg-properties'))
	-- assume all properties are allowed
	local req_prop = {}
	if requested_properties and requested_properties ~= '' then
		-- the properties should be written as P1234, P2345 and other 
		-- version corresponding to the applicable property-identifiers in the config
		for i in string.gmatch(requested_properties,"([^ ,;:]+)") do
			-- get the requested properties and be able to access them
			-- like req_prop['P345'] to verify if it was requested
			req_prop[i] = 1
			-- cheat to make #req_prop indicate populated table
			req_prop[1] = 1
		end
	end
	local short_links = getArgument(frame, conf:a('arg-short'))
	if short_links and short_links ~= '' then
		short_links = true
	else
		short_links = false
	end
	if not links_shown or links_shown == '' then
		links_shown = limits['links-shown'] and limits['links-shown'] or 10
	else
		links_shown = tonumber(links_shown)
	end
	--mw.log(dump(conf_claims))
	local somedataonwikidata = (short_links and false or true)
	for _, props in ipairs(conf_claims) do
		-- if we're called with a list of approved properties, check if this one is "approved"
		if #req_prop==0 or req_prop[props.prop] then
			local links = {}
			local checkedonwikidata = false
			if string.find(props.prop, "([pP]%d+)") then
				-- the property is a Pnnn type, and therefore on Wikidata
				links = findMainLinksOnWikidata(props, pagetitle, short_links)
				checkedonwikidata = true
			else
				-- the property is of another annotation, and therefore a local construct
				-- get the value from the call
				local wikivalue = getArgument(frame, props.prop)
				--mw.log("props.prop="..props.prop.." wikivalue="..wikivalue)
				if wikivalue then
					links = findMainLinksLocal(props, pagetitle, short_links, wikivalue)
				end
			end
			if links == nil then
				return cmodule:getNoWikilink(contLang:getCode())
			end
			for _,v in ipairs(links) do
				--  calling out to templates are bad style
				if short_links and props.short then
					-- if short links were requested, and a short definition exists for this property, let's use it
					if #output==0 then
						output[#output+1] = v.text
					else
						output[#output] = output[#output] .. cmodule:getMessage(contLang:getCode(),'short-list-separator') .. v.text
					end
					somedataonwikidata = true
				elseif not short_links then
					-- only if short links were not requested
					output[#output+1] = "* "
						.. (v.langcode and frame:expandTemplate{ title="Språkikon", args = { v.langcode } } .. " " or "")
						.. (checkedonwikidata and addLinkback(v.text, props.prop) or v.text)
				end
				if links_shown>0 then
					links_shown = links_shown - 1
				else
					break
				end
			end
			if links_shown==0 then
				break
			end
		end
	end
	if short_links and #output then
		-- if these are short links, output the whole thing with linkback to wikidata
		output = (somedataonwikidata and addLinkback(output[#output], nil) or output[#output])
	elseif not short_links then
		output = table.concat(output,"\n")
	else
		output = ""
	end
	return cmodule:getText(contLang:getCode(), output) .. cmodule:getCategory(contLang:getCode(), output)
end

return p