Difference between revisions of "Module:HonorUtils"
From Pathfinder Wiki
DesignerThan (talk | contribs) (cleaned up the module) |
DesignerThan (talk | contribs) (rename function's after the cleanup) |
||
Line 23: | Line 23: | ||
end | end | ||
− | function p. | + | function p.getVariantsData( frame, landingPage ) |
if ( frame == nil ) then | if ( frame == nil ) then | ||
frame = mw.getCurrentFrame() | frame = mw.getCurrentFrame() | ||
Line 72: | Line 72: | ||
} | } | ||
− | tVariants = p. | + | tVariants = p.expandHonorVariants( frame, tVariants ) |
− | tVariants = p. | + | tVariants = p.translateHonorVariants( frame, tVariants ) |
return tVariants | return tVariants | ||
end | end | ||
− | function p. | + | function p.expandHonorVariants( frame, tVariants ) |
local tRetVariants = tVariants | local tRetVariants = tVariants | ||
if ( tVariants == nil ) then | if ( tVariants == nil ) then | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants: No Variants to Expand (nil)!') |
return {} | return {} | ||
end | end | ||
if ( tVariants['answers'] == nil ) then | if ( tVariants['answers'] == nil ) then | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants: No Answer Variants to Expand!') |
else | else | ||
-- expand the answer Variants | -- expand the answer Variants | ||
local tAnswers = tVariants['answers'] | local tAnswers = tVariants['answers'] | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants: There are '..#tAnswers..' Answer Variants to Expand!') |
for key, variant in pairs(tAnswers) do | for key, variant in pairs(tAnswers) do | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants (key, variant): '..key..', '..variant) |
-- seperate Countries and Authority | -- seperate Countries and Authority | ||
local tSplitAnsVariant = mw.text.split(variant, ';;', notUseLUAPattern) | local tSplitAnsVariant = mw.text.split(variant, ';;', notUseLUAPattern) | ||
Line 102: | Line 102: | ||
-- seperate the Countries | -- seperate the Countries | ||
tActVariant['countries']['split'] = mw.text.split(tActVariant['countries']['orig'], ';', notUseLUAPattern) | tActVariant['countries']['split'] = mw.text.split(tActVariant['countries']['orig'], ';', notUseLUAPattern) | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants (Countries - Orig): '..tActVariant['countries']['orig']) |
else | else | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants: There are no countries for that variant!') |
end | end | ||
-- set the authority | -- set the authority | ||
if ( tSplitAnsVariant[2] == nil ) then | if ( tSplitAnsVariant[2] == nil ) then | ||
tActVariant['authority'] = nil | tActVariant['authority'] = nil | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants (Authority): nil') |
else | else | ||
tActVariant['authority'] = tSplitAnsVariant[2] | tActVariant['authority'] = tSplitAnsVariant[2] | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants (Authority): '..tActVariant['authority']) |
end | end | ||
-- save act. Variant into the table | -- save act. Variant into the table | ||
Line 118: | Line 118: | ||
end | end | ||
tVariants['answers'] = tAnswers | tVariants['answers'] = tAnswers | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants: '..#tVariants['answers']..' Answer Variants expanded!') |
end | end | ||
if ( tVariants['requirements'] == nil ) then | if ( tVariants['requirements'] == nil ) then | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants: No Requirement Variants to Expand!') |
else | else | ||
-- expand the requirement Variants | -- expand the requirement Variants | ||
local tRequirements = tVariants['requirements'] | local tRequirements = tVariants['requirements'] | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants: There are '..#tRequirements..' Requirement Variants to Expand!') |
for key, variant in pairs(tRequirements) do | for key, variant in pairs(tRequirements) do | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants (key, variant): '..key..', '..variant) |
-- seperate Authorities | -- seperate Authorities | ||
local tSplitReqVariant = mw.text.split(variant, ';', notUseLUAPattern) | local tSplitReqVariant = mw.text.split(variant, ';', notUseLUAPattern) | ||
Line 137: | Line 137: | ||
end | end | ||
tVariants['requirements'] = tRequirements | tVariants['requirements'] = tRequirements | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.expandHonorVariants: '..#tVariants['requirements']..' Requirement Variants expanded!') |
end | end | ||
return tRetVariants | return tRetVariants | ||
end | end | ||
− | function p. | + | function p.translateHonorVariants( frame, tVariants ) |
local tRetVariants = tVariants | local tRetVariants = tVariants | ||
if ( tRetVariants == nil ) then | if ( tRetVariants == nil ) then | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: There are no Variants (nil)!') |
end | end | ||
if ( tRetVariants['answers'] == nil ) then | if ( tRetVariants['answers'] == nil ) then | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: There are no Answer Variants!') |
else | else | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: There are '..#tRetVariants['answers']..' Answer Variants to translate!') |
for varKey, variant in pairs(tRetVariants['answers']) 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. | + | mw.log('HonorUtils.translateHonorVariants: Answer Variant is invalid!') |
else | else | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: There are '..#variant['countries']['split']..' Answer Variant Countries to translate!') |
for countryKey, country in pairs(variant['countries']['split']) do | for countryKey, country in pairs(variant['countries']['split']) do | ||
local trans = utilities.localize( country, 'Countries' ) | local trans = utilities.localize( country, 'Countries' ) | ||
-- check if Localization returned something, if not set the original string | -- check if Localization returned something, if not set the original string | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants (country, trans): '..country..', '..trans) |
if ( trans == '' ) then | if ( trans == '' ) then | ||
trans = country | trans = country | ||
Line 168: | Line 168: | ||
} | } | ||
end | end | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: '..#tRetVariants['answers'][varKey]['countries']['split']..' Answer Variant Countries were translated!') |
end | end | ||
end | end | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: '..#tRetVariants['answers']..' Answer Variants were translated!') |
end | end | ||
if ( tRetVariants['requirements'] == nil ) then | if ( tRetVariants['requirements'] == nil ) then | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: There are no Requirement Variants!') |
else | else | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: There are '..#tRetVariants['requirements']..' Requriement Variants to translate!') |
for varKey, variant in pairs(tRetVariants['requirements']) 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. | + | mw.log('HonorUtils.translateHonorVariants: Requriement Variant is invalid!') |
else | else | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: There are '..#variant['authorities']..' Requriement Variant Authorities to translate!') |
for authorityKey, authority in pairs(variant['authorities']) do | for authorityKey, authority in pairs(variant['authorities']) do | ||
local trans = utilities.localize( authority, 'Authorities' ) | local trans = utilities.localize( authority, 'Authorities' ) | ||
Line 193: | Line 193: | ||
} | } | ||
end | end | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: '..#tRetVariants['requirements'][varKey]['authorities']..' Requriement Variant Authorities were translated!') |
end | end | ||
end | end | ||
− | mw.log('HonorUtils. | + | mw.log('HonorUtils.translateHonorVariants: '..#tRetVariants['requirements']..' Requriement Variants were translated!') |
end | end | ||
return tRetVariants | return tRetVariants |
Revision as of 08:38, 22 July 2021
Documentation for this module may be created at Module:HonorUtils/doc
local p = {}
local utilities = require ( "Module:Utilities" )
local notUseLUAPattern = true
function p.getVariantData( frame, variantType, variantNumber, landingPage )
-- check input variables
if ( frame == nil ) then
frame = mw.getCurrentFrame()
end
mw.log( 'HonorUtils.getVariantData (frame): '..frame:getTitle() )
if ( landingPage == nil or landingPage == "" ) then
landingPage = frame:expandTemplate{ title='GetHonorLandingPage' }
end
mw.log( 'HonorUtils.getVariantData (landingPage): '..landingPage )
local timeStart1 = os.clock()
mw.log('test1: '..frame:preprocess( '{{#dpl:|debug=0|title=AY Honors/Music|include={HonorLanding}:answerKey_1}}' ))
mw.log('test duration 1: '..tostring(os.clock() - timeStart1))
timeStart1 = os.clock()
mw.log('test2: '..frame:preprocess( '{{#dpl:|debug=0|title=AY Honors/Music|include={HonorLanding}:answerKey_1:answerKey_2:requirements_1:requirements_2}}' ))
mw.log('test duration 2: '..tostring(os.clock() - timeStart1))
end
function p.getVariantsData( frame, landingPage )
if ( frame == nil ) then
frame = mw.getCurrentFrame()
mw.log( 'HonorUtils.getExpandedHonorVriantsV2 (frame): '..frame:getTitle() )
end
if ( landingPage == nil or landingPage == "" ) then
landingPage = frame:expandTemplate{ title='GetHonorLandingPage' }
end
local parentFrame = frame:getParent()
local srcIsHonor = false
-- see if template has variants as arguments
local tAnswerVariants = getHonorVariants( 'answerKey', landingPage, srcIsHonor )
if ( tAnswerVariants == nil ) then
mw.log('HonorUtils.getExpandedHonorVariantsV2 (parentFrame:getTitle): '..parentFrame:getTitle())
local cnt_answerKey_variants = parentFrame.args['cnt_answerKey_variants']
if ( cnt_answerKey_variants ~= nil and cnt_answerKey_variants ~= '' ) then
tAnswerVariants = {}
-- get answer Variants
mw.log('HonorUtils.getExpandedHonorVariantsV2 (cnt_answerKey_variants): '..cnt_answerKey_variants)
for actVariant = 1, tonumber(cnt_answerKey_variants), 1 do
mw.log('HonorUtils.getExpandedHonorVariantsV2 (actVariant) answerkey: '..actVariant)
strActVariant = 'answerKey_' .. actVariant
tAnswerVariants[actVariant] = parentFrame.args[strActVariant]
mw.log('HonorUtils.getExpandedHonorVariantsV2 (tAnswerVariants[actVariant]): '..tAnswerVariants[actVariant])
end
end
end
local tRequirementVariants = getHonorVariants( 'requirements', landingPage, srcIsHonor )
if ( tRequirementVariants == nil ) then
local cnt_requirements_variants = parentFrame.args['cnt_requirements_variants']
if ( cnt_requirements_variants ~= nil and cnt_requirements_variants ~= '' ) then
tRequirementVariants = {}
-- get requirement Variants
mw.log('HonorUtils.getExpandedHonorVariantsV2 (cnt_answerKey_variants): '..cnt_requirements_variants)
for actVariant = 1, tonumber(cnt_requirements_variants), 1 do
mw.log('HonorUtils.getExpandedHonorVariantsV2 (actVariant) requirements: '..actVariant)
strActVariant = 'requirements_' .. actVariant
tRequirementVariants[actVariant] = parentFrame.args[strActVariant]
mw.log('HonorUtils.getExpandedHonorVariantsV2 (tRequirementVariants[actVariant]): '..tRequirementVariants[actVariant])
end
end
end
local tVariants = {
answers = tAnswerVariants,
requirements = tRequirementVariants
}
tVariants = p.expandHonorVariants( frame, tVariants )
tVariants = p.translateHonorVariants( frame, tVariants )
return tVariants
end
function p.expandHonorVariants( frame, tVariants )
local tRetVariants = tVariants
if ( tVariants == nil ) then
mw.log('HonorUtils.expandHonorVariants: No Variants to Expand (nil)!')
return {}
end
if ( tVariants['answers'] == nil ) then
mw.log('HonorUtils.expandHonorVariants: No Answer Variants to Expand!')
else
-- expand the answer Variants
local tAnswers = tVariants['answers']
mw.log('HonorUtils.expandHonorVariants: There are '..#tAnswers..' Answer Variants to Expand!')
for key, variant in pairs(tAnswers) do
mw.log('HonorUtils.expandHonorVariants (key, variant): '..key..', '..variant)
-- seperate Countries and Authority
local tSplitAnsVariant = mw.text.split(variant, ';;', notUseLUAPattern)
local tActVariant = {
orig = variant,
countries = {}
}
if ( tSplitAnsVariant[1] ~= '') then
tActVariant['countries']['orig'] = tSplitAnsVariant[1]
-- seperate the Countries
tActVariant['countries']['split'] = mw.text.split(tActVariant['countries']['orig'], ';', notUseLUAPattern)
mw.log('HonorUtils.expandHonorVariants (Countries - Orig): '..tActVariant['countries']['orig'])
else
mw.log('HonorUtils.expandHonorVariants: There are no countries for that variant!')
end
-- set the authority
if ( tSplitAnsVariant[2] == nil ) then
tActVariant['authority'] = nil
mw.log('HonorUtils.expandHonorVariants (Authority): nil')
else
tActVariant['authority'] = tSplitAnsVariant[2]
mw.log('HonorUtils.expandHonorVariants (Authority): '..tActVariant['authority'])
end
-- save act. Variant into the table
tAnswers[key] = tActVariant
end
tVariants['answers'] = tAnswers
mw.log('HonorUtils.expandHonorVariants: '..#tVariants['answers']..' Answer Variants expanded!')
end
if ( tVariants['requirements'] == nil ) then
mw.log('HonorUtils.expandHonorVariants: No Requirement Variants to Expand!')
else
-- expand the requirement Variants
local tRequirements = tVariants['requirements']
mw.log('HonorUtils.expandHonorVariants: There are '..#tRequirements..' Requirement Variants to Expand!')
for key, variant in pairs(tRequirements) do
mw.log('HonorUtils.expandHonorVariants (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.expandHonorVariants: '..#tVariants['requirements']..' Requirement Variants expanded!')
end
return tRetVariants
end
function p.translateHonorVariants( frame, tVariants )
local tRetVariants = tVariants
if ( tRetVariants == nil ) then
mw.log('HonorUtils.translateHonorVariants: There are no Variants (nil)!')
end
if ( tRetVariants['answers'] == nil ) then
mw.log('HonorUtils.translateHonorVariants: There are no Answer Variants!')
else
mw.log('HonorUtils.translateHonorVariants: 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.translateHonorVariants: Answer Variant is invalid!')
else
mw.log('HonorUtils.translateHonorVariants: There are '..#variant['countries']['split']..' Answer Variant Countries to translate!')
for countryKey, country in pairs(variant['countries']['split']) do
local trans = utilities.localize( country, 'Countries' )
-- check if Localization returned something, if not set the original string
mw.log('HonorUtils.translateHonorVariants (country, trans): '..country..', '..trans)
if ( trans == '' ) then
trans = country
end
tRetVariants['answers'][varKey]['countries']['split'][countryKey] = {
orig = country,
translated = trans
}
end
mw.log('HonorUtils.translateHonorVariants: '..#tRetVariants['answers'][varKey]['countries']['split']..' Answer Variant Countries were translated!')
end
end
mw.log('HonorUtils.translateHonorVariants: '..#tRetVariants['answers']..' Answer Variants were translated!')
end
if ( tRetVariants['requirements'] == nil ) then
mw.log('HonorUtils.translateHonorVariants: There are no Requirement Variants!')
else
mw.log('HonorUtils.translateHonorVariants: 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.translateHonorVariants: Requriement Variant is invalid!')
else
mw.log('HonorUtils.translateHonorVariants: There are '..#variant['authorities']..' Requriement Variant Authorities to translate!')
for authorityKey, authority in pairs(variant['authorities']) do
local trans = utilities.localize( authority, 'Authorities' )
-- check if Localization returned something, if not set the original string
if ( trans == '' ) then
trans = authority
end
tRetVariants['requirements'][varKey]['authorities'][authorityKey] = {
orig = authority,
translated = trans
}
end
mw.log('HonorUtils.translateHonorVariants: '..#tRetVariants['requirements'][varKey]['authorities']..' Requriement Variant Authorities were translated!')
end
end
mw.log('HonorUtils.translateHonorVariants: '..#tRetVariants['requirements']..' Requriement Variants were translated!')
end
return tRetVariants
end
function getHonorVariants( variantType, src, srcIsHonor, variantNumber )
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 = {}
if ( variantNumber ~= nil and variantNumber <= cntOfVariants ) then
-- only get the defined Variant
tVariantArguments[1] = variantType..'_'..variantNumber
else
-- get all variants
for actVariant = 1, cntOfVariants, 1 do
mw.log('HonorUtils.getHonorVariants (actVariant): '..actVariant)
tVariantArguments[actVariant] = variantType .. '_' .. actVariant
end
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