Module:NGC table

Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Code

-- Module that check the presence of labels, descriptions and other properties
-- of NGC objects. This module is built to be used in the pages of the
-- WikiProject NGC
-- Author: User:Paperoastro

local wikidata = require('Module:Wikidata')
 
-------------------------------------------------------------------------------
-- text for the header of the table
local h_txt = {}
 
h_txt["en"] = {"Item", "NGC", "Type", "Constellation"}
h_txt["it"] = {"Elemento", "NGC", "Tipo", "Costellazione"}

-------------------------------------------------------------------------------
-- languages code for checking the existence of labels and descriptions
local lang_code = {"de","en","es","fr","it","nl","pl","pt","ru"}

-------------------------------------------------------------------------------
-- background colors for the table:
-- red = element missing
-- yellow = one between label and description missing
-- green = element are present
local back_col = {}
back_col[0] = "#FF0000"  -- red
back_col[1] = "#F4FF00"  -- yellow
back_col[2] = "#00FF00"  -- green

-------------------------------------------------------------------------------
-- LOCAL FUNCTIONS
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- monitoring of label and descriptions
local function monLabDesc ( itemId, lang )
    local col = {}  -- color used
    for i = 1,#lang do
        col[i] = 0 
        local entity = mw.wikibase.getEntityObject( itemId )

        if entity then
            -- check the presence of labels in the languages 'lang_code'
            if entity.labels ~= nil then
            	if entity.labels[lang[i]] ~= nil then
					col[i] = col[i] + 1
				end
			end
			
            -- check the presence of description in the languages 'lang_code'
            if entity.descriptions ~= nil then
            	if entity.descriptions[lang[i]] ~= nil then
					col[i] = col[i] + 1
            	end
            end
        end  -- if entity
    end  -- for i
   
    return col
end

-------------------------------------------------------------------------------
-- PUBLIC FUNCTIONS
-------------------------------------------------------------------------------
local p = {}

-------------------------------------------------------------------------------
-- return the background colors used for the table
function p.getCol( frame )
    i = tonumber( frame.args[1] )
    if i >= 0 and i <= 2 then
        return back_col[i]
    else
        return ""
    end
end

-------------------------------------------------------------------------------
-- return the header of the table
function p.Header( frame )
    local lang = frame:preprocess("{{int:lang}}") -- user language
 
    if ( h_txt[lang] == nil ) then
        lang = "en"
    end
 
    local s = "{|class=\"wikitable\"\n" .. "|-\n"

    -- columns for monitoring other properties
    for i = 1,#h_txt["en"] do
        s = s .. "! " .. h_txt[lang][i] .. " !"
    end

    -- columns for monitoring labels and descriptions
    for i = 1,#lang_code do
        s = s .. "! " .. lang_code[i] .. " !"
    end

    return s:sub(1, #s-1) .. "\n"
end

-------------------------------------------------------------------------------
-- return the footer of the tabe
function p.Footer()
    return "|}\n"
end

-------------------------------------------------------------------------------
-- main function
function p.Rows( frame )
    local lang = frame:preprocess("{{int:lang}}") -- user language
    local rows = ""          -- string for the output
    local np = #h_txt["en"]  -- number of columns for property values
    local nl = #lang_code    -- number of columns for label and description

    -- input arguments:
    -- Q.. -> item
    -- <ref .. -> note to the previous row

    for _, v in pairs(frame.args) do
        local item   -- item of the object
        local dum
       
        -- trim not standard characters from the string
        v = v:gsub("^%s*(.-)%s*$", "%1")
       
        -- replace "@" with "="
        v = v:gsub( "%@", "=" )
       
        qstart,qend = string.find( v, "Q%w+", 1 )

        rows = rows .. "|-\n"  -- New row

        -- Only text
        if qstart == nil or string.find( v, "=", 1 ) ~= nil then
			rows = rows .. '| colspan="' .. np+nl .. '" | ' .. v
		-- Item
    	else
    		item = v:sub( qstart, qend )
            -- Column 1: wikilink to the item and eventually note
            rows = rows .. '| ' .. wikidata.formatEntityId( item, {lang=lang} )
            -- Add note
            if qend < #v then
	            rows = rows .. v:sub(qend+1, #v)
            end
            
   			-- Column 2: NGC catalogue code (P528)
   			dum =  wikidata._formatStatements( {item=item, property="P528", lang=lang, qualifier="P972", qualifiervalue="Q14534"} )
			if dum ~= nil then
				rows = rows .. '|| ' .. dum
			else
				rows = rows .. "|| bgcolor='" .. back_col[0] .. "'|&nbsp;"
			end

			-- Column 3: type of the object (P31)
			dum = wikidata._formatStatements( {item=item, property="P31", lang=lang} )
			if dum ~= nil then
				rows = rows .. '|| ' .. dum
			else
				rows = rows .. "|| bgcolor='" .. back_col[0] .. "'|&nbsp;"
			end
			
			-- Column 4: constellation (P59)
			dum = wikidata._formatStatements( {item=item, property="p59", lang=lang} )
			if dum  ~= nil then
				rows = rows .. '|| ' .. dum
			else
				rows = rows .. "|| bgcolor='" .. back_col[0] .. "'|&nbsp;"
			end	

			-- Columns np+1 to np+nl
			dum = monLabDesc( item, lang_code )
			for j = 1,#dum do
				rows = rows .. "|| bgcolor='" .. back_col[dum[j]] .. "'|&nbsp;"
			end


		end

        rows = rows .. '\n'

    end  -- for k,v

    return rows -- writeTable( tab )

end

return p