Modulo:Autore: differenze tra le versioni

Da Wikisource.
Contenuto cancellato Contenuto aggiunto
m fix
nuove funzioni
Riga 21: Riga 21:
-- SPERIMENTALE --
-- SPERIMENTALE --


-- elemento di Wikidata: supporto anticipato per la Fase 2
-- elemento di Wikidata
local item = nil
local item = mw.wikibase.getEntityObject()
if mw.wikibase~=nil and mw.wikibase.getEntity~=nil then
item = mw.wikibase.getEntity()
end


function cda(test)
function cda(test)
Riga 95: Riga 92:
end
end
end
end
end
end
end

local gender = ''
if item and item.claims and item.claims.P21 and #item.claims.P21 == 1 then
local claim = item.claims.P21[1]
if claim.mainsnak.datavalue and claim.mainsnak.datavalue.value and claim.mainsnak.datavalue.value['numeric-id'] then
local n = claim.mainsnak.datavalue.value['numeric-id']
if n == 6581097 then
gender = 'male'
elseif n == 6581072 then
gender = 'female'
end
end
end
end
Riga 101: Riga 111:
-- Restituisce il collegamento corrispondente al codice fornito
-- Restituisce il collegamento corrispondente al codice fornito
function sitelink(dbname)
function sitelink(dbname)
if item~=nil and item.sitelinks[dbname]~=nil then
if item and item:getSitelink(dbname) then
return item.sitelinks[dbname].title
return item:getSitelink(dbname)
end
end
end
end
Riga 353: Riga 363:


-- AUTORI SENZA ELEMENTO SU WIKIDATA
-- AUTORI SENZA ELEMENTO SU WIKIDATA
if not item then
if mw.wikibase~=nil and mw.wikibase.getEntity~=nil and item==nil then
addCat('Autori non collegati a Wikidata')
addCat('Autori non collegati a Wikidata')
end
end
Riga 380: Riga 390:
add(' ('..nascita..' – '..morte..')')
add(' ('..nascita..' – '..morte..')')
end
end

local oa = lang:gender(gender, 'o', 'a', 'o/a')


-- EVENTUALE ALTRO NOME
-- EVENTUALE ALTRO NOME
if args['Eventuale altro nome']~=nil then
if args['Eventuale altro nome']~=nil then
add(', noto/a anche come \'\'\''..args['Eventuale altro nome']..'\'\'\'')
add(', not'..oa..' anche come \'\'\''..args['Eventuale altro nome']..'\'\'\'')
end
end
-- EVENTUALE PSEUDONIMO
-- EVENTUALE PSEUDONIMO
if args['Eventuale pseudonimo']~=nil then
if args['Eventuale pseudonimo']~=nil then
add(', noto/a anche con lo pseudonimo di \'\'\''..args['Eventuale pseudonimo']..'\'\'\'')
add(', not'..oa..' anche con lo pseudonimo di \'\'\''..args['Eventuale pseudonimo']..'\'\'\'')
end
end


-- EVENTUALE NOME REALE
-- EVENTUALE NOME REALE
-- TODO: sesso (M/F) da Wikidata
if args['Eventuale nome reale']~=nil then
if args['Eventuale nome reale']~=nil then
add(', nato/a \'\'\''..args['Eventuale nome reale']..'\'\'\'')
add(', nat'..oa..' \'\'\''..args['Eventuale nome reale']..'\'\'\'')
end
end



Versione delle 23:56, 25 feb 2014

Questo modulo serve al {{Autore}} per generare l'incipit e le categorie delle pagine in cui è utilizzato, integrando informazioni da Wikidata quando possibile.


local p = {}

function p.autodetect(frame)
	local aut = frame:getParent().args[1]
	local t = mw.title.getCurrentTitle().text
	if aut==nil or aut=='' then
		local m = string.match(t,'^Libri di (.+)$')
		if m==nil then
			m = string.match(t,'^Testi di (.+)$')
		end
		if m==nil then
			m = string.match(t,'in cui è citato (.+)$')
		end
		if m~=nil then
			return m
		end
	end
	return aut
