Module:Navigation with arrows: Difference between revisions

From International Robin Hood Bibliography
mNo edit summary
mNo edit summary
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
--[[
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 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.
local util = require 'Module:Utilities'
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
local pageName = nil
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'
local pFrame
(etc.)
local bra, ket, testing = '[[', ']]', false
]]--
 
local utilCat = require 'Module:UtilCategories'
-- local pFrame
local pageNameUc
local smwToExecute = ''


function p.createNavigation(frame)
function p.createNavigation(frame)
-- pageNameUc = utilCat.ucSanitize(frame:getParent().args.PageName)
pFrame = frame:getParent()
mw.log(print(utilCat.ucSanitize('King and subject tales')))
local smwToExecute = ''
pageName = getFrameParam('PageName')
for catIdx = 1, 6, 1 do
if isValidParam(pageName) then
local cat1 = 'Row' .. catIdx .. 'Cat1'
testing = setTestState()
if isValidParam( frame.getParent()[cat1] ) then
pageName = util.ucSanitize(pageName)
smwToExecute = smwToExecute .. buildQuery(cat1, 'Row' .. catIdx .. 'Cat2', 'Row' .. catIdx .. 'Lp', 'Row' .. catIdx .. 'LpSect', 'Row' .. catIdx .. 'LpAlias')
local startIdx, maxNavLines, step = 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 = smwToExecute .. '<br/>' 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
end
end
return frame:preprocess(smwToExecute)
if not testing then return frame:preprocess(smwToExecute)
else return smwToExecute end
end
end


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


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


function buildLandingPage(lp, cat1, lpSect, lpAlias)
 
local landingPage = lp
function buildLandingPage(lp, cat, lpSect, lpAlias)
if not isValidParam(landingPage) then
local landingPage
landingPage = cat1
if isValidParam(lp) then landingPage = lp else landingPage = cat end
if isValidParam(lpSect) then landingPage = landingPage .. '#' .. lpSect
end
end
if isValidParam(lpAlias) then
if isValidParam(lpSect) then
if isValidParam(lpSect) or isValidParam(lpAlias) then
landingPage = landingPage .. '#' .. lpSect
landingPage = landingPage .. '|'
if isValidParam(lpAlias) then landingPage = landingPage .. lpAlias
else landingPage = landingPage .. lp
end
end
landingPage = landingPage .. '|' .. lpAlias
end
end
return '[[' .. landingPage .. ']]'
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
end


function isValidParam(param)
function isValidParam(param)
return param ~= nil and param ~= ''
return param ~= nil and param ~= ''
end
end
function setTestState()
local test = getFrameParam('Test')
local testState = false
if isValidParam(test) and test == 'true' then
bra = 'BRABRA'
ket = 'KETKET'
testState = true
end
return testState
end


return p
return p

Revision as of 23:22, 20 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.


--[[
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


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 = 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 = smwToExecute .. '<br/>' 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')
	local testState = false
	if 	isValidParam(test) and test == 'true' then
		bra = 'BRABRA'
		ket = 'KETKET'
		testState = true
	end
	return testState
end


return p