Die Dokumentation für dieses Modul kann unter Modul:Wikidata2/doc erstellt werden

local check		= require('Modul:Check2')
local yesno		= require('Modul:Yesno')
local languages	= require('Modul:Languages')

local cfgI18		= mw.loadData('Modul:Wikidata2/i18n')
local cfgParams		= mw.loadData('Modul:Wikidata2/Params')
local cfgDelimiters	= mw.loadData('Modul:Wikidata2/Delimiters')
local cfgFormats	= mw.loadData('Modul:Wikidata2/Formats')
local cfgPOITypes	= mw.loadData('Modul:Wikidata2/POITypes')
local cfgGeography	= mw.loadData('Modul:Wikidata2/Geography')
local cfgUnits		= mw.loadData('Modul:Wikidata2/Units')

local lang = mw.language.getContentLanguage()

-- Auswertung und Prüfung von übergebenen Parametern
local function wdGetParameter ( Frame, Params, Parameter )
	local argsParent = Frame:getParent().args
	local argsFrame = Frame.args
	for key,value in pairs ( argsParent ) do
		argsParent[string.lower(key)] = value
	end
	for key,value in pairs ( argsFrame ) do
		argsParent[string.lower(key)] = value
	end
	return argsParent[Parameter] or argsFrame[Parameter] or Params[Parameter]
end

local function getEntityId ()
	local pageID = mw.wikibase.getEntityIdForCurrentPage()
	local result = {}
	if ( pageID ~= nil ) then
		result = { 
			["id"]		= pageID, 
			["hasID"]	= true, 
			["errCode"] = 1, 
			["errMsg"]  = cfgI18["getEntityId"]["1"]
			}
	else
		result = { 
			["id"]		= "", 
			["hasID"]	= false, 
			["errCode"] = 0, 
			["errMsg"]  = cfgI18["getEntityId"]["0"]
			}
	end
	return result
end

local wdEntityId = getEntityId()
local wdErrCategory = ''

local wd = {}

function wd.formatNumberT ( number )
	local decimalPosition = mw.ustring.find( number, ".", 1, true)
	local minus = mw.ustring.find( number, "-", 1, true)

	number = mw.ustring.gsub ( number, '%.', ',' )
	if ( minus ) then number = mw.ustring.sub( number, 2) end 
	if (not decimalPosition) then
		decimalPosition = mw.ustring.len ( number ) + 1;
	end
	if (decimalPosition > 4 ) then
		local i = decimalPosition - 3;
		while (i > 1) do
			number = mw.ustring.sub(number, 1, i - 1) .. '.' .. mw.ustring.sub( number, i );
			decimalPosition = decimalPosition + 1;
			i = i - 3;
		end
	end
	if ( minus ) then number = '−' .. number end
	return number;
end

function wd.getLabel ( id )
	if ( id == nil or id == "" or id == "self" ) then 
		if ( wdEntityId.hasID ) then 
			id = wdEntityId.id
		else 
			return { 
				["label"]	= '', 
				["errCode"]		= 2, 
				["errMsg"]		= cfgI18["getLabel"]["2"]
				}
		end
	end
	local result	= {}
	local label = mw.wikibase.label ( id )
	if ( label ~= nil  ) then 
		result = { 
			["label"]	= label, 
			["errCode"] = 1, 
			["errMsg"]  = cfgI18["getLabel"]["1"]
			}
	else 
		result = { 
			["label"]	= "", 
			["errCode"] = 2, 
			["errMsg"]  = cfgI18["getLabel"]["2"]
			}
	end
	return result
end

function wd.getLabelLang ( id, lang )
	if ( id == nil or id == "" or id == "self" ) then
		if ( wdEntityId.hasID ) then 
				id = wdEntityId.id
		else 
			return { 
				["label"]	= "", 
				["errCode"] = 2, 
				["errMsg"]  = cfgI18["getLabelLang"]["2"]
				}
		end
	end
	local result	= {}
	local entityLabel = mw.wikibase.getLabelByLang( id, lang )
	if ( entityLabel ~= nil  ) then
		result = { 
			["label"]	= entityLabel, 
			["errCode"] = 1, 
			["errMsg"]  = cfgI18["getLabelLang"]["1"]
		}
	else
		result = { 
			["label"]	= "", 
			["errCode"] = 0, 
			["errMsg"]  = cfgI18["getLabelLang"]["0"]
			}
	end
	return result
