Modulo:Software: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
separato p.categories() con p.wikidataCategories() e aggiunto p.wikidataCategoriesFromMainTopic() per #Categorizzare le categorie
refactoring disastroso per semplificazione Modulo:Software/Configurazione +generatore Template:Software/Linguaggio/man +categorizzazione delle categorie... per linguaggio e.g. Categoria:GNOME
Riga 11:
 
local MAX_RECURSION = 4 -- [[Modulo:Software/man#Considerazioni implementative]]
 
local UNKNOWN_WD_LANGUAGE = "[[Categoria:Linguaggio di programmazione da Wikidata non previsto]]"
local UNKNOWN_WD_LICENSE = ""
 
local TEMPLATE_UNKNOWN_ARG_WARN = "<span style=\"font-size:75%\">(non&nbsp;in&nbsp;[[Template:Software/Linguaggio|lista]])</span>"
local TEMPLATE_UNKNOWN_ARG_CAT = "[[Categoria:Software in linguaggio non riconosciuto]]"
 
local LICENSE_CAT = "Software in licenza %s"
 
-------------------------------- Sgabuzzino ------------------------------------
Line 28 ⟶ 36:
return false
end
 
local function rawProperty(property)
return mWikidata._formatStatement(property, {formatting = 'raw'})
end
 
--[[
* @param string WIkidataItem
* @TODO ora prende il primo, a prescindere dalla lingua (credo).
* @return string
]]
local function shortWikidataLabel(from)
local s = ''
local label = mw.wikibase.label(from)
local sitelink = mw.wikibase.sitelink(from) or label
if string.len(label) > 10 then
label = mWikidata._getProperty( { 'P1813', n = 1, from = from } ) or label
end
return "[[" .. sitelink .. "|" .. label .. "]]"
end
 
 
--[[
Line 51 ⟶ 79:
--[[
* Poi non ditemi che PHP fa schifo. asd.
* Sembra che l'operatore "#" ogni tanto non vada col Modulo:Wikidata.
* 00:28, 28 feb 2017‎ Valerio Bozzolan
]]
Line 59 ⟶ 88:
end
return i
end
 
local function rawProperty(property)
return mWikidata._formatStatement(property, {formatting = 'raw'})
end
 
Line 95 ⟶ 120:
from = from and from[1]
if not from then
error("QuestQuesta non è una categoria, o è assente la proprietà Wikidata P301")
end
return rawProperty(from)
end
 
--[[
* Formatta una categoria.
*
* @param category string Primo argomento per sprinf
* @param part string Placeholder per sprintf
* @param man boolean Mostrare solo la categoria invece che categorizzare?
* @return string
]]
local function formatCategory(category, part, man)
local colon = man and ':' or ''
return "[[" .. colon .. "Categoria:" .. string.format(category, part) .. "]]"
end
 
--[[
* Categorie di errore nel caso non ci siano linguaggi.
*
* @param free boolean|nil È software libero?
]]
local function noLanguageWarning(free)
return free and '[[Categoria:Software libero senza linguaggio]]' or '[[Categoria:Software senza linguaggio]]'
end
 
Line 127 ⟶ 174:
]]
function p.categories(frame)
return p._categorize( onlySoftwareArguments(frame) ) and p.wikidataCategorieslicenseCategories(frame)
end
 
function p.wikidataCategoriesFromMainTopic(frame)
return p._wikidataCategories( fromItemMainTopic() )
end
 
Line 136 ⟶ 187:
function p.wikidataCategories(frame)
return p._wikidataCategories( fromItem(frame) )
end
 
function p.wikidataCategoriesFromMainTopic(frame)
return p._wikidataCategories( fromItemMainTopic() )
end
 
--[[
* Tutte le categorie applicabili da Wikidata.
* Categorie legate alle licenze.
*
* Per ora ci sono solo le licenze, ma qui ci devono andare anche i linguaggi.
*
* @param from string|nil Wikidata Item
* @return string
]]
function p.licenseCategories_wikidataCategories(framefrom)
return p._licenseCategories( fromItem(framefrom) .. p._languageCategories(from)
end
 
--[[
* Categorie legate alle licenze.
* Licenze.
*
* @return string
]]
function p.licenseslicenseCategories(frame)
return p._licenseCategories( fromItem(frame) )
local s = ''
for i, l in pairs( p._licenses( fromItem(frame) ) ) do
local space = i == 1 and '' or '<br />'
s = s .. space .. rawProperty(l)
end
return s
end
 
Line 173 ⟶ 218:
 
local args = frame and onlySoftwareArguments(frame)
local is_free = p._isFreeSoftware(args)
 
local tl_has = p._templateHasLanguages(args) and true or false --exclude nil
--[[
* Linguaggi dal Template:Software e da Wikidata
]]
local tl_has = p._templateHasLanguages(args) and true or false
local tl_languages = p._getTemplateLanguages(args)
local wd_languages = mWikidata._getClaims('P277')
Line 183 ⟶ 225:
-- È il namespace principale?
local nszero = mw.title.getCurrentTitle().namespace == 0
 
-- Tutti i linguaggi sono identificati?
local allFound = true
 
-- Il template si è arricchito grazie a Wikidata?
local improved = false
 
-- Tutti i linguaggi sono identificati da Wikidata e dal template?
local tl_allFound = true
local wd_allFound = true
 
-- Il template non mostra alcun linguaggio?
local noLanguages = not tl_has
 
local outputLanguages = {} -- {key = {label1, category1}, key = {label2, category2}}
local outputLanguage = function(label, category, note)
return {label = label, category = category, note = note or '' }
end
 
if wd_languages then
for i, language in pairs(wd_languages) do
local wikidataIDlanguageId = rawProperty(language)
language = conf.wikidataToLanguageSluglanguage[wikidataIDlanguageId]
if language then
outputLanguages[languageId] = outputLanguage(
if tl_languages[language] == nil then
shortWikidataLabel(languageId),
-- 'c' = 'nota linguaggio c'
tl_languages[p._languageCategory(language], = ''is_free)
)
noLanguages = false
improved = true
end
else
allFoundwd_allFound = false
end
end
Line 219 ⟶ 267:
end
else
-- No, `#var` non fa quello che uno si aspetterebbe
if p._templateHasExtraInformations(args) or count(tl_languages) > count(wd_languages) then
-- Nel template c'è qualcosa in più rispetto a Wikidata
Line 236 ⟶ 283:
end
 
if noLanguages and nszerotl_languages then
for languageSlug, note in pairs(tl_languages) do
if p._isFreeSoftware(frame) then
if languageSlug == 'sconosciuto' then
s = s .. '[[Categoria:Software libero senza linguaggio]]'
outputLanguages[languageSlug] = outputLanguage(
else
"Sconosciuto",
s = s .. '[[Categoria:Software senza linguaggio]]'
TEMPLATE_UNKNOWN_ARG_CAT,
note
)
else
local languageId = conf.languageSlugToWikidata[languageSlug]
local language = conf.language[languageId]
if language then
outputLanguages[languageId] = outputLanguage(
shortWikidataLabel(languageId),
p._languageCategory(language, is_free),
note
)
else
-- Mostrala comunque, a caso, così, tanto per
outputLanguages[languageSlug] = outputLanguage(
languageSlug,
p._languageCategory(languageSlug, is_free),
note
)
tl_allFound = false
end
end
end
end
 
if not allFound and nszero then
s = s .. '[[Categoria:Linguaggio di programmazione da Wikidata non previsto]]'
end
 
local i = 0
for id, language in pairs(outputLanguages) do
local softwareLiberoSiNo = p.isFreeSoftware(args)
for language, note in pairs(tl_languages) do
local glue = i > 0 and '<br />' or ''
s = s .. glue .. frame:expandTemplate{language.label .. language.category .. language.note
title = 'Software/Linguaggio',
args = {language, args['Categorie'], softwareLiberoSiNo}
} .. note
i = i + 1
end
 
if args['LinguaggioAltri'] then
local glue = i > 0 and '<br />' or ''
s = s .. args['LinguaggioAltri']
s = s .. glue .. args['LinguaggioAltri']
end
 
if nszero then
if not tl_allFound then
s = s .. TEMPLATE_UNKNOWN_ARG_WARN .. TEMPLATE_UNKNOWN_ARG_CAT
end
if not wd_allFound then
s = s .. UNKNOWN_WD_LANGUAGE
end
if noLanguages then
s = s .. noLanguageWarning(is_free)
end
end
 
return s
end
 
function p.manLanguages(frame)
local s = '<table class="wikitable"><tr><th>Parametro</th><th>Risultato</th><th>Categoria automatica</th>'
local TD = '<td>%s</td>'
for id, language in pairs( conf.language ) do
s = s .. '<tr>'
local slugs, i = '', 0
for slug, sub_id in pairs( conf.languageSlugToWikidata ) do
if sub_id == id then
local glue = i > 0 and '<br />' or ''
slug = "<code>" .. slug .. "</code>"
slugs = slugs .. glue .. slug
i = i + 1
end
end
s = s .. string.format(TD, slugs)
s = s .. string.format(TD, shortWikidataLabel(id) )
s = s .. string.format(TD, p._languageCategory(language, false, true, '//') )
s = s .. '</tr>'
end
return s .. '</table>'
end
 
Line 424 ⟶ 520:
function p._getTemplateLanguages(args)
local languages = {}
for i=0,3 do
local v = args['Linguaggio']
local j = i == 0 and '' or i
if v then
languages[local p._preferredLanguageSlug(v) ] = args['NotaLinguaggioLinguaggio'] or.. ''j]
end
for i=1,3 do
v = args['Linguaggio' .. i]
if v then
languages[v = v == 'sconosciuto' or p._preferredLanguageSlug(v)
languages[ v ] = args['NotaLinguaggio' .. ij] or ''
end
end
Line 604 ⟶ 698:
function p._hasAProprietaryLicense(from)
return p._hasALicenseOfType(PROPRIETARY, from)
end
 
 
--[[
* Tutte le categorie applicabili da Wikidata.
*
* Per ora ci sono solo le licenze, ma qui ci devono andare anche i linguaggi.
*
* @param from string|nil Wikidata Item
* @return string
]]
function p._wikidataCategories(from)
return p._licenseCategories(from)
end
 
Line 628 ⟶ 709:
local s = ''
for i, l in pairs( p._licenses( from ) ) do
l = conf.licenseCategory[ rawProperty(l) ]
l = conf.licenseCategory[l]
if l then
s = s .. string.formatformatCategory("[[Categoria:Software in licenza %s]]"LICENSE_CAT, l)
else
s = s .. UNKNOWN_WD_LICENSE
end
end
return s
end
 
 
 
--------------------------- Non usate. Perchè? Boh. ----------------------------
 
--[[
* Categorie legate ai linguaggi da Wikidata.
* Albero inverso di Modulo:Software/Configurazione
*
* @param from string|nil E.g.Wikidata 'cpp'Item
* @return string|nil E.g. 'Q2407'
]]
function p._languageCategories(from)
local _languageSlugToWikidata = {}
local s = ''
function p._languageSlugToWikidata(slug)
local languages = mWikidata._getClaims('P277', {from = from} ) -- Property:programming language
if next(_languageSlugToWikidata) == nil then
local free = p._isFreeSoftwareByWikidata(from)
for q,l in pairs(conf.wikidataToLanguageSlug) do
local missing = false
_languageSlugToWikidata[l] = q
if languages then
for _, l in pairs( languages ) do
l = conf.language[ rawProperty(l) ]
if l then
s = s .. p._languageCategory(l, free)
else
missing = true
end
end
else
s = noLanguageWarning(free)
end
if missing then
return _languageSlugToWikidata[ p._preferredLanguageSlug(slug) ]
s = s .. UNKNOWN_WD_LANGUAGE
end
return s
end
 
--[[
* La categoria di uno specifico linguaggio di programmazione.
* Usata solo per debug.
*
* @param language table
* @param free boolean|nil È software libero?
* @param man boolean|nil È a fini di documentazione?
* @param deefault string|nil Valore di default nel caso mancasse
* @see Modulo:Software/Configurazione
* @return string
]]
function p.languageSlugToWikidata_languageCategory(framelang, free, man, default)
local s
return p._languageSlugToWikidata( getArgs(frame)[1] )
if lang.cat then
s = free and lang.free and "Software libero in %s" --free = true
or free == false and lang.nonfree and "Software proprietario in %s" --free = false
or "Software in %s" --free = nil
s = formatCategory(s, lang.cat, man)
end
return s or default or ''
end