Modul:Faktenbox/Zeilen/Work

Aus FürthWiki

Die Dokumentation für dieses Modul kann unter Modul:Faktenbox/Zeilen/Work/Doku erstellt werden

local p = {}

local com = require("Modul:Common")
local str = require("Modul:String")
local wik = require("Modul:Wiki")
local geo = require("Modul:Geo")
local smw = require("Modul:SMW")
local atr = require("Modul:Attribut")
local wob = require("Modul:Wörterbuch")
local lfd = require("Modul:LinkFormDatum")

local fab_red_links = require("Modul:Faktenbox/Zeilen/RedLinks")

function p.zeileKategorieSeite_(fbdata, kindex)
	-- Konsolenzeile zum Debuggen:
	-- =p.zeileKategorieSeite_({pagename="fpn",kategorie={"kat1","kat2"},kategorie_einzahl={"k1","k2"},klasse={"kla1","kla2"}}, 2)
	local t = ""
	local kategorie = wob.mehrzahl((fbdata.kategorie or fbdata.klasse)[kindex or 1])
	local kategorie_einzahl = (fbdata.kategorie_einzahl or fbdata.klasse)[kindex or 1]
	t = t .. "<dt>[[:Kategorie:" .. kategorie .. "|" .. kategorie_einzahl .. "]]</dt>"
	if wik.pageExists("Kategorie:" .. fbdata.pagename) then
		t = t .. "<dd>[[:Kategorie:" .. fbdata.pagename .. "|" .. fbdata.pagename .. "]]</dd>"
	else
		t = t .. "<dd>'''" .. fbdata.fullpagename .. "'''</dd>"
	end
	return t
end

function p.zeileStrasseHnr_(fbdata)
	-- Konsolenzeile zum Debuggen:
	-- =p.zeileStrasseHnr_{strasse_hnr={strasse={{name="Erlanger Straße",hausnummer={"11", "13"}},{name="XY Straße",hausnummer={}}},anzahlGebaeude=3}}
	local t = ""
	for is, sh in ipairs(fbdata.strasse_hnr.strasse) do							-- Straßen/Hausnummer-Kombis durchiterieren
		t = t .. "<dt>"
		if is > 1 then
			if is == 2 and #sh.hausnummer == 0 then
				t = t ..  "Ecke "												-- Sonderfall Ecke
			else
				t = t .. is .. ". "												-- Numerierungsanzeige "2., "3." usw.
			end
		else
			is = ""																-- keine 1, aber 2, 3, ... für weitere Verwendung
		end
		t = t .. "Straße"
		if #sh.hausnummer > 0 then
			t = t .. " / " .. "Hausnummer"										-- nur wenn Hausnummern vorhanden
			if #sh.hausnummer > 1 then
				t = t .. "n"													-- Mehrzahl
			end
		end
		t = t .. "</dt><dd>[[" .. sh.name .. "]]"
		smw.addAttrToList(fbdata.attr.list, "Straße" .. is, sh.name)
		for ih, h in ipairs(sh.hausnummer) do									-- Hausnummern durchiterieren
			local strasse_hnr_ = sh.name .. " " .. h
			if ih > 1 then
				t = t .. ","													-- Kommas davor ab 2. Hausnummer
			end
			if wik.pageExists(strasse_hnr_) then								-- Link, wenn Seite vorhanden
				t = t .. " [[" .. strasse_hnr_ .. "|" .. h .. "]]"
			else
				t = t .. " " .. h
			end
			smw.addAttrToList(fbdata.attr.list, "Hausnummer" .. is, h)
			smw.addAttrToList(fbdata.attr.list, "StraßeHnr", strasse_hnr_)
		end
		t = t .. "</dd>"
	end
	smw.addAttrToList(fbdata.attr.list, "AnzahlGebäude", fbdata.strasse_hnr.anzahlGebaeude)
	return t
end

function p.zeileAktenNr_(fbdata)
	-- Konsolenzeile zum Debuggen:
	-- =p.zeileAktenNr_{pargs={AktenNr="D-5-63-000-1464"}}
	local t = ""
	local aktennr = str.splitAndStrip(fbdata.pargs["AktenNr"], ";")
	if #aktennr > 0 then
		t = t .. "<dt>" .. "Akten-Nr.</dt><dd>"
		for i, a in ipairs(aktennr) do
			if i > 1 then
				t = t .. ",<br>"
			end
			local p = smw.getPages("[[:+]][[AktenNr::" .. a .. "]]", true)
			if #p > 1 then
				t = t .. "{{#queryformlink:form=Abfrage Denkmalschutz|link text=" .. a .. "|query string=Abfrage Denkmalschutz[AktenNr]=" .. a .. "&_run}}"
			else
				t = t .. a
			end
			smw.addAttrToList(fbdata.attr.list, "AktenNr", a)
			t = t .. " (" .. #p .. ")" -- Anzahl der Artikel mit dieser Akten-Nr. in Klammern
		end
		t = t .. "</dd>"
	end
	return t
