Modul:IsIn

Aus skandinavien-wiki.net

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

-- documentation
local isInDoc = {
	suite  = 'IsIn',
	serial = '2022-10-22',
	item   = 99527605
}

-- require( 'strict' )
local wu = require( 'Module:Wikidata utilities' )

local wikidata = {
	instanceOf = 'P31',
	disambigId = 'Q4167410'
}

local isIn = {
	moduleInterface = isInDoc
}

function isIn.isDisambig( frame )
	local page, pageId
	if not frame then
		return 'false'
	end
	if type( frame ) == 'table' and not frame.args then
		return 'false'
	end
	if type( frame ) == 'table' then
		page = frame.args.page or frame.args[ 1 ] or ''
	else
		page = frame
	end
	page = mw.text.trim( page )
	if page == '' then
		return 'false'
	end
	pageId = mw.wikibase.getEntityIdForTitle( page )
	if not pageId then
		return 'false'
	end
	local values = wu.getValues( pageId, wikidata.instanceOf )
	if values == '' then
		return 'false'
	end
	for i, v in ipairs( values ) do
		if v.id == wikidata.disambigId then
			return 'true'
		end
	end
	return 'false'
end

function isIn.checkTarget( frame )
	local page = mw.title.getCurrentTitle()

	local args = frame.args
	args.page = args.page or args[ 1 ] or ''
	args.page = mw.text.trim( args.page )
	args.enterTarget = args.enterTarget or ''
	local targetPage = mw.ustring.gsub( args.page, '^' .. page.nsText .. ':', '' )
	if targetPage == '' then
		return 'emptyTarget'
	elseif targetPage == page.text then
		return 'isItself'
	elseif args.enterTarget == args.page then
		return 'enterTarget'
	end

	if page.nsText ~= '' then
		targetPage = page.nsText .. ':' .. targetPage
	end
	local targetObj = mw.title.new( targetPage )
	if not targetObj then
		return 'invalidTitle'
	elseif not targetObj.exists then
		return 'notExisting'
	elseif targetObj.isRedirect then
		return 'isRedirect'
	elseif isIn.isDisambig( targetPage ) == 'true' then
		return 'isDisambig'
	else
		return args.page
	end
end

function isIn.addCategories( frame )
	local args = frame:getParent().args
	local cat, catSort
	local i = -1
	local result = ''
	repeat
		i = i + 2
		cat = args[ i ] or ''
		catSort = args[ i + 1 ] or ''
		if cat ~= '' then
			result = result .. '[[Category:' .. cat
				.. ( catSort ~= '' and ( '|' .. catSort ) or '' )
				.. ']]'
		end
	until i > 30 or cat == ''
	return result
end

function isIn.addPageCategory( frame )
	local page = mw.title.getCurrentTitle()
	if page.nsText == '' then -- only main namespace
		page = 'Category:' .. page.text
		local title = mw.title.new( page )
		if title and title.exists then
			return '[[' .. page.. '| ]]'
		end
	end
	return ''
end

return isIn