Module:BIC

From Wikimedia Commons, the free media repository
Jump to: navigation, search
Lua
CodeDiscussionEditHistoryNo test API DocumentationSubpagesLinksTestsResultsSandbox All modules

Module for support of Template:BIC and previous Template:Bien Catalogado now merged. That is for monuments of Spanish heritage registered as Bien de Interés Cultural (BIC) and other buildings registered at regional level. See documentation of the template for more information.

Messages can be translated at Module:BIC/lang.

This module generates missing optional parameters based on patterns of the identifier of the object:

if id begins with defaults to
RI-52 type=JH
RI-53 type=CH
RI-54 type=SH
RI-55 type=ZA
7-INM type=BC, link=ar, idurl=id
IPA- type=BC, link=ct, idurl=id
IPAPC- type=ZA, link=ct
08019/ type=BC, link=ct, idurl=id
03. type=BC, link=vc
12. type=BC, link=vc
46. type=BC, link=vc

otherwise assumes type=M and links to http://toolserver.org/~platonides/wlm/ids.php

Code

-- Support for Template:BIC

local i18n = require("Module:BIC/lang")
local this = {}

function this.templateBIC(frame)
    -- Get language
    local args = frame.args
    local lang = "en" -- default
    if i18n[args.lang1] then
        lang = args.lang1
    elseif i18n[args.lang2] then
        lang = args.lang2 -- first fallback
    elseif i18n[args.lang3] then
        lang = args.lang3 -- second fallback
    end
    local i18nLang = i18n[lang]
    
    -- Get template parameters
    local pargs = frame:getParent().args
    local id = pargs[1] or ""
    local db = mw.ustring.lower(pargs.link or "")
    local idurl = pargs.idurl or ""
    local kind = mw.ustring.upper(pargs.type or "")
    
    -- Get defaults based on id patterns
    if id ~= "" then
        if db == "" then
            db = this.getDb(id)
        end
        if idurl == "" then
            idurl = this.getIdurl(id)
        end
        if kind == "" then
            kind = this.getKind(id)
        end
    end

    local namespace = mw.title.getCurrentTitle().nsText
    
    -- Layout
    local ret = {}
    table.insert(ret, '{| cellspacing="4" ')
    table.insert(ret, 'style="text-align:left; color:#000; background:#F9FFF0;')
    table.insert(ret, ' border:1px solid #BBA22C; margin:1px;"\n')
    table.insert(ret, '| style="width:32px; height:32px;" ')
    table.insert(ret, '| [[File:Icone chateau fort.svg|30px|link=]]\n')
    table.insert(ret, '| style="font-size:9pt; padding:1pt; line-height:1.1em;" |')
    -- from Template:Lang
    table.insert(ret, '<p><span lang="' .. lang .. '" xml:lang="' .. lang .. '" class="description ' .. lang .. '">')
    if namespace == "Category" then
        table.insert(ret, i18nLang.text1Cat)
    else
        table.insert(ret, i18nLang.text1File)
    end
    if kind == "CH" then
        table.insert(ret, i18nLang.text2CH)
    elseif kind == "JH" then
        table.insert(ret, i18nLang.text2JH)
    elseif kind == "SH" then
        table.insert(ret, i18nLang.text2SH)
    elseif kind == "ZA" then
        table.insert(ret, i18nLang.text2ZA)
    elseif kind == "BC" or kind == "BI" then
        table.insert(ret, i18nLang.text2BC)
    else
        table.insert(ret, i18nLang.text2M)
    end
    if kind == "BC" then
        if db == "ar" then
            table.insert(ret, i18nLang.text3BCar)
        elseif db == "ct" then
            table.insert(ret, i18nLang.text3BCct)
        elseif db == "vc" then
            table.insert(ret, i18nLang.text3BCvc)
        else
            table.insert(ret, i18nLang.text3BC)
        end
    elseif kind == "BI" then
        if db == "ct" then
            table.insert(ret, i18nLang.text3BIct)
        else
            table.insert(ret, i18nLang.text3BI)
        end
    else
        if db == "ct" then
            table.insert(ret, i18nLang.text3BICct)
        elseif db == "vc" then
            table.insert(ret, i18nLang.text3BICvc)
        else
            table.insert(ret, i18nLang.text3BIC)
        end
    end
    table.insert(ret, i18nLang.text4)
    table.insert(ret, '</span><span style="white-space:nowrap;">')
    if id ~= "" then
        local idEncode = mw.uri.encode(id, PATH)
        if idurl ~= "" then
            if db == "ar" then
                table.insert(ret, '[http://www.sipca.es/censo/' .. idurl .. '/.html')
            elseif db == "ct" then
                if mw.ustring.find(idurl, "^bcn/") then
                    table.insert(ret, '[http://w10.bcn.es/APPS/cat_patri/editElement.do?reqCode=inspect&id.identificador=')
                    table.insert(ret, mw.ustring.sub(idurl, 8))
                    table.insert(ret, '&id.districte=' .. mw.ustring.sub(idurl, 5, 6))
                else
                    table.insert(ret, '[http://invarquit.cultura.gencat.cat/Cerca/Fitxa?index=0&consulta=&codi=')
                    table.insert(ret, idurl)
                end
            elseif db == "vc" then
                if kind == "BC" then
                    table.insert(ret, '[http://www.cult.gva.es/dgpa/brl/Detalles_brl.asp?IdInmueble=')
                else
                    table.insert(ret, '[http://www.cult.gva.es/dgpa/bics/Detalles_bics.asp?IdInmueble=')
                end
                table.insert(ret, idurl)
            else
                table.insert(ret, '[https://tools.wmflabs.org/heritage/api/api.php?action=search&srcountry=es&format=html&srid=')
                table.insert(ret, idEncode .. '&userlang=' .. args.lang1)
            end
        else
            table.insert(ret, '[https://tools.wmflabs.org/heritage/api/api.php?action=search&srcountry=es&format=html&srid=')
            table.insert(ret, idEncode .. '&userlang=' .. args.lang1)
        end
        table.insert(ret, ' ' .. id .. ']') 
    else
        table.insert(ret, "''" .. i18nLang.text5 .. "''")
    end
    table.insert(ret, '.</span></p>\n|}')
    
    return table.concat(ret)