end

function p.zeileGeoDaten_(fbdata)
	local t = ""
	if fbdata.geoobjects ~= nil then
		for i, o in ipairs(fbdata.geoobjects) do
			local geokoordinate = o["Geokoordinate"]
			if geokoordinate ~= nil then
				local double = false											-- doppelte Koordinaten ausfiltern
				for d = 1, i - 1 do
					if fbdata.geoobjects[d]["Geokoordinate"] == geokoordinate then
						double = true
						break
					end
				end
				if not double then
					if t ~= "" then
						t = t .. ",<br>"
					end
					t = t .. geo.osmLink(fbdata.frame, geokoordinate)
				end
			end
		end
		if fbdata.geoobjects.geojson.exists then
			if t ~= "" then
				t = t .. ",<br>"
			end
			t = t .. "[[" .. fbdata.geoobjects.geojson.fullpagename .. "|GeoJson-Karte]]"
		end
		if t ~= "" then
			t = "<dt>Geo-Daten</dt><dd>" .. t .. "</dd>"
		end
	end
	return t
end

function p.zeileDatum_(args)
	-- Konsolenzeile zum Debuggen:
	-- =p.zeileDatum_{attr_date="Weihedatum",date="1959/11/15",attr_day="Weihetag",attr_year="Weihejahr"}
	-- =p.zeileDatum_{attr_date="Weihedatum",fbdata={klasse={},pargs={["Weihedatum"]="1959/11/15"}}}
	local t = ""
	local attr_date = args.attr_date
	local date = args.date or args.fbdata.pargs[attr_date]
	if date ~= nil then
		local attr_day = args.attr_day or string.gsub(attr_date, "datum", "tag")
		local attr_year = args.attr_year or string.gsub(attr_date, "datum", "jahr")
		local attr_text = args.attr_text or atr.attrText(attr_date, args.fbdata.klasse[1])
		t = t .. "<dt>" .. (attr_text or attr_date) .. "</dt><dd>"
		if com.isTrue(args.val_show_only) then
			t = t .. lfd.linkFormDate(date)
		else
			local lfd_date = lfd.linkFormDate(date, attr_date, attr_day, attr_year)
			t = t .. lfd_date
			smw.addAttrToList(args.fbdata.attr.list, attr_date .. "Lfd", lfd_date)
		end
		t = t .. "</dd>"
	end
	return t
end

function p.zeileSemantikBrowsenAbfrage_(fbdata)
	-- Konsolenzeile zum Debuggen:
	-- =p.zeileSemantikBrowsenAbfrage_{fullpagename="Rathaus"}
	local t = ""
	t = t .. "&nbsp;"
	t = t .. "[[Bild:IconSearch.svg|16px|semantisches Browsen|link=[[Spezial:Durchsuchen/" .. fbdata.fullpagename .. "]]]]"
	t = t .. "&nbsp;&nbsp;&nbsp;"
	t = t .. "[[Spezial:Durchsuchen/" .. fbdata.fullpagename .. "|Sem. Browsen]]"
	if fbdata.kategorie ~= nil and wik.pageExists("Formular:Abfrage " .. fbdata.kategorie[1]) then
		t = t .. " / {{#queryformlink:form=Abfrage " .. fbdata.kategorie[1] .. "|link text=Abfrage}}"
	end
	return t
end