end

function wd.getSiteLink ( id, wiki )
	if ( wiki == nil ) then wiki = 'dewikivoyage' end
	if ( id == nil or id == "" or id == "self" ) then 
		if ( wdEntityId.hasID ) then 
				id = wdEntityId.id
		else 
			return { 
				["sitelink"]	= '', 
				["errCode"]		= 2, 
				["errMsg"]		= cfgI18["getSiteLink"]["2"]
				}
		end
	end
	local result	= {}
	local sitelink = mw.wikibase.getSitelink ( id, wiki )
	if ( sitelink == nil ) then
		result = { 
			["sitelink"]	= '', 
			["errCode"]		= 0, 
			["errMsg"]		= cfgI18["getSiteLink"]["0"]
		}
	else
		result = { 
			["sitelink"]	= sitelink, 
			["errCode"]		= 1, 
			["errMsg"]		= cfgI18["getSiteLink"]["1"]
			}
	end
	return result
end

-- zählt die Einträge einer Eigenschaft
function wd.getPropertyCount ( claim )
	return #claim
end

function wd.getBestStatement ( id, property )
	if ( id == nil or id == "" or id == "self" ) then
		if ( wdEntityId.hasID ) then
			id = wdEntityId.id
		else 
			return { 
				["statement"]	= {}, 
				["errCode"]		= 2, 
				["errMsg"]		= cfgI18["getStatement"]["2"],
				["count"]		= 0
				}
		end
	end
	local result	= {}
	local statement = mw.wikibase.getBestStatements( id, property )
	if ( next(statement) == nil ) then
		result = { 
			["statement"]	= {}, 
			["errCode"]		= 0, 
			["errMsg"]		= cfgI18["getStatement"]["0"],
			["count"]		= 0
		}
	else
		result = { 
			["statement"]	= statement, 
			["errCode"]		= 1, 
			["errMsg"]		= cfgI18["getStatement"]["1"],
			["count"]		= wd.getPropertyCount ( statement )
			}
	end
	return result
end

function wd.getStatementAll ( id, property )
	if ( id == nil or id == "" or id == "self" ) then
		if ( wdEntityId.hasID ) then
			id = wdEntityId.id
		else 
			return { 
				["statement"]	= {}, 
				["errCode"]		= 2, 
				["errMsg"]		= cfgI18["getStatement"]["2"],
				["count"]		= 0
				}
		end
	end
	local result	= {}
	local statement = mw.wikibase.getAllStatements( id, property )
	if ( next(statement) == nil ) then
		result = { 
			["statement"]	= {}, 
			["errCode"]		= 0, 
			["errMsg"]		= cfgI18["getStatement"]["0"],
			["count"]		= 0
		}
	else
		result = { 
			["statement"]	= statement, 
			["errCode"]		= 1, 
			["errMsg"]		= cfgI18["getStatement"]["1"],
			["count"]		= wd.getPropertyCount ( statement )
			}
	end
	return result
end


function wd.getPlainWikidataId (id)
    if (( id == nil or id == "" or id == "self" ) and wdEntityId.hasID) then
        return wdEntityId.id
    else
        return id
    end
end

function wd.getWikidataLink (id, property)
    local uri = 'wikidata:' .. wd.getPlainWikidataId(id)
    if (property ~= nil) then
        uri = uri .. '#' .. property
    end
    return uri
end

function wd.Entity ()
	return wdEntityId
end

function wd.EntityId ()
	return wdEntityId.id
end

function wd.hasEntity ()
	return wdEntityId.hasID
end

function wd.EntityIdErrCode ()
	return wdEntityId.errCode
end

function wd.EntityIdErrText ()
	return wdEntityId.errMsg
end

function wd.Label ()
	return wd.getLabel().label
end

