Difference between revisions of "Module:HonorUtils"

From Pathfinder Wiki
Line 6: Line 6:
 
landingPage = frame:expandTemplate{ title='GetHonorLandingPage' }
 
landingPage = frame:expandTemplate{ title='GetHonorLandingPage' }
 
end
 
end
local tVariants = {}
+
local srcIsHonor = false
tVariants['answers'] = {}
+
local tVariants = {
tVariants['answers']['orig'] = getHonorVariants( 'answer', landingPage, false )
+
answers = getHonorVariants( 'answer', landingPage, srcIsHonor ),
tVariants['answers']['split'] = mw.text.split(tVariants['answers']['orig'], '|', notUseLUAPattern)
+
requirements = getHonorVariants( 'requirement', landingPage, srcIsHonor )
tVariants['requirements'] = {}
+
}
tVariants['requirements']['orig'] = getHonorVariants( 'requirement', landingPage, false )
 
tVariants['requirements']['split']  = mw.text.split(tVariants['requirements']['orig'], '|', notUseLUAPattern)
 
 
 
 
tVariants = p.expandHonorVariantsV2( frame, tVariants )
 
tVariants = p.expandHonorVariantsV2( frame, tVariants )
Line 26: Line 24:
 
return {}
 
return {}
 
end
 
end
if ( tVariants['answers'] == nil or tVariants['answers']['split'] == nil ) then
+
if ( tVariants['answers'] == nil ) then
 
mw.log('HonorUtils.expandHonorVariantsV2: No Answer Variants to Expand!')
 
mw.log('HonorUtils.expandHonorVariantsV2: No Answer Variants to Expand!')
 
else
 
else
 
-- expand the answer Variants
 
-- expand the answer Variants
local tAnswers = tVariants['answers']['split']
+
local tAnswers = tVariants['answers']
 
