Jump to content

Module:ElementNTLDStats: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 14: Line 14:
local ErrorHandling    = require('Module:ErrorHandling')
local ErrorHandling    = require('Module:ErrorHandling')
local mw              = mw  -- MediaWiki API
local mw              = mw  -- MediaWiki API
-- Classic TLDs not integrated into NTLDStats database
local CLASSIC_TLDS = {
    com=true, net=true, org=true, info=true,
    edu=true, gov=true, mil=true, int=true,
    aero=true, asia=true, cat=true, coop=true,
    jobs=true, mobi=true, museum=true, post=true,
    tel=true, travel=true, xxx=true
}


-- Create an NTLDStats block
-- Create an NTLDStats block
Line 42: Line 51:
             end
             end


            -- Load TLD config for classic list
-- Use CLASSIC_TLDS constant defined above
            local constants = template.config.constants or {}


             -- Determine page name and TLD identifier
             -- Determine page name and TLD identifier
Line 51: Line 59:
             local ext = tldName:match("%.([^%.]+)$")
             local ext = tldName:match("%.([^%.]+)$")


            -- Skip classic TLDs
-- Skip classic TLDs
             if constants.classicTLDs and (
             if CLASSIC_TLDS[string.lower(tldName)] or (ext and CLASSIC_TLDS[string.lower(ext)]) then
              constants.classicTLDs[string.lower(tldName)] or
              (ext and constants.classicTLDs[string.lower(ext)])
            ) then
                 return ""
                 return ""
             end
             end

Revision as of 01:59, 29 April 2025

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

--[[ 
 * Module:ElementNTLDStats
 * Renders the NTLDStats element for generic TLDs (not ccTLDs), displaying a link to ntldstats.com.
 * Designed for integration with the Blueprint template system.
 ]]

local p = {}

p.elementName = "ntldstats"

-- Dependencies
local TemplateHelpers = require('Module:TemplateHelpers')
local ConfigRepository = require('Module:ConfigRepository')
local ErrorHandling    = require('Module:ErrorHandling')
local mw               = mw  -- MediaWiki API

-- Classic TLDs not integrated into NTLDStats database
local CLASSIC_TLDS = {
    com=true, net=true, org=true, info=true,
    edu=true, gov=true, mil=true, int=true,
    aero=true, asia=true, cat=true, coop=true,
    jobs=true, mobi=true, museum=true, post=true,
    tel=true, travel=true, xxx=true
}

-- Create an NTLDStats block
function p.createBlock()
    return function(template, args)
        local ctx = template._errorContext

        -- Protected execution wrapper
        local function execute(func, ...)
            if ctx then
                return ErrorHandling.protect(
                    ctx,
                    "ElementBlock_ntldstats",
                    func,
                    "",
                    ...
                )
            else
                local ok, result = pcall(func, ...)
                return ok and result or ""
            end
        end

        return execute(function()
            -- Skip for ccTLDs
            if args._type == "ccTLD" then
                return ""
            end

-- Use CLASSIC_TLDS constant defined above

            -- Determine page name and TLD identifier
            local titleObj = mw.title.getCurrentTitle()
            local pageName = titleObj and titleObj.text or ""
            local tldName = pageName:gsub("^%.", "")
            local ext = tldName:match("%.([^%.]+)$")

-- Skip classic TLDs
            if CLASSIC_TLDS[string.lower(tldName)] or (ext and CLASSIC_TLDS[string.lower(ext)]) then
                return ""
            end

            -- Render divider and NTLDStats link
            local divider = TemplateHelpers.renderDividerBlock("Find out more:")
            local stats = string.format(
                '|-\n| colspan="2" | <div class="ntldstats icannwiki-centered">[[File:NTLDStatsLogo.png|100px|alt=NTLDStats|link=https://ntldstats.com/tld/%s]]</div>',
                tldName
            )
            return divider .. "\n" .. stats
        end)
    end
end

return p