end

-- SPERIMENTALE --

-- elemento di Wikidata
local item = mw.wikibase.getEntityObject()

function cda(test)
	if item or test then
		local m = require('Modulo:Controllo di autorità')
		if item then
			return m.box(item)
		else
			return m.test()
		end
	end
	return ''
end

-- stringa da restituire
local output = ''

function add(str)
	output = output..str
end

-- aggiunge una categoria
function addCat(name)
	add('[['..mw.site.namespaces[14].name..':'..mw.text.trim(name)..']]')
end

-- aggiunge un template
function addTmp(title, args)
	add(mw.getCurrentFrame():expandTemplate{ title = title, args = (args or {}) })
end

function datetostring(anno, giornoMese)
	if anno=='' then
		return nil
	end
	anno = mw.getCurrentFrame():expandTemplate{ title = 'Autore/Anno', args = { anno } }
	if giornoMese=='' then
		return anno
	end
	return mw.getCurrentFrame():expandTemplate{ title = 'DataGiorno', args = { giornoMese } }..' '..anno
end

-- conta il numero di pagine in una data categoria
function pagesInCat(cat)
	return mw.site.stats.pagesInCategory(cat, 'pages')
end

-- crea un collegamento a una categoria (non vuota) relativa all'autore
function catLink(cat, icon)
	local fcat = mw.ustring.format(cat, nomeAutore)
	if pagesInCat(fcat) > 0 then
		add('<div>[[File:'..(icon or 'Nuvola filesystems folder open.png')..'|20px]] \'\'[[:'..mw.site.namespaces[14].name..':'..fcat..'|'..mw.ustring.format(cat, (args.Nome or '')..' '..(args.Cognome or ''))..']] ('..pagesInCat(fcat)..')\'\'</div>')
	end
end

-- TODO: asserzioni multiple, formattazione, fonti e qualificatori
function claim(prop)
	if item==nil then
		return nil
	end
	local claims = item.claims
	if claims~=nil and claims[prop]~=nil then
		for index,claim in pairs(claims[prop]) do
			if claim.mainsnak.datavalue~=nil and claim.mainsnak.datavalue.value~=nil then
				if claim.mainsnak.datavalue.value['numeric-id']~=nil then
					return mw.wikibase.sitelink('Q'..claim.mainsnak.datavalue.value['numeric-id'])
				else
					return claim.mainsnak.datavalue.value
				end
			end
		end
	end
end

local gender = ''
if item and item.claims and item.claims.P21 and #item.claims.P21 == 1 then
	local claim = item.claims.P21[1]
	if claim.mainsnak.datavalue and claim.mainsnak.datavalue.value and claim.mainsnak.datavalue.value['numeric-id'] then
		local n = claim.mainsnak.datavalue.value['numeric-id']
		if n == 6581097 then
			gender = 'male'
		elseif n == 6581072 then
			gender = 'female'
		end
	end
end

-- Restituisce il collegamento corrispondente al codice fornito
function sitelink(dbname)
	if item and item:getSitelink(dbname) then
		return item:getSitelink(dbname)
	end
end

function fallback(a,b)
	if a~=nil and mw.text.trim(a)~='' then
		return mw.text.trim(a)
	end
	return b
end

-- collegamento a Wikipedia in italiano
function wikipedia()
	return fallback(args['Nome della pagina su Wikipedia'], sitelink('itwiki'))
end

-- collegamento a Wikiquote in italiano
function wikiquote()
	return fallback(args['Nome della pagina su Wikiquote'], sitelink('itwikiquote'))
end

-- collegamento a Wikinotizie
function wikinews()
	return args['Nome della pagina su Wikinotizie']
end

-- collegamento a Commons (per gallerie)
function commons()
	return sitelink('commonswiki')
end

-- titolo della categoria su Commons
function commonsCat()
	return fallback(args['Nome della pagina su Commons'], claim('P373'))
