Kontentke ótiw

Module:Portal-inline

Wikipedia, erkin enciklopediya

Bul moduldiń hújjetin Module:Portal-inline/doc betinde jarata alasız

-- This module implements [[Template:Random portal component]]

local p = {}

local mRandom = require('Module:Random')
local currentTitle = mw.title.getCurrentTitle()

-- tracking function added by BHG 29/04/2019
-- called as subPageTrackingCategories(pages, args.max)
local function subPageTrackingCategories(pages, max, header)
    local retval = "";
    local availableSubPageCount = 0;
    local i;
    local thispagetitle = mw.title.getCurrentTitle().text

    -- don't track DYK etc, only selected/featured articles, biogs etc
    if ((string.find(header, "/[sS]aylanǵan") == -1) and (string.find(header, "/[Tt]igilgen") == -1)) then
        return retval
    end
    -- no tracking unless we are in Portal namespace
    if (mw.title.getCurrentTitle().nsText ~= "Portal") then
        return ""
    end

-- no tracking if this is a subpage
    if ((mw.ustring.match(thispagetitle, "/") ~= nil) and (thispagetitle ~= "AB/AD")) then
        return retval
    end

    -- limit checking to prevent Lua overload
    local myMaxCheck = 60
    if tonumber(max) < myMaxCheck then
        myMaxCheck = tonumber(max)
    end
    for i=1,myMaxCheck do
        local aSubPage = mw.title.new(pages.subpage .. '/' .. i)
        if (aSubPage.exists) then
            availableSubPageCount = availableSubPageCount + 1;
        end
    end
    if myMaxCheck >= tonumber(max) then
        if (availableSubPageCount < tonumber(max)) then
            retval = retval .. "[[Kategoriya:!Tosınarlı portal komponenti maks. da kórsetilgeninen kemlew astbetler]]"
        elseif (availableSubPageCount > tonumber(max)) then
            retval = retval .. "[[Kategoriya:!Tosınarlı portal komponenti kórsetilgennen kóbirek astbetlerge iye]]"
        end
    end
    -- before categorising, check what type of subpage we are categorising, and if detected, categorise images separately
    local subpageType = "subpages" -- generic type
    local subpageName = pages.subpage
    subpageName = mw.ustring.gsub(subpageName, "^[^/]*/", "")
    subpageName = mw.ustring.lower(subpageName)
    if ((mw.ustring.find(subpageName, "picture", 1, true) ~= nil) or
        (mw.ustring.find(subpageName, "image", 1, true) ~= nil) or
        (mw.ustring.find(subpageName, "panorama", 1, true) ~= nil)) then
        subpageType = "image subpages"
    end
    if (availableSubPageCount < 2) then
        retval = retval .. "[[Kategoriya:!2 den kem bolǵan tosınarlı portal komponenti" .. subpageType .. "]]"
    elseif (availableSubPageCount <= 5) then
        retval = retval .. "[[Kategoriya:!2-5 ten kem bolǵan tosınarlı portal komponenti" .. subpageType .. "]]"
    elseif (availableSubPageCount <= 10) then
        retval = retval .. "[[6-10 nan kem bolǵan tosınarlı portal komponenti" .. subpageType .. "]]"
    elseif (availableSubPageCount <= 15) then
        retval = retval .. "[[Kategoriya:!11–15 ten kem bolǵan tosınarlı portal komponenti" .. subpageType .. "]]"
    elseif (availableSubPageCount <= 20) then
        retval = retval .. "[[Kategoriya:!16–20 dan kem bolǵan tosınarlı portal komponenti 16–20 disponíveis " .. subpageType .. "]]"
    elseif (availableSubPageCount <= 25) then
        retval = retval .. "[[Kategoriya:!21–25 ten kem bolǵan tosınarlı portal komponenti" .. subpageType .. "]]"
    elseif (availableSubPageCount <= 30) then
        retval = retval .. "[[Kategoriya:!26–30 dan kem bolǵan tosınarlı portal komponenti" .. subpageType .. "]]"
    elseif (availableSubPageCount <= 40) then
        retval = retval .. "[[Kategoriya:!31–40 tan kem bolǵan tosınarlı portal komponenti" .. subpageType .. "]]"
    elseif (availableSubPageCount <= 50) then
        retval = retval .. "[[Kategoriya:!41–50 den kem bolǵan tosınarlı portal komponenti" .. subpageType .. "]]"
    else
        retval = retval .. "[[Kategoriya:!50 den kem bolǵan tosınarlı portal komponenti" .. subpageType .. "]]"
    end
    return retval;
