Module:Auteur koptekst

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:Auteur koptekst/doc

local p = {}

--Function retrieves year from a Wikidata timestamp in format +YYYY-MM-DD...
function year(timestamp)
	return mw.text.split(mw.text.split(timestamp, "+")[2], "-")[1];
end

--Main function
function p.main(frame)
	parent = frame:getParent();
	item_number = mw.wikibase.getEntityIdForCurrentPage();
	if(not item_number) then return ""; end --if there is no Wikidata item, return empty string
	item = mw.wikibase.getEntity(item_number);

	--Header
	--First letter of authors surname is needed for a link to authors directory
	name_first_letter = "";
	if(parent.args["letter"]) then --value of this letter may be set in argument "letter"
		name_first_letter = parent.args["letter"];
	else --otherwise take the first letter of the last word of the page title
		name_first_letter = mw.text.split(item.sitelinks.nlwikisource.title, ":")[2]; --remove "Auteur:"
		name_first_letter = mw.text.split(item.sitelinks.nlwikisource.title, " "); --split strings into words 
		name_first_letter = name_first_letter[#name_first_letter]; --take the last word
		name_first_letter = mw.text.truncate( name_first_letter, 1,"" ); --take first letter of last word
	end
	--Link to authors directory
	directory = "[[Hoofdportaal:Lijst van auteurs#"..name_first_letter..
			"|← Lijst van auteurs: ''"..name_first_letter.."'']]";
	
	--Authors name
	title = "";
	if(item.labels.nl) then title = item.labels.nl.value; --title get a value of Wikidata label
	else title = mw.text.split(mw.title.getCurrentTitle().text, " %(")[1]; end --otherwise page name with text in brackets removed
	
	--Years of life of an author
	life = "";
	if(item.claims["P569"] and item.claims["P570"]) then --if there is a date of death, show years of life
		life = "<center>("..year(item.claims["P569"][1].mainsnak.datavalue.value.time)..
		  "—"..year(item.claims["P570"][1].mainsnak.datavalue.value.time)..")</center>";
	elseif(item.claims["P569"]) then --if only date of birth, author is alive, show only his year of birth
		life = "<center>(geboren "..year(item.claims["P569"][1].mainsnak.datavalue.value.time)..")</center>";
	end
	
	--Nationality of an author
	nationality = "";
	if(item.claims["P27"] and #item:getBestStatements('P27')>0) then 
		--Getting flag of authors country
		country_id = item:getBestStatements('P27')[1].mainsnak.datavalue.value.id;
		country_item = mw.wikibase.getEntity(country_id); --Getting item about country of an author
		if(country_item.claims["P41"]) then
			nationality = "<center>[[File:"..country_item.claims["P41"][1].mainsnak.datavalue.value..'|50x25px]]</center>';
		end
		
		--Getting demonym, for this get P1549 from an item about country
		nationality_text = ""; nl_index = -1;
		if(country_item.claims["P1549"]) then
			--iterate over P1549 values, untill there is one in dutch
			for i=1,#country_item.claims["P1549"],1 do
				if(country_item.claims["P1549"][i].mainsnak.datavalue.value.language=="nl") then
				  nl_index = i; break; end
			end
		end
		
		if(country_item.claims["P1549"] and nl_index~=-1) then --if demonym found among P1549 values, add it
			nationality_text = "<center>"..country_item.claims["P1549"][nl_index].mainsnak.datavalue.value.text.."</center>";
		elseif(country_item.labels.nl) then --otherwise, just a name of a country
			nationality_text = "<center>"..country_item.labels.nl.value.."</center>";
		end
		
		nationality = nationality..nationality_text;
	end
	
	--Getting together the header
	header = '{| style="width:100%;background:#fb6"\n|-\n'..
		'| width="25%" | '..directory..'\n'..
		'| width="50%" | '.."<center><big>'''"..title.."'''</big></center>"..life..'\n'..
		'| width="25%" | '..nationality..'\n'..'|}';
	
	
	--Linkbox
	op = {};
	if(item.sitelinks.nlwiki) then --adding link to Wikipedia article, if present
		table.insert(op, '[[:w:nl:'..item.sitelinks.nlwiki.title..'|Wikipedia artikel]]'); end
	if(item.sitelinks.nlwikiquote) then --adding link to Wikiquote article, if present
		table.insert(op, '[[:wikiquote:nl:'..item.sitelinks.nlwikiquote.title..'|Wikiquote]]'); end
	if(item.sitelinks.commonswiki and (not string.match(item.sitelinks.commonswiki.title, "Category:")) ) then --adding link to Commons category, if present
		table.insert(op, '[[:commons:'..item.sitelinks.commonswiki.title..'|Commons gallery]]'); end
	if(item.claims["P935"]) then --adding link to Commons page, if present
		table.insert(op, '[[:commons:Category:'..item.claims["P935"][1].mainsnak.datavalue.value..'|Commons category]]'); end
	table.insert(op, '[[:wikidata:'..item_number..'|Wikidata item]]'); --adding link to Wikidata item
	
	op_list = table.concat(op, ", "); --concatenate all the links
	if(op_list~="") then op_list = '\n{| class="wikitable"\n|-\n| '.."'''Zusterprojecten:''' "..op_list..'\n|}'; end --getting together the linkbox
	

	return frame:preprocess(header..op_list); --final result
end

return p;