Module:Sandbox/P.a.a/Test

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια
Πήδηση στην πλοήγηση Πήδηση στην αναζήτηση

Πολύ αρχικό στάδιο για τη δημηιουργία module για πληροφορίες προσώπου με υποστήριξη wikidata και whitelist

Το αποτέλεσμα φαίνεται στη σελίδα Χρήστης:P.a.a/Test

local Date = require('Module:Sandbox/P.a.a/Test/Date')._Date;
local infoboximage = require('Module:InfoboxImage').InfoboxImage;
local infobox = require('Module:Infobox').infobox;
local separatedentries = require('Module:Separated entries')._main
local wdmodule = require('Module:Wikidata')
local navbar = require('Module:Navbar')._navbar;
local TableTools = require('Module:TableTools')

local cfg = require('Module:Sandbox/P.a.a/Test/config')
local args = {};
local extraargs = {};

local rowcounter = 0;
local rows = {}

local fetchwikidata = 'ALL'
local qid = nil;

local A = {}
setmetatable(A, {
	__index = function(t, key)
		local found = nil
		if('table' == type(cfg.arguments[key].aliases)) then
			for _,v in ipairs(cfg.arguments[key].aliases) do
				if args[v] then
					found = args[v];
					break
				end
			end
		else
			found = args[cfg.arguments[key].aliases];
		end
		return found
	end
})

local WD = {}
setmetatable(WD,{
	__index = function(t, key)
		local found = false;
		if nil == cfg.arguments[key].wikidata then return nil end
		
		if('table' == type(cfg.arguments[key].aliases)) then
			for _,v in ipairs(cfg.arguments[key].aliases) do
				if (fetchwikidata == 'ALL') or TableTools.inArray(fetchwikidata, v) then
					found = true;
					break
				end
			end
		elseif (fetchwikidata == 'ALL') or TableTools.inArray(fetchwikidata, cfg.arguments[key].aliases) then
			found = true;
		end
		
		local ret = nil;
		if found then
			local wdfn = cfg.arguments[key].wikidata.fn
			local wdparams = mw.clone(cfg.arguments[key].wikidata.params)

			if qid then
				wdparams.id = qid
			end

			ret = wdmodule[wdfn](wdparams)
		end
		return ret
	end
})

local function split(str, pat)
	local t = {}  -- NOTE: use {n = 0} in Lua-5.0
	local fpat = "(.-)" .. pat
	local last_end = 1
	local s, e, cap = str:find(fpat, 1)
	while s do
		if s ~= 1 or cap ~= "" then
			table.insert(t, mw.text.trim(cap))
		end
		last_end = e+1
		s, e, cap = str:find(fpat, last_end)
	end
	if last_end <= #str then
		cap = str:sub(last_end)
		table.insert(t, mw.text.trim(cap))
	end
	return t
end

local function addrow(row)
	rowcounter = rowcounter + 1;
	local rowstr = tostring(rowcounter);
	if row.header then
		rows["header" .. rowstr] = row.header
	elseif row.label and row.data then
		rows["label" .. rowstr] = row.label;
		rows["data" .. rowstr] = row.data;
	elseif row.data then
		rows["data" .. rowstr] = row.data
	end
end

local function addHeader(str)
	addrow({header=str})
end

local function addData(str)
	addrow({data=str})
end

local function addLabelData(str1, str2)
	addrow({
		label=str1,
		data=str2
	})
end

local function addStyle(styles)
	local stylekeys = {"bodyclass","bodystyle","abovestyle","aboveclass","headerstyle","imagestyle","captionstyle","belowclass","belowstyle"};
	for k, v in ipairs(stylekeys) do
		if styles[v] then
			rows[v] = styles[v]
		end
	end
end

local function addBelow(str)
	rows["below"] = str;
end

local function addAbove(str)
	rows["above"] = str
end

local function is_set( var )
	return not (var == nil or var == '');
end

local function any_set(props)
	for _,v in ipairs(props) do
		if is_set(A[v]) or WD[v] then return true end
	end
	return false
end

