Module:HenkvD/Wikidata infobox

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:HenkvD/Wikidata infobox/doc

local Wikidata = require 'Module:HenkvD/Wikidata'
local p = {}

local t= { -- infobox table
    { 'begin',      '',         'Beginning',            '',         'Beginning'             }, -- first line, do not remove
--  { type,         property,   description,            item,       description             },
-- general properities
    { 'image',      'P18',      'Image of the item',    '',         ''                      },
-- geographical properties
    { 'smallimage', 'P41',      'National flag',        'Q186516',  'National flag'         },
    { 'smallimage', 'P94',      'Coat of arms',         'Q14659',   'Coat of arms'          },
    { 'image',      'P242',     'Locator map',          'Q2221906', 'Location'              },
    { 'header',     '',         'Misc',                 '',         'This is a header'      },
    { 'item',       'P85',      'Anthem',               'Q23691',   'National anthem'       },
    { 'string',     'P299',     'ISO 3166-1 numeric',   'Q2725758', 'ISO 3166-1 numeric'    },
-- person properties
    { 'header',     '',         'Misc',                 '',         ''                      }, -- empty header
    { 'item',       'P19',      'Place of birth',       'Q1322263', 'Place of birth'        },
    { 'date',       'P569',     'Date of birth',        'Q47223',   'Date of birth'         },
    { 'item',       'P20',      'Place of death',       '',         'Place of death'        },
    { 'date',       'P570',     'Date of death',        '',         'Date of death'         },
-- taxobox properties
    { 'header',     '',         'Misc',                 'Q11398',   'Scientific classification' }, 
    { 'item',       'P273',     'Domain',               'Q146481',  'Domain'                },
    { 'item',       'P75',      'Kingdom',              'Q36732',   'Kingdom'               },
    { 'item',       'P76',      'Phylum',               'Q38348',   'Phylum'                },
    { 'item',       'P77',      'Class',                'Q37517',   'Class'                 },
    { 'item',       'P70',      'Order',                'Q36602',   'Order'                 },
    { 'item',       'P71',      'Family',               'Q35409',   'Family'                },
    { 'item',       'P74',      'Genus',                'Q34740',   'Genus'                 },
    { 'item',       'P89',      'Species',              'Q7432',    'Species'               },
    { 'header',     '',         'Misc',                 'Q36642',   'Binomial nomenclature' }, 
    { 'item',       'P225',     'Taxon name',           '',         ''                      },
    { 'header',     '',         'Misc',                 '',         ''                      }, -- empty header
    { 'image',      'P181',     'Range map',            '',         'Range map'             },
-- etc
    { 'header',     '',         'Closing',              '',         'Closing'               } -- last line, do not remove
    } -- end of infobox table

local colors = {
    ['default'] = '#f0f0f0;',        
    ['P107'] = {                    -- main type GND
        ['Q215627'] = '#aaa;',      -- person
        ['Q43229'] = '#fcfcfc;',    -- organization
--        ['Q1656682'] =            -- event
--        ['Q386724'] =             -- creatve work
--        ['Q1969448'] =            -- term
--        ['Q618123'] =             -- place , geographical feature
--        ['Q11651459']=            -- disambiguation
    },
    ['P75'] = {                     -- biological kingdom
        ['Q10876'] = 'lightgrey',   -- bacteria
        ['Q101274'] = 'khaki',      -- protozoa
        ['Q862296'] = 'burlywood',  -- chromista
        ['Q756'] = 'lightgreen',    -- plantae
        ['Q764'] = 'lightblue',     -- fungi
        ['Q729'] = 'pink',          -- animalia
     },
-- etc
}     
 
local errors = {
    ["entity-not-found"] = "Entity not found.",
    ["infobox-type-invalid"] = "Infobox type invalid on line ",
    }

local layout = { -- infobox table layout
    [1]='<table class="infobox" style="width:20em;">',
    [2]="<tr style='background-color:$1'><td colspan=2><center>'''$2'''</center></td></tr>", -- label
    [4]='<tr><td colspan=2>[[Image:$1|250x250px]]<br><center>$2</center></td></tr>',    -- image
    [7]='<tr><td colspan=2><center><table border=0><tr>',                               -- begin of small image
    [8]='<td>[[Image:$1|125x125px]]<br><center>$2</center></td>',                       -- small image
    [11]='</table></center></td></tr>',                                                 -- end of small image
    [12]='<tr><td>$1</td><td>$2</td></tr>',                                             -- item
    [15]='<tr><td>$1</td><td>$2</td></tr>',                                             -- string
    [16]='<tr><td>$1</td><td>$2</td></tr>',                                             -- date
-- values, coordinates etc
    [90]="<tr style='background-color:$1'><td colspan=2>'''$2'''</td></tr>",            -- header
    [99]='</table>'
    }

local property_type     =1
local property          =2
local property_desc     =3
local item              =4
local item_desc         =5
local property_options  =6
local entity = mw.wikibase.getEntityObject()

