Modulo:DTBase: differenze tra le versioni

Da Wikitrek.
Copiato da memoryg2
 
Wikimedia Commons description added
 
(181 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
--- This module represent the package containing basic functions to access data from the WikiBase instance DataTrek
-- @module p
-- @author Luca Mauri [[Utente:Lucamauri]]
-- Add other authors below
-- Keyword: wikitrek
-- Keyword: wikitrek
local p = {}
local p = {}
Riga 7: Riga 11:
end
end
function p.ExtLinks(frame)
function p.ExtLinks(frame)
local AllRows
local AllRows = ""
local Item = mw.wikibase.getEntity()
local Item = mw.wikibase.getEntity()
if not Item then
if not Item then
Riga 19: Riga 23:
local LinkID
local LinkID
local LinkWiki
local LinkWiki
local ExternalIDList = ""
if not LinkStatement['qualifiers']['P19'] then
if not LinkStatement['qualifiers']['P19'] then
Riga 34: Riga 39:
end
end
--[=[
if not AllRows then
if not AllRows then
AllRows = "''Nessun collegamento generico [[:datatrek:Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|trovato su DataTrek]]''"
--AllRows = "''Nessun collegamento generico [[:datatrek:Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|trovato su DataTrek]]''"
AllRows = "''Nessun collegamento generico trovato su DataTrek''"
end
end
]=]
--return AllRows .. string.char(10) .. string.char(10) .. "=== Interwiki ===" .. string.char(10) .. "* " .. frame:expandTemplate{title = 'InterlinkMA', args = {Nome=Item:getSitelink("enma")}} .. string.char(10) .. p.SiteLinksInterwiki()
 
return AllRows .. string.char(10) .. string.char(10) .. "=== Interwiki ===" .. string.char(10) .. p.SiteLinksInterwiki() .. string.char(10) .. "=== Identificativi esterni ===" .. string.char(10) .. p.ExternalID()
if AllRows ~= "" then
AllRows = AllRows .. string.char(10) .. string.char(10)
end
ExternalIDList = p.ExternalID(frame)
if ExternalIDList ~= nil and ExternalIDList ~= "" then
ExternalIDList = string.char(10) .. "=== Identificativi esterni ===" .. string.char(10) .. ExternalIDList
end
return AllRows .. "=== Interwiki ===" .. string.char(10) .. p.SiteLinksInterwiki() .. ExternalIDList
end
end
function p.Categories(frame)
function p.Categories(frame)
local Opening = '[[Categoria:'
local Opening = '[[Category:'
local CategoryP = 'P30'
local CategoryP = 'P30'
local AbbrP = 'P24'
local SeriesP = 'P16'
local InstanceQ
local InstanceQ
local SeriesQ
local SeriesQ
local SeriesItem
local AllCategories = {}
local AllCategories = {}
Riga 54: Riga 74:
end
end
--Categories manually inserted into the Item
local ItemCategories = Item:getAllStatements(CategoryP)
local ItemCategories = Item:getAllStatements(CategoryP)
for _, ItemCategory in pairs(ItemCategories) do
for _, ItemCategory in pairs(ItemCategories) do
Riga 59: Riga 80:
end
end
--Categories from the Instance's Item
InstanceQ = mw.wikibase.getEntity(Item['claims']['P14'][1].mainsnak.datavalue.value['id'])
InstanceQ = mw.wikibase.getEntity(Item['claims']['P14'][1].mainsnak.datavalue.value['id'])
local InstanceCategories = InstanceQ:getAllStatements(CategoryP)
local InstanceCategories = InstanceQ:getAllStatements(CategoryP)
local ItemSeries = Item:getAllStatements(SeriesP)
for _, InstanceCategory in pairs(InstanceCategories) do
for _, InstanceCategory in pairs(InstanceCategories) do
AllCategories[#AllCategories + 1] = Opening .. InstanceCategory.mainsnak.datavalue['value'] .. ']]'
local InstanceValue = InstanceCategory.mainsnak.datavalue['value']
AllCategories[#AllCategories + 1] = Opening .. InstanceValue .. ']]' --InstanceCategory.mainsnak.datavalue['value'] .. ']]'
--Category generated joining the item's Instance category plus the Series short name IF it exists
if ItemSeries then
for _, ItemSer in pairs(ItemSeries) do
SeriesItem = mw.wikibase.getEntity(ItemSer.mainsnak.datavalue.value['id'])
AllCategories[#AllCategories + 1] = Opening .. InstanceValue .. " di " .. SeriesItem.claims[AbbrP][1].mainsnak.datavalue['value'] .. ']]' --SeriesItem.labels['it'].value .. ']]'
end
end
end
end
--Non ha senso aggiungere categoria per la serie perchè diventerebbe troppo grande
--Non ha senso aggiungere categoria per la serie perchè diventerebbe troppo grande
--[===[
--[=[SeriesQ = mw.wikibase.getEntity(Item['claims']['P16'][1].mainsnak.datavalue.value['id'])
SeriesQ = mw.wikibase.getEntity(Item['claims']['P16'][1].mainsnak.datavalue.value['id'])
local SeriesCategories = SeriesQ:getAllStatements(CategoryP)
local SeriesCategories = SeriesQ:getAllStatements(CategoryP)
for _, SeriesCategory in pairs(SeriesCategories) do
for _, SeriesCategory in pairs(SeriesCategories) do
AllCategories[#AllCategories + 1] = Opening .. SeriesCategory.mainsnak.datavalue['value'] .. ']]'
AllCategories[#AllCategories + 1] = Opening .. InstanceCategory.mainsnak.datavalue['value'] .. " DI " .. SeriesCategory.mainsnak.datavalue['value'] .. ']]'
end
end]=]
]===]
 
return table.concat(AllCategories, string.char(10))
return table.concat(AllCategories, string.char(10))
end
end
Riga 83: Riga 114:
local Item = mw.wikibase.getEntity()
local Item = mw.wikibase.getEntity()
if not Item then
if not Item then
Item = mw.wikibase.getEntity('Q1')
return "''Nessun collegamento generico trovato su DataTrek''"
end
end
Riga 94: Riga 125:
enmb = 'Memory Beta (inglese)',
enmb = 'Memory Beta (inglese)',
sto = 'Star Trek Online wiki',
sto = 'Star Trek Online wiki',
wikidata = 'Pagina della entità su Wikidata',
enwiki = 'Wikipedia (inglese)',
enwiki = 'Wikipedia (inglese)',
itwiki = 'Wikipedia (inglese)'
itwiki = 'Wikipedia (italiano)',
dewiki = 'Wikipedia (tedesco)',
dema = 'Memory Alpha (tedesco)',
demb = 'Memory Beta (tedesco)',
fanlore = 'Fanlore',
trekipedia = 'Trekipedia',
commonswiki = 'Wikimedia Commons'
}
}
for _, SiteLink in pairs(SiteLinks) do
for _, SiteLink in pairs(SiteLinks) do
local TitleLabel
local TitleLabel
Riga 104: Riga 143:
if string.find(string.lower(TitleLabel), 'datatrek') then
if string.find(string.lower(TitleLabel), 'datatrek') then
-- Un link a DataTrek deve portare alla Entità
-- Un link a DataTrek deve portare alla Entità
-- Questo non dovrebbe mai esistere lo aggiungo a mano alla fine del ciclo
AllLinks[#AllLinks + 1] = "* [[:" .. SiteLink['site'] .. ":Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|''" .. SiteLink['title'] .. "'']], Pagina della entità su " .. TitleLabel
AllLinks[#AllLinks + 1] = "* [[:" .. SiteLink['site'] .. ":Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|''" .. SiteLink['title'] .. "'']], Pagina della entità su " .. TitleLabel
elseif string.find(string.lower(TitleLabel), 'wikitrek') then
elseif string.find(string.lower(TitleLabel), 'wikitrek') then
Riga 111: Riga 151:
end
end
end
end
-- Interlink a DataTrek
AllLinks[#AllLinks + 1] = "* [[" .. 'datatrek' .. ":Item:" .. Item.id .. "|''" .. Item.id .. "'']], Pagina della entità su " .. Titles['datatrek']
--AllLinks[#AllLinks + 1] = "* [[" .. 'datatrek' .. ":Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "'']], Pagina della entità su " .. Titles['datatrek']
return table.concat(AllLinks, string.char(10))
return table.concat(AllLinks, string.char(10))
end
end
function p.ExternalID()
function p.ExternalID(frame)
local AllExtID = {}
local AllExtID = {}
local AllSources
local SourcesP = {}
local Item = mw.wikibase.getEntity()
local Item = mw.wikibase.getEntity()
local AllP
local AllP
local finalList = ""
if not Item then
if not Item then
Item = mw.wikibase.getEntity('Q1')
Item = mw.wikibase.getEntity('Q1')
Riga 123: Riga 169:
AllP = mw.wikibase.orderProperties(Item:getProperties())
AllP = mw.wikibase.orderProperties(Item:getProperties())
SourcesP = {P200 = true}
for _, Property in pairs(AllP) do
for _, Property in pairs(AllP) do
if Item.claims[Property][1].mainsnak.datatype == 'external-id' then
if Item.claims[Property][1].mainsnak.datatype == 'external-id' then
AllExtID[#AllExtID + 1] = "* ["  .. p.ExtIDLink(Property, Item.claims[Property][1].mainsnak.datavalue.value) .. " ''" .. Item.claims[Property][1].mainsnak.datavalue.value .. "''], " .. (mw.wikibase.getLabelByLang(Property, 'it') or mw.wikibase.getLabel(Property)) -- [[:" .. SiteLink['site'] .. ":" .. SiteLink['title'] .. "|''" .. SiteLink['title'] .. "'']], " .. TitleLabel
-- Sets semantic property
mw.smw.set((mw.wikibase.getLabelByLang(Property, 'it') or mw.wikibase.getLabel(Property)) .. " = " .. Item.claims[Property][1].mainsnak.datavalue.value)
if (SourcesP[Property]) then
--ID is for external source
if AllSources == nil then
AllSources = {}
end
table.insert(AllSources, "* " .. frame:expandTemplate{title = 'CitazioneIEEE', args = {'Contributori Memory Alpha', Item.sitelinks['itma'].title, 'Memory Alpha', Item.claims[Property][1].qualifiers['P201'][1].datavalue.value.time, p.ExtIDLink(Property, Item.claims[Property][1].mainsnak.datavalue.value)}})
else
--ID is normal external link
AllExtID[#AllExtID + 1] = "* ["  .. p.ExtIDLink(Property, Item.claims[Property][1].mainsnak.datavalue.value) .. " ''" .. Item.claims[Property][1].mainsnak.datavalue.value .. "''], " .. (mw.wikibase.getLabelByLang(Property, 'it') or mw.wikibase.getLabel(Property))
end
end
end
end
end
return table.concat(AllExtID, string.char(10))
if AllExtID ~= nil and AllExtID ~= "" then
finalList = table.concat(AllExtID, string.char(10))
end
if AllSources ~= nil and AllSources ~= "" then
finalList = finalList .. string.char(10) .. "== Fonti Esterne (" .. frame:expandTemplate{title = 'Beta'} .. ") ==" .. string.char(10) .. table.concat(AllSources, string.char(10))
end
mw.smw.set("finalList = " .. finalList)
return finalList
end
end
--------------------------------------------------------------------------------
-- Return a URL for the external IDentifier
--
-- @param Property The P beign processed
-- @param The ID value itself
--
-- @return string String containing escaped URL
--------------------------------------------------------------------------------
function p.ExtIDLink(Property, Value)
function p.ExtIDLink(Property, Value)
local ExtIDP = 'P5'
local ExtIDP = 'P5'
local URL
local URL
local FullLink
URL = mw.wikibase.getEntity(Property).claims[ExtIDP][1].mainsnak.datavalue.value
URL = mw.wikibase.getEntity(Property).claims[ExtIDP][1].mainsnak.datavalue.value
return string.gsub(URL, '$1', Value)
if string.find(Value, "[%%%+%-%*%?]") ~= nil and string.find(Value, " ") ~= nil then
FullLink = string.gsub(URL, '$1', mw.uri.encode(Value, "QUERY"):gsub("%%", "%%%%"))
else
FullLink = string.gsub(URL, '$1', Value)
end
return FullLink
end
end
function p.LinkToEntity(frame, AddSemantic)
function p.LinkToEntity(frame, AddSemantic)
Riga 151: Riga 235:
if mw.wikibase.getEntity() then
if mw.wikibase.getEntity() then
if AddSemantic then
if AddSemantic then
Text = "Modifica i dati nella [[DataTrek ID::Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|pagina della entità]] su ''DataTrek''"
Text = "Modifica i dati nella [[DataTrek ID::" .. mw.wikibase.getEntityIdForCurrentPage() .. "|pagina della entità]] su ''DataTrek''"
else
else
Text = "Modifica i dati nella [[datatrek:Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|pagina della entità]] su ''DataTrek''"
Text = "Modifica i dati nella [[datatrek:Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|pagina della entità]] su ''DataTrek''"
Riga 165: Riga 249:
       :wikitext(Text)
       :wikitext(Text)
     return  tostring(p)
     return  tostring(p)
end
--------------------------------------------------------------------------------
-- Set the semantic property for the linked DataTrek entity on the current page
--
-- @param frame The frame of the page
--------------------------------------------------------------------------------
function p.SemanticToEntity(frame)
if mw.wikibase.getEntity() ~= nil then
mw.smw.set("DataTrek ID = " .. mw.wikibase.getEntityIdForCurrentPage())
end
end
--------------------------------------------------------------------------------
-- Set the semantic property for the linked DataTrek entity on the current page
-- to be used as a plain text string
--
-- @param frame The frame of the page
--------------------------------------------------------------------------------
function p.SemanticToItem(frame)
if mw.wikibase.getEntity() ~= nil then
mw.smw.set("DataTrek Item = " .. mw.wikibase.getEntityIdForCurrentPage())
mw.smw.set("DataTrek ID = " .. mw.wikibase.getEntityIdForCurrentPage())
end
end
end
function p.LabelByLang(frame)
function p.LabelByLang(frame)
Riga 257: Riga 363:
return Value
return Value
end
end
function p.LabelOrLink(QItem)
 
--------------------------------------------------------------------------------
-- Return a label ora wikilink or a link to the special Placeholder page for a
-- given Property
-- Return string containing label or link
--
-- @param QItem The item identifier in the from 'Q0'
-- @param[opt=nil] SMWProperty Name of the semantic property to add
-- @param[opt=false] AddSemantic Wether to add sematinc or not
-- @param[opt=""] ForcedLabel Specific label to show regardless DataTrek values
-- @param[opt=false] ForceString Force to return string even in case of Page
--                              that should return link
--
-- @treturn string String containing label or wikilink
--------------------------------------------------------------------------------
function p.LabelOrLink(QItem, SMWProperty, AddSemantic, ForcedLabel, ForceString)
local Label
local Label
local WTLink
local WTLink
if AddSemantic and SMWProperty and (SMWProperty ~= "") then
AddSemantic = true
else
AddSemantic = false
end
ForceString = ForceString or false
local Item = mw.wikibase.getEntity(QItem)
local Item = mw.wikibase.getEntity(QItem)
if not Item then
if Item == nil then
return "''Elemento non trovato''"
return "'''Error'''"
end
end
if not Item['claims'] or not Item['claims']['P20'] then
if ForcedLabel ~= "" and ForcedLabel ~= nil then
Label = ForcedLabel
elseif not Item['claims'] or not Item['claims']['P20'] then
Label = Item.labels['it'].value
Label = Item.labels['it'].value
else
else
Riga 276: Riga 407:
end
end
if not mw.wikibase.getSitelink(QItem) then
if not mw.wikibase.getSitelink(QItem) and string.find(Label, "Categoria:", 1, true) == nil then
return Label
--https://wikitrek.org/wiki/Speciale:AboutTopic/Q64
if AddSemantic then
mw.smw.set(SMWProperty .. "=" .. Label)
end
if ForceString then
return Label
else
return "[[Special:AboutTopic/" .. QItem .. "|" .. Label .. "]]"
end
else
else
WTLink = Item.sitelinks['wikitrek'].title
if Item.sitelinks == nil then
WTLink = Label
else
WTLink = Item.sitelinks['wikitrek'].title
end
if not Label then
if not Label then
Label = WTLink
Label = WTLink
end
end
return "[[" .. WTLink .. "|" .. Label .. "]]"
if ForceString then
return WTLink
end
if string.find(WTLink, "Categoria:", 1, true) ~= nil then
return "[[" .. WTLink .. "]]"
elseif AddSemantic then
return "[[" .. SMWProperty .. "::" .. WTLink .. "|" .. Label .. "]]"
else
return "[[" .. WTLink .. "|" .. Label .. "]]"
end
end
end
end
end
Riga 289: Riga 443:
-- using the LDoc format
-- using the LDoc format
-- @param Item Father of Previous and Next
-- @param Item Father of Previous and Next
-- @param[opt="Navigatore"]
-- @param[opt="Navigatore"] Title
-- @return Table
-- @return Table
function p.MakeNavTable(Item, Title)
function p.MakeNavTable(Item, Title)
Riga 309: Riga 463:
local Table
local Table
Title = Title or "Navigatore"
--Title = Title or "Navigatore"
if not Item then
if not Item then
Item = mw.wikibase.getEntity("Q1")
Item = mw.wikibase.getEntity("Q1")
Riga 315: Riga 469:
if not Item["P7"] then
if not Item["P7"] then
Previous = "no prev"
Previous = "''nessuno''"
elseif Item["P7"][1].mainsnak == nil then
Previous = p.LabelOrLink(Item["P7"][1].datavalue.value.id)
else
else
Previous = p.LabelOrLink(Item["P7"][1].datavalue.value.id)
Previous = p.LabelOrLink(Item["P7"][1].mainsnak.datavalue.value.id)
end
end
if not Item["P23"] then
if not Item["P23"] then
Next = "no next"
Next = "''nessuno''"
elseif Item["P23"][1].mainsnak == nil then
Next = p.LabelOrLink(Item["P23"][1].datavalue.value.id)
else
else
Next = p.LabelOrLink(Item["P23"][1].datavalue.value.id)
Next = p.LabelOrLink(Item["P23"][1].mainsnak.datavalue.value.id)
end
end
Table = "<div class='separatorebox'>'''" .. Title .. "'''</div>"
--Table = "<div class='separatorebox'>'''" .. Title .. "'''</div>"
Table = Table .. string.char(10) .. "<table class='wikitable' style='width:100%'>"
--Table = Table .. string.char(10) .. "<table class='wikitable' style='width:100%'>"
Table = Table .. string.char(10) .. "<tr><th>&lt; Precedente</th><th>Successivo &gt;</th></tr>"
Table = "<table class='wikitable' style='width:100%'>"
if Title ~= nil then
Table = Table .. string.char(10) .. "<caption>" .. Title .. "</caption>"
end
Table = Table .. string.char(10) .. "<tr><th id='P7' title='P7'>&lt; Precedente</th><th id='P23' title='P23'>Successivo &gt;</th></tr>"
Table = Table .. string.char(10) .. "<tr><td style='text-align:center; width:50%;'>" .. Previous .. "</td>"
Table = Table .. string.char(10) .. "<tr><td style='text-align:center; width:50%;'>" .. Previous .. "</td>"
Table = Table .. string.char(10) .. "<td style='text-align:center; width:50%;'>" .. Next .. "</td></tr>"
Table = Table .. string.char(10) .. "<td style='text-align:center; width:50%;'>" .. Next .. "</td></tr>"
Riga 346: Riga 508:
return table.concat("* " .. AllP, string.char(10))
return table.concat("* " .. AllP, string.char(10))
end
function p.ListReferences(frame)
local AllReferences = {}
local Item = mw.wikibase.getEntityIdForCurrentPage()
if not Item then
Item = 'Q1'
end
local Statements = mw.wikibase.getAllStatements(Item, 'P58')
if not Statements then
return "Nessun riferimento trovato"
else
for _, Statement in pairs(Statements) do
local ReferenceItem = Statement.mainsnak.datavalue.value.id
local Reference = mw.wikibase.getSitelink(ReferenceItem)
if not Reference then
--Reference = Statement.mainsnak.datavalue.value.id
AllReferences[#AllReferences + 1] = "* [[Special:AboutTopic/" .. ReferenceItem .. "]] - " .. ReferenceItem .. " - " .. mw.wikibase.getLabelByLang(mw.wikibase.getEntity(ReferenceItem).claims['P14'][1].mainsnak.datavalue.value.id, "it")
else
if frame.args['AddSemantic'] then
Reference = "Riferimento::" .. Reference
end
AllReferences[#AllReferences + 1] = "* [[" .. Reference .. "]] (" .. mw.wikibase.getLabelByLang(mw.wikibase.getEntity(ReferenceItem).claims['P14'][1].mainsnak.datavalue.value.id, "it") .. ")"
end
end
return table.concat(AllReferences, string.char(10))
end
end
--- generates a list of backlink using SMW query.
--
-- @param frame Info from MW session
-- @return A bullet list of backlinks
function p.ListBackReferences(frame)
-- See example here https://github.com/SemanticMediaWiki/SemanticScribunto/blob/master/docs/mw.smw.getQueryResult.md
-- See also here https://doc.semantic-mediawiki.org/md_content_extensions_SemanticScribunto_docs_mw_8smw_8getQueryResult.html
local AllBackReferences = {}
local QueryResult = mw.smw.getQueryResult('[[Riferimento::' .. mw.title.getCurrentTitle().text .. ']]|?DataTrek ID|?Istanza')
    if QueryResult == nil then
        return "''Nessun risultato''"
    end
    if type(QueryResult) == "table" then
        local Row = ""
        local ImagesList = ""
        local ResultText = ""
        for k, v in pairs(QueryResult.results) do
            if string.sub(v.fulltext, 1, 5) == "File:" then
--IF the back reference is a media, don't list it, but show the thumbnail only
--Row = "[[:" .. v.fulltext .. "]]"
ImagesList = ImagesList .. v.fulltext .. "|" .. frame:expandTemplate{ title = v.fulltext} .. string.char(10)
else
Row = "[[" .. v.fulltext .. "]]"
if v.printouts['DataTrek ID'][1] ~= nil then
Row = Row .. " - " .. v.printouts['DataTrek ID'][1]
if v.printouts['Istanza'][1] ~= nil then
Row = Row .. " - " .. v.printouts['Istanza'][1].fulltext
end
end
AllBackReferences[#AllBackReferences + 1] = "*" .. Row
end
        end
       
        ResultText = table.concat(AllBackReferences, string.char(10))
        ResultText = "<div style='column-count:3;-moz-column-count:3;-webkit-column-count:3'>" .. string.char(10) .. ResultText .. string.char(10) .. "</div>"
       
        if not (ImagesList == nil or ImagesList == "") then
        ResultText = ResultText .. string.char(10) .. "=== Immagini collegate ===" .. string.char(10) .. frame:extensionTag( "gallery", ImagesList)
        end
        return ResultText --table.concat(AllBackReferences, string.char(10))
    else
    return "''No table''"
    end
    return QueryResult
end
--- Writes a gneric UL list from property, adding SMW link if specified
--
-- @param PName Info from MW session
-- @param SMWPrefix
-- @return A bullet list of backlinks
function p.PropertyList(frame)
--{{#invoke:DTBase|PropertyList|P59|Scritto da}}
local AllReferences = {}
local Item = mw.wikibase.getEntityIdForCurrentPage()
if not Item then
Item = 'Q1'
end
local Statements = mw.wikibase.getAllStatements(Item, frame.args["Property"])
if not Statements then
return "Nessun riferimento trovato"
elseif table.getn(Statements) == 1 then
return p.LabelOrLink(Statements[1].mainsnak.datavalue.value.id, frame.args["SMWPrefix"], true)
else
for _, Statement in pairs(Statements) do
--local ReferenceItem = Statement.mainsnak.datavalue.value.id
AllReferences[#AllReferences + 1] = "<li>" .. p.LabelOrLink(Statement.mainsnak.datavalue.value.id, frame.args["SMWPrefix"], true) .. "</li>"
end
return "<ul>" .. table.concat(AllReferences, string.char(10)) .. "</ul>"
end
end
end
return p
return p