end

function this.getDb(id)
    local dbDefault = ""
    if mw.ustring.find(id, "^[17]%-INM") then
        dbDefault = "ar"
    elseif mw.ustring.find(id, "^08019/") or mw.ustring.find(id, "^IPA") then
        dbDefault = "ct"
    elseif mw.ustring.find(id, "^PH%-874") then
        dbDefault = "vc"
    elseif mw.ustring.find(id, "^[014][236]%.") then -- strictly 03. 12. 46.
        dbDefault = "vc"
    end
    return dbDefault
end

function this.getIdurl(id)
    local idurlDefault = ""
    if mw.ustring.find(id, "^IPA%-") then
        idurlDefault = mw.ustring.sub(id, 5)
    elseif mw.ustring.find(id, "^[17]%-INM") then
        idurlDefault = id
    end
    return idurlDefault
end

function this.getKind(id)
    local kindDefault = "M"
    if mw.ustring.find(id, "^RI%-52") then
        kindDefault = "JH"
    elseif mw.ustring.find(id, "^RI%-53") then
        kindDefault = "CH"
    elseif mw.ustring.find(id, "^RI%-54") then
        kindDefault = "SH"
    elseif mw.ustring.find(id, "^RI%-55") or mw.ustring.find(id, "^IPAPC%-") then
        kindDefault = "ZA"
    elseif mw.ustring.find(id, "^PH%-874") then
        kindDefault = "ZA"
    elseif mw.ustring.find(id, "^08019/") or mw.ustring.find(id, "^IPA%-") then
        kindDefault = "BC"
    elseif mw.ustring.find(id, "^[014][236]%.") or mw.ustring.find(id, "^7%-INM") then
        kindDefault = "BC"
    end
    return kindDefault
end

return this