function p.infobox(frame )
    if  not entity then
        return '<span class="error">' .. errors["entity-not-found"] .. '</span>'
    end
    local backgroundcolor=getbackgroundcolor()
    local result = layout[1] .. mw.ustring.gsub(mw.ustring.gsub(layout[2],'$1',backgroundcolor),'$2',mw.wikibase.label(entity.id))
    local detail = ''
    local previousheader = ''
    for line=1, 999 do     -- check all properties
        if t[line] and t[line][property] then
            local propertyid= t[line][property]
            if ( getitemvalue(line, propertyid) ~= '' ) or ( t[line][property] == '' ) then 
                if t[line][property_type] == 'begin' then -- do nothing
-- image
                elseif t[line][property_type] == 'image' then
                    detail = detail .. mw.ustring.gsub(mw.ustring.gsub(layout[4],'$1',getimagevalue(line,propertyid)),'$2',getitem(line))
-- smallimage
                elseif t[line][property_type] == 'smallimage' then
                    if t[line-1][property_type] ~= 'smallimage' then detail = detail .. layout[7] end
                    detail = detail .. mw.ustring.gsub(mw.ustring.gsub(layout[8],'$1',getimagevalue(line,propertyid)),'$2',getitem(line))
                    if t[line+1][property_type] ~= 'smallimage' then detail = detail .. layout[11] end
-- item
                elseif t[line][property_type] == 'item' then
                    detail = detail .. mw.ustring.gsub(mw.ustring.gsub(layout[12],'$1',getitem(line)),'$2',getitemvalue(line,propertyid))
-- string
                elseif t[line][property_type] == 'string' then
                    detail = detail .. mw.ustring.gsub(mw.ustring.gsub(layout[15],'$1',getitem(line)),'$2',getstringvalue(line,propertyid))
-- date
                elseif t[line][property_type] == 'date' then
                    detail = detail .. mw.ustring.gsub(mw.ustring.gsub(layout[16],'$1',getitem(line)),'$2',getdatevalue(line,propertyid))
-- header
                elseif t[line][property_type] == 'header' then
                    if detail ~= "" then
                        result = result .. previousheader .. detail
                        detail = ""
                    end
                    if t[line][item_desc] ~= '' then
                        previousheader = mw.ustring.gsub(mw.ustring.gsub(layout[90],'$1',backgroundcolor),'$2',getitem(line) )
                    else
                        previousheader = ''
                    end
-- others / error
                else
                    return '<span class="error">' .. errors["infobox-type-invalid"] .. line .. ': ' ..
                        ' ' .. t[line][property_type] .. ' ' .. t[line][property] .. '</span>'
                end
            end
        end
    end
    result = result .. layout[99] 
    return result
end

function getimagevalue (line,propertyid)
    if entity.claims[propertyid] and entity.claims[propertyid][0] then
        return entity.claims[propertyid][0].mainsnak.datavalue.value
    else
        return '' 
    end
end

function getitemvalue (line,propertyid)
    local options = { ["args"] = { ["property"] = propertyid } }
    return Wikidata.formatStatements(options)
end

function getstringvalue (line,propertyid)
    local options = { ["args"] = { ["property"] = propertyid } }
    return Wikidata.formatStatements(options)
end

function getdatevalue (line,propertyid)
    local options = { ["args"] = { ["property"] = propertyid } }
    return Wikidata.formatStatements(options)
end

function getitem (line)
    local label = '' 
    local link = ''
    if t[line][item] ~= '' then
        label = mw.wikibase.label(t[line][item])
        link = mw.wikibase.sitelink(t[line][item])
    end
    if link  == nil then link  = '' end
    if label == nil then label = '' end
    if  link ~= '' then
        if label ~= '' then return '[[' .. link .. '|' .. label .. ']]'
        else                return '[[' .. link .. ']]'
        end
    else
        if label == '' and t[line][property] ~= '' and t[line][item] ~= '' then 
            label = mw.wikibase.label(t[line][property])    -- 1st fallback: get description of the property
            if label == nil then label = '' end
        end
        if label == '' then 
            label = t[line][item_desc]                      -- 2nd fallback: get item description from infobox table
        end
    end
    return label
end

function getbackgroundcolor ()
    local color = colors.default
    for c1 in pairs (colors) do
        local c2 = ''    
        if entity.claims[c1] and entity.claims[c1][0] then
            c2 = 'Q' .. entity.claims[c1][0].mainsnak.datavalue.value["numeric-id"]
        end
        if colors[c1][c2] then 
            color = colors[c1][c2]
        end
    end
    return color 
end

function p.listcolors ()
    local label=''
    local result = 'Colors of Wikidata infoboxes<br><table>'
    result = result .. "<tr><td>Default</td><td style='background-color:" .. colors.default .. "'>&nbsp;</td></tr>" 
    for c1 in pairs (colors) do
        if type ( colors[c1] ) == "table" then 
            label= mw.wikibase.label(c1)
            if label == nil then label = c1 end
            result = result .. '<tr><td>' .. label .. "</td>" 
            for c2 in pairs (colors[c1]) do   
                result = result .. "<td style='background-color:" .. colors[c1][c2] .. "'>"
                label= mw.wikibase.label(c2)
                if label == nil then label = c2 end
                result = result .. label .. "</td>" 
            end
            result = result .. '</tr>'
        end
    end
    result = result .. '</table>' 
    return result
end    

return p