Module:Genealogy

From Wikiversity
Jump to navigation Jump to search

An investigation into some ideas for meta:Wikimedia genealogy project.


function listOfRelations( args, property )
	if args.wikidata == '' then
		-- Nillify if not given.
		args.wikidata = nil
	end
	local item = mw.wikibase.getEntity( args.wikidata )
	if item == nil then
		return ''
	end
	local list = mw.html.create( 'ol' )
	list:attr( 'style', 'margin:0' )
	local statements = item:getBestStatements( property )
	for _, statement in pairs( statements ) do
		local relatedId = statement.mainsnak.datavalue.value.id
		local relatedItem = mw.wikibase.getEntity( relatedId )
		local relatedLabel = mw.wikibase.label( relatedId )
		local sitelink = relatedItem:getSitelink( mw.language.getContentLanguage().code .. 'wikiversity' )
		local li = mw.html.create( 'li' )
		li:attr( 'style', 'list-style-type:none' )
		if sitelink ~= nil then
			li:wikitext( '[[' .. sitelink .. '|' .. relatedLabel .. ']]\n' )
		else
			li:wikitext( '[[Genealogy/' .. relatedLabel .. '|' .. relatedLabel .. ']]\n' )
		end
		list:node( li )
	end

	return tostring( list )
end

function parents( args )
	local out = ''
	local father = 'P22';
	out = out .. listOfRelations( args, father )
	local mother = 'P25';
	out = out .. listOfRelations( args, mother )
	return out
end

return {
	-- =p.parents({args={wikidata='Q8018316'}})
	parents = function( frame ) return parents( frame.args ) end;
	siblings = function( frame ) return listOfRelations( frame.args, 'P3373' ) end;
	spouses = function( frame ) return listOfRelations( frame.args, 'P26' ) end;
	children = function( frame ) return listOfRelations( frame.args, 'P40' ) end;
}