function p.zeile_(args)
	-- Konsolenzeile zum Debuggen:
	-- =p.zeile_{attr_name="Vorgänger",attr_text="t",fbdata={pargs={["Vorgänger"]="; v11 , V12 ; v13 (a(b))"}},attr_val="; v1 ; v2, v3 (a(b))",val_prefix="pre",val_postfix="post",exclude_bracket_values=true,delimiter=","}
	local t = ""
	-- Attribut-Name/Wert aufbereiten (splitten, in table umwandeln)
	local attr_name = args.attr_name
	local attr_val
	if args.fbdata ~= nil then
		attr_val = args.attr_val or args.fbdata.pargs[attr_name] or {}
	else
		attr_val = args.attr_val or {}
	end
	if type(attr_val) ~= "table" then -- Liste in table umwandeln/zerteilen
		if args.delimiter ~= nil then
			attr_val = str.splitAndStrip(attr_val, args.delimiter or ";")
		elseif attr_val ~= "" then
			attr_val = { attr_val }
		else
			attr_val = {}
		end
	end
	-- Zeile zusammenbauen
	if #attr_val > 0 then
		-- Attribut-Text ermitteln und in Zeile übernehmen
		local attr_text = args.attr_text or atr.attrText(attr_name, args.fbdata.klasse[1])
		if #attr_val > 1 then
			attr_text = wob.mehrzahl(attr_text) -- Mehrzahl
		end
		t = t .. "<dt>" .. attr_text .. "</dt>"
		-- Attribut-Werte in Zeile übernehmen
		t = t .. "<dd>"
		if args.val_prefix ~= nil then
			t = t .. args.val_prefix .. " " -- Prefix
		end
		for idx, val in ipairs(attr_val) do -- Werte-Liste
			if idx > 1 then
				t = t .. ", " -- Listen-Kommata
			end
			if not wik.pageExists("Attribut:" .. attr_name) then				-- Spezialbehandlung Attribut nicht vorhanden
				t = t .. p.setStandardAttr(args.fbdata.attr.list, attr_name, val, args.exclude_bracket_values, args.val_show_only)
			else
				local attr_type = mw.smw.getPropertyType(attr_name)
				if attr_type == "_wpg" then 									-- Seiten-Attribute Spezialbehandlung
					t = t .. p.setPageAttr(args.fbdata.attr.list, attr_name, val, args.val_show_only)
				elseif attr_type == "_uri" then 								-- URL Spezialbehandlung
					t = t .. p.setUrlAttr(args.fbdata.attr.list, attr_name, val, args.val_show_only)
				elseif attr_type == "_ema" then 								-- E-Mail-Attribute Spezialbehandlung
					t = t .. p.setEmailAttr(args.fbdata.attr.list, attr_name, val, args.val_show_only)
				elseif attr_type == "_tel" then 								-- Telefon-Attribute Spezialbehandlung
					t = t .. p.setTelAttr(args.fbdata.attr.list, attr_name, val, args.val_show_only)
				elseif attr_type == "_boo" then 								-- Wahrheitswert-Attribute Spezialbehandlung
					t = t .. p.setBoolAttr(args.fbdata.attr.list, attr_name, val, args.val_show_only, args.fbdata.klasse[1])
				else															-- Standard-Attribute
					t = t .. p.setStandardAttr(args.fbdata.attr.list, attr_name, val, args.exclude_bracket_values, args.val_show_only)
				end
			end
		end
		if args.val_postfix ~= nil then
			t = t .. " " .. args.val_postfix -- Postfix
		end
		t = t .. "</dd>"
	end
	return t
end

function p.setPageAttr(attrlist, name, val, val_show_only)
	local t = ""
	if string.find(val, "[#=<>%[%]%|%{%}]") ~= nil then							-- problematische Wikitext/HTML-Inhalte vorhanden?
		t = t .. val															-- ==> kein Link, sondern nur Attribut versteckt setzen
	elseif wik.pageExists(val) then												-- eine vorhandene Seite regulär verlinken bzw. setzen
		t = t .. "[[" .. val .. "]]"
	else
		local val_pre_bracket, val_without_bracket, val_post_bracket = str.bracketSplitAndStrip(val)	-- nach Klammern suchen und aufteilen
		if wik.pageExists(val_without_bracket) then								-- Seite ohne Klammern vorhanden?
			if val_pre_bracket ~= "" then										-- Klammer-Text vor eigentlicher Seite
				t = t .. val_pre_bracket .. " "
			end
			t = t .. "[[" .. val_without_bracket .. "]]"						-- das eigentliche Attribut verlinken
			if val_post_bracket ~= "" then										-- Klammer-Text nach eigentlicher Seite
				t = t .. " " .. val_post_bracket
			end
			val = val_without_bracket
		-- auch ohne Klammern existiert keine Seite => dann roten Link anzeigen?
		elseif fab_red_links.red_links[name] ~= false then						-- auch nil als default als roten Link
			t = t .. "[[" .. val .. "]]"
		else
			t = t .. val
		end
	end
	if not val_show_only then
		smw.addAttrToList(attrlist, name, val)
	end
	return t
end