end

function p.autore(frame)
	if frame==nil or frame:getParent()==nil then
		error('Nessun frame rilevato')
	end

	-- parametri passati al template Autore
	args = {}
	for k, v in pairs(frame:getParent().args) do
		if v==nil or v=='' or mw.text.trim(v)=='' then
			args[k] = nil
		else
			args[k] = mw.text.trim(v)
		end
	end

	-- lingua predefinita
	local lang = mw.language.getContentLanguage()

	-- nome dell'autore: Nome, Cognome e Disambigua
	nomeAutore = {}
	for i, v in pairs({'Nome', 'Cognome', 'Disambigua'}) do
		if args[v]~=nil and mw.text.trim(args[v])~='' then
			table.insert(nomeAutore, mw.text.trim(args[v]))
		end
	end
	if #nomeAutore > 0 then
		nomeAutore = table.concat(nomeAutore, ' ')
	else
		error('Nome autore non valido')
	end

	local cognonome = (args.Cognome or '')..(args.Nome or '')

	local attrs = {
		nome = 'Nome',
		cognome = 'Cognome',
		altroNome = 'Eventuale altro nome',
		pseudonimo = 'Eventuale pseudonimo',
		nomeReale = 'Eventuale nome reale',
		giornoMeseNascita = 'Giorno e mese di nascita',
		annoNascita = 'Anno di nascita',
		giornoMeseMorte = 'Giorno e mese di morte',
		annoMorte = 'Anno di morte',
		secoloAttivita = 'Secolo di attività',
		professioneNazionalita = 'Professione e nazionalità',
		wikipedia = 'Nome della pagina su Wikipedia',
		wikiquote = 'Nome della pagina su Wikiquote',
		wikinotizie = 'Nome della pagina su Wikinotizie',
		commons = 'Nome della pagina su Commons',
		immagine = 'Nome dell\'immagine'
	}

	local dati = mw.html.create('span'):attr('id', 'dati')
	for k, v in pairs(attrs) do
		dati:attr('data-'..k, (args[v] or ''))
	end
	add(tostring(dati))
 
	-- CAT: AUTORI OMONIMI (cioè che usano il campo Disambigua per distinguersi da omonimi)
	if args.Disambigua~=nil then
		addCat('Autori omonimi')
	end

	if mw.title.getCurrentTitle().nsText=='Autore' then
		-- CAT: AUTORI-NATI NELL'ANNO
		if args['Anno di nascita']~=nil then
			local a = args['Anno di nascita']
			if a~='' and tostring(tonumber(a))==a and frame:callParserFunction('#titleparts', a, '1', '2')=='' then
				addCat('Nati nel '..a)
			end
		end

		-- CAT: AUTORI-MORTI NELL'ANNO
		if args['Anno di morte']~=nil then
			local a = args['Anno di morte']
			if a~='' and tostring(tonumber(a))==a and frame:callParserFunction('#titleparts', a, '1', '2')=='' then
				addCat('Morti nel '..a)
			end
		end
	end

	-- CAT: AUTORI-INIZIALE
	addCat('Autori')
	addCat('Autori-'..mw.ustring.upper(frame:expandTemplate{ title = 'Prima lettera', args = { (args.Cognome or '')..(args.Nome or '') } }))

	-- CAT: AUTORI DEL SECOLO
	local secolo = (args['Secolo di attività'] or '')
	if secolo=='' then
		addCat('Autori senza secolo indicato')
	else
		if lang:ucfirst(secolo)=='Antichità' then
			addCat('Autori dell\'Antichità')
		elseif lang:ucfirst(secolo)=='Alto Medioevo' then
			addCat('Autori dell\'Alto Medioevo')
		else
			addTmp('Autore/CategorieSecolo', { secolo, cognonome })
		end

		-- CAT: AUTORI PER NAZIONALITÀ E SECOLO
		local plur = frame:expandTemplate{ title = 'Autore/PluraleNazionalità', args = { args['Nazionalità'] } }
		if plur~=nil and plur~='' then
			if lang:ucfirst(secolo)=='Antichità' then
				addCat('Autori '..plur..' dell\'Antichità')
			elseif lang:ucfirst(secolo)=='Alto Medioevo' then
				addCat('Autori '..plur..' dell\'Alto Medioevo')
			else
				addTmp('Autore/CategorieSecolo', { secolo, cognonome, plur })
			end
		end
	end

	local att = nil
	-- CAT: AUTORI PER ATTIVITÀ
	if args['Attività']~=nil then
		att = args['Attività']
		addTmp('Autore/CategorieAttività', { att, cognonome }) 

		-- CAT: AUTORI PER ATTIVITÀ E SECOLO
		if lang:ucfirst(secolo)=='Antichità' then
			addTmp('Autore/CategorieAttività', { att, cognonome, 'dell\'Antichità' })
		elseif lang:ucfirst(secolo)=='Alto Medioevo' then
			addTmp('Autore/CategorieAttività', { att, cognonome, 'dell\'Alto Medioevo' })
		else
			addTmp('Autore/CategorieAttivitàSecolo', { att, secolo, cognonome })
		end
	else
		addCat('Autori senza attività')
	end 

	-- CAT: AUTORI PER NAZIONALITÀ
	if args['Nazionalità']~=nil then
		local naz = args['Nazionalità']
		local plur = frame:expandTemplate{ title = 'Autore/PluraleNazionalità', args = { args['Nazionalità'] } }
		if plur~=nil and plur~='' then
			addCat('Autori '..plur)
			-- AUTORI PER ATTIVITÀ + NAZIONALITÀ
			if att then
				addTmp('Autore/CategorieAttività', { att, cognonome, plur })
			end
		else
			addCat('Autori di nazionalità non censita')
		end
	else
		addCat('Autori senza nazionalità')
	end

	-- CAT: AUTORI CON/SENZA OPERE
	if (pagesInCat('Testi di '..nomeAutore) + pagesInCat('Traduzioni di '..nomeAutore)) > 0 then
		addCat('Autori con opere su Wikisource')
	else
		addCat('Autori senza opere su Wikisource')
	end

	-- CAT: AUTORI DI TESTI MUSICALI
	if pagesInCat('Testi musicati da '..nomeAutore) > 0 then
		addCat('Autori di testi musicali')
	end

	-- CAT: AUTORI CITATI IN OPERE PUBBLICATE
	if pagesInCat('Testi in cui è citato '..nomeAutore) > 0 then
		addCat('Autori citati in opere pubblicate')
	end

	-- CAT: AUTORI SENZA DATI ANAGRAFICI 
	if (
	(args['Giorno e mese di nascita'] or '')..
	(args['Anno di nascita'] or '')..
	(args['Giorno e mese di morte'] or '')..
	(args['Anno di morte'] or '')
	)=='' and args.Nome~='Anonimo' then
		addCat('Autori senza dati anagrafici')
	end

	-- CAT: AUTORI SENZA DATI BIOGRAFICI 
	if (
	(args['Professione e nazionalità'] or '')..
	(args['Attività'] or '')..
	(args['Nazionalità'] or '')
	)=='' then
		addCat('Autori senza dati biografici')
	end

	-- CAT: AUTORI DA CONTROLLARE PER COPYRIGHT 
	if args['Anno di morte']~=nil and tonumber(args['Anno di morte'])~=nil and tonumber(args['Anno di morte']) >= 1926 then
		addCat('Autori da controllare')
	end 

	-- CAT: AUTORI CON/SENZA IMMAGINE 
	if args['Nome dell\'immagine']~=nil then
		addCat('Autori con immagine')
	else
		addCat('Autori senza immagine|'..cognonome)
	end

	local wp  = wikipedia()
	local com = commons()
	local wq  = wikiquote()
	local wn  = wikinews()

	-- CAT: AUTORI SENZA VOCE SU WIKIPEDIA 
	if (wp or '')=='' then
		addCat('Autori senza voce su Wikipedia')
	end

	-- CAT: AUTORI SENZA VOCE SU WIKIQUOTE 
	if (wq or '')=='' then
		addCat('Autori senza voce su Wikiquote')
	end

	-- CAT: AUTORI SENZA VOCE SU COMMONS 
	if (com or '')=='' then
		addCat('Autori senza voce su Commons')
	end

	-- AUTORI SENZA ELEMENTO SU WIKIDATA
	if not item then
		addCat('Autori non collegati a Wikidata')
	end

	-- IMMAGINE AUTORE
	if args['Nome dell\'immagine']~=nil then
		add('[[File:'..args['Nome dell\'immagine']..'|thumb|180px|right|'..(args.Nome or '')..' '..(args.Cognome or '')..']]')
	end

	-- NOME E COGNOME
	add('\'\'\''..mw.text.trim((args.Nome or '')..' '..(args.Cognome or ''))..'\'\'\'')

	-- DATI ANAGRAFICI
	local annoNascita = (args['Anno di nascita'] or '')
	local giornoMeseNascita = (args['Giorno e mese di nascita'] or '')
	local annoMorte = (args['Anno di morte'] or '')
	local giornoMeseMorte = (args['Giorno e mese di morte'] or '')

	if annoNascita=='' and annoMorte=='' then
		if secolo~=nil then
			add('&nbsp;('..secolo..')')
		end
	else
		local nascita = (datetostring(annoNascita, giornoMeseNascita) or '????')
		local morte = (datetostring(annoMorte, giornoMeseMorte) or '...')
		add('&nbsp;('..nascita..'&nbsp;–&nbsp;'..morte..')')
	end

	local oa = lang:gender(gender, 'o', 'a', 'o/a')

	-- EVENTUALE ALTRO NOME
	if args['Eventuale altro nome']~=nil then
		add(', not'..oa..' anche come \'\'\''..args['Eventuale altro nome']..'\'\'\'')
	end
	-- EVENTUALE PSEUDONIMO
	if args['Eventuale pseudonimo']~=nil then
		add(', not'..oa..' anche con lo pseudonimo di \'\'\''..args['Eventuale pseudonimo']..'\'\'\'')
	end

	-- EVENTUALE NOME REALE
	if args['Eventuale nome reale']~=nil then
		add(', nat'..oa..' \'\'\''..args['Eventuale nome reale']..'\'\'\'')
	end

	-- DATI BIOGRAFICI
	if args['Professione e nazionalità']~=nil then
		add(', '..lang:lcfirst(args['Professione e nazionalità'])..'.')
	else
		if args['Attività']~=nil then
			add(', ')
			if args['Nazionalità']~=nil then
				addTmp('Autore/Attività', { args['Attività'] })
				add(' '..args['Nazionalità']..'.')
			else
				addTmp('Autore/Attività', { args['Attività']..'.' })
			end
		
		else
			add('.')
		end
	end

	add(cda(args.test))

	-- INTERPROGETTO
	add(tostring(mw.html.create('div'):css('margin-top', '15px')
	:wikitext(frame:expandTemplate{ title = 'Interprogetto', args = { wAutore = wp, qAutore = wq, nAutore = wn, CommonsAutore = com } })))

	if args.Nome~='Anonimo' then
		-- LINK ALLA CATEGORIA TESTI DI ...
		catLink('Testi di %s')

		-- LINK ALLA CATEGORIA TESTI MUSICATI DA...
		catLink('Testi musicati da %s', 'Nuvola filesystems folder music.png')
	end

	-- LINK ALLA CATEGORIA TRADUZIONI DI ...
	catLink('Traduzioni di %s')

	-- LINK ALLA CATEGORIA TESTI IN CUI È CITATO ...
	catLink('Testi in cui è citato %s')

	if wn~=nil then
		addCat('Autori con pagina su Wikinews')
	end

	return output
end

return p