Module:Navigation with arrows

From International Robin Hood Bibliography
Revision as of 06:48, 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.


local p = {}
--[[ 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. If no valid value is supplied for 'Row1Lp' (etc.), that for 'Row1Cat1' is used instead. A valid value supplied for 'Row1LpSect' (etc.) is not used unless a valid value is supplied for 'Row1LpAlias' 
(etc.)
]]--
local util = require 'Module:Utilities'
-- local pFrame
local pageNameUc
local smwToExecute = ''

function p.createNavigation(frame)
	-- pageNameUc = util.ucSanitize(frame:getParent().args.PageName)
	mw.log(print(util.ucSanitize('King and subject tales')))
	
	for catIdx = 1, 6, 1 do
		local cat1 = 'Row' .. catIdx .. 'Cat1'
		if isValidParam( frame.getParent()[cat1] ) then
			smwToExecute = smwToExecute .. buildQuery(cat1, 'Row' .. catIdx .. 'Cat2', 'Row' .. catIdx .. 'Lp', 'Row' .. catIdx .. 'LpSect', 'Row' .. catIdx .. 'LpAlias')
		end
	end
	return frame:preprocess(smwToExecute)
end

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

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

function buildLandingPage(lp, cat1, lpSect, lpAlias)
	local landingPage = lp
	if not isValidParam(landingPage) then
		landingPage = cat1
	end
	if isValidParam(lpAlias) then
		if isValidParam(lpSect) then
			landingPage = landingPage .. '#' .. lpSect
		end
		landingPage = landingPage .. '|' .. lpAlias
	end
	return '[[' .. landingPage .. ']]'
end

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

return p