Module:Navigation with arrows: Difference between revisions

From International Robin Hood Bibliography
mNo edit summary
mNo edit summary
Line 1: Line 1:
local p = {}
local p = {}
local util = require 'Module:Utilities'
local u = require 'Module:Utilities'
local pageName = nil
local pageName = nil
local pFrame
local pFrame
Line 9: Line 9:
pFrame = frame:getParent()
pFrame = frame:getParent()
local smwToExecute = ''
local smwToExecute = ''
pageName = getFrameParam('PageName')
pageName = u.getFrameParam('PageName', pFrame)
if isValidParam(pageName) then
if u.isValidParam(pageName) then
testing = setTestState()
testing = u.setTestState(pFrame)
pageName = util.ucSanitize(pageName)
pageName = u.ucSanitize(pageName)
local startIdx, maxNavLines, step = 1, 6, 1
local startIdx, maxNavLines, step = 1, 6, 1
for catIdx = startIdx, maxNavLines, step do
for catIdx = startIdx, maxNavLines, step do
local cat1, cat1Val  = 'Row' .. catIdx .. 'Cat1', nil
local cat1, cat1Val  = 'Row' .. catIdx .. 'Cat1', nil
cat1Val = getFrameCategoryParam(cat1)
cat1Val = u.getFrameCategoryParam(cat1, pFrame)
if isValidParam(cat1Val) then
if u.isValidParam(cat1Val) then
if catIdx > 1 then smwToExecute = smwToExecute .. '<br/>' end
if catIdx > 1 then smwToExecute = smwToExecute .. '<br/>' end
local cat2, cat2Val  = 'Row' .. catIdx .. 'Cat2', nil
local cat2, cat2Val  = 'Row' .. catIdx .. 'Cat2', nil
cat2Val = getFrameCategoryParam(cat2)
cat2Val = u.getFrameCategoryParam(cat2, pFrame)
smwToExecute = smwToExecute .. buildQuery( cat1Val, cat2Val, getFrameParam('Row' .. catIdx .. 'Lp'), getFrameParam('Row' .. catIdx .. 'LpSect'), getFrameParam('Row' .. catIdx .. 'LpAlias') )
smwToExecute = smwToExecute .. buildQuery( cat1Val, cat2Val, u.getFrameParam('Row' .. catIdx .. 'Lp', pFrame), u.getFrameParam('Row' .. catIdx .. 'LpSect', pFrame), u.getFrameParam('Row' .. catIdx .. 'LpAlias', pFrame) )
else break
else break
end
end
Line 33: Line 33:
function buildQuery(cat1Val, cat2Val, lp, lpSect, lpAlias)
function buildQuery(cat1Val, cat2Val, lp, lpSect, lpAlias)
local crit = '[[Category:' .. cat1Val .. ']]'
local crit = '[[Category:' .. cat1Val .. ']]'
if isValidParam(cat2Val) then crit = crit .. '[[Category:' .. cat2Val .. ']]' end
if u.isValidParam(cat2Val) then crit = crit .. '[[Category:' .. cat2Val .. ']]' end
return buildQueryLine(crit, '<<', 'descending', 'ItemPrevious') .. buildLandingPage(lp, cat1Val, lpSect, lpAlias) .. buildQueryLine(crit, '>>', 'ascending', 'ItemNext')
return buildQueryLine(crit, '<<', 'descending', 'ItemPrevious') .. buildLandingPage(lp, cat1Val, lpSect, lpAlias) .. buildQueryLine(crit, '>>', 'ascending', 'ItemNext')
end
end
Line 45: Line 45:
function buildLandingPage(lp, cat, lpSect, lpAlias)
function buildLandingPage(lp, cat, lpSect, lpAlias)
local landingPage
local landingPage
if isValidParam(lp) then landingPage = lp else landingPage = cat end
if u.isValidParam(lp) then landingPage = lp else landingPage = cat end
if isValidParam(lpSect) then landingPage = landingPage .. '#' .. lpSect
if u.isValidParam(lpSect) then landingPage = landingPage .. '#' .. lpSect
end
end
if isValidParam(lpSect) or isValidParam(lpAlias) then
if u.isValidParam(lpSect) or u.isValidParam(lpAlias) then
landingPage = landingPage .. '|'
landingPage = landingPage .. '|'
if isValidParam(lpAlias) then landingPage = landingPage .. lpAlias
if u.isValidParam(lpAlias) then landingPage = landingPage .. lpAlias
else landingPage = landingPage .. lp
else landingPage = landingPage .. lp
end
end
Line 60: Line 60:




function getFrameParam(paramName)
--[[function getFrameParam(paramName)
local paramVal = nil
local paramVal = nil
if isValidParam(paramName)  
if isValidParam(paramName)  
Line 93: Line 93:
return testState
return testState
end
end
 
--]]