mw.log('HonorUtils.expandHonorVariantsV2: There are '..#tAnswers..' Answer Variants to Expand!')
 
mw.log('HonorUtils.expandHonorVariantsV2: There are '..#tAnswers..' Answer Variants to Expand!')
 
for key, variant in pairs(tAnswers) do
 
for key, variant in pairs(tAnswers) do
Line 55: Line 53:
 
tAnswers[key] = tActVariant
 
tAnswers[key] = tActVariant
 
end
 
end
tVariants['answers']['split'] = tAnswers
+
tVariants['answers'] = tAnswers
mw.log('HonorUtils.expandHonorVariantsV2: '..#tVariants['answers']['split']..' Answer Variants expanded!')
+
mw.log('HonorUtils.expandHonorVariantsV2: '..#tVariants['answers']..' Answer Variants expanded!')
 
end
 
end
if ( tVariants['requirements'] == nil or tVariants['requirements']['split'] == nil ) then
+
if ( tVariants['requirements'] == nil ) then
 
mw.log('HonorUtils.expandHonorVariantsV2: No Requirement Variants to Expand!')
 
mw.log('HonorUtils.expandHonorVariantsV2: No Requirement Variants to Expand!')
 
else
 
else
 
-- expand the requirement Variants
 
-- expand the requirement Variants
local tRequirements = tVariants['requirements']['split']
+
local tRequirements = tVariants['requirements']
 
mw.log('HonorUtils.expandHonorVariantsV2: There are '..#tRequirements..' Requirement Variants to Expand!')
 
mw.log('HonorUtils.expandHonorVariantsV2: There are '..#tRequirements..' Requirement Variants to Expand!')
 
for key, variant in pairs(tRequirements) do
 
for key, variant in pairs(tRequirements) do
Line 74: Line 72:
 
tRequirements[key] = tActReqVariant
 
tRequirements[key] = tActReqVariant
 
end
 
end
tVariants['requirements']['split'] = tRequirements
+
tVariants['requirements'] = tRequirements
mw.log('HonorUtils.expandHonorVariantsV2: '..#tVariants['requirements']['split']..' Requirement Variants expanded!')
+
mw.log('HonorUtils.expandHonorVariantsV2: '..#tVariants['requirements']..' Requirement Variants expanded!')
 
end
 
end
 
return tRetVariants
 
return tRetVariants
Line 120: Line 118:
 
mw.log('HonorUtils.translateHonorVariantsV2: There are no Variants (nil)!')
 
mw.log('HonorUtils.translateHonorVariantsV2: There are no Variants (nil)!')
 
end
 
end
if ( tRetVariants['answers'] == nil or tRetVariants['answers']['split'] == nil) then
+
if ( tRetVariants['answers'] == nil ) then
 
mw.log('HonorUtils.translateHonorVariantsV2: There are no Answer Variants!')
 
mw.log('HonorUtils.translateHonorVariantsV2: There are no Answer Variants!')
 
else
 
else
mw.log('HonorUtils.translateHonorVariantsV2: There are '..#tRetVariants['answers']['split']..' Answer Variants to translate!')
+
mw.log('HonorUtils.translateHonorVariantsV2: There are '..#tRetVariants['answers']..' Answer Variants to translate!')
for varKey, variant in pairs(tRetVariants['answers']['split']) do
+
for varKey, variant in pairs(tRetVariants['answers']) do
 
if ( variant == nil or variant['countries'] == nil or variant['countries']['split'] == nil ) then
 
if ( variant == nil or variant['countries'] == nil or variant['countries']['split'] == nil ) then
 
mw.log('HonorUtils.translateHonorVariantsV2: Answer Variant is invalid!')
 
mw.log('HonorUtils.translateHonorVariantsV2: Answer Variant is invalid!')
Line 131: Line 129:
 
for countryKey, country in pairs(variant['countries']['split']) do
 
for countryKey, country in pairs(variant['countries']['split']) do
 
local trans = frame:expandTemplate{ title = 'Localize', args = { country, 'Answer Variants' } }
 
local trans = frame:expandTemplate{ title = 'Localize', args = { country, 'Answer Variants' } }
tRetVariants['answers']['split'][varKey]['countries']['split'][countryKey] = {
+
tRetVariants['answers'][varKey]['countries']['split'][countryKey] = {
 
orig = country,
 
orig = country,
 
translated = trans
 
translated = trans
 
}
 
}
 
end
 
end
mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['answers']['split'][varKey]['countries']..' Answer Variant Countries were translated!')
+
mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['answers'][varKey]['countries']..' Answer Variant Countries were translated!')
 
end
 
end
 
if ( variant['authority'] == nil ) then
 
if ( variant['authority'] == nil ) then
Line 143: Line 141:
 
mw.log('HonorUtils.translateHonorVariantsV2: There is a Answer Variant Authority to translate!')
 
mw.log('HonorUtils.translateHonorVariantsV2: There is a Answer Variant Authority to translate!')
 
local trans = frame:expandTemplate{ title = 'Localize', args = { variant['authority'], 'Authorities' } }
 
local trans = frame:expandTemplate{ title = 'Localize', args = { variant['authority'], 'Authorities' } }
tRetVariants['answers']['split'][varKey]['authority'] = {
+
tRetVariants['answers'][varKey]['authority'] = {
 
orig = variant['authority'],
 
orig = variant['authority'],
 
translated = trans
 
translated = trans
Line 150: Line 148:
 
end
 
end
 
end
 
end
mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['answers']['split']..' Answer Variants were translated!')
+
mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['answers']..' Answer Variants were translated!')
 
end
 
end
if ( tRetVariants['requirements'] == nil or tRetVariants['requirements']['split'] == nil) then
+
if ( tRetVariants['requirements'] == nil ) then
 
mw.log('HonorUtils.translateHonorVariantsV2: There are no Requirement Variants!')
 
mw.log('HonorUtils.translateHonorVariantsV2: There are no Requirement Variants!')
 
else
 
else
mw.log('HonorUtils.translateHonorVariantsV2: There are '..#tRetVariants['requirements']['split']..' Requriement Variants to translate!')
+
mw.log('HonorUtils.translateHonorVariantsV2: There are '..#tRetVariants['requirements']..' Requriement Variants to translate!')
for varKey, variant in pairs(tRetVariants['requirements']['split']) do
+
for varKey, variant in pairs(tRetVariants['requirements']) do
 
if ( variant == nil or variant['authorities'] == nil ) then
 
if ( variant == nil or variant['authorities'] == nil ) then
 
mw.log('HonorUtils.translateHonorVariantsV2: Requriement Variant is invalid!')
 
mw.log('HonorUtils.translateHonorVariantsV2: Requriement Variant is invalid!')
Line 163: Line 161:
 
for authorityKey, authority in pairs(variant['authorities']) do
 
for authorityKey, authority in pairs(variant['authorities']) do
 
local trans = frame:expandTemplate{ title = 'Localize', args = { authority, 'Authorities' } }
 
local trans = frame:expandTemplate{ title = 'Localize', args = { authority, 'Authorities' } }
tRetVariants['requirements']['split'][varKey]['authorities'][authorityKey] = {
+
tRetVariants['requirements'][varKey]['authorities'][authorityKey] = {
 
orig = authority,
 
orig = authority,
 
translated = trans
 
translated = trans
 
}
 
}
 
end
 
end
mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['requirements']['split'][varKey]['authorities']..' Requriement Variant Authorities were translated!')
+
mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['requirements'][varKey]['authorities']..' Requriement Variant Authorities were translated!')
 
end
 
end
 
end
 
end
mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['answers']['split']..' Requriement Variants were translated!')
+
mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['requirements']..' Requriement Variants were translated!')
 
end
 
end
 
return tRetVariants
 
return tRetVariants
Line 215: Line 213:
 
if (cntOfVariants == nil or cntOfVariants == 0) then
 
if (cntOfVariants == nil or cntOfVariants == 0) then
 
mw.log('HonorUtils.getHonorVariants: There are no '..variantType..' variants!')
 
mw.log('HonorUtils.getHonorVariants: There are no '..variantType..' variants!')
return ''
+
return nil
 
end
 
end
 
mw.log('HonorUtils.getHonorVariants (cntOfVariants): '..cntOfVariants)
 
mw.log('HonorUtils.getHonorVariants (cntOfVariants): '..cntOfVariants)
Line 229: Line 227:
 
strVariant = mw.ustring.gsub(strVariant,"[\n\r]", "")
 
strVariant = mw.ustring.gsub(strVariant,"[\n\r]", "")
 
mw.log('HonorUtils.getHonorVariants (strVariant): '..strVariant)
 
mw.log('HonorUtils.getHonorVariants (strVariant): '..strVariant)
return strVariant
+
tVariants = mw.text.split(strVariant, '|', notUseLUAPattern)
 +
mw.log('HonorUtils.getHonorVariants (#tVariants): '..#tVariants)
 +
return tVariants
 
end
 
end
  

Revision as of 23:23, 10 July 2021

Documentation for this module may be created at Module:HonorUtils/doc

local p = {}
local notUseLUAPattern = true

function p.getExpandedHonorVariantsV2( frame, landingPage )
	if ( landingPage == nil or landingPage == "" ) then
		landingPage = frame:expandTemplate{ title='GetHonorLandingPage' }
	end
	local srcIsHonor = false
	local tVariants = {
		answers = getHonorVariants( 'answer', landingPage, srcIsHonor ),
		requirements = getHonorVariants( 'requirement', landingPage, srcIsHonor )
	}
	
	tVariants = p.expandHonorVariantsV2( frame, tVariants )
	
	tVariants = p.translateHonorVariantsV2( frame, tVariants )
	return tVariants
end

function p.expandHonorVariantsV2( frame, tVariants )
	local tRetVariants = tVariants
	if ( tVariants == nil ) then
		mw.log('HonorUtils.expandHonorVariantsV2: No Variants to Expand (nil)!')
		return {}
	end
	if ( tVariants['answers'] == nil ) then
		mw.log('HonorUtils.expandHonorVariantsV2: No Answer Variants to Expand!')
	else
		-- expand the answer Variants
		local tAnswers = tVariants['answers']
		mw.log('HonorUtils.expandHonorVariantsV2: There are '..#tAnswers..' Answer Variants to Expand!')
		for key, variant in pairs(tAnswers) do
			mw.log('HonorUtils.expandHonorVariantsV2 (key, variant): '..key..', '..variant)
			-- seperate Countries and Authority
			local tSplitAnsVariant = mw.text.split(variant, ';;', notUseLUAPattern)
			local tActVariant = {
				orig = variant,
				countries = {}
			}
			tActVariant['countries']['orig'] = tSplitAnsVariant[1]
			-- seperate the Countries
			tActVariant['countries']['split'] = mw.text.split(tActVariant['countries']['orig'], ';', notUseLUAPattern)
			mw.log('HonorUtils.expandHonorVariantsV2 (Countries - Orig): '..tActVariant['countries']['orig'])
			-- set the authority
			if ( tSplitAnsVariant[2] == nil ) then
				tActVariant['authority'] = nil
				mw.log('HonorUtils.expandHonorVariantsV2 (Authority): nil')
			else
				tActVariant['authority'] = tSplitAnsVariant[2]
				mw.log('HonorUtils.expandHonorVariantsV2 (Authority): '..tActVariant['authority'])
			end
			-- save act. Variant into the table
			tAnswers[key] = tActVariant
		end
		tVariants['answers'] = tAnswers
		mw.log('HonorUtils.expandHonorVariantsV2: '..#tVariants['answers']..' Answer Variants expanded!')
	end
	if ( tVariants['requirements'] == nil ) then
		mw.log('HonorUtils.expandHonorVariantsV2: No Requirement Variants to Expand!')
	else
		-- expand the requirement Variants
		local tRequirements = tVariants['requirements']
		mw.log('HonorUtils.expandHonorVariantsV2: There are '..#tRequirements..' Requirement Variants to Expand!')
		for key, variant in pairs(tRequirements) do
			mw.log('HonorUtils.expandHonorVariantsV2 (key, variant): '..key..', '..variant)
			-- seperate Authorities
			local tSplitReqVariant = mw.text.split(variant, ';', notUseLUAPattern)
			local tActReqVariant = {
				orig = variant,
				authorities = tSplitReqVariant
			}
			tRequirements[key] = tActReqVariant
		end
		tVariants['requirements'] = tRequirements
		mw.log('HonorUtils.expandHonorVariantsV2: '..#tVariants['requirements']..' Requirement Variants expanded!')
	end
	return tRetVariants
end

function p.getExpandedHonorVariants(frame, variantsString, landingPage, targetPage)
	-- check the input variables
	if (frame == nil or landingPage == nil or landingPage == "" or targetPage == nil or targetPage == "") then
		return "fatal error"
	end
	if (variantsString == nil or variantsString == "") then
		local linkTitle = frame:expandTemplate{ title = 'Localize', args = { targetPage } }
		return {
			{
				url = "[[" .. landingPage .. "/" .. targetPage .. frame:expandTemplate{title='GetLangSuffix'} .. "|" .. linkTitle .. "]]"
			}
		}
	end
	
	local t_variants = p.expandHonorVariants(frame, variantsString)
	t_variants = p.translateHonorVariants(frame, t_variants)
	t_variants = p.createHonorVariantsURLs(frame, t_variants, landingPage, targetPage)
	return t_variants
end

function p.expandHonorVariants(frame, variantsString)
	if (variantsString == nil or variantsString == "") then return {} end
	
	local t_variants = mw.text.split( variantsString, ';;', notUseLUAPattern )
	
	for key, variant in pairs(t_variants) do
		t_variants[key] = {
			orig = variant,
			countries = mw.text.split( variant, ';', notUseLUAPattern )
		}
	end
	
	return t_variants
end

function p.translateHonorVariantsV2( frame, tVariants )
	local tRetVariants = tVariants
	if ( tRetVariants == nil ) then
		mw.log('HonorUtils.translateHonorVariantsV2: There are no Variants (nil)!')
	end
	if ( tRetVariants['answers'] == nil ) then
		mw.log('HonorUtils.translateHonorVariantsV2: There are no Answer Variants!')
	else
		mw.log('HonorUtils.translateHonorVariantsV2: There are '..#tRetVariants['answers']..' Answer Variants to translate!')
		for varKey, variant in pairs(tRetVariants['answers']) do
			if ( variant == nil or variant['countries'] == nil or variant['countries']['split'] == nil ) then
				mw.log('HonorUtils.translateHonorVariantsV2: Answer Variant is invalid!')
			else
				mw.log('HonorUtils.translateHonorVariantsV2: There are '..#variant['countries']['split']..' Answer Variant Countries to translate!')
				for countryKey, country in pairs(variant['countries']['split']) do
					local trans = frame:expandTemplate{ title = 'Localize', args = { country, 'Answer Variants' } }
					tRetVariants['answers'][varKey]['countries']['split'][countryKey] = {
						orig = country,
						translated = trans
					}
				end
				mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['answers'][varKey]['countries']..' Answer Variant Countries were translated!')
			end
			if ( variant['authority'] == nil ) then
				mw.log('HonorUtils.translateHonorVariantsV2: Answer Variant has no authority set!')
			else
				mw.log('HonorUtils.translateHonorVariantsV2: There is a Answer Variant Authority to translate!')
				local trans = frame:expandTemplate{ title = 'Localize', args = { variant['authority'], 'Authorities' } }
				tRetVariants['answers'][varKey]['authority'] = {
					orig = variant['authority'],
					translated = trans
				}
				mw.log('HonorUtils.translateHonorVariantsV2: Answer Variant Authority got translated!')
			end
		end
		mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['answers']..' Answer Variants were translated!')
	end
	if ( tRetVariants['requirements'] == nil ) then
		mw.log('HonorUtils.translateHonorVariantsV2: There are no Requirement Variants!')
	else
		mw.log('HonorUtils.translateHonorVariantsV2: There are '..#tRetVariants['requirements']..' Requriement Variants to translate!')
		for varKey, variant in pairs(tRetVariants['requirements']) do
			if ( variant == nil or variant['authorities'] == nil ) then
				mw.log('HonorUtils.translateHonorVariantsV2: Requriement Variant is invalid!')
			else
				mw.log('HonorUtils.translateHonorVariantsV2: There are '..#variant['authorities']..' Requriement Variant Authorities to translate!')
				for authorityKey, authority in pairs(variant['authorities']) do
					local trans = frame:expandTemplate{ title = 'Localize', args = { authority, 'Authorities' } }
					tRetVariants['requirements'][varKey]['authorities'][authorityKey] = {
						orig = authority,
						translated = trans
					}
				end
				mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['requirements'][varKey]['authorities']..' Requriement Variant Authorities were translated!')
			end
		end
		mw.log('HonorUtils.translateHonorVariantsV2: '..#tRetVariants['requirements']..' Requriement Variants were translated!')
	end
	return tRetVariants
end

function p.translateHonorVariants(frame, variants)
	local ret_t_variants = variants
	for varKey, variant in pairs(variants) do
		for countryKey, country in pairs(variant['countries']) do
			local trans = frame:expandTemplate{ title = 'Localize', args = { country, 'Answer Variants' } }
			local t_countryTrans = {
				orig = country,
				translation = trans
			}
			ret_t_variants[varKey]['countries'][countryKey] = t_countryTrans
		end
	end
	return ret_t_variants
end

function p.createHonorVariantsURLs(frame, variants, landingPage, targetPage)
	local t_variants = variants
	local langSuffix = frame:expandTemplate{title='GetLangSuffix'}
	for key, variant in pairs(variants) do
		local linkTitle = ""
		for countryKey, country in pairs(variant['countries']) do
			if (#linkTitle > 0) then
				linkTitle = linkTitle .. " & " .. country['translation']
			else
				linkTitle = country['translation']
			end
		end
		t_variants[key]['url'] ="[[" .. landingPage .. "_(" .. variant['orig'] .. ")/" .. targetPage .. langSuffix .. "|" .. linkTitle .. "]]"
	end
	return t_variants
end

function getHonorVariants(variantType, src, srcIsHonor)
	if (variantType == nil or variantType == '') then
		variantType = 'requirement'
	end
	local cntOfVariants = tonumber(p.getHonorMetadata(src, 'cnt_'..variantType..'_variants', srcIsHonor))
	if (cntOfVariants == nil or cntOfVariants == 0) then
		mw.log('HonorUtils.getHonorVariants: There are no '..variantType..' variants!')
		return nil
	end
	mw.log('HonorUtils.getHonorVariants (cntOfVariants): '..cntOfVariants)
	local actVariant = 0
	local tVariantArguments = {}
	for actVariant = 1, cntOfVariants, 1 do
		mw.log('HonorUtils.getHonorVariants (actVariant): '..actVariant)
		tVariantArguments[actVariant] = variantType .. '_variant_' .. actVariant
	end
	local strVariantArguments = mw.text.listToText(tVariantArguments, ':', ':')
	mw.log('HonorUtils.getHonorVariants (strVariantArguments): '..strVariantArguments)
	local strVariant = p.getHonorMetadata(src, strVariantArguments, srcIsHonor)
	strVariant = mw.ustring.gsub(strVariant,"[\n\r]", "")
	mw.log('HonorUtils.getHonorVariants (strVariant): '..strVariant)
	tVariants = mw.text.split(strVariant, '|', notUseLUAPattern)
	mw.log('HonorUtils.getHonorVariants (#tVariants): '..#tVariants)
	return tVariants
end

function p.getHonorMetadata(src, metadata, srcIsHonor)
	local frame = mw.getCurrentFrame()
	local lPage = ''
	if (src ~= '' and src ~= nil) then
		if (srcIsHonor == true) then
			lPage = 'AY Honors/' .. src
		else
			lPage = src
		end
	end
	lPage = frame:expandTemplate{ title = 'GetHonorLandingPage', args = {page = lPage}}
	return frame:preprocess( '{{#dpl:|debug=0|title=' .. lPage .. '|include={HonorLanding}:' .. metadata ..'}}' )
end

return p