Op den Inhalt sprangen

Modul:Sozial Medien

Vu Wikipedia
Dokumentatioun vum Modul:Sozial Medien [liesen] [änneren] [Versioune weisen]

D'Dokumentatioun fir dëst Modul steet hei ënnendrënner an ass vun der Säit Modul:Sozial Medien/doc hei agebonne ginn. Kuckt och: Hëllef:Moduler.

local function getCatForId( id )
	local title = mw.title.getCurrentTitle()
	local namespace = title.namespace
	if namespace == 0 then
		return '[[Kategorie:Wikipedia-Artikele mat ' .. id .. '-Identifianten]]'
	elseif namespace == 2 and not title.isSubpage then
		return '[[Kategorie:Benotzersäite mat ' .. id .. '-Identifianten]]'
	else
		return '[[Kategorie:Aner Säite mat ' .. id .. '-Identifianten]]'
	end
end

local function append(str, c, length)
	while str:len() < length do
		str = c .. str
	end
	return str
end

local function fbLink( id )
	return '[https://www.facebook.com/' .. id .. ' ' .. id .. ']' .. getCatForId( 'Facebook' )
end

local function bsLink( id )
	return '[https://bsky.app/profile/' .. id .. ' ' .. id .. ']' .. getCatForId( 'Bluesky' )
end

local function twitterLink( id )
	return '[https://twitter.com/' .. id .. ' ' .. id .. ']' .. getCatForId( 'Twitter' )
end

local function instaLink( id )
	return '[https://www.instagram.com/' .. id .. ' ' .. id .. ']' .. getCatForId( 'Instagram' )
end

local function twitchLink( id )
	return '[https://www.twitch.tv/' .. id .. ' ' .. id .. ']' .. getCatForId( 'Twitch' )
end


local function getIdsFromWikidata( itemId, property )
	local ids = {}
	local statements = mw.wikibase.getBestStatements( itemId, property )
	if statements then
		for _, statement in ipairs( statements ) do
			if statement.mainsnak.datavalue then
				table.insert( ids, statement.mainsnak.datavalue.value )
			end
		end
	end
	return ids
end

local function matchesWikidataRequirements( itemId, reqs )
	for _, group in ipairs( reqs ) do
		local property = 'P' .. group[1]
		local qid = group[2]
		local statements = mw.wikibase.getBestStatements( itemId, property )
		if statements ~= nil then
			for _, statement in ipairs( statements ) do
				if statement.mainsnak.datavalue ~= nil then
					if statement.mainsnak.datavalue.value['numeric-id'] == qid then
						return true
					end
				end
			end
		end
	end
	return false
end

local function createRow( id, label, rawValue, link, withUid )
	if link then
		if withUid then
			return '*<span class="nowrap">' .. label .. ' <span class="uid">' .. link .. '</span></span>\n'
		else
			return '*<span class="nowrap">' .. label .. ' ' .. link .. '</span>\n'
		end
	else
		return '* <span class="error">The ' .. id .. ' id ' .. rawValue .. ' is not valid.</span>[[Kategorie:Wikipedia-Artikele mat feelerhaften Autoritéitskontroll-Identifianten (' .. id .. ')]]\n'
	end
end

--In this order: name of the parameter, label, propertyId in Wikidata, formatting function
local conf = {
    { 'Bluesky', '[[Bluesky]]', 12361, bsLink },
    { 'Facebook', '[[Facebook]]', 2013, fbLink },
	{ 'Twitter', '[[Twitter]]', 2002, twitterLink },
	{ 'Instagram', '[[Instagram]]', 2003, instaLink },
	{ 'Twitch', '[[Twitch]]', 5797, twitchLink }
}

-- Check that the Wikidata item has this property-->value before adding it
local reqs = {}

local p = {}

function p.authorityControl( frame )
	local parentArgs = frame:getParent().args
	--Create rows
	local elements = {}

	--redirect PND to GND
	if (parentArgs.GND == nil or parentArgs.GND == '') and parentArgs.PND ~= nil and parentArgs.PND ~= '' then
		parentArgs.GND = parentArgs.PND
	end

	--Wikidata fallback if requested
	local itemId = mw.wikibase.getEntityIdForCurrentPage()
	if itemId ~= nil then
		for _, params in ipairs( conf ) do
			if params[3] ~= 0 then
				local val = parentArgs[params[1]]
				if not val or val == '' then
					local canUseWikidata = nil
					if reqs[params[1]] ~= nil then
						canUseWikidata = matchesWikidataRequirements( itemId, reqs[params[1]] )
					else
						canUseWikidata = true
					end
					if canUseWikidata then
						local wikidataIds = getIdsFromWikidata( itemId, 'P' .. params[3] )
						if wikidataIds[1] then
							parentArgs[params[1]] = wikidataIds[1]
						end
					end
				end
			end
		end
	end

	--Configured rows
	local rct = 0
	for _, params in ipairs( conf ) do
		local val = parentArgs[params[1]]
		if val and val ~= '' then
			table.insert( elements, createRow( params[1], params[2] .. ':', val, params[4]( val ), true ) )
			rct = rct + 1
		end
	end
	local Navbox = require('Module:Navbox')
	local elementscats = ''
	if rct > 13 then
		elementscats  = '[[Kategorie:AK mat ' .. rct .. ' Elementer]]'
	end

	if #elements ~= 0 then
		return Navbox._navbox( {
			name  = 'Sozial Medien',
			bodyclass = 'hlist',
			group1 = 'Sozial Medien' .. elementscats,
			list1 = table.concat( elements )
			} )
	else
		return ""
	end
end

return p