return p
return p

Revision as of 17:24, 25 May 2022

The module expects template calls like {{NavigationArrows|Row1Cat1=|Row1Cat2=|Row1Lp=|Row1LpSect=|Row1LpAlias=}}, where only the first parameter is required to exist and hold a non-empty string. The parameter set |Row1Cat1=|Row1Cat2=|Row1Lp=|Row1LpSect=|Row1LpAlias= may be repeated up to five times with Row1Cat1 incremented to Row2Cat1, Row1Cat2 to Row2Cat2 etc., the highest meaningful set of parameters being Row6Cat1 etc. The landing page link and text are Row1Cat1 (etc.) unless a valid value is supplied for Row1LpSect (etc.), in which case a valid value for Row1LpAlias (etc.) takes precedence over any for Row1Cat1 (etc.) as text. For category names that are to be uppercased (and have any relevant entity reference normalized to a corresponding apostrophe), the template parameters Row1Cat1Uc and Row1Cat2Uc must be used instead of Row1Cat1 and Row1Cat2.


local p = {}
local u = require 'Module:Utilities'
local pageName = nil
local pFrame
local testing = false


function p.createNavigation(frame)
	pFrame = frame:getParent()
	local smwToExecute = ''
	pageName = u.getFrameParam('PageName', pFrame)
	if u.isValidParam(pageName) then
		testing = u.setTestState(pFrame)
		pageName = u.ucSanitize(pageName)
		local startIdx, maxNavLines, step = 1, 6, 1
		for catIdx = startIdx, maxNavLines, step do
			local cat1, cat1Val  = 'Row' .. catIdx .. 'Cat1', nil
			cat1Val = u.getFrameCategoryParam(cat1, pFrame)
			if u.isValidParam(cat1Val) then
				if catIdx > 1 then smwToExecute = smwToExecute .. '<br/>' end
				local cat2, cat2Val  = 'Row' .. catIdx .. 'Cat2', nil
				cat2Val = u.getFrameCategoryParam(cat2, pFrame)
				smwToExecute = smwToExecute .. buildQuery( cat1Val, cat2Val, u.getFrameParam('Row' .. catIdx .. 'Lp', pFrame), u.getFrameParam('Row' .. catIdx .. 'LpSect', pFrame), u.getFrameParam('Row' .. catIdx .. 'LpAlias', pFrame) )
				else break
			end
		end
	end
	if not testing then return frame:preprocess(smwToExecute)
	else return '<nowiki>' .. smwToExecute .. '</nowiki>' end
end


function buildQuery(cat1Val, cat2Val, lp, lpSect, lpAlias)
	local crit = '[[Category:' .. cat1Val .. ']]'
	if u.isValidParam(cat2Val) then crit = crit .. '[[Category:' .. cat2Val .. ']]' end
	return buildQueryLine(crit, '<<', 'descending', 'ItemPrevious') .. buildLandingPage(lp, cat1Val, lpSect, lpAlias) .. buildQueryLine(crit, '>>', 'ascending', 'ItemNext')
end


function buildQueryLine(criteria, directionOperator, direction, outroTemplate)
	return '{{#ask:' .. criteria .. '[[Utitle::' .. directionOperator .. pageName .. ']]|order=' .. direction .. '|searchlabel=|format=template|introtemplate=' .. outroTemplate .. 'Prefix|template=ItemPrint|outrotemplate=' .. outroTemplate .. '|link=none|limit=1|sort=Utitle}}'
end


function buildLandingPage(lp, cat, lpSect, lpAlias)
	local landingPage
	if u.isValidParam(lp) then landingPage = lp else landingPage = cat end
	
	if u.isValidParam(lpSect) then landingPage = landingPage .. '#' .. lpSect
	end
	
	if u.isValidParam(lpSect) or u.isValidParam(lpAlias) then
		landingPage = landingPage .. '|'
		if u.isValidParam(lpAlias) then landingPage = landingPage .. lpAlias
			else landingPage = landingPage .. lp
		end
	end
 	return '[[' .. landingPage .. ']]'
end


--[[function getFrameParam(paramName)
	local paramVal = nil
	if isValidParam(paramName) 
		then paramVal = pFrame.args[paramName] 
			if paramVal == '' then paramVal = nil end
	end
	return paramVal
end


function getFrameCategoryParam(catName)
	local val = getFrameParam(catName)
	if not isValidParam(val) then 
		val = getFrameParam(catName .. 'Uc')
		if val then val = util.ucSanitize(val) end
	end
	return val
end


function isValidParam(param)
	return param ~= nil and param ~= ''
end


function setTestState()
	local test = getFrameParam('Test')
	local testState = false
	if isValidParam(test) and test == 'true' then
		testState = true
	end
	return testState
end
--]]

return p