Moduł:Znajdź w pobliżu
Z Nonsensopedii, polskiej encyklopedii humoru
Moduł do gadżetu automatycznego sugerowania zobaczteżów na podstawie lokalizacji.
local tools = require('Moduł:Narzędzia')
local p = {}
-- FUNKCJE LOKALNE
local function getCoordsOfPage(title)
local result = mw.smw.ask{
'[[' .. title .. ']]',
'?Ma lokalizację',
mainlabel = '-'
}
if result then
return result[1]['Ma lokalizację']
end
return nil
end
-- FUNKCJE PUBLICZNE
function p.findToHtml(frame)
local args = tools.getArgs(frame)
local title = args['1']
if not title then
return 'Nie podano tytułu strony docelowej'
end
local extraParams = {}
local i = 2
while args[tostring(i)] do
extraParams[i-1] = args[tostring(i)]
i = i + 1
end
local minResults = args['min'] or 5
minResults = tonumber(minResults)
if minResults > 30 then minResults = 30 end
local wx = p.findToWikitext(title, extraParams, minResults)
return frame:preprocess(wx)
end
function p.findToWikitext(title, extraParams, minResults)
local locTitle = getCoordsOfPage(title)
if not locTitle then
return 'Nie znaleziono współrzędnych strony docelowej.'
end
local function buildQueryConds(kms)
return '[[Ma lokalizację::' .. locTitle .. ' (' .. kms .. ' km)]] [[!' ..
title .. ']] ' .. table.concat(extraParams, ' ')
end
local currentDistance = 0
local queryConds = ''
local results = {}
while #results < minResults and currentDistance < 150 do
results = {}
if currentDistance < 80 then
currentDistance = currentDistance + 5
else
currentDistance = currentDistance + 10
end
queryConds = buildQueryConds(currentDistance)
local smwResult = mw.smw.ask{
queryConds,
'?#-=page',
mainlabel = '-',
limit = 30
}
if smwResult then
for i, v in ipairs(smwResult) do results[i] = v.page end
end
end
local compound = '{{#compound_query:[[' .. title .. ']] ; ?Ma lokalizację ; icon=Red-marker.png |' ..
queryConds .. ' ; ?Ma lokalizację|format=leaflet|height=280}}'
local targetContent = mw.title.new(title):getContent()
local preDuplicates = '\n<pre>'
local pre = '\n<pre>'
for i, v in ipairs(results) do
local item = '\n* [[' .. v
-- pipe trick
if mw.ustring.find(v, '(', 1, true) then item = item .. '|' end
item = item .. ']]'
local pat = '%*%s-%[%[' .. tools.escapePattern(v) .. '[^%]]-%]%]'
if mw.ustring.find(targetContent, pat) then
preDuplicates = preDuplicates .. item
else
pre = pre .. item
end
end
local res = compound
if preDuplicates ~= '\n<pre>' then
res = res .. "'''Zobaczteże już obecne w artykule:'''" .. preDuplicates ..
'\n</pre>\n'
end
res = res .. "'''Nowe zobaczteże:'''" .. pre ..
'\n</pre>\nPromień wyszukiwania: ' .. currentDistance .. ' km'
return res
end
return p