Modul:Faktenbox/Tabs/Adressen: Unterschied zwischen den Versionen

Kommentar
(Die Seite wurde neu angelegt: „local p = {} local com = require("Modul:Common") local str = require("Modul:String") local wik = require("Modul:Wiki") local que = require("Modul:Queries") local lfd = require("Modul:LinkFormDatum") local fab_tabs = require("Modul:Faktenbox/Tabs") function p.adressenTab_(fbdata) -- mw.log("adressenTab_()") -- mw.logObject(fbdata) -- Konsolenzeile zum Debuggen: -- =p.adressenTab_({fullpagename="Jakob Bock"}) -- =p.adressenTab_({fullpagename="Ludwig E…“)
 
(Kommentar)
 
(8 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 18: Zeile 18:
-- =p.adressenTab_({fullpagename="Rathaus"})
-- =p.adressenTab_({fullpagename="Rathaus"})
-- =p.adressenTab_({fullpagename="E-Tankstelle Hochstraße"})
-- =p.adressenTab_({fullpagename="E-Tankstelle Hochstraße"})
-- =p.adressenTab_({fullpagename="TestPerson2"})
-- =p.adressenTab_({fullpagename="Jakob Wassermann"})
-- =p.adressenTab_({fullpagename="Jakob Wassermann"})
-- =p.adressenTab_({fullpagename="Hedwig Gellinger"})
local t = ""
local t = ""


Zeile 32: Zeile 32:
["AdresseBis"] = { querypostfix = "", default = ""}}
["AdresseBis"] = { querypostfix = "", default = ""}}


-- DB-Abfrage
-- SMW-DB-Abfrage
local query = "[[-Has subobject::".. fbdata.fullpagename .. "]][[SubObjektTyp::Adresse]]|?#-|limit=100"
local query = "[[-Has subobject::".. fbdata.fullpagename .. "]][[SubObjektTyp::Adresse]]|?#-|limit=100"
for i, a in pairs(attr) do
for i, a in pairs(attr) do
Zeile 39: Zeile 39:
local addresses = mw.smw.ask(query)
local addresses = mw.smw.ask(query)


if addresses ~= nil then
if addresses ~= nil then -- Adressen vorhanden?


-- defaults fehlender Attribute und Sortier-Index setzen
-- defaults setzen fehlender Attribute
for i, _ in pairs(addresses) do
for i, _ in pairs(addresses) do
for j, a in pairs(attr) do
for j, a in pairs(attr) do
addresses[i][j] = addresses[i][j] or a.default
addresses[i][j] = addresses[i][j] or a.default
end
end
addresses[i].von_sort_index = smwDate2SortIndex(addresses[i]["AdresseVon"])
addresses[i].bis_sort_index = smwDate2SortIndex(addresses[i]["AdresseBis"])
end
end


-- Tabelle sortieren
-- Chronologische Sortierung
for i = 1, #addresses - 1 do -- max. #addresses - 1 Durchgänge nötig
if #addresses > 1 then
local swap_this_interation = false
-- Sortier-Indizes setzen
for j = 1, #addresses - 1 do
for i, _ in pairs(addresses) do
local swap = vonBisSortCompare(addresses[j], addresses[j+1])
addresses[i].von_sort_index, addresses[i].bis_sort_index = p.sortIndex(addresses[i])
if swap then
addresses[j], addresses[j+1] = addresses[j+1], addresses[j]
end
swap_this_interation = swap_this_interation or swap
end
if not swap_this_interation then
break
end
end
-- Tabelle sortieren
table.sort(addresses, function(a1, a2) return a1.bis_sort_index < a2.von_sort_index end)
end
end


Zeile 86: Zeile 79:
end
end
if e["AdresseVon"] ~= "" then
if e["AdresseVon"] ~= "" then
if e["AdresseBis"] ~= "" then
if e["AdresseBis"] == "" then
table.insert(bracket, "von " .. lfd.formatDate(e["AdresseVon"]))
table.insert(bracket, "seit")
end
table.insert(bracket, lfd.formatDate(e["AdresseVon"]))
end
if e["AdresseBis"] ~= "" and e["AdresseBis"] ~= e["AdresseVon"] then
if e["AdresseVon"] ~= "" then
table.insert(bracket, "-")
else
else
table.insert(bracket, "seit " .. lfd.formatDate(e["AdresseVon"]))
table.insert(bracket, "bis")
end
end
end
table.insert(bracket, lfd.formatDate(e["AdresseBis"]))
if e["AdresseBis"] ~= "" then
table.insert(bracket, "bis " .. lfd.formatDate(e["AdresseBis"]))
end
end
if #bracket > 0 then
if #bracket > 0 then
Zeile 100: Zeile 97:
t = t .. "\n"
t = t .. "\n"
end
end
end
 
end -- Ende if Adressen vorhanden


-- Tab draus machen
-- Tab draus machen
Zeile 108: Zeile 106:
end
end


function smwDate2SortIndex(date)
function p.sortIndex(address)
-- mw.log("sortIndex(address)")
-- mw.logObject(address)
-- Datums-Index holen
local von_sort_index = p.smwDate2SortIndex(address["AdresseVon"], "von")
local bis_sort_index = p.smwDate2SortIndex(address["AdresseBis"], "bis")
-- Ggf. Index von Geburtshaus/-ort setzen
-- ganz nach vorne => höchste Prio = -99999999
if string.find(address["Adressart"], "Geburt") == 1 then -- beginnt mit "Geburt"
von_sort_index = -99999999
end
-- Ggf. Index von "Letzter Wohnort" (nicht unbedingt Fürth!) setzen
-- ganz nach hinten => niedrigste Prio = 99999999
if address["Adressart"] == "Letzter Wohnort" then -- exakte Übereinstimmung
von_sort_index = 99999999
bis_sort_index = 99999999
-- Ggf. Index von fehlenden von/bis setzen
-- wenn fehlt, ganz nach hinten (aber vor "Letzter Wohnort") => drittniedrigste Prio = 77777777
-- "Letzter Wohnort" => zweitniedrigste Prio = 88888888
elseif von_sort_index == 0 and bis_sort_index == 0 then
if string.find(address["Adressart"], "Letzter Wohnort") == 1 then -- beginnt mit "Letzter Wohnort"
von_sort_index = 88888888
bis_sort_index = 88888888
else
von_sort_index = 77777777
bis_sort_index = 77777777
end
-- Übernahme fehlender Index von
elseif von_sort_index == 0 and bis_sort_index ~= 0 then
von_sort_index = bis_sort_index
-- Übernahme fehlender Index bis
elseif bis_sort_index == 0 and von_sort_index ~= 0 then
bis_sort_index = von_sort_index
-- Korrektur von > bis
elseif von_sort_index > bis_sort_index then
von_sort_index = bis_sort_index -- den kleineren übernehmen
end
-- mw.log(von_sort_index .. " - " .. bis_sort_index)
return von_sort_index, bis_sort_index
end
 
function p.smwDate2SortIndex(date, vonbis)
-- mw.log("smwDate2SortIndex(" .. date .. ")")
-- mw.log("smwDate2SortIndex(" .. date .. ")")
-- Datum in Index wandeln
-- Bsp.: 23 Apr. 1989 => 19890423
-- Konsolenzeile zum Debuggen:
-- Konsolenzeile zum Debuggen:
-- =p.smwDate2SortIndex("2000")
-- =p.smwDate2SortIndex("2000")
Zeile 116: Zeile 157:
local sort_index = ""
local sort_index = ""
local date=str.split2(date, " ")
local date=str.split2(date, " ")
 
if #date == 1 then -- nur Jahr vorhanden
if #date == 1 then -- nur Jahr
sort_index = tostring(date[1])
sort_index = tostring(date[1]) ..  
if vonbis == "von" then
"0101" -- 1. Januar
sort_index = sort_index .. "9999"
elseif #date == 2 then -- nur Monat + Jahr
else
sort_index = tostring(date[2]) ..
sort_index = sort_index .. "0000"
string.format("%02d", com.getMonthnr(date[1])) ..
end
"01" -- 1. Tag
elseif #date == 2 then -- nur Monat + Jahr vorhanden
elseif #date == 3 then -- komplettes Datum
sort_index = tostring(date[2]) .. string.format("%02d", com.getMonthnr(date[1]))
sort_index = tostring(date[3]) ..
if vonbis == "von" then
string.format("%02d",com.getMonthnr(date[2])) ..
sort_index = sort_index .. "99"
string.format("%02d", tonumber(string.match(date[1], "%d*")))
else
sort_index = sort_index .. "00"
end
elseif #date == 3 then -- komplettes Datum vorhanden
sort_index = tostring(date[3]) .. string.format("%02d",com.getMonthnr(date[2])) .. string.format("%02d", tonumber(string.match(date[1], "%d*")))
end
end
sort_index = tonumber(sort_index) or 0
sort_index = tonumber(sort_index) or 0
-- mw.log("sort_index = " .. tostring(sort_index))
-- mw.log("sort_index = " .. tostring(sort_index))
return sort_index
return sort_index
end
function vonBisSortCompare(k1, k2)
-- mw.log("vonBisSortCompare(k1, k2)")
-- mw.logObject(k1)
-- mw.logObject(k2)
local swap = false
if k1.bis_sort_index ~= 0 then
if k2.von_sort_index ~= 0 then
swap = ( k1.bis_sort_index > k2.von_sort_index )
elseif k2.bis_sort_index ~= 0 then
swap = ( k1.bis_sort_index > k2.bis_sort_index )
end
elseif k1.von_sort_index ~= 0 then
if k2.von_sort_index ~= 0 then
swap = ( k1.von_sort_index > k2.von_sort_index )
elseif k2.bis_sort_index ~= 0 then
swap = ( k1.von_sort_index > k2.bis_sort_index )
end
else
if k2.von_sort_index ~= 0 or k2.bis_sort_index ~= 0 then
swap = true
end
end
-- mw.log("swap = " .. tostring(swap))
return swap
end
end


return p
return p