function p.setUrlAttr(attrlist, name, val, val_show_only)
	local t = ""
	local url = val
	if string.sub(url, 1, 7) ~= "http://" and
		string.sub(url, 1, 8) ~= "https://" then
		url = "https://" .. url													-- https:// ist heute Standard
	end
	local urltext = str.split2(url, "://")
	urltext = urltext[#urltext]
	t = t .. "[" .. url .. " " .. urltext .. "]"
	if not val_show_only then
		smw.addAttrToList(attrlist, name, url)
	end
	return t
end

function p.setEmailAttr(attrlist, name, val, val_show_only)
	local t = ""
	local val_pre_bracket, email, val_post_bracket = str.bracketSplitAndStrip(val) -- nach Klammern suchen und aufteilen
	if val_pre_bracket ~= "" then
		t = t .. val_pre_bracket .. " "
	end
	t = t .. "[mailto:" .. email .. " " .. email .. "]"
	if val_post_bracket ~= "" then
		t = t .. " " .. val_post_bracket
	end
	if not val_show_only then
		smw.addAttrToList(attrlist, name, email)
	end
	return t
end

function p.setTelAttr(attrlist, name, val, val_show_only)
	local t = ""
	local val_pre_bracket, tel, val_post_bracket =								-- nach Klammern suchen und aufteilen
		str.bracketSplitAndStrip(val)
	tel = val_pre_bracket .. " " .. tel											-- Klammer am Anfang wieder anfügen
	tel = p.getTelUri(tel)
	if string.sub(tel, 1, 1) == "+" then
		t = t .. "[tel:" .. tel .. " " .. val .. "]"
	else
		t = t .. val
	end
	if not val_show_only then
		smw.addAttrToList(attrlist, name, tel)
	end
	return t
end

function p.setBoolAttr(attrlist, name, val, val_show_only, klasse)
	local t = ""
	if not val_show_only then
		smw.addAttrToList(attrlist, name, val)
	end
	-- Anzeigeformat abfragen
	local attr_attr = atr.getAttrAttributes(name, klasse)
	local anzeigeformat = attr_attr["Anzeigeformat"]
	anzeigeformat = str.splitAndStrip(anzeigeformat, ",")
	-- Anzeigeformat Spezialbehandlungen
	if anzeigeformat[1] == "tick" then
		anzeigeformat = {"✓", "✕"}
	elseif anzeigeformat[1] == "x" then
		anzeigeformat = {"✕"}
	end
	-- Anzeige
	if com.isTrue(val) then
		t = t .. (anzeigeformat[1] or "ja")
	else
		t = t .. (anzeigeformat[2] or "nein")
	end
	return t
end

function p.setStandardAttr(attrlist, name, val, exclude_bracket_values, val_show_only)
	local t = ""
	local val_pre_bracket, val_post_bracket = "", ""
	if com.isTrue(exclude_bracket_values) then									-- Klammer-Text separieren (kein Attribut-Wert)
		val_pre_bracket, val, val_post_bracket = str.bracketSplitAndStrip(val)	-- nach Klammern suchen und aufteilen
	end
	if val_pre_bracket ~= "" then
		t = t .. val_pre_bracket .. " "
	end
	t = t .. val
	if val_post_bracket ~= "" then
		t = t .. " " .. val_post_bracket
	end
	if not val_show_only then
		smw.addAttrToList(attrlist, name, val)
	end
	return t
end

function p.getTelUri(val)
	-- Alles außer Zahlen und + am Anfang ausfiltern
	local tel = ""
	local pre_plus = false
	for i = 1, mw.ustring.len(val) do
		local char = mw.ustring.sub(val, i, i)
		if (char == "+" and not pre_plus) or tonumber(char) ~= nil then			-- + am Anfang erlaubt
			tel = tel .. char
			pre_plus = true
		end
	end
	-- Sonderbehandlungen
	if mw.ustring.sub(tel, 1, 2) == "00" then									-- 00 ...
		tel = "+" .. mw.ustring.sub(tel, 3)
	end
	if mw.ustring.sub(tel, 1, 1) == "0" and										-- beginnt mit 0zzz (z = Zahl 1-9)
		(tonumber(mw.ustring.sub(tel, 2, 2)) or 0) > 0 then						-- dann wird +49 angenommen
		tel = "+49" .. mw.ustring.sub(tel, 2)
	elseif mw.ustring.sub(tel, 1, 4) == "+490" then								-- +49(0) ...
		tel = "+49" .. mw.ustring.sub(tel, 5)
	end
	return tel
end

return p