Modul:Benutzer:MisterSynergy/pullfromwikidata

aus Wikipedia, der freien Enzyklopädie

Die Dokumentation für dieses Modul kann unter Modul:Benutzer:MisterSynergy/pullfromwikidata/Doku erstellt werden

-- in use at: [[User:MisterSynergy/pullfromwikidata]] (template); [[User:MisterSynergy/pullfromwikidata.js]] (js script using the template)

local pullFromWikidata = {}

pullFromWikidata.allExternalIdsLinked = function(frame)
	local entity = mw.wikibase.getEntity() -- check whether the article is connected to Wikidata
	if not entity then
		mw.log('pullFromWikidata.allExternalIdsLinked(): article not connected')
		return {}
	end

	local externalIdProperties = {}
	for i, property in ipairs(entity:getProperties()) do
		local propObject = mw.wikibase.getEntity(property)
		if propObject.datatype == 'external-id' then
			local formatters = {}
			local formatterClaims = propObject:getBestStatements('P1630')
			for j, formatterClaim in ipairs(formatterClaims) do
				if formatterClaim.rank ~= 'deprecated' and formatterClaim.mainsnak.snaktype == 'value' then
					table.insert(formatters, formatterClaim.mainsnak.datavalue.value)
				end
			end
			externalIdProperties[property] = formatters
		end
	end
--[=[	
	for externalIdProperty, formatters in pairs(externalIdProperties) do
		mw.log(externalIdProperty .. ' -- formatters: ' .. table.concat(formatters, ' -- '))
	end
	mw.log('----')
]=]	
	
	local claims = entity.claims or {}
	if claims == {} then
		mw.log('pullFromWikidata.allExternalIdsLinked(): no claims found')
		return {}
	end
	
	local identifiers = {}
	for property, claim in pairs(claims) do
		if externalIdProperties[property] ~= nil then
			if identifiers[property] == nil then
				identifiers[property] = {}
				identifiers[property]['formatters'] = externalIdProperties[property]
				identifiers[property]['identifiers'] = {}
			end
			propClaims = claims[property]
			for j, propClaim in ipairs(propClaims) do
				if propClaim.rank ~= 'deprecated' then
					local identifier = {}
					identifier['snaktype'] = propClaim.mainsnak.snaktype
					if propClaim.mainsnak.snaktype == 'value' then
						identifier['value'] = propClaim.mainsnak.datavalue.value
					else
						identifier['value'] = ''
					end
					table.insert(identifiers[property]['identifiers'], identifier)
				end
			end
		end
	end
--[=[	
	for property, values in pairs(identifiers) do
		mw.log(property .. ':')
		for i, formatter in ipairs(values['formatters']) do
			mw.log('Formatter: ' .. formatter)
		end
		for j, identifier in ipairs(values['identifiers']) do
			if identifier.snaktype == 'value' then
				mw.log('Identifier: ' .. identifier.value)
			end
		end
	end
]=]	
	return identifiers
end

local p = {}

function p.allExternalIdsLinked(frame)
	local success, identifiers = pcall(pullFromWikidata.allExternalIdsLinked, frame)
	local wikitext = ''
	for property, values in pairs(identifiers) do
		if wikitext == '' then
			wikitext = '<ul>\n'
		end
		arguments = {}
		arguments['1'] = property
		wikitext = wikitext .. '<li><span>' .. frame:expandTemplate{ title='Wikidata', args=arguments } .. ':</span><ul class="mistersynergy-pullfromwikidata-innerlist">'
		for j, identifier in ipairs(values['identifiers']) do
			wikitext = wikitext .. '<li>'
			if identifier.snaktype == 'value' then
				for i, formatter in ipairs(values['formatters']) do
					if i > 1 then
						wikitext = wikitext .. ', '
					end
					wikitext = wikitext .. '[' .. string.gsub(formatter, '$1', string.gsub(identifier.value, ' ', '+'), 1) .. ' ' .. identifier.value .. ']'
				end
			else
				wikitext = wikitext .. "''" .. identifier.snaktype .. "''"
			end
			wikitext = wikitext .. '</li>'
		end
		wikitext = wikitext .. '</ul></li>\n'
--		for i, formatter in ipairs(values['formatters']) do
--			mw.log('Formatter: ' .. formatter)
--		end
	end
	if wikitext ~= '' then
		wikitext = wikitext .. '</ul>\n'
	end
	return wikitext or ''
end

function p.pullFromWikidata()
	return pullFromWikidata
end

return p