local function main(frame)
	
	local pframe = frame:getParent();
	
	for k, v in pairs( pframe.args ) do
		if v ~= '' then
			args[k] = v;
		end
	end

	for k, v in pairs( frame.args ) do
		extraargs[k] = v;
	end

	if args.fetchwikidata and (args.fetchwikidata ~= 'ALL')  then
		fetchwikidata = split(args.fetchwikidata,';')
	end
	
	qid = args.id

	addStyle({
		bodyclass = 'biography vcard',
		headerstyle = 'background:#EEEEEE; line-height: 1.5em',
		imagestyle   = 'padding:0.7em 0.8em',
		captionstyle = 'font-size:0.9em;',
		belowclass = 'plainlinks noprint',
		belowstyle = 'border-top:1px solid #DFDFDF; text-align:right; font-size:90%;',
		abovestyle = A['AboveStyle']
	})
	
	addAbove(separatedentries({
			separator='<br/>',
			A['HonorificPrefix'] or WD['HonorificPrefix'],
			A['Name'] or WD['Name'],
			A['ΗonorificΣuffix'] or WD['ΗonorificΣuffix']
	}));

	rows['image'] = infoboximage{args={image=(A['Image'] or WD['Image']),upright='yes'}}
	if is_set(A['Caption']) then rows['caption'] = A['Caption'] end
	
	local generalinfoparams = {
		'NativeName', 'NativeNameLang', 'Pronunciation', 'ShortName', 'BirthName', 'BirthDate', 'BirthPlace', 'Baptised',
		'DisappearedDate', 'DisappearedPlace', 'Status', 'DeathDate', 'DeathPlace', 'DeathCause', 'MannerOfDeath', 'BurialPlace',
		'BurialPlaceCoordinates', 'RestingPlace', 'RestingPlaceCoordinates', 'Residence', 'Nationality', 'OtherNames', 'Nickname',
		'Ethnicity', 'Citizenship', 'Demeship', 'Religion', 'FeastDay', 'ReligiousOrder', 'Height', 'Weight'
	}
	
	if any_set(generalinfoparams) then
		addHeader('Γενικές πληροφορίες')

		--addLabelData('Όνομα στη μητρική γλώσσα', A['NativeName'] or WD['NativeName'])
		--addLabelData('Προφορά', A['Pronunciation'] or WD['Pronunciation'])
		addLabelData('Όνομα γεννήσεως', A['BirthName'] or WD['BirthName'])
		addLabelData('Σύντομο όνομα', A['ShortName'] or WD['ShortName'])

		local rawBirthDate = wdmodule.getRawValueFromLua({id=qid, property='P569'})
		local rawDeathDate = wdmodule.getRawValueFromLua({id=qid, property='P570'})
		local birth = Date(rawBirthDate.year, rawBirthDate.month, rawBirthDate.day)
		local death = Date(rawDeathDate.year, rawDeathDate.month, rawDeathDate.day)
		local age = death-birth

		addLabelData('Γέννηση', separatedentries({
			separator='<br/>',
			A['BirthDate'] or WD['BirthDate'],
			A['BirthPlace'] or WD['BirthPlace']
		}))
		addLabelData('Βάπτιση', A['Baptised'])
		
		addLabelData('Θάνατος', separatedentries({
			separator='<br/>',
			A['DeathDate'] or WD['DeathDate'] .. ' (' .. age.years .. ' χρονών)',
			A['DeathPlace'] or WD['DeathPlace']
		}))
	end

	local educationparams = {
		'NativeLanguage', 'Languages', 'Education', 'AlmaMater'
	}
	
	if any_set(educationparams) then
		addHeader('Εκπαίδευση και γλώσσες')
		
		addLabelData('Μητρική γλώσσα', A['NativeLanguage'] or WD['NativeLanguage'])
		addLabelData('Ομιλούμενες γλώσσες', A['Languages'] or WD['Languages'])
		addLabelData('Εκπαίδευση', A['Education'] or WD['Education'])
		addLabelData('Σπουδές', A['AlmaMater'] or WD['AlmaMater'])
	end
	
	addBelow('[//www.wikidata.org/wiki/Special:ItemByTitle?site=elwiki&page=' .. mw.title.getCurrentTitle():partialUrl() .. ' δεδομένα] ' ..
			 navbar({'Πληροφορίες προσώπου', mini=true, fontstyle='font-size:75%;'}))
	return infobox(rows);
end

return {
	main = main,
}