Module:Navigation with arrows

From International Robin Hood Bibliography
Revision as of 18:39, 20 May 2022 by Henryfunk (talk | contribs)

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.


--[[
Expects template call like '{{NavigationArrows|Row1Cat1=|Row1Cat2=|Row1Lp=|Row1LpSect=|Row1LpAlias=}}' where only the first parameter is required to exist and have a value other than the 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 '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 util = require 'Module:Utilities'
local pageName = nil
local pFrame
local bra, ket, testing = '[[', ']]', false
if testing then
	bra = 'BRA'
	ket = 'KET'
end


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


function buildQuery(cat1Val, cat2Val, lp, lpSect, lpAlias)
	local crit = bra .. 'Category:' .. cat1Val .. ket
	if isValidParam(cat2Val) then crit = crit .. bra .. 'Category:' .. cat2Val .. ket 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 .. bra .. 'Utitle::' .. directionOperator .. pageName .. ket .. '|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 isValidParam(lp) then landingPage = lp else landingPage = cat end
	
	if isValidParam(lpSect) then landingPage = landingPage .. '#' .. lpSect
	end
	
	if isValidParam(lpSect) or isValidParam(lpAlias) then
		landingPage = landingPage .. '|'
		if isValidParam(lpAlias) then landingPage = landingPage .. lpAlias
			else landingPage = landingPage .. lp
		end
	end
 	return bra .. landingPage .. ket
end


function getFrameParam(catName)
	local catVal = nil
	if isValidParam(catName) 
		then catVal = pFrame.args[catName] 
			if catVal == '' then catVal = nil end
	end
	return catVal
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')
	return isValidParam(test) and test == 'true'
end


return p