Modulis:Slimības ārējās saites

Dokumentācijas ikona Moduļa dokumentācija[izveidot]
function viafLink( id )
    if not string.match( id, '^%d+$' ) then
        return false
    end
    return '[https://viaf.org/viaf/' .. id .. ' ' .. id .. ']'
end

function icd9Link( id )
    return '[http://www.icd9data.com/getICD9Code.ashx?icd9=' .. id .. ' ' .. id .. ' ]' 
end

function icd10Link( id )
    return '[http://apps.who.int/classifications/icd10/browse/2010/en#/' .. id .. ' ' .. id .. ' ]' 
end

function nclLink( id )
    if not string.match( id, '^%d+$' ) then
        return false
    end
    return '[http://aleweb.ncl.edu.tw/F/?func=accref&acc_sequence=' .. id .. '&CON_LNG=ENG ' .. id .. ']' 
end

function append(str, c, length)
    while str:len() < length do
        str = c .. str
    end
    return str
end

function getIdsFromWikidata( item, property )
    local ids = {}
    if not item.claims[property] then
        return ids
    end
    for _, statement in pairs( item.claims[property] ) do
		if statement.mainsnak.datavalue then
			table.insert( ids, statement.mainsnak.datavalue.value )
		end
    end
    return ids
end

function createRow( id, label, rawValue, link, withUid )
    if link then
        if withUid then
            return '* ' .. label .. ' <span class="uid">' .. link .. '</span>\n'
        else
            return '* ' .. label .. ' ' .. link .. '\n'
        end
    else
        return '* <span class="error">' .. id .. ' identifikators ' .. rawValue .. ' nav derīgs.</span>[[Category:Raksti ar nederīgiem autoritatīvās vadības identifikatoriem]]\n'
    end
end

--In this order: name of the parameter, label, propertyId in Wikidata, formatting function
local conf = {
    { 'ICD-9', 'ICD-9', 493, icd9Link },
    { 'ICD-10', 'ICD-10', 494, icd10Link },
}

local p = {}

function p.authorityControl( frame )
    local parentArgs = frame:getParent().args
    --Create rows
    local elements = {}

    --Wikidata fallback if requested
    local item = mw.wikibase.getEntityObject()
    if item ~= nil and item.claims ~= nil then
        for _, params in pairs( conf ) do
            if params[3] ~= 0 then
                local val = parentArgs[params[1]]
                if not val or val == '' then
                	local canUseWikidata = true
                    if canUseWikidata then
                        local wikidataIds = getIdsFromWikidata( item, 'P' .. params[3] )
                        if wikidataIds[1] then
                            parentArgs[params[1]] = wikidataIds[1]
                            --[=[
                            if wikidataIds[2] then
                                parentArgs[params[1]] = parentArgs[params[1]] .. 'atd' .. wikidataIds[2]
                            end
                            ]=]
                        end
                    end
                end
            end
        end
    end

    --Configured rows
    local rct = 0
    for k, params in pairs( conf ) do
        local val = parentArgs[params[1]]
        if val and val ~= '' then
--[[
if string.find( val, "atd", 1, true ) then
            table.insert( elements, createRow( params[1], params[2] .. ':', val, params[4]( val ), ',', params[4]( val ), true ) )
            rct = rct + 1
            table.insert( elements, createRow( params[3], params[2] .. ':', val, params[4]( val ), ',', params[4]( val ), true ) )
            rct = rct + 1
else
	]]
            table.insert( elements, createRow( params[1], params[2] .. ':', val, params[4]( val ), true ) )
            rct = rct + 1
        end
--end
    end
    local Navbox = require('Module:Navbox')
    local elementscats = ''
    if rct == 0 then
    	elementscats  = '[[Kategorija:Lapas ar tukšu slimības ārējo saišu veidni]]'
        return elementscats
	else

    return Navbox._navbox( {
        name  = 'Slimības ārējās saites',
        bodyclass = 'hlist',
		group1 = 'Identifikatori' .. elementscats,
        list1 = table.concat( elements )
    } )
end
end

return p