function wd.getQualifier ( property, qualifier, formatting, delimiter )
	local formatString = formatting or ''
	local delimiterString = delimiter or ','
	local result = {
		["errCode"]	 = 0,
		["errText"]	 = '',
		["qualifier"]  = ''
	}

	local wdValue
	if property.qualifiers ~=  nil then
		if property.qualifiers[qualifier] ~= nil then
			local out = {}
			for k,v in pairs(property.qualifiers[qualifier]) do
				if ( v.datavalue.type == 'time' ) then 
					wdValue = v.datavalue.value.time
					-- Workaround für Timestamps, die nur ein Jahr repräsentieren
					wdValue = mw.ustring.gsub ( wdValue, '%-00%-00T00:00:00Z', '-01-01T00:00:00Z' )
					if ( formatString ~= '' ) then
						wdValue = lang:formatDate ( formatString, wdValue )
					end
					out[#out + 1] = wdValue
				end
				if ( v.datavalue.type == 'wikibase-entityid' ) then 
					wdValue = v.datavalue.value.id
					out[#out + 1] = wdValue
				end
			end
			result.errCode = 1
			result.errText = cfgI18["getQualifier"]["1"]
			result.qualifier = table.concat(out, delimiterString)
		else
			result.errText = cfgI18["getQualifier"]["0"]
		end  
	else
		result.errText = cfgI18["getQualifier"]["0"]
	end

	return result
end

function wd.getLangQualifier ( property )
	
	local result = ''

	if property.qualifiers ~=  nil then
		if property.qualifiers.P407 ~= nil then
			if property.qualifiers.P407[1] ~= nil then
				if property.qualifiers.P407[1].datavalue ~= nil then
					if property.qualifiers.P407[1].datavalue.value ~= nil then
						if property.qualifiers.P407[1].datavalue.value.id ~= nil then
							local langID = property.qualifiers.P407[1].datavalue.value.id
							result = languages.id2code[langID] or 'no_lang'
							if languages.id2code[langID] == nil then 
								wdErrCategory = wdErrCategory .. '[[Kategorie:Wikidata:Unbekannter Sprachcode]]' 
							end
						else
							result = 'no_lang'
						end
					else
						result = 'no_lang'
					end
				else
					result = 'no_lang'
				end
			else
				result = 'no_lang'
			end
		else
			result = 'no_lang'
		end
	else
		result = 'no_lang'
	end

	return result
end

-- helper function round
-- n: value to round
-- idp: number of digits after the decimal point
local function round( n, idp )
	local m = 10^( idp or 0 )
	if n >= 0 then
		return math.floor( n * m + 0.5 ) / m
	else
		return math.ceil( n * m - 0.5 ) / m
	end
end

function wd.getProperty ( claim, formatting, showRanking, label, parameters )
	
	if ( claim[1] ~= nil ) then	
		
		if ( claim[1].mainsnak.snaktype == 'novalue' ) then return '' end
			
		local wdValue  = ''
		local unit

		local delimiter = cfgDelimiters[claim[1].mainsnak.property]
		if ( delimiter == nil ) then delimiter = cfgDelimiters["default"] end

		if ( formatting == nil ) then formatting = 'default' end
		local formatString = cfgFormats[formatting] or cfgFormats["default"]
		local formatStringParameters = mw.ustring.match ( formatString, '//.-#' )
		if ( formatStringParameters ~= nil ) then
			formatString = string.sub ( formatString, 1 , string.find ( formatString, formatStringParameters ) - 1 ) .. '#'
		end

		local qualifierParameters = mw.ustring.match ( formatString, '#QUALIFIER:.-#' ) or ''
		local qualifierParameter = mw.ustring.gsub( qualifierParameters, '#QUALIFIER:(.-):(.-)#', '%1' ) or ''
		local qualifierFormat = mw.ustring.gsub( qualifierParameters, '#QUALIFIER:(.-):(.-)#', '%2' ) or ''
		local qualifierResult = {}

		if ( showRanking == nil ) then showRanking = false end

		if ( label == nil ) then label = wd.getLabel().label end

		local out = {}

		if ( claim[1].mainsnak.datatype == 'wikibase-item' ) then 
			for k,v in pairs ( claim ) do
				wdValue = wd.getLabel( v.mainsnak.datavalue.value.id ).label
			if ( wdValue == '' ) then
				wdValue = wd.getLabelLang( v.mainsnak.datavalue.value.id, 'en' ).label
			end
			if ( formatStringParameters ~= nil ) then
				local tmpFormatStringParameters = string.sub ( formatStringParameters, 3 )
				if ( string.sub ( tmpFormatStringParameters, 1, 9 ) == 'sitelink:' ) then
					local interwiki = string.sub ( tmpFormatStringParameters, 10 )
					interwiki = mw.ustring.gsub( interwiki, '#', '' )
						if ( interwiki ~= 'no' ) then
							local Link = wd.getSiteLink (v.mainsnak.datavalue.value.id, interwiki )
							if ( Link.errCode == 1 ) then 
								if ( interwiki == 'dewiki' ) then
									wdValue = '[[w:' .. Link.sitelink .. '|' .. wdValue .. ']]' 
								elseif ( interwiki == 'dewikivoyage' ) then
									wdValue = '[[' .. Link.sitelink .. '|' .. Link.sitelink:gsub ( '%(.*%)', '' ) .. ']]'
								end
							end
						end
					end
				else
					local deLink = wd.getSiteLink ( v.mainsnak.datavalue.value.id )
					if ( deLink.errCode == 1 ) then wdValue = '[[' .. deLink.sitelink .. '|' .. wdValue .. ']]' end
				end
				wdValue = mw.ustring.gsub ( mw.ustring.gsub ( mw.ustring.gsub ( formatString, '#ID#', v.mainsnak.datavalue.value.id ), '#LABEL#', label ), '#PROPERTY#', wdValue )
				if showRanking then
					out[#out + 1] = '<span class="wd-rank-' .. v.rank .. '">' .. wdValue .. '</span>'
				else
					out[#out + 1] = wdValue
				end
			end
		elseif ( claim[1].mainsnak.datatype == 'commonsMedia' ) then
			for k,v in pairs ( claim ) do
				wdValue = v.mainsnak.datavalue.value
				wdValue = mw.ustring.gsub ( formatString, '#PROPERTY#', wdValue )
				out[#out + 1] = wdValue
			end
		elseif ( claim[1].mainsnak.datatype == 'quantity' ) then
			for k,v in pairs ( claim ) do
				if v.mainsnak.datavalue then
					unit = cfgUnits[ v.mainsnak.datavalue.value.unit:gsub( 'https?://www.wikidata.org/entity/', '' ) ] or ''
					wdValue = tonumber( v.mainsnak.datavalue.value.amount )
					if unit ~= '' then
						if unit.factor ~= 1 then
							wdValue = round( wdValue * unit.factor, 1 )
						end
						unit = unit.si
					end
					
					wdValue = tostring ( wdValue )
					wdValue = mw.ustring.gsub ( mw.ustring.gsub ( formatString, '#FORMAT:T#', wd.formatNumberT ( wdValue ) ), '#PROPERTY#', wdValue )
					if ( qualifierParameters ~= '' ) then
						qualifierResult = wd.getQualifier ( v, qualifierParameter, qualifierFormat )
						wdValue = mw.ustring.gsub ( wdValue, qualifierParameters, qualifierResult.qualifier )
					end
					wdValue = mw.ustring.gsub ( wdValue, '#UNIT#', unit )
					if showRanking then
						out[#out + 1] = '<span class="wd-rank-' .. v.rank .. '">' .. wdValue .. '</span>'
					else
						out[#out + 1] = wdValue
					end
				end
			end
		elseif ( claim[1].mainsnak.datatype == 'globe-coordinate' ) then
			local lat		= ''
			local long	  = ''
			local precision	= ''
			for k,v in pairs ( claim ) do
				lat		= tonumber ( v.mainsnak.datavalue.value.latitude )
				long	  = tonumber ( v.mainsnak.datavalue.value.longitude )
				precision	= tonumber ( v.mainsnak.datavalue.value.precision )
				wdValue = mw.ustring.gsub ( mw.ustring.gsub ( mw.ustring.gsub ( formatString, '#LAT#', lat ), '#LONG#', long ), '#PRECISION#', precision )
				if showRanking then
					out[#out + 1] = '<span class="wd-rank-' .. v.rank .. '">' .. wdValue .. '</span>'
				else
					out[#out + 1] = wdValue
				end
			end
		elseif ( claim[1].mainsnak.datatype == 'url' ) then
			if ( claim[1].mainsnak.datavalue ~= nil ) then
				local qualifierLang = mw.ustring.match ( formatString, '#PROPERTY:LANG:.-#' ) or ''
				local qualifierLangPlus = mw.ustring.match ( formatString, '#PROPERTY:LANG%+:.-#' ) or ''
				if ( qualifierLang ~= '' ) then
					local qLang = string.lower ( mw.ustring.gsub( qualifierLang, '#PROPERTY:LANG:(.-)#', '%1' ) ) or ''
					local urlList = {}
					local langCode = ''
					for k,v in pairs ( claim ) do
						langCode = wd.getLangQualifier(v)
						urlList[langCode] = mw.ustring.gsub ( formatString, '#PROPERTY:LANG:(.-)#', v.mainsnak.datavalue.value )
						if ( ( langCode ~= qLang ) and ( langCode ~= 'en' ) and ( langCode ~= 'no_lang' ) ) then
							urlList['xxx'] = mw.ustring.gsub ( formatString, '#PROPERTY:LANG:(.-)#', v.mainsnak.datavalue.value )
						end
					end
					out[#out + 1] = urlList[qLang] or urlList['en'] or urlList['xxx'] or urlList['no_lang']
				elseif ( qualifierLangPlus ~= '' ) then
					local qLang = string.lower ( mw.ustring.gsub( qualifierLangPlus, '#PROPERTY:LANG%+:(.-)#', '%1' ) ) or ''
					local urlList = {}
					local langCode = ''
					for k,v in pairs ( claim ) do
						langCode = wd.getLangQualifier(v)
						if ( ( langCode == 'no_lang' ) or ( langCode == qLang ) ) then
							urlList[langCode] = mw.ustring.gsub ( formatString, '#PROPERTY:LANG%+:(.-)#', v.mainsnak.datavalue.value )
						else
							urlList[langCode] = mw.ustring.gsub ( formatString, '#PROPERTY:LANG%+:(.-)#', v.mainsnak.datavalue.value ) .. ' (' .. langCode .. ')'
							if ( ( langCode ~= qLang ) and ( langCode ~= 'en' ) ) then
								urlList['xxx'] = mw.ustring.gsub ( formatString, '#PROPERTY:LANG%+:(.-)#', v.mainsnak.datavalue.value ) .. ' (' .. langCode .. ')'
							end
						end
					end
					out[#out + 1] = urlList[qLang] or urlList['en'] or urlList['xxx'] or urlList['no_lang']
				else
					for k,v in pairs ( claim ) do
						out[#out + 1] = mw.ustring.gsub ( mw.ustring.gsub ( mw.ustring.gsub ( formatString, '#PROPERTY#', v.mainsnak.datavalue.value ), '#PROPERTY_NO_HTTP#', v.mainsnak.datavalue.value:gsub ( 'https?://', '' ) ), '#LABEL#', label )
					end
				end
			else
				out[#out + 1] = '<em>Wert auf Wikidata fehlt.</em>'
			end
		elseif ( claim[1].mainsnak.datatype == 'string' or claim[1].mainsnak.datatype == 'external-id' ) then
			for k,v in pairs ( claim ) do
				out[#out + 1] = mw.ustring.gsub ( formatString, '#PROPERTY#', v.mainsnak.datavalue.value )
			end
		end
		local propertyValue	 = table.concat(out, delimiter)
		if ( parameters ~= nil ) then
			for k,v in pairs ( parameters ) do
				propertyValue	 = mw.ustring.gsub ( propertyValue	, '#PARAMETER' .. k .. '#', v )
			end
		end
		return propertyValue
	else
		return ''
	end

end

-- Funktionen für spezielle Properties zur Verwendung in anderen Modulen
function wd.getDomain ( id )
	local result = wd.getBestStatement ( id, 'P17' )
	if ( result.errCode == 1 ) then
		if ( result.statement[1].mainsnak.datavalue ~= nil ) then
			local country = result.statement[1].mainsnak.datavalue.value["id"]
			result = wd.getBestStatement ( country, 'P78' )
			if ( result.errCode == 1 ) then
				if ( result.statement[1].mainsnak.datavalue ~= nil ) then
					return mw.wikibase.label ( result.statement[1].mainsnak.datavalue.value["id"] ) or ''
				else 
					return ''
				end
			else
				return ''
			end
		else
			return ''
		end
	else
		return ''
	end
end

function wd.getCoordinates ( id )
	local item = wd.getBestStatement ( id, 'P625' )
	local result = {
		["errCode"]	 = 0,
		["lat"]		  = 0,
		["long"]	 = 0,
		["precision"]  = 0
		}
	result.errCode = item.errCode
	if ( item.errCode == 1 ) then
		result.lat = tonumber ( item.statement[1].mainsnak.datavalue.value.latitude )
		result.long = tonumber ( item.statement[1].mainsnak.datavalue.value.longitude )
		result.precision = tonumber ( item.statement[1].mainsnak.datavalue.value.precision )
	end
	return result
end

function wd.getProvince ( id )
	local newSearch				= true
	local provinceID				= id
	local provinceLabelID		= ''
	local labelProvince			= wd.getLabel().label
	local labelProvinceLink		= ''
	local labelProvinceLabel	= ''
	
	local isAn	  = {}
	
	local result = {
		["errCode"]			= 0,
		["errTxt"]			= '',
		["errTxtShort"]	= '',
		["provinceID"]		= '',
		["provinceCode"]	= '',
		["province"]		= '',
		["provinceLink"]	= '',
		["provinceLabel"] = 'Provinz'
	}
	
	local item = wd.getBestStatement ( id, 'P131' )
	result.errCode = item.errCode
	while newSearch do
		if ( item.errCode == 1 and item.statement[1].mainsnak.datavalue ~= nil ) then
			provinceID = 'Q' .. item.statement[1].mainsnak.datavalue.value["numeric-id"]

			-- Prüfung ob das or irgendwann mal wieder auf das originale Objekt verwiesen wird (Schleife)
			if ( provinceID == id ) then
				result.errCode = 4
				result.errTxt = cfgI18["getProvince"]["4"]
				result.errTxtShort = '[[:wikidata:' .. id .. '|' .. labelProvince .. ']] bildet eine Schleife (Selbstreferenz). [[Kategorie:Artikel mit selbstreferenzierenden Verwaltungseinheiten]]'
				newSearch = false
			else
				isAn = wd.getBestStatement ( provinceID, 'P31' )
				if ( isAn.errCode == 1 ) then
					provinceLabelID = 'Q' .. isAn.statement[1].mainsnak.datavalue.value["numeric-id"]
					labelProvince = wd.getLabel ( provinceID ).label
					labelProvinceLabel = wd.getLabel ( provinceLabelID ).label
					if ( cfgGeography["provinces"][provinceLabelID] ) then
						result.errCode			= 1
						result.errTxt			= cfgI18["getProvince"]["1"]
						result.provinceID		= provinceID
						result.province		= labelProvince
						local itemCode = wd.getBestStatement ( provinceID, 'P300' )
						if ( itemCode.errCode == 1 and itemCode.statement[1].mainsnak.datavalue ~= nil ) then
							result.provinceCode = wd.getProperty ( itemCode.statement )
						end
						labelProvinceLink		= wd.getSiteLink ( provinceID ).sitelink
						if ( labelProvinceLink == '' ) then
							result.provinceLink	  = labelProvince
						else
							result.provinceLink	  = '[[' .. labelProvinceLink .. '|' .. mw.ustring.gsub( labelProvinceLink, '%s%(.*%)', '' ) .. ']]'
						end
						-- derzeit eigenes Label. Wenn Wikidata gewünscht: labelProvinceLabel benutzen
						result.provinceLabel = cfgGeography["provinces"][provinceLabelID]
						newSearch = false
						if ( cfgGeography["countries"][provinceLabelID] ) then
							result.errTxtShort = result.errTxtShort .. 'Provinzsuche endet beim Staat[[Kategorie:Artikel mit unbekannter Verwaltungseinheit auf Wikidata]]'
						end
					else
						item = wd.getBestStatement ( provinceID, 'P131' )
					end
				else
					result.errCode = 2
					result.errTxt = cfgI18["getProvince"]["2"] .. wd.getLabel ( provinceID ).label
					result.errTxtShort = 'Verwaltungseinheit [[:wikidata:' .. provinceID .. '|' .. provinceID .. ']] hat kein <em>ist ein(e)</em>[[Kategorie:Artikel mit unbekannter Verwaltungseinheit auf Wikidata]]'
					newSearch = false
				end
			end
		else
			if ( provinceID == 'self' ) then provinceID = wd.EntityId() end
			result.errCode = 0
			result.errTxt = cfgI18["getProvince"]["0"]
			result.errTxtShort = '[[:wikidata:' .. provinceID .. '|' .. labelProvince .. ']] hat keine übergeordnete Region.[[Kategorie:Artikel ohne übergeordnete Verwaltungseinheit auf Wikidata]]'
			newSearch = false
		end
	end
	return result
end

-- Funktionen zur Verwendung in Vorlagen
function wd.LabelByID ( frame )
	local display  = ''
	local errorStr = ''
	local id = wdGetParameter ( frame, cfgParams["LabelByID"], "id" )
	local result = wd.getLabel(id)
	display = result.label
	if ( result.errCode ~= 1 ) then
		errorStr = errorStr .. check._error ( 'Funktion <span style="font-weight: bold; font-style:italic">LabelByID</span>: ' .. result.errMsg .. "ID: " .. id, 'Wikidata2' )
	end
	return errorStr .. check._testParams ( frame:getParent().args, cfgParams["LabelByID"], 'Wikidata2', 'lower' ) .. display
end

function wd.LabelLang ( frame )
	local display  = ''
	local lang= wdGetParameter ( frame, cfgParams["LabelLang"], "lang" )
	local result = wd.getLabelLang ( 'self', lang )
	if ( result.label ~= nil ) then 
		display = result.label
	end
	return check._testParams ( frame:getParent().args, cfgParams["LabelLang"], "Wikidata2", 'lower' ) .. display
end

function wd.LabelLangByID ( frame )
	local display  = ''
	local id= wdGetParameter ( frame, cfgParams["LabelLang"], "id" )
	local lang= wdGetParameter ( frame, cfgParams["LabelLang"], "lang" )
	local result = wd.getLabelLang ( id, lang )
	if ( result.label ~= nil ) then 
		display = result.label
	end
	return check._testParams ( frame:getParent().args, cfgParams["LabelLangByID"], 'Wikidata2', 'lower' ) .. display
end

function wd.LabelLangByIDRaw ( id, lang )
	return wd.getLabelLang ( id, lang )
end

function wd.siteLink ( frame )
	local display  = ''
	local id = wdGetParameter ( frame, cfgParams["siteLink"], "id" )
	local wiki= wdGetParameter ( frame, cfgParams["siteLink"], "wiki" )
	local result = wd.getSiteLink ( id, wiki )
	if ( result.sitelink ~= nil ) then 
		display = result.sitelink
	end
	return check._testParams ( frame:getParent().args, cfgParams["siteLink"], "Wikidata2", 'lower' ) .. display
end

function wd.dumpEntity ( frame )
	local vd = require('Modul:VarDump')
	local display  = ''
	local id = wdGetParameter ( frame, cfgParams["dumpEntity"], "id" )
	local property = mw.ustring.gsub ( wdGetParameter ( frame, cfgParams["dumpEntity"], "p" ), 'p', 'P' )
	if ( property == '' ) then
		display  = display .. vd.dump ( mw.wikibase.getEntityObject(id) )
	else
		display  = display .. vd.dump ( wd.getStatementAll ( id, property ).statement )
	end
	return check._testParams ( frame:getParent().args, cfgParams["dumpEntity"], "Wikidata2", 'lower' ) .. display
end

function wd.getValue ( frame )
	local errorStr = ''
	local display  = ''
	local category  = ''
	local result	= {}
	local id = wdGetParameter ( frame, cfgParams["getValue"], "id" )
	if ( id == 'self' ) then id = wdEntityId.id end
	local property = mw.ustring.gsub ( wdGetParameter ( frame, cfgParams["getValue"], "p" ), 'p', 'P' )
	local category  = '[[Kategorie:Seiten, die die Wikidata-Eigenschaft '.. property .. ' benutzen]]'
	local ranking = wdGetParameter ( frame, cfgParams["getValue"], "ranking" )
	local formatting = wdGetParameter ( frame, cfgParams["getValue"], "format" )
	if (formatting == '' ) then formatting = property end
	local showRanking = wdGetParameter ( frame, cfgParams["getValue"], "showRanking" )
	
	if ( ranking == 'all' ) then
		result = wd.getStatementAll ( id, property )
	else
		result = wd.getBestStatement ( id, property )
	end
	
	if ( result.errCode == 1 ) then
		display = display .. wd.getProperty ( result.statement, formatting, yesno ( showRanking ), wd.getLabel(id).label )
	else
		errorStr = errorStr .. check._error ( 'FEHLER (Code: ' .. result.errCode .. '): ' .. result.errMsg .. '(ID= ' .. id .. ', Property= ' .. property .. ')', 'Wikidata2' )
		if ( result.errCode == 2 ) then display = display .. '[[Kategorie:Artikel ohne Objekt auf Wikidata]]' end
	end
	
	return category .. errorStr .. check._testParams ( frame:getParent().args, cfgParams["getValue"], 'Wikidata2', 'lower' ) .. display
end

function wd.getPOIType ( frame )
	local errorStr	= ''
	local display	= ''
	local poiType	= ''
	local result	= {}
	local id = wdGetParameter ( frame, cfgParams["getValue"], "id" )
	result = wd.getBestStatement ( id, 'P31' )
	
	if ( result.errCode == 0 ) then
		-- Property nicht befüllt > Standard
		display = display .. cfgPOITypes["default"]
	elseif ( result.errCode == 1 ) then
		-- was gefunden, ersten Eintrag holen, wenn nichts brauchbares da, dann Standard
		poiType = cfgPOITypes["Q" .. result.statement[1].mainsnak.datavalue.value["numeric-id"]]
		if ( poiType == nil ) then poiType = cfgPOITypes["default"] end
		display = display .. poiType
	else
		-- kein Objekt mit der ID vorhanden: Fehlermeldung und Standardtyp
		display = display .. cfgPOITypes["default"]
		errorStr = errorStr .. check._error ( 'FEHLER (Code: ' .. result.errCode .. '): ' .. result.errMsg .. '(ID= ' .. id .. ')', 'Wikidata2' ) .. '[[Kategorie:Artikel ohne Objekt auf Wikidata]]'
	end

	return errorStr .. check._testParams ( frame:getParent().args, cfgParams["getPOIType"], 'Wikidata2', 'lower' ) .. display
end

function wd.getPOITypeDefault ()
	return cfgPOITypes["default"]
end

function wd.getOfficialWebsite ( frame )
	local display	= ''
	local category	= ''
	local result	= {}
	local text		= wdGetParameter ( frame, cfgParams["getValue"], "text" ) or ''
	local id		= wdGetParameter ( frame, cfgParams["getValue"], "id" ) or wdEntityId
	local vd = require('Modul:VarDump')
	
	result = wd.getBestStatement ( id, 'P856' )
	local i
	local url
	local statements
	
	if ( result.errCode == 0 ) then
		-- Property nicht befüllt > Standard
		display = 'Eine offizielle Webseite ist nicht bekannt.<span class="wd-tools-message-inline"> Bitte auf [[:wikidata:' .. wdEntityId.id .. '|Wikidata]] nachtragen.</span>[[Kategorie:Artikel ohne offizielle Webseite auf Wikidata]]'
	elseif ( result.errCode == 1 ) then
		-- was gefunden, ersten Eintrag holen, wenn nichts brauchbares da, dann Standard
		-- display = display .. wd.getProperty ( result.statement, 'P856-link+site' )
		-- urls can contain percent signs
		category = category .. '[[Kategorie:Seiten, die die Wikidata-Eigenschaft P856 benutzen]]'
		display = wd.getProperty ( result.statement, 'P856-lang+:de' )
		if ( text ~= '' ) then 
			display = '<span class="wikidata-content">' .. display .. '</span>' .. ' – ' .. text
		else
			display = '<span class="wikidata-content">' .. display .. '</span>' .. ' – Offizielle Webseite von ' .. wd.getLabel(id).label
		end
	else
		-- kein Objekt mit der ID vorhanden: Fehlermeldung und Standardtyp
		display = 'Eine offizielle Webseite ist nicht bekannt.<span class="wd-tools-message-inline"> Objekt bitte auf [[:wikidata:Wikidata:Main Page|Wikidata]] nachtragen.</span>[[Kategorie:Artikel ohne Objekt auf Wikidata]]'
	end

	return category .. wdErrCategory .. check._testParams ( frame:getParent().args, cfgParams["getOfficialWebsite"], 'Wikidata2', 'lower' ) .. display
end

return wd