Modul:Sandlådan/Moonhouse/test
Dokumentationen för denna modul kan skapas på Modul:Sandlådan/Moonhouse/test/dok
local L10n = {}
L10n.para = {
municipalityQid = "municipality",
text = "text",
frameWidth = "width",
frameHeight = "height",
zoom = "zoom",
highlightFill = "highlight_fill",
normalFill = "normal_fill",
municipalityStrokeColor = "municipality_stroke_color",
municipalityStrokeWidth = "municipality_stroke_width",
municipalityFillOpacity = "municipality_fill_opacity",
countyStrokeColor = "county_stroke_color",
outsideCountyFillOpacity = "outside_county_fill_opacity",
outsideCountyFillColor = "outside_county_fill_color",
title = "title",
markerSymbol = "marker_symbol",
marker = "marker",
markerColor = "marker_color",
markerSize = "marker_size",
coord = "koordinater",
id = "id",
capitalQid = "capital_qid",
hideCapital = "dölj_centralort",
}
L10n.defaults = {
text = "Karta",
frameWidth = "300",
frameHeight = "200",
zoom = 6,
highlightFill = "#ff3333",
normalFill = "#ffffff",
municipalityStrokeColor = "#999999",
municipalityStrokeWidth = 1,
municipalityFillOpacity = 0.7,
countyStrokeColor = "#ff0000",
outsideCountyFillOpacity = 0.7,
outsideCountyFillColor = "#999999",
markerColor = "5E74F3",
markerSize = "small",
}
L10n.error = {
noCoords = "Koordinater måste vara specificerade på Wikidata eller i |" .. ( type(L10n.para.coord)== 'table' and L10n.para.coord[1] or L10n.para.coord ) .. "=",
wikidataCoords = "Koordinaterna för ort hittades inte på Wikidata"
}
L10n.str = {
-- valid values for icon, frame, and plain parameters
affirmedWords = ' '..table.concat({
"på",
"sant",
"ja",
"j"
}, ' ')..' ',
declinedWords = ' '..table.concat({
"av",
"falskt",
"nej",
"n",
}, ' ')..' '
}
function getParameterValue(args, param_id, suffix)
suffix = suffix or ''
if type( L10n.para[param_id] ) ~= 'table' then
mw.logObject(param_id)
return args[L10n.para[param_id]..suffix]
end
for _i, paramAlias in ipairs(L10n.para[param_id]) do
if args[paramAlias..suffix] then
return args[paramAlias..suffix]
end
end
return nil
end
function trimArgs(argsTable)
local cleanArgs = {}
for key, val in pairs(argsTable) do
if type(val) == 'string' then
val = val:match('^%s*(.-)%s*$')
if val ~= '' then
-- control characters inside json need to be escaped, but stripping them is simpler
-- See also T214984
cleanArgs[key] = val:gsub('%c',' ')
end
else
cleanArgs[key] = val
end
end
return cleanArgs
end
function isAffirmed(val)
if not(val) then return false end
return string.find(L10n.str.affirmedWords, ' '..val..' ', 1, true ) and true or false
end
function makeCoords(args, plainOutput)
local coords, lat, long
local frame = mw.getCurrentFrame()
if getParameterValue(args, 'coord') then
coords = frame:preprocess( getParameterValue(args, 'coord') )
lat, long = parseCoords(coords)
else
lat, long = wikidataCoords(getParameterValue(args, 'id') or mw.wikibase.getEntityIdForCurrentPage())
end
if plainOutput then
return lat, long
end
return {[0] = long, [1] = lat}
end
function wikidataCoords(item_id)
if not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then
error(L10n.error.noCoords, 0)
end
local coordStatements = mw.wikibase.getBestStatements(item_id, 'P625')
if not coordStatements or #coordStatements == 0 then
error(L10n.error.wikidataCoords, 0)
end
local hasNoValue = ( coordStatements[1].mainsnak and coordStatements[1].mainsnak.snaktype == 'novalue' )
if hasNoValue then
error(L10n.error.wikidataCoords, 0)
end
local wdCoords = coordStatements[1]['mainsnak']['datavalue']['value']
return tonumber(wdCoords['latitude']), tonumber(wdCoords['longitude'])
end
function municipalities(args, municipality_qid)
local normalFill = getParameterValue(args, 'normalFill') or L10n.defaults.normalFill
local highlightFill = getParameterValue(args, 'highlightFill') or L10n.defaults.highlightFill
local data = {}
data.type = "ExternalData"
data.service = "geoshape"
data.query = "SELECT ?id ?idLabel ?highlighted (if(?highlighted = '0', '".. normalFill .."', '".. highlightFill .."') as ?fill) WHERE { ?id wdt:P31 wd:Q127448. ?id wdt:P131 ?county. wd:".. municipality_qid .." wdt:P131 ?county. BIND(if(?id = wd:".. municipality_qid ..", '1', '0') as ?highlighted) MINUS { ?id wdt:P576 wikibase:timeValue. } SERVICE wikibase:label { bd:serviceParam wikibase:language \"[sv,nb,da,AUTO_LANGUAGE]\". } } "
data.properties = {}
data.properties.stroke = getParameterValue(args, 'municipalityStrokeColor') or L10n.defaults.municipalityStrokeColor --"#999999"
data.properties["stroke-width"] = getParameterValue(args, 'municipalityStrokeWidth') or L10n.defaults.municipalityStrokeWidth -- 1
data.properties["fill-opacity"] = getParameterValue(args, 'municipalityFillOpacity') or L10n.defaults.municipalityFillOpacity -- 0.7
return mw.text.jsonEncode(data)
end
function county(args, municipality_qid)
local data = {}
data.type = "ExternalData"
data.service = "geomask"
data.query = "SELECT ?id WHERE { ?id wdt:P31 wd:Q200547. ?id wdt:P150 wd:".. municipality_qid ..". MINUS { ?id wdt:P576 ?_. } }"
data.properties = {}
data.properties.fill = getParameterValue(args, 'outsideCountyFillColor') or L10n.defaults.outsideCountyFillColor -- "#999999"
data.properties["stroke"] = getParameterValue(args, 'countyStrokeColor') or L10n.defaults.countyStrokeColor --"#ff0000"
data.properties["fill-opacity"] = getParameterValue(args, 'outsideCountyFillOpacity') or L10n.defaults.outsideCountyFillOpacity -- 0.5
return mw.text.jsonEncode(data)
end
function capital_qid(municipality_qid)
if not(mw.wikibase.isValidEntityId(municipality_qid)) or not(mw.wikibase.entityExists(municipality_qid)) then
mw.log("Municipality QID isn't valid/exists.")
return nil
end
local capital = mw.wikibase.getBestStatements(municipality_qid, 'P36')
if not capital or #capital == 0 then
mw.log("Can't find property capital for municipality QID.")
return nil
end
local hasNoValue = ( capital[1].mainsnak and capital[1].mainsnak.snaktype == 'novalue' )
if hasNoValue then
mw.log("No value found for municipality capital statement.")
return nil
end
return capital[1]['mainsnak']['datavalue']['value']['id']
end
function capital_coords(args, municipality_qid)
local qid = getParameterValue(args, 'capitalQid') or capital_qid(municipality_qid)
-- If user provided a QID to mark a place instead of inferred capital, an error should be reported if it can't be found.
local reportError = getParameterValue(args, 'capitalQid') and true or false
if not(mw.wikibase.isValidEntityId(qid)) or not(mw.wikibase.entityExists(qid)) then
if reportError then
error(L10n.error.noCoords, 0)
else
return nil
end
end
local coordStatements = mw.wikibase.getBestStatements(qid, 'P625')
if not coordStatements or #coordStatements == 0 then
if reportError then
error(L10n.error.wikidataCoords, 0)
else
return nil
end
end
local hasNoValue = ( coordStatements[1].mainsnak and coordStatements[1].mainsnak.snaktype == 'novalue' )
if hasNoValue then
if reportError then
error(L10n.error.wikidataCoords, 0)
else
return nil
end
end
local wdCoords = coordStatements[1]['mainsnak']['datavalue']['value']
return {[0] = tonumber(wdCoords['longitude']), [1] = tonumber(wdCoords['latitude'])}
end
function capital(args, municipality_qid)
local data = {}
local coords = capital_coords(args, municipality_qid)
if not coords then
return nil
end
data.type = "Feature"
data.geometry = {
type = "Point",
coordinates = coords
}
data.properties = {
title = getParameterValue(args, 'title') or mw.getCurrentFrame():getParent():getTitle()
}
data.properties["marker-symbol"] = getParameterValue(args, 'marker') or L10n.defaults.marker
data.properties["marker-color"] = getParameterValue(args, 'markerColor') or L10n.defaults.markerColor
data.properties["marker-size"] = getParameterValue(args, 'markerSize') or L10n.defaults.markerSize
return mw.text.jsonEncode(data)
end
function mapContent(args)
local content = {};
local municipality_qid = getParameterValue(args, 'municipalityQid') or mw.wikibase.getEntityIdForCurrentPage()
content[1] = county(args, municipality_qid)
content[2] = municipalities(args, municipality_qid)
if not isAffirmed(getParameterValue(args, 'hideCapital')) and capital(args, municipality_qid) then
content[3] = capital(args, municipality_qid)
end
local contentArray = '[\n' .. table.concat( content, ',\n') .. '\n]'
mw.logObject(contentArray)
return contentArray
end
function mapframecode(args)
local height = getParameterValue(args, 'frameHeight') or L10n.defaults.frameHeight
local width = getParameterValue(args, 'frameWidth') or L10n.defaults.frameWidth
local zoom = getParameterValue(args, 'zoom') or L10n.defaults.zoom
local text = getParameterValue(args, 'text') or L10n.defaults.text
return "<mapframe height=\"".. height .."\" zoom=\"".. zoom .."\" text=\"".. text .."\" width=\"".. width .."\">" .. mapContent(args) .."</mapframe>"
end
local p = {}
function p.main(frame)
local parent = frame.getParent(frame)
local args = trimArgs(parent.args)
mw.logObject(parent.args)
mw.logObject(args)
return frame:preprocess(mapframecode(args))
end
return p