Module:Country links: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
refactor for easier testing of different languages
update donate links to use wmf parameters
 
(5 intermediate revisions by 2 users not shown)
Line 16: Line 16:
"AZ", "BH", "BD", "BT", "BN", "KH", "CN", "TL", "HK", "IN",
"AZ", "BH", "BD", "BT", "BN", "KH", "CN", "TL", "HK", "IN",
"IL", "JP", "JO", "KZ", "KW", "MO", "MY", "MV", "MN", "NP",
"IL", "JP", "JO", "KZ", "KW", "MO", "MY", "MV", "MN", "NP",
"OM", "PK", "PH", "QA", "SG", "LK", "TW", "TJ", "TH", "AE",
"OM", "PK", "PH", "QA", "SG", "LK", "TW", "TJ", "TH", "VN"
"VN"
}
}
},
},
Line 27: Line 26:
"EE", "FR", "GE", "DE", "GR", "HU", "IS", "IT", "LV", "LI",
"EE", "FR", "GE", "DE", "GR", "HU", "IS", "IT", "LV", "LI",
"LT", "LU", "MT", "MC", "ME", "NL", "NO", "PL", "PT", "IE",
"LT", "LU", "MT", "MC", "ME", "NL", "NO", "PL", "PT", "IE",
"RO", "RS", "SK", "SI", "ES", "SE", "CH", "UA", "GB", "VA"
"RO", "RS", "SK", "SI", "ES", "SE", "CH", "GB", "VA"
}
}
},
},
Line 43: Line 42:
name_code = "r005",
name_code = "r005",
countries = {
countries = {
"AR", "BO", "BR", "CL", "CO", "EC", "FK", "GF", "GY", "PY",
"AR", "BO", "BR", "CL", "CO", "EC", "FK", "GF", "GY", "PE",
"PE", "SR", "UY", "VE"
"SR", "UY", "VE"
}
}
},
},
Line 59: Line 58:
function p.get_country_name( language_code, country_code )
function p.get_country_name( language_code, country_code )
local name
local name
if country_names_data[language_code] == nil then language_code = 'en' end
if country_names_data[language_code] == nil then
language_code = 'en'
end
local name = country_names_data[language_code][country_code]
if country_names_data[language_code][country_code] == nil then
name = country_names_data['en'][country_code]
else
name = country_names_data[language_code][country_code]
end
return name
return name
end
end
Line 66: Line 71:
function p.get_donate_url( language_code, country_code )
function p.get_donate_url( language_code, country_code )
local url = 'https://donate.wikimedia.org/wiki/Special:FundraiserLandingPage'
local url = 'https://donate.wikimedia.org/wiki/Special:FundraiserLandingPage'
url = url .. '?utm_medium=Waystogive&utm_source=Waystogive&utm_campaign=C11_Waystogive'
url = url .. '?wmf_medium=Waystogive&wmf_source=Waystogive&wmf_campaign=C11_Waystogive'
url = url .. '&uselang=' .. language_code
url = url .. '&uselang=' .. language_code
url = url .. '&country=' .. country_code
url = url .. '&country=' .. country_code
Line 93: Line 98:


function p.build_all_regions( language_code )
function p.build_all_regions( language_code )
local wikitext = ''
local out = ''
for i,v in ipairs(p.regions) do
for i,v in ipairs(p.regions) do
local region_name = p.get_country_name( language_code, v.name_code )
local region_name = p.get_country_name( language_code, v.name_code )
wikitext = wikitext .. '\n== ' .. region_name .. ' ==\n'
out = out .. '<div class="mw-collapsible mw-collapsed">\n'
out = out .. '<div class="mw-collapsible-toggle continent-header">' .. region_name .. '</div>\n'
wikitext = wikitext .. p.make_donate_links( language_code, v.countries )
out = out .. '<div class="mw-collapsible-content country-list">\n'
out = out .. p.make_donate_links( language_code, v.countries )
out = out .. '</div>\n'
out = out .. '</div>\n'
end
end
return wikitext
return out
end
end


function p.main( frame )
function p.main( frame )
local language_code = p.page_language( frame )
local language_code = p.page_language( frame )
return p.build_all_regions( language_code )
return p.build_all_regions( language_code )
end
end
function p.page_language( frame )
function p.page_language( frame )
local full_title = mw.title.getCurrentTitle().prefixedText
local full_title = mw.title.getCurrentTitle().prefixedText
Line 116: Line 125:
return 'en'
return 'en'
end
end
end

-- version we can #invoke with parameters
function p.get_country_name_wrapper( frame )
return p.get_country_name( frame.args[1], frame.args[2] )
end
end



