Modulo:StagioniTV/sandbox: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
err.
Omega Bot (discussione | contributi)
m Bot: rimuovo no globals obsoleto
 
(72 versioni intermedie di 2 utenti non mostrate)
Riga 1:
require('Modulo:No globalsstrict')
 
local wikidata = require('Modulo:Wikidata')
Riga 5:
local mNumeroRomano = require('Modulo:Numero romano')
local getArgs = require('Modulo:Arguments').getArgs
local errorCategorycategoria_errore = '[[Categoria:Errori di compilazione del template Stagioni televisiveStagioniTV]]'
local messaggio_errore = 'Template non compilato correttamente, controlla le [[Template:StagioniTV|istruzioni]]'
local cfg = mw.loadData('Modulo:StagioniTV/Configurazione')
local p = {}
 
---- sotto-funzione per restituire l'ordinale femminile nella costruzione dei collegamenti
local function ordinalefemminile(num)
return mw.getCurrentFrame():expandTemplate {
Riga 15 ⟶ 18:
end
 
---- sotto-funzione per evitare potenziali errori restituendo il titolo del template in corsivo in presenza di "'"'
function p.prova(frame)
local function safe_for_italics( str )
if str and str ~= '' then
if str:sub(1,1) == "'" then str = "<span></span>" .. str; end
if str:sub(-1,-1) == "'" then str = str .. "<span></span>"; end
-- Remove newlines as they break italics.
str = str:gsub( '\n', ' ' );
return "''" .. str .. "''"
end
end
 
---- sotto-funzione per evitare il corsivo nel titolo del template quando sono presenti caratteri speciali/non latini
---- ispirata al modulo:IsLatin
local function titolo_template( sitelink, etichetta )
local titolotemplate
 
if etichetta ~= nil and etichetta ~= '' then
local corsivo = true
for pos = 1, mw.ustring.len(etichetta) do
local charval = mw.ustring.codepoint(etichetta, pos)
if (charval >= 880 and charval < 8192) or (charval >= 8960) then
corsivo = false
break
end
end
etichetta = (corsivo and safe_for_italics(etichetta)) or etichetta
titolotemplate = mw.ustring.format("[[%s|<span style=\"color:white;\">%s</span>]]", sitelink, etichetta)
end
return titolotemplate
end
 
function p.main(frame)
local args = getArgs(frame)
return p._prova_main(args)
end
 
function p._prova_main(args)
local template, sfondo, categoria, target, stagioneoedizione, titlepreposizione, preposizionelinks, errore
local id, localID, localID2, tipo, titolo, etichetta, nome, sitelink, stagioni
local pageTitle = args[1] or args.titolo
local tbl = {}
local lang = mw.language.getContentLanguage()
local sep = '&nbsp;<b>&middot;</b>&#32;'
if not cfg.titolo[args[1]] or~= args.titolonil then
id = cfg.titolo[args[1]].id
if string.find(mw.title.getCurrentTitle().text, '^Episodi d[ei]') then
tipo = cfg.titolo[args[1]].tipo or 'serie TV'
localID = wikidata._getProperty({ 'P361', n = 1, from = mw.wikibase.getEntityIdForCurrentPage(), formatting = 'raw' })
nome = cfg.titolo[args[1]].nome
end
preposizione = cfg.titolo[args[1]].preposizione or 'di'
stagioni = cfg.titolo[args[1]].stagioni
etichetta = cfg.titolo[args[1]].etichetta or wikidata._getLabel({ cfg.titolo[args[1]].id, 'it'})
sitelink = cfg.titolo[args[1]].sitelink or mw.wikibase.sitelink( cfg.titolo[args[1]].id )
else
 
-- Rileva l'identificativo della scheda wikidata collegata alla fiction o al programma TV,
-- se nessun titolo è specificato prova a recuperlo dalla voce della stagione/edizione, se questa ha una sua scheda su wikidata
if not args[1] or args.titolo then
if string.find(mw.title.getCurrentTitle().text, '^Episodi d[ei]') then
localID = wikidata._getProperty({ 'P361', n = 1, from = mw.wikibase.getEntityIdForCurrentPage(), formatting = 'raw' })
localID2 = wikidata._getProperty({ 'P179', n = 1, from = mw.wikibase.getEntityIdForCurrentPage(), formatting = 'raw' })
end
if localID ~= nil then
id = localID
elseif localID2 ~= nil then
id = localID2
end
else
else id = mw.wikibase.getEntityIdForTitle( pageTitle )
id = mw.wikibase.getEntityIdForTitle( pageTitle )
end
end
if wikidata._instanceOf({ 'Q581714', from = id }) then tipo = 'animata'
elseif wikidata._instanceOf({ 'Q5398426', from = id }) then tipo = 'serie TV'
else tipo = 'programma'
end
if id then
titolo = wikidata._getLink({ id })
etichetta = wikidata._getLabel({ id, 'it'})
sitelink = mw.wikibase.sitelink( id )
stagioni = wikidata._getProperty({ 'P2437', n = 1, from = id, formatting = 'raw' })
else errore = 'Template non compilato correttamente, controlla le istruzioni'
end
 
-- Identifica il tipo di programma (serie televisiva, serie animata o programma TV generico)
local tbl = {}
-- ed estrae gli altri dati fondamentali da Wikidata (etichetta in italiano, numero stagioni, sitelink)
local lang = mw.language.getContentLanguage()
-- se uno di essi non è presente è generato un messaggio di errore
local sep = args.debug and '<br />' or '&nbsp;<b>&middot;</b>&#32;'
local genere = wikidata._getProperty({ 'P136', from = id, formatting = 'raw' })
if wikidata._instanceOf({ 'Q581714', from = id }) or genere ~= nil and string.find(genere, 'Q581714') then
if tipo == 'animata' then sfondo = 'background:#ab110c;'
tipo = 'animata'
elseif tipo == 'serie TV' then sfondo = 'background:#115806;'
elseif wikidata._instanceOf({ 'Q5398426', from = id }) then
elseif tipo == 'programma' then sfondo = 'background:#161e8e;'
tipo = 'serie TV'
end
elseif wikidata._instanceOf({ 'Q15416', from = id }) then
tipo = 'programma'
elseif wikidata._instanceOf({ 'Q526877', from = id }) then
tipo = 'serie TV'
end
if tipo == 'programma'id then
titolo = wikidata._getLink({ id })
title = mw.title.new(string.format('Categoria:Edizioni de %s', etichetta))
etichetta = wikidata._getLabel({ id, 'it'})
preposizione = (title and title.exists) and 'de' or 'di'
sitelink = mw.wikibase.sitelink( id )
else title = mw.title.new(string.format('Episodi de %s (prima stagione)', etichetta))
stagioni = wikidata._getProperty({ 'P2437', n = 1, from = id, formatting = 'raw' })
preposizione = (title and title.exists) and 'de' or 'di'
else
end
errore = messaggio_errore
for i = 1, stagioni do
local ordinaleF = ordinalefemminile(i)
if tipo == 'programma' then
target = string.format('%s (%s edizione)', etichetta, ordinaleF) else
target = string.format('Episodi %s %s (%s stagione)', preposizione, etichetta, ordinaleF)
end
 
if tipo == 'programma' then stagioneoedizione = 'edizione' else stagioneoedizione = 'stagione' end
-- Questa parte del codice genere i collegamenti alle singole edizioni/stagioni e la relativa categoria
local label = args.formato == 'numeri romani' and mNumeroRomano.main({ i }) or
if not etichetta or not sitelink or not stagioni or not tipo then
(args.formato == 'forma breve' and string.format('%dª', i) or
errore = messaggio_errore
string.format('%s %s', lang:ucfirst(ordinaleF), stagioneoedizione))
else
table.insert(tbl, string.format('[[%s|%s]]', target, label))
---- per rendere l'uso del template più leggero prova a estrarre il nome del programma e la preposizione usata nei link direttamente
if target == mw.title.getCurrentTitle().text and mw.title.getCurrentTitle().namespace == 0 then
---- dalle voci in cui è usato, altrimenti verifica l'esistenza della voce della prima stagione con la sintassi standard
if stagioni == '1' then
if tipo == 'programma' then
if args[2] ~= 'elenco' and string.find(mw.title.getCurrentTitle().text, '^.+%(%a+edizione%)$') then
categoria = string.format('[[Categoria:Edizioni di programmi televisivi|%s]]', etichetta) else
nome = string.match(mw.title.getCurrentTitle().text, '^(.+) %(%a+edizione%)$')
categoria = string.format('[[Categoria:Liste di episodi di serie televisive|%s]]', etichetta)
preposizione = mw.title.new(string.format('Categoria:Edizioni de %s', nome)).exists and 'de' or 'di'
elseif mw.title.new(string.format('%s (prima edizione)', etichetta)).exists then
nome = etichetta
preposizione = mw.title.new(string.format('Categoria:Edizioni de %s', etichetta)).exists and 'de' or 'di'
else
nome = sitelink
preposizione = mw.title.new(string.format('Categoria:Edizioni de %s', sitelink)).exists and 'de' or 'di'
end
elseif stagionitipo ~== 'serie TV' or tipo == '1animata' then
if args[2] ~= 'elenco' and string.find(mw.title.getCurrentTitle().text, '^Episodi d[ei] ') then
if tipo == 'programma' then
if string.find(mw.title.getCurrentTitle().text, '^Episodi d[ei].+stagione%)$') then
categoria = string.format('[[Categoria:Edizioni %s %s|%02d]]', preposizione, etichetta, i) else
nome = string.match(mw.title.getCurrentTitle().text, '^Episodi d[ei] (.+) %(%a+ stagione%)$')
categoria = string.format('[[Categoria:Stagioni %s %s|%02d]]', preposizione, etichetta, i)
preposizione = string.match(mw.title.getCurrentTitle().text, '^Episodi (d[ei])')
elseif string.find(mw.title.getCurrentTitle().text, '^Episodi d[ei].+$') then
nome = string.match(mw.title.getCurrentTitle().text, '^Episodi d[ei] (.+)$')
preposizione = string.match(mw.title.getCurrentTitle().text, '^Episodi (d[ei])')
end
elseif mw.title.new(string.format('Episodi di %s (prima stagione)', etichetta)).exists then
nome = etichetta
preposizione = 'di'
elseif mw.title.new(string.format('Episodi de %s (prima stagione)', etichetta)).exists then
nome = etichetta
preposizione = 'de'
else
nome = sitelink
preposizione = mw.title.new(string.format('Episodi de %s (prima stagione)', sitelink)).exists and 'de' or 'di'
end
end
end
end
 
local links = mw.text.listToText(tbl, sep, sep) .. (categoria or '')
---- qui vengono generati i link alle stagioni/edizioni e la categoria appropriata
if not nome or not preposizione or not stagioni or not tipo then
errore = messaggio_errore
else
for i = 1, stagioni do
-- prevede un massimo di 80 stagioni per evitare eventuali timeout causati da vandalismi su Wikidata
if i > 80 then categoria = categoria_errore break end
local ordinaleF = ordinalefemminile(i)
if tipo == 'programma' then
target = string.format('%s (%s edizione)', nome, ordinaleF)
else
target = string.format('Episodi %s %s (%s stagione)', preposizione, nome, ordinaleF)
end
if tipo == 'programma' then
stagioneoedizione = 'edizione'
else
stagioneoedizione = 'stagione'
end
local label = args.formato == 'numeri romani' and mNumeroRomano.main({ i }) or (args.formato == 'forma breve' and string.format('%dª', i) or string.format('%dª %s', i, stagioneoedizione))
table.insert(tbl, string.format('[[%s|%s]]', target, label))
if (target == mw.title.getCurrentTitle().text and mw.title.getCurrentTitle().namespace == 0) or (mw.title.getCurrentTitle().text == string.format('Episodi %s %s', preposizione, nome) and mw.title.getCurrentTitle().namespace == 0) or args.debugcat then
if stagioni == '1' or stagioni == '1±0' then
if tipo == 'programma' then
categoria = string.format('[[Categoria:Edizioni di programmi televisivi|%s]]', nome)
elseif tipo == 'animata' then
categoria = string.format('[[Categoria:Liste di episodi di serie animate|%s]]', nome)
else
categoria = string.format('[[Categoria:Liste di episodi di serie televisive|%s]]', nome)
end
elseif tipo == 'programma' then
categoria = string.format('[[Categoria:Edizioni %s %s|%02d]]', preposizione, nome, i)
else
categoria = string.format('[[Categoria:Stagioni %s %s|%02d]]', preposizione, nome, i)
end
end
end
local catsp
if not id or not etichetta or not stagioni then
template =if mw.title.getCurrentTitle().namespacetext == 0string.format('Episodi andspeciali errorCategory%s or%s', preposizione, nome) nilthen
catsp = string.format('[[Categoria:Stagioni %s %s|Episodi speciali]]', preposizione, nome)
elseif args[2] == 'elenco' then template = links
end
elseif args[3] == 'elenco' then template = links
elseif args[4] == 'elenco' then template = links
local speciali = (args[2] == 's' or args[3] == 's' or args[4] == 's') and sep..string.format('[[Episodi speciali %s %s|Episodi speciali]]', preposizione, nome)
links = mw.text.listToText(tbl, sep, sep) .. (speciali or '') .. (categoria or catsp or '')
end
 
-- Infine è generato il navbox con i dati su estratti e formattati
 
---- con sfondo del titolo del template diverso a seconda del tipo di programma
if tipo == 'animata' then
sfondo = 'background:#ab110c;'
elseif tipo == 'serie TV' then
sfondo = 'background:#206600;'
elseif tipo == 'programma' then
sfondo = 'background:#2e2eff;'
end
 
---- se sono presenti più di una stagione/edizione genera il navbox, altrimenti solo la categoria
---- se è richiesto solo l'elenco dei collegamenti genera solo i link, utile per l'inclusione in altri template
if not id or not etichetta or not stagioni or not tipo then
template = mw.title.getCurrentTitle().namespace == 0 and categoria_errore or nil
elseif args[2] == 'elenco' then
template = links
elseif stagioni == '1' or stagioni == '1±0' then
template = categoria
else
template = navbox._navbox({
name = 'Stagioni televisiveStagioniTV',
title = string.formattitolo_template("[[%s|<span style=\"color:white;\">''%s''</span>]]", sitelink, etichetta),
titlestyle = sfondo,
image = string.format("[[File:Blue pencil.svg |frameless |12px |alt=Modifica su Wikidata|link=https://www.wikidata.org/wiki/%s#P2437|Modifica su Wikidata]]", id),
imagestyle = 'width:5%;',
list1 = links
})