Module:Messtischblatt

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Usage for files

[edit]

See {{Messtischblatt}}

Usage for categories

[edit]

{{#invoke:Messtischblatt|getCats}}

Sample at Category:Messtischblatt Dortmund

Code

local p = {}

local mtb = mw.loadData('Module:Messtischblatt/data')
local lng = nil
local tnt = nil


function exists(wpage, ign_redir)
    local t = mw.title.new(wpage,'')
    local e = t.exists
    if ign_redir then
        if t:getContent():find('.ategory ?.edirect') or t.isRedirect then
            e = false
        end
    end
    return e
end

function padleft(s, l, c)
    return string.rep(c or '0', l - #tostring(s)) .. s
end

function padright(s, l, c)
    return s .. string.rep(c or '0', l - #tostring(s))
end

function tr(s)
    if not lng then
        local cf = mw.getCurrentFrame()
        if cf then
            lng = cf:preprocess('{{int:lang}}')
            if lng and not (lng == 'en' or lng == 'us') then
                tnt = mw.loadData('Module:Messtischblatt/trans')
            end
        end
    end

    if tnt and tnt[s] and tnt[s][lng] then return tnt[s][lng] else return s end
end

function tr_nil(s)
    if s then return s else return '' end
end


function p.wth(f)
    local ret = {}
    ret.concat = table.concat
    ret.insert = table.insert

    if f.args.docollapsed then
        ret:insert('{| class="wikitable collapsible collapsed"\n!')
        ret:insert(tr('Show list of sheets known for this ' .. f.args.dorc))
        ret:insert('\n|-\n|\n')
    end

    ret:insert('{| class="wikitable sortable"\n')
    ret:insert('|+ style="padding-bottom:1em" |<small>Map Sheet')
    if not f.args.t then ret:insert('s') end

    ret:insert('</small> Messtischbl')
    if f.args.t then ret:insert('att \'\'' .. f.args.t .. '\'\'')
    else ret:insert('ätter') end

    ret:insert(' [[:Category:Messtischblatt')
    if f.args.t then
        ret:insert(' No.' .. f.args[1] .. '|No.' .. f.args[1] ..']]')
        ret:insert(' <small>in grid row ')
        ret:insert(tonumber(string.sub(f.args[1], 1, 2)))
        ret:insert(' and column ')
        ret:insert(tonumber(string.sub(f.args[1], 3, 6)))
        ret:insert('</small>\n')
    elseif f.args.dorc then
        local s = 'of ' .. f.args.dorc .. ' ' .. tonumber(f.args[1])
        ret:insert(' ')
        ret:insert(s)
        ret:insert('|')
        ret:insert(s)
        ret:insert(']]\n')
        ret:insert('! data-sort-type="number"|')
        ret:insert(mw.getContentLanguage():ucfirst(f.args.inv))
        ret:insert('\n')
    else
        ret:insert('|of the whole grid]]\n')
        ret:insert('! data-sort-type="number"|Row\n')
        ret:insert('! data-sort-type="number"|Column\n')
    end

    ret:insert('!Name of Sheet (today)\n')
    ret:insert('!Sheet Title\n')
    ret:insert('! data-sort-type="number"|Year of Publication\n')
    ret:insert('! data-sort-type="number"|Year of Data Survey\n')
    ret:insert('!Permanent Link\n')
    
    return ret:concat('')
end

function p.wtf(f)
    local ret = '\n|}'

    if f.args.f then
        local cs = 5
        if #f.args[1] < 4 then cs = 6 end
        if #f.args[1] < 2 then cs = 7 end
        ret = '\n|-\n! colspan="' .. cs .. '" style="align:center;"|'
            .. f.args.f .. ret
    end

    if f.args.docollapsed then
        ret = ret .. '\n|}'
    end

    return ret
end

function p.getEditionsById(frame)
    local ret = {}
    ret.concat = table.concat
    ret.insert = table.insert

    local c = 0
    local t = ''
    local v = mtb[frame.args[1]]

    if v then
        c = 1
        t = v.title
        if v.t_now then t = v.t_now end

        ret:insert('|-')
        ret:insert('')

        if v.editions then
            for n, e in ipairs(v.editions) do
                local te = v.title
                if e.sheet_title then te = e.sheet_title end

                ret:insert('[[:de:' .. te .. '|' .. te .. ']]')
                ret:insert(tr_nil(e.pub_year))
                ret:insert(tr_nil(e.rec_year))

                if e.urls then
                    local urls = {}
                    for m, u in ipairs(e.urls) do
                        table.insert(urls, '[' .. u .. ']')
                    end
                    ret:insert(table.concat(urls, ' '))
                end

                ret:insert('-')
                c = n
            end

            ret[#ret] = nil
        else
            for i = 2,5 do ret:insert('') end
        end

        local rs = ''
        if c > 1 then
            rs = 'rowspan="' .. c .. '" style="vertical-align:center;"|'
        end

        if frame.args.dorc == 'column' or frame.args.doboth then
            ret[2] = ret[2] .. rs .. string.sub(frame.args[1], 1, 2) .. '\n|'
        end

        if frame.args.dorc == 'row' or frame.args.doboth then
            ret[2] = ret[2] .. rs .. string.sub(frame.args[1], 3, 6) .. '\n|'
        end

        ret[2] = ret[2] .. rs .. t
    end

    -- wrap wikitable header and footer for presentation
    local wth = ''
    local wtf = '' 

    if #ret > 0 then
        if not (frame.args.deco == 'no') then
            local f = mw.clone(frame)
            f.args.t = t
            f.args.f = c .. ' editions found'

            if not (frame.args.header == 'no') then wth = p.wth(f) end
            if not (frame.args.footer == 'no') then wtf = p.wtf(f) end
        end
    else
        return nil
    end

    return wth .. ret:concat('\n|') .. wtf
end

function p.getEditionsByRC(frame)
    local ret = {}
    ret.concat = table.concat
    ret.insert = table.insert

    local f = mw.clone(frame)
    if not f.args[1] then
        local pt = mw.text.split(mw.title.getCurrentTitle().text, '%s')
        f.args[1] = pt[#pt]
        f.args.dorc = pt[#pt - 1]
        f.args.docollapsed = 'yes'
    end
    f.args.deco = 'no'

    local n = tonumber(f.args[1])
    if n then
        if f.args.dorc == 'row' or f.args.doboth then
            for c = 1,110 do
                f.args[1] = string.format('%02d%02d', n, c)
                ret:insert(p.getEditionsById(f))
            end
            f.args.inv = 'column'
        else
            for r = 1,89 do
                f.args[1] = string.format('%02d%02d', r, n)
                ret:insert(p.getEditionsById(f))
            end
            f.args.inv = 'row'
        end
        f.args[1] = string.format('%02d', n)
    end

    -- wrap wikitable header and footer for presentation
    local wth = ''
    local wtf = ''

    if #ret > 0 then
        if not (frame.args.deco == 'no') then
            f.args.f = f.args.inv

            if #ret > 1 then f.args.f = f.args.f .. 's' end
            f.args.f = #ret .. ' ' .. f.args.f .. ' with map sheets found in '
                    .. f.args.dorc .. ' ' .. n

            if not (frame.args.header == 'no') then wth = p.wth(f) end
            if not (frame.args.footer == 'no') then wtf = p.wtf(f) end
        end
    else
        return nil
    end

    return wth .. ret:concat('\n') .. wtf
end

function p.getAllEditions(frame)
    local ret = {}
    ret.concat = table.concat
    ret.insert = table.insert

    local f = {
        args = {
            1,
            doboth = 'yes',
        }
    }

    for r = 1,89 do
        f.args[1] = r
        ret:insert(p.getEditionsByRC(f))
    end

    -- wrap wikitable header and footer for presentation
    local wth = ''
    local wtf = ''

    if #ret > 0 then
        if not (frame.args.deco == 'no') then
            if not (frame.args.header == 'no') then wth = p.wth('') end
            if not (frame.args.footer == 'no') then
                wtf = p.wtf('', 'all map sheets found in the whole grid')
            end
        end
    end

    return wth .. ret:concat('\n') .. wtf
end

function p.getEditionsByRow(frame)
    return p.getEditionsByRC({args = { frame.args[1], dorc = 'row'}})
end

function p.getEditionsByColumn(frame)
    return p.getEditionsByRC({args = { frame.args[1], dorc = 'column'}})
end

function p.getEditionsByGridID(frame)
    return p.getEditionsById(frame)
end

function p.getCatsForCategoryPages(f)
    local r = {}
    r.__ = table.concat
    r._ = table.insert

    local place = f.args.place
    local place2 = f.args.place2
    local place3 = f.args.place3
    local place4 = f.args.place4
    local place5 = f.args.place5

    local pt = mw.text.split(mw.title.getCurrentTitle().text, '[%s.]')
    local dozs = pt[#pt - 1]
    local n = tonumber(pt[#pt])
    local c = "[[Category:"
    local cf = "]]\n"
    
    local omo = function (s, k)
        local ps = { "Old", "Historical" }
        for _, p in ipairs(ps) do
            if exists(string.sub(c,3)..p.." maps of "..s, true) then 
                r:_(c) r:_(p) r:_(" maps of ") r:_(s) r:_('|MTB') r:_(k) r:_(cf)
            end 
        end
    end
    
    if n and (dozs == 'column' or dozs == 'row') then
        if dozs == 'column' then
            r:_(c) r:_('Messtischblatt by column and row|')
        end
        if dozs == 'row' then
            r:_(c) r:_('Messtischblatt by row and column|')
        end
        if n > 99 then r:_('A') end
        r:_(string.format('%2d', n)) r:_(cf)
    elseif n and (dozs == 'No') then
        local z = tonumber(mw.ustring.sub(pt[#pt], 1, 2))
        local s = tonumber(mw.ustring.sub(pt[#pt], 3))
        local _z = mw.ustring.format('%2d', z)
        local _s = mw.ustring.format('%2d', s)
        if s > 99 then _s = 'A' .. _s end
        r:_(c)    r:_('Messtischblatt of row ') r:_(z) r:_('|') r:_(_s) r:_(cf)
        r:_(c) r:_('Messtischblatt of column ') r:_(s) r:_('|') r:_(_z) r:_(cf)
    elseif not n and pt[1] == 'Messtischblatt' then
        local drop_umlauts = require("Module:Sortist").drop_umlauts
        local t = mw.ustring.sub(mw.title.getCurrentTitle().text, 16)
        r:_(c) r:_('Messtischblatt by name|') r:_(t) r:_(cf)
        omo(t, drop_umlauts(t))
        if place then omo(place, drop_umlauts(place)) end
        if place2 then omo(place2, drop_umlauts(place2)) end
        if place3 then omo(place3, drop_umlauts(place3)) end
        if place4 then omo(place4, drop_umlauts(place4)) end
        if place5 then omo(place5, drop_umlauts(place5)) end
    end

    return r:__('')
end

function p.getCatsForFilePages(f)
    local r = {}
    r.__ = table.concat
    r._ = table.insert

    local coll  = not string.find(f.args.collage or 'n', "^[nNfF0]") and "COLL"
    local crop  = not string.find(f.args.crop    or 'n', "^[nNfF0]") and "CROP"
    local check = not string.find(f.args.check   or 'y', "^[nNfF0]")

    local idlpr = f.args.prussian or f.args.late_prussian
    local idepr = f.args.old_prussian or f.args.early_prussian
    local idsax = f.args.saxon

    -- uncropped map sheets: 'name' the sheet is refered by today, if changed
    -- cropped sheets: name of a place the crop is about, place of interest
    local place = f.args.place or f.args[10]
    -- origin of the map sheet's digitized version, where the file is 'from'
    local from  = f.args.from  or f.args[9]
    local lat   = f.args.lat   or f.args[8]
    local lon   = f.args.lon   or f.args[7]
    local rev   = f.args.rev   or f.args[6]  or ''
    local year  = f.args.year  or f.args[5]
    local gridx = f.args.gridx or f.args[4]
    local gridy = f.args.gridy or f.args[3]
    local id    = f.args.id    or f.args[2]
    local name  = f.args.name  or f.args[1]

    local c = "[[Category:"
    local _ = function (s) return s or '' end
    local a = function (s) return s or "AAA" end
    local z = function (s) return s or "ZZZ" end

    local drop_umlauts = name and require("Module:Sortist").drop_umlauts

    if check then
        local e = "<div style=\"color:red;font-size:x-large\">Error: " ..
            "at least one of gridy, gridx, year or rev parameters does not " ..
            "match given id, see [[Template:Messtischblatt]] for help.</div>"
        if id and gridy and gridx and year then
            if not id == "df_dk_0010001_"..gridy..gridx..'_'..year..rev then
                return e
            end
        else return e
        end
    end

    if id and not from then
        if string.match(id, "^df_dk") then from = "Deutsche Fotothek" end
    end

    if from then
        r:_(c.."Messtischblatt from "..from.."]]")
    end

    if gridx and not lon and not crop then
        local x = tonumber(gridx)
        -- lacks Bessel to WGS84 geoid translation
        if x then lon =  5.0 + ( (45+(10*x)) / 60 ) end
    end

    if gridy and not lat and not crop then
        local y = tonumber(gridy)
        -- lacks Bessel to WGS84 geoid translation
        if y then lat = 56.0 - ( ( 3+( 6*y)) / 60 ) end
    end

    if lat and lon and not coll then
        r:_("== "..tr("Coordinates of map center").." ==")
        r:_("* "..mw.getCurrentFrame():expandTemplate{
                title = "inline coordinates",
                args = { [1]=lat, [2]=lon, [9]="dim:10km" }
            })
        r:_(c.."Media with locations]]")
    end

    if year then
        local k = z(name and drop_umlauts(name))..'_'..padleft(z(gridy), 3)
            ..'_'..padleft(z(gridx), 3)..'_'..year..'_'..rev
        r:_(c..(string.sub(year,1,2)+1).."th-century Messtischblatt|"..k.."]]")
        r:_(c..(string.sub(year,1,3))  .."0s Messtischblatt|"        ..k.."]]")
        r:_(c..(year)                  .." maps|"                    ..k.."]]")
        r:_(c.."Messtischblatt by year|"..(year)..'_'                ..k.."]]")
    end

    if gridx then
        local k = padleft(z(gridy), 3)..'_'..z(year)..'_'..rev
        local x = tonumber(gridx) or gridx -- remove leading zeroes if present
        r:_(c.."Messtischblatt of column "..x..'|'..k.."]]")
        r:_(c.."Messtischblatt by column and row|"..padleft(x, 3)..'_'..k.."]]")
    end

    if gridy then
        local k = padleft(z(gridx), 3)..'_'..z(year)..'_'..rev
        local y = tonumber(gridy) or gridy -- remove leading zeroes if present
        r:_(c.."Messtischblatt of row " .. y ..'|'..k.."]]")
        r:_(c.."Messtischblatt by row and column|"..padleft(y, 3)..'_'..k.."]]")
    end

    if gridy and gridx then
        local k = _(coll).._(crop)..z(year)..'_'..rev
        r:_(c.."Messtischblatt No."..gridy..gridx..'|'..k.."]]")
    end

    if gridy and gridx and year and not coll then
        local o = nil
        local t = function (t) return t or {} end
        for k, v in pairs(t(t(mtb[gridy..gridx]).editions)) do
            if tonumber(v.pub_year or 0) == tonumber(year) then
                o = string.match(_(t(v.urls)[1]), "[0-9]+$")
            end
        end
        -- 2016-02-22:
        -- What good is a permalink, if an arbitrary website update removes it!?
        -- There is a "new" permalink now, different in format and id usage.
        --if o then
        --    r:_("* [http://kartenforum.slub-dresden.de/vkviewer/permalinkv2?"..
        --        "objectid="..o..' '..tr(
        --        "Browse this map in Virtual Map Forum with an OSM background")..
        --        "]")
        --end
    end

    local mref = function (s, id)
    	local k = padleft(id, 4)..'_'..z(year)..'_'..rev
        r:_(c.."Messtischblatt by "..s.." reference|"..k.."]]")
    end

    if idlpr then mref("prussian",       idlpr) end
    if idepr then mref("early prussian", idepr) end
    if idsax then mref("saxon",          idsax) end

    if name then
        local k = padleft(z(gridy), 3)..'_'..padleft(z(gridx), 3)..'_'..
            _(coll).._(crop)..z(year)..'_'..rev
        r:_(c.."Messtischblatt "..name..'|'..k.."]]")
        k  = drop_umlauts(    name..'_'..k )
        r:_(c.."Messtischblatt by name|"..k.."]]")
        r:_(c.."Messtischblatt|"..k.."]]")
    end

    if f.args.dump then
        r:_("<p style=\"color:red\">"..mw.text.nowiki(r:__('\n')).."</p>")
    end

    return r:__('\n')
end

function p.getCats(f)
    local  currentNs = mw.title.getCurrentTitle().namespace
    if     currentNs == 14 then    -- called from CategoryNs
        return p.getCatsForCategoryPages(f)
    elseif currentNs ==  6 then    -- called from FileNs (Template transcluded)
        return p.getCatsForFilePages(f:getParent())
    else                           -- called when testing in ModuleNs
        return p.getCatsForFilePages(f)
    end
end

return p