Jump to content

Module:Person

From Namtars

Documentation for this module may be created at Module:Person/doc

local utils = require( 'Module:Utils' )

local p = {}

-- Combine students named on this page with people who name it as their teacher.
function p.students(frame)
	local args = frame:getParent().args
	local currentTitle = mw.title.getCurrentTitle().fullText
	local students = {}
	local byTitle = {}

	local function yearFrom(value)
		if type(value) == 'table' then value = value[1] end
		if value == nil then return nil end
		local text = tostring(value)
		return tonumber(text:match('^%d%d?/%d%d?/(%-?%d+)$'))
			or tonumber(text:match('^%d%d?/(%-?%d+)$'))
			or tonumber(text:match('^(%-?%d+)%-'))
			or tonumber(text)
			or tonumber(text:match('(%d%d%d%d)'))
	end

	local function addStudent(name, birth)
		if not name then return end
		name = mw.text.trim(name)
		local title = mw.title.new(name)
		if not title or name == '' then return end
		local key = title.fullText
		local year = yearFrom(birth)
		if byTitle[key] then
			byTitle[key].year = byTitle[key].year or year
		else
			local student = { name = key, year = year }
			byTitle[key] = student
			table.insert(students, student)
		end
	end

	for name in (args['Students'] or ''):gmatch('[^,]+') do
		addStudent(name)
	end

	local related = mw.smw.ask {
		'[[Has teacher::' .. currentTitle .. ']]',
		'mainlabel=-',
		'?#-=Student',
		'?Birth year#ISO=Birth',
		'limit=500'
	} or {}
	for _, row in ipairs(related) do
		addStudent(row.Student, row.Birth)
	end

	-- Students specified only on this page also need their birth years.
	for _, student in ipairs(students) do
		if not student.year then
			local rows = mw.smw.ask {
				'[[' .. student.name .. ']]',
				'mainlabel=-',
				'?Birth year#ISO=Birth',
				'limit=1'
			} or {}
			student.year = yearFrom(rows[1] and rows[1].Birth)
		end
	end

	table.sort(students, function(a, b)
		if a.year ~= b.year then
			if a.year == nil then return false end
			if b.year == nil then return true end
			return a.year < b.year
		end
		return mw.ustring.lower(a.name) < mw.ustring.lower(b.name)
	end)

	local names = {}
	for _, student in ipairs(students) do
		table.insert(names, student.name)
	end
	return table.concat(names, ',')
end

local function propertiesPlanMono()
	return {
		['Tibetan name'] = {'Tibetan name', '@bo'},
		['Wylie transliteration'] = {'Wylie transliteration', '@bo-x-ewts'}
	}
end
local function propertiesPlan()
	return {
		['Phonetic name']                 = {'Phonetic name'},
		['Cover image']                   = {'Has cover image', 'file'},
		['Cover image caption']           = {'Has caption'},
		['Description']                   = {'Description text'},
		['Main quote']                    = {'Has main quote'},
		['Main quote source']             = {'Main quote source'},		
		['Oldest known root mindstream']  = {'Oldest known root mindstream'},
		['Has closest known predecessor'] = {'Has closest known predecessor'},
		['Emanation type']                = {'Emanation type', ','},
		['Birth place']                   = {'Birth place'},
		['Death place']                   = {'Death place'},
		['Roles']                         = {'Has role', ','},
		['Places of activity']            = {'Place of activity', ','},
		['Teachers']                      = {'Has teacher', ','},
		['Students']                      = {'Has student', ','},
		['Dharma friends']                = {'Is Dharma friend of', ','},
		['Has related map']               = {'Has related map', 'GeoJSON'}
	}
end

local function infoboxSectionPlan()
	local sectionMap = {
		['Cover'] = {
			heading = '',
			template = 'Infobox/Cover',
			subobject = 'false',
			order = '1',
			rows = {
				'Tibetan name',
				'Cover image',
				'Cover image caption',
				'Description',
				'Main quote',
				'Main quote source'
			}
		},
		['Timeline'] = {
			heading = 'Timeline',
			template = 'Infobox/Person timeline',
			subobject = 'false',
			order = '2',
			rows = {
				'Birth',
				'Death',
				'Birth place',
				'Death place',
			}
		},
		['Mindstream'] = {
			heading = 'Mindstream',
			template = 'Infobox/Mindstream',
			subobject = 'false',
			order = '3',
			rows = {
				'Oldest known root mindstream',
				'Has closest known predecessor',
				'Is immediate successor',
				'Emanation type',
			}
		},
		['Training'] = {
			heading = 'Training',
			template = 'Infobox/Training',
			subobject = 'false',
			order = '4',
			rows = {
				'Teachers',
			}
		},
		['Activity'] = {
			heading = 'Activity',
			template = 'Infobox/Activity',
			subobject = 'false',
			order = '5',
			rows = {
				'Roles',
				'Places of activity',
				'Students',
				'Dharma friends'
			}
		},
		['Names'] = {
			heading = 'Names',
			template = 'Infobox/Names',
			subobject = 'true',
			order = '6',
			rows = {
				'Phonetic name',
				'Tibetan name',
				'Wylie transliteration',
				'Names'
			}
		},
		['Custom Map'] = {
			heading = '',
			template = 'Infobox/Custom map',
			subobject = 'false',
			order = '7',
			rows = {
				'Include custom map',
				'Has related map'
			}
		},
		['Quotes'] = {
			heading = 'Quotes',
			template = 'Infobox/Quotes',
			subobject = 'true',
			order = '8',
			rows = {
				'Quotes'
			}
		},
		['Resources'] = {
			heading = 'External resources',
			template = 'Infobox/Person external profiles',
			subobject = 'true',
			order = '9',
			rows = {
				'Resource'
			}
		}
	}
	return sectionMap
end

function p.infobox(frame)
	local args      = frame:getParent().args
	local monoProps = utils.setPropertiesMono(propertiesPlanMono(), args) or {}
	mw.smw.set(monoProps)
	local cat        = 'People'
	local properties = propertiesPlan()
	local sections   = infoboxSectionPlan()
	local students   = p.students(frame)
	local displayArgs = students ~= '' and { Students = students } or nil
	local infobox    = utils.createInfobox(frame, cat, properties, sections, displayArgs)
	return infobox
end

return p