Latest revision as of 18:11, 3 June 2024

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

local p = {}
local country_names_data = mw.loadJsonData( 'Module:Country links/data.json' )
p.regions = {
    {
        name = "Africa",
        name_code = "r002",
        countries = {
            "DZ", "AO", "BI", "TD", "EG", "GH", "KE", "LR", "MA", "NE",
            "NG", "ZA", "UG"
        }
    },
    {
        name = "Asia",
        name_code = "r142",
        countries = {
            "AZ", "BH", "BD", "BT", "BN", "KH", "CN", "TL", "HK", "IN",
            "IL", "JP", "JO", "KZ", "KW", "MO", "MY", "MV", "MN", "NP",
            "OM", "PK", "PH", "QA", "SG", "LK", "TW", "TJ", "TH", "VN"
        }
    },
    {
        name = "Europe",
        name_code = "r150",
        countries = {
            "AL", "AD", "AT", "BE", "BA", "BG", "HR", "CY", "CZ", "DK",
            "EE", "FR", "GE", "DE", "GR", "HU", "IS", "IT", "LV", "LI",
            "LT", "LU", "MT", "MC", "ME", "NL", "NO", "PL", "PT", "IE",
            "RO", "RS", "SK", "SI", "ES", "SE", "CH", "GB", "VA"
        }
    },
    {
        name = "North America",
        name_code = "r003",
        countries = {
            "US", "AW", "BS", "BB", "BZ", "BM", "CA", "CR", "DM", "GD",
            "GP", "HT", "HN", "JM", "MQ", "MX", "NI", "PA", "PR", "KN",
            "LC", "PM", "VC", "TT", "TC"
        }
    },
    {
        name = "South America",
        name_code = "r005",
        countries = {
            "AR", "BO", "BR", "CL", "CO", "EC", "FK", "GF", "GY", "PE",
            "SR", "UY", "VE"
        }
    },
    {
        name = "Oceania",
        name_code = "r009",
        countries = {
            "AS", "AU", "CK", "FJ", "PF", "GU", "KI", "MH", "FM", "NR",
            "NZ", "PW"
        }
    }
}

function p.get_country_name( language_code, country_code )
    local name
    if country_names_data[language_code] == nil then
        language_code = 'en'
    end
    if country_names_data[language_code][country_code] == nil then
        name = country_names_data['en'][country_code]
    else
        name = country_names_data[language_code][country_code]
    end
    return name
end

function p.get_donate_url( language_code, country_code )
    local url = 'https://donate.wikimedia.org/wiki/Special:FundraiserLandingPage'
    url = url .. '?wmf_medium=Waystogive&wmf_source=Waystogive&wmf_campaign=C11_Waystogive'
    url = url .. '&uselang=' .. language_code
    url = url .. '&country=' .. country_code
    return url
end

function p.make_donate_links( language_code, country_list )

    local wikitext = ''
    local t = {}
    for key, value in ipairs(country_list) do
        table.insert(t, {value, p.get_country_name(language_code, value)})
    end

    -- sort by country name
    table.sort(t, function (k1, k2) return k1[2] < k2[2] end)

    for key, value in ipairs(t) do
        local url = p.get_donate_url( language_code, value[1] )
        local link_text = value[2]
        wikitext = wikitext .. '* [' .. url .. ' ' .. link_text .. ']\n'
    end

    return wikitext
end

function p.build_all_regions( language_code )
    local out = ''
    for i,v in ipairs(p.regions) do
        local region_name = p.get_country_name( language_code, v.name_code )
        out = out .. '<div class="mw-collapsible mw-collapsed">\n'
        out = out .. '<div class="mw-collapsible-toggle continent-header">' .. region_name .. '</div>\n'
        out = out .. '<div class="mw-collapsible-content country-list">\n'
        out = out .. p.make_donate_links( language_code, v.countries )
        out = out .. '</div>\n'
        out = out .. '</div>\n'
    end
    return out
end

function p.main( frame )
    local language_code = p.page_language( frame )
    return p.build_all_regions( language_code )
end
    
function p.page_language( frame )
    local full_title = mw.title.getCurrentTitle().prefixedText
    local t = mw.text.split( full_title, '/' )
    local lang = t[#t]
    if mw.language.isKnownLanguageTag( lang ) then
        return lang
    else
        return 'en'
    end
end

-- version we can #invoke with parameters
function p.get_country_name_wrapper( frame )
	return p.get_country_name( frame.args[1], frame.args[2] )
end

return p