end
local function getRandomNumber(max)
    -- gets a random integer between 1 and max; max defaults to 1
    return mRandom.number{max or 1}
end

local function expandArg(args, key)
    -- Emulate how unspecified template parameters appear in wikitext. If the
    -- specified argument exists, its value is returned, and if not the argument
    -- name is returned inside triple curly braces.
    local val = args[key]
    if val then
        return val
    else
        return string.format('{{{%s}}}', key)
    end
end

local function getPages(args)
    local pages = {}
    pages.root = args.rootpage or currentTitle.prefixedText
    pages.subpage = pages.root .. '/' .. expandArg(args, 'subpage')
    pages.random = pages.subpage .. '/' .. getRandomNumber(args.max)
    pages.footer = 'Standart: Box-footer'
    return pages
end

local function tryExpandTemplate(frame, title, args)
    local success, result = pcall(frame.expandTemplate, frame, {title = title, args = args})
    if success then
        return result
    else
        local msg = string.format(
            '<strong class="error">Bul “[[% s]]” beti joq.</strong>',
            title
        )
        if mw.title.getCurrentTitle().namespace == 100 then -- is in the portal namespace
            msg = msg .. '[[Kategoriya:!Portais itibarǵa mútáj]]'
        end
        return msg
    end
end

local function getHeader(frame, pages, header, template)
    return tryExpandTemplate(
        frame,
        template or pages.root .. '/box-header_portal',
        {header, pages.random}
    )
end

local function getRandomSubpageContent(frame, pages)
    return tryExpandTemplate(
        frame,
        pages.random
    )
end

local function getFooter(frame, pages, link)
    return tryExpandTemplate(
        frame,
        pages.footer,
        {link}
    )
end

function p._main(args, frame)
    frame = frame or mw.getCurrentFrame()
    local pages = getPages(args)

    local ret = {}
    ret[#ret + 1] = getHeader(frame, pages, args.header or 'subpage', args.headertemplate)
    ret[#ret + 1] = getRandomSubpageContent(frame, pages)
    if not args.footer or not args.footer:find('%S') then
        ret[#ret + 1] = '<div style="clear:both;"></div></div>'
    else
        ret[#ret + 1] = getFooter(frame, pages, string.format(
            '[[%s|%s]]',
            pages.subpage,
            expandArg(args, 'footer')
        ))
    end

    return table.concat(ret, '\n') .. subPageTrackingCategories(pages, args.max, args.header)
end

function p._nominate(args, frame)
    frame = frame or mw.getCurrentFrame()
    local pages = getPages(args)
    
    local ret = {}
    ret[#ret + 1] = getHeader(frame, pages, expandArg(args, 'header'), args.headertemplate)
    ret[#ret + 1] = getRandomSubpageContent(frame, pages)
    ret[#ret + 1] = getFooter(frame, pages, string.format(
        '[[/Nomeie/%s|Sugere]] • [[%s|%s]] ',
        expandArg(args, 'subpage'),
        pages.subpage,
        args.footer or 'Arquivo'
    ))

    return table.concat(ret, '\n') .. subPageTrackingCategories(pages, args.max, args.header)
end

local function makeInvokeFunction(func)
    return function (frame)
        local args = require('Module:Arguments').getArgs(frame, {
            trim = false,
            removeBlanks = false,
            wrappers = {
                'Template:Random portal component',
                'Template:Random portal component/BHG-test',
                'Template:Random portal component with nominate'
            }
        })
        return func(args, frame)
    end
end

p.main = makeInvokeFunction(p._main)
p.nominate = makeInvokeFunction(p._nominate)

return p