Gaan na inhoud

Module:WdIk

in Wikipedia, die vrye ensiklopedie

Dokumentasie vir hierdie module kan geskep word by: Module:WdIk/doc

require('strict') -- force strict type checks, and block use of global variables

local p = {};     --All Lua modules on Wikipedia must begin by defining a variable 
                    --that will hold their externally accessible functions.
                    --Such variables can have whatever name you want and may 
                    --also contain various data as well as functions.
                    
                    
-- local functions must go at the top (before you call them) otherwise the interpretor thinks they are not defined
local function toSentenceCase(string)
	return string:gsub("^%l", string.upper)
end

local function getPencilStr(itemID, propertyID)
	return '  [[Lêer:OOjs UI icon edit-ltr-progressive.svg|wysig|10px|text-top|class=noviewer|link=https://www.wikidata.org/wiki/' .. itemID .. '#' .. propertyID .. ']]'
end

local function getImage(item)
	
	local test_img = '[[Lêer:Test.svg|200px|beeld onderskrif]]'
	-- Get claims for image property (P18)
	local images = item:getBestStatements('P18')
	
	-- Handle case where no images exist
    if not images or #images == 0 then
        return nil, "No image property found"
    end
    
	-- Return the first image URL
    local image1 = test_img
    
    image1 = '[[Lêer:' .. images[1].mainsnak.datavalue.value .. '|frameless|300px]]'
    
    return image1
end

local function getImgCaptionInLanguage(item, languageCode)
	-- Get claims for image property (P18)
    local claims = item:getBestStatements("P18")
    
    -- Handle case where no images exist
    if not claims or #claims == 0 then
    	mw.addWarning("No image property found")
        return ''
    end
    
    -- Get qualifiers for the first image claim
    local qualifiers = claims[1].qualifiers
	
    if not qualifiers or not qualifiers.P2096 then
        mw.addWarning("No img captions found")
        return ''
    end
    
    for _, value in pairs(qualifiers.P2096) do
	    if value.datavalue.value.language == 'af' then
	        return value.datavalue.value.text
	    end
	end
    
    mw.addWarning("No img caption found for language " .. languageCode)
    return '' 
end

local function addDataRow(frame, ItemID, propertyID, tableObj, propertyName)
	
	-- Data row | data | propertyName
    local dataRow = tableObj:tag('tr')
    dataRow:tag('td'):css('font-weight','bold'):wikitext(propertyName)
    -- parser functions and other special text must run through frame:preprocess otherwise it will be treated as plain text
    dataRow:tag('td'):wikitext(frame:preprocess('{{#statements:' .. propertyID .. '|from=' .. ItemID .. '}}'))
    :wikitext(getPencilStr(ItemID, propertyID))
	
end
                    
p.kas = function(frame)     --Add a function to "p".  
                                        --Such functions are callable in Wikipedia
                                        --via the #invoke command.
                                        --"frame" will contain the data that Wikipedia
                                        --sends this function when it runs. 
                                 -- 'kas' is a name of your choice. The same name needs to be referred to when the module is used.
    
    
    -- Get the item ID for the current page
    local itemId = mw.wikibase.getEntityIdForCurrentPage()
    
    -- Return nil if no item is connected
    if not itemId then
        mw.addWarning("Wikidata itemID not found, defaulting to Q42")
        itemId = 'Q42'
    end
    
    local item = mw.wikibase.getEntity(itemId)
    
    -- todo remove below?
    if item == nil then
        mw.addWarning("Wikidata item not found")
        return ''
    end
    
    local title = toSentenceCase(item:getLabel()) or toSentenceCase(mw.title.getCurrentTitle().text)
	
	local image = getImage(item)
 
	-- local image_desc = 'beeld onderskrif-qwer' -- get from P2096
	local image_desc = getImgCaptionInLanguage(item, 'af')
	
	
    -- reference docs : https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#mw.html:wikitext
    local kas_root = mw.html.create('table')
        :addClass('infobox')
        -- :attr( 'class', 'infobox' )
        :css( 'width', '300px' ) -- el problemo | how to handle content wrapping?

    -- First row with merged columns | Title
    local tr1 = kas_root:tag('tr')
    tr1
    :tag('td')
    :attr('colspan', 2) -- merges 2 columns into 1 cell
    :css({
            ['text-align'] = 'center',
            ['background-color'] = '#00c3ff',
            ['font-weight'] = 'bold',
        })
    :wikitext(title)

    -- Second row | Image
    local tr2 = kas_root:tag('tr')
    	tr2
    	:tag('td')
    	:attr('colspan', 2) -- merges 2 columns into 1 cell
    	:css('text-align','center')
        :wikitext(image .. image_desc)
    
	-- Third row | data | Geboortenaam
    local tr3 = kas_root:tag('tr')
    tr3
    :tag('td'):css('font-weight','bold'):wikitext('Geboortenaam')
    :tag('td'):wikitext(frame:preprocess('{{#statements:P1477|from=' .. itemId .. '}}'))
    :wikitext(getPencilStr(itemId, 'P1477'))
    
	-- Fourth row | data | Geboortedatum
	addDataRow(frame, itemId, 'P569', kas_root, 'Geboortedatum')
   
   -- Fifth row | data | Geboorteplek
	addDataRow(frame, itemId, 'P19', kas_root, 'Geboorteplek')

	return tostring(kas_root)
	
end -- end of function


return p    --All modules end by returning the variable containing their functions to Wikipedia.