Autopatrolled, New page reviewers, Rollbackers
2,168
edits
No edit summary |
No edit summary |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
require(' | require('strict') | ||
local p = {} | local p = {} | ||
Line 7: | Line 7: | ||
local function round(n, decimals) | local function round(n, decimals) | ||
local pow = 10^(decimals or 0) | local pow = 10^(decimals or 0) | ||
return math.floor(n * pow + 0.5) / pow | |||
end | end | ||
Line 102: | Line 103: | ||
-- Finds a parameter in a transclusion of {{Coord}}. | -- Finds a parameter in a transclusion of {{Coord}}. | ||
local function coord2text(para,coord) -- this should be changed for languages which do not use Arabic numerals or the degree sign | local function coord2text(para,coord) -- this should be changed for languages which do not use Arabic numerals or the degree sign | ||
local lat, long = mw.ustring.match(coord,'<span class="p%-latitude latitude">([^<]+)</span><span class="p%-longitude longitude">([^<]+)</span>') | |||
if lat then | |||
return tonumber(para == 'longitude' and long or lat) | |||
end | |||
local result = mw.text.split(mw.ustring.match(coord,'%-?[%.%d]+°[NS] %-?[%.%d]+°[EW]') or '', '[ °]') | local result = mw.text.split(mw.ustring.match(coord,'%-?[%.%d]+°[NS] %-?[%.%d]+°[EW]') or '', '[ °]') | ||
if para == 'longitude' then result = {result[3], result[4]} end | if para == 'longitude' then result = {result[3], result[4]} end | ||
if not tonumber(result[1]) or not result[2] then return error('Malformed coordinates value', 2) end | if not tonumber(result[1]) or not result[2] then | ||
mw.log('Malformed coordinates value') | |||
mw.logObject(para, 'para') | |||
mw.logObject(coord, 'coord') | |||
return error('Malformed coordinates value', 2) | |||
end | |||
return tonumber(result[1]) * hemisphereMultipliers[para][result[2]] | return tonumber(result[1]) * hemisphereMultipliers[para][result[2]] | ||
end | end | ||
Line 125: | Line 135: | ||
if args.AlternativeMap then | if args.AlternativeMap then | ||
return args.AlternativeMap | return args.AlternativeMap | ||
elseif args.relief | elseif args.relief then | ||
local digits = mw.ustring.match(args.relief,'^[1-9][0-9]?$') or '1' -- image1 to image99 | |||
if map('image' .. digits) ~= '' then | |||
return map('image' .. digits) | |||
end | |||
end | end | ||
return map('image') | |||
end | end | ||
Line 161: | Line 173: | ||
end | end | ||
end | end | ||
local retval = frame:extensionTag{name = 'templatestyles', args = {src = ' | local retval = frame:extensionTag{name = 'templatestyles', args = {src = 'Module:Location map/styles.css'}} | ||
if args.float == 'center' then | if args.float == 'center' then | ||
retval = retval .. '<div class="center">' | retval = retval .. '<div class="center">' | ||
end | end | ||
if args.caption and args.caption ~= '' and args.border ~= 'infobox' then | if args.caption and args.caption ~= '' and args.border ~= 'infobox' then | ||
retval = retval .. '<div class="locmap noviewer thumb ' | retval = retval .. '<div class="locmap noviewer noresize thumb ' | ||
if args.float == '"left"' or args.float == 'left' then | if args.float == '"left"' or args.float == 'left' then | ||
retval = retval .. 'tleft' | retval = retval .. 'tleft' | ||
Line 182: | Line 194: | ||
retval = retval .. '"><div style="position:relative;width:' .. width .. 'px' .. (args.border ~= 'none' and ';border:1px solid lightgray">' or '">') | retval = retval .. '"><div style="position:relative;width:' .. width .. 'px' .. (args.border ~= 'none' and ';border:1px solid lightgray">' or '">') | ||
else | else | ||
retval = retval .. '<div class="locmap" style="width:' .. width .. 'px;' | |||
if args.float == '"left"' or args.float == 'left' then | if args.float == '"left"' or args.float == 'left' then | ||
retval = retval .. 'float:left;clear:left' | retval = retval .. 'float:left;clear:left' | ||
Line 191: | Line 204: | ||
retval = retval .. 'float:right;clear:right' | retval = retval .. 'float:right;clear:right' | ||
end | end | ||
retval = retval .. '"><div style="width:' .. width .. 'px;padding:0"><div style="position:relative;width:' .. width .. 'px">' | |||
end | end | ||
local image = getContainerImage(args, map) | local image = getContainerImage(args, map) | ||
local currentTitle = mw.title.getCurrentTitle() | local currentTitle = mw.title.getCurrentTitle() | ||
retval = string.format( | retval = string.format( | ||
'%s[[File:%s|%spx|%s%s]]', | '%s[[File:%s|%spx|%s%s|class=notpageimage]]', | ||
retval, | retval, | ||
image, | image, | ||
Line 208: | Line 222: | ||
end | end | ||
if args.overlay_image then | if args.overlay_image then | ||
return retval .. '<div style="position:absolute;top:0;left:0">[[File:' .. args.overlay_image .. '|' .. width .. 'px]]</div>' | return retval .. '<div style="position:absolute;top:0;left:0">[[File:' .. args.overlay_image .. '|' .. width .. 'px|class=notpageimage]]</div>' | ||
else | else | ||
return retval | return retval | ||
Line 227: | Line 241: | ||
retval = retval .. '<div style="padding-top:0.2em">' | retval = retval .. '<div style="padding-top:0.2em">' | ||
else | else | ||
retval = retval .. '<div style="font-size: | retval = retval .. '<div style="font-size:91%;padding-top:3px">' | ||
end | end | ||
retval = retval | retval = retval | ||
Line 234: | Line 248: | ||
elseif args.caption ~= '' then | elseif args.caption ~= '' then | ||
-- This is not the pipe trick. We're creating a link with no text on purpose, so that CSS can give us a nice image | -- This is not the pipe trick. We're creating a link with no text on purpose, so that CSS can give us a nice image | ||
retval = retval .. '<div class="thumbcaption"><div class="magnify">[[:File:' .. getContainerImage(args, map) .. '| ]]</div>' .. args.caption .. '</div>' | retval = retval .. '<div class="thumbcaption"><div class="magnify">[[:File:' .. getContainerImage(args, map) .. '|class=notpageimage| ]]</div>' .. args.caption .. '</div>' | ||
end | end | ||
Line 277: | Line 291: | ||
end | end | ||
local function markOuterDiv(x, y, imageDiv, labelDiv) | local function markOuterDiv(x, y, imageDiv, labelDiv, label_size) | ||
return mw.html.create('div') | return mw.html.create('div') | ||
:addClass('od') | :addClass('od') | ||
:cssText('top:' .. round(y, 3) .. '%;left:' .. round(x, 3) .. '%') | :addClass('notheme') -- T236137 | ||
:cssText('top:' .. round(y, 3) .. '%;left:' .. round(x, 3) .. '%;font-size:' .. label_size .. '%') | |||
:node(imageDiv) | :node(imageDiv) | ||
:node(labelDiv) | :node(labelDiv) | ||
Line 292: | Line 307: | ||
if marksize ~= 0 then | if marksize ~= 0 then | ||
builder:wikitext(string.format( | builder:wikitext(string.format( | ||
'[[File:%s|%dx%dpx|%s|link=%s%s]]', | '[[File:%s|%dx%dpx|%s|link=%s%s|class=notpageimage]]', | ||
mark, | mark, | ||
marksize, | marksize, | ||
Line 309: | Line 324: | ||
end | end | ||
local builder = mw.html.create('div') | local builder = mw.html.create('div') | ||
:cssText(' | :cssText('width:' .. label_width .. 'em') | ||
local distance = round(marksize / 2 + 1) | local distance = round(marksize / 2 + 1) | ||
if position == 'top' then -- specified top | if position == 'top' then -- specified top | ||
Line 315: | Line 330: | ||
elseif position == 'bottom' then -- specified bottom | elseif position == 'bottom' then -- specified bottom | ||
builder:addClass('pv'):cssText('top:' .. distance .. 'px;left:' .. (-label_width / 2) .. 'em') | builder:addClass('pv'):cssText('top:' .. distance .. 'px;left:' .. (-label_width / 2) .. 'em') | ||
elseif position == 'left' or (tonumber(x) > 70 and position ~= 'right') then -- specified left or autodetected to left | |||
builder:addClass('pl'):cssText('right:' .. distance .. 'px') | |||
else -- specified right or autodetected to right | else -- specified right or autodetected to right | ||
builder:addClass('pr'):cssText('left:' .. distance .. 'px') | builder:addClass('pr'):cssText('left:' .. distance .. 'px') | ||
Line 324: | Line 341: | ||
end | end | ||
return builder:done() | return builder:done() | ||
end | |||
local function getX(longitude, left, right) | |||
local width = (right - left) % 360 | |||
if width == 0 then | |||
width = 360 | |||
end | |||
local distanceFromLeft = (longitude - left) % 360 | |||
-- the distance needed past the map to the right equals distanceFromLeft - width. the distance needed past the map to the left equals 360 - distanceFromLeft. to minimize page stretching, go whichever way is shorter | |||
if distanceFromLeft - width / 2 >= 180 then | |||
distanceFromLeft = distanceFromLeft - 360 | |||
end | |||
return 100 * distanceFromLeft / width | |||
end | |||
local function getY(latitude, top, bottom) | |||
return 100 * (top - latitude) / (top - bottom) | |||
end | end | ||
Line 375: | Line 409: | ||
-- error('Coordinates from [[Module:Coordinates]] and individual coordinates cannot both be provided') | -- error('Coordinates from [[Module:Coordinates]] and individual coordinates cannot both be provided') | ||
-- end | -- end | ||
longitude = coord2text('longitude', args.coordinates) | |||
latitude = coord2text('latitude', args.coordinates) | |||
elseif not longitude and not latitude and args.useWikidata then | |||
-- If they didn't provide either coordinate, try Wikidata. If they provided one but not the other, don't. | |||
local entity = mw.wikibase.getEntity() | |||
if entity and entity.claims and entity.claims.P625 and entity.claims.P625[1].mainsnak.snaktype == 'value' then | |||
local value = entity.claims.P625[1].mainsnak.datavalue.value | |||
longitude, latitude = value.longitude, value.latitude | |||
end | |||
if args.link and (currentTitle.namespace == 0) then | if args.link and (currentTitle.namespace == 0) then | ||
builder:wikitext('[[Category:Location maps with linked markers with coordinates from Wikidata]]') | builder:wikitext('[[Category:Location maps with linked markers with coordinates from Wikidata]]') | ||
end | end | ||
end | |||
if not longitude then | |||
error('No value was provided for longitude') | |||
elseif not latitude then | |||
error('No value was provided for latitude') | |||
end | end | ||
if currentTitle.namespace > 0 then | if currentTitle.namespace > 0 then | ||
Line 391: | Line 439: | ||
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Decimal]]') | builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Decimal]]') | ||
end | end | ||
end | |||
if ((tonumber(args.lat_deg) or 0) < 0) and ((tonumber(args.lat_min) or 0) ~= 0 or (tonumber(args.lat_sec) or 0) ~= 0 or (args.lat_dir and args.lat_dir ~='')) then | |||
builder:wikitext('[[Category:Location maps with negative degrees and minutes or seconds]]') | |||
end | |||
if ((tonumber(args.lon_deg) or 0) < 0) and ((tonumber(args.lon_min) or 0) ~= 0 or (tonumber(args.lon_sec) or 0) ~= 0 or (args.lon_dir and args.lon_dir ~= '')) then | |||
builder:wikitext('[[Category:Location maps with negative degrees and minutes or seconds]]') | |||
end | |||
if (((tonumber(args.lat_min) or 0) < 0) or ((tonumber(args.lat_sec) or 0) < 0)) then | |||
builder:wikitext('[[Category:Location maps with negative degrees and minutes or seconds]]') | |||
end | |||
if (((tonumber(args.lon_min) or 0) < 0) or ((tonumber(args.lon_sec) or 0) < 0)) then | |||
builder:wikitext('[[Category:Location maps with negative degrees and minutes or seconds]]') | |||
end | end | ||
if args.skew or args.lon_shift or args.markhigh then | if args.skew or args.lon_shift or args.markhigh then | ||
Line 406: | Line 466: | ||
end | end | ||
end | end | ||
if map('x') ~= '' then | |||
x = tonumber(mw.ext.ParserFunctions.expr(map('x', { latitude, longitude }))) | |||
else | |||
x = tonumber(getX(longitude, map('left'), map('right'))) | |||
end | |||
if map('y') ~= '' then | |||
y = tonumber(mw.ext.ParserFunctions.expr(map('y', { latitude, longitude }))) | |||
else | |||
y = tonumber(getY(latitude, map('top'), map('bottom'))) | |||
end | |||
if (x < 0 or x > 100 or y < 0 or y > 100) and not args.outside then | |||
mw.log('Mark placed outside map boundaries without outside flag set. x = ' .. x .. ', y = ' .. y) | |||
local parent = frame:getParent() | |||
if parent then | |||
mw.log('Parent is ' .. parent:getTitle()) | |||
end | |||
mw.logObject(args, 'args') | |||
if currentTitle.namespace == 0 then | if currentTitle.namespace == 0 then | ||
local key = currentTitle.prefixedText | local key = currentTitle.prefixedText | ||
builder:wikitext('[[Category:Location maps with marks outside map and outside parameter not set|' .. key .. ' ]]') | builder:wikitext('[[Category:Location maps with marks outside map and outside parameter not set|' .. key .. ' ]]') | ||
end | end | ||
end | |||
local mark = args.mark or map('mark') | local mark = args.mark or map('mark') | ||
if mark == '' then | if mark == '' then | ||
Line 416: | Line 494: | ||
local marksize = tonumber(args.marksize) or tonumber(map('marksize')) or 8 | local marksize = tonumber(args.marksize) or tonumber(map('marksize')) or 8 | ||
local imageDiv = markImageDiv(mark, marksize, args.label or mw.title.getCurrentTitle().text, args.link or '', args.alt, args[2]) | local imageDiv = markImageDiv(mark, marksize, args.label or mw.title.getCurrentTitle().text, args.link or '', args.alt, args[2]) | ||
local label_size = args.label_size or 91 | |||
local labelDiv | local labelDiv | ||
if args.label and args.position ~= 'none' then | if args.label and args.position ~= 'none' then | ||
labelDiv = markLabelDiv(args.label, | labelDiv = markLabelDiv(args.label, label_size, args.label_width or 6, args.position, args.background, x, marksize) | ||
end | end | ||
return builder:node(markOuterDiv(x, y, imageDiv, labelDiv)) | return builder:node(markOuterDiv(x, y, imageDiv, labelDiv, label_size)) | ||
end | end | ||