Module:Random portal component/sandbox: Difference between revisions
Appearance
Content deleted Content added
add a namespace check to the isPortalSubpage function in case it gets reused anywhere else |
split the "selected page" check out into an isSelectedPage function |
||
Line 9: | Line 9: | ||
["AC/DC"] = true, |
["AC/DC"] = true, |
||
} |
} |
||
local function isSelectedPage(header) |
|||
-- Whether a page is selected (a selected article, selected list, etc.) |
|||
return string.find(header, "^'*[sS]elected") ~= nil |
|||
end |
|||
local function isInPortalNamespace(title) |
local function isInPortalNamespace(title) |
||
Line 30: | Line 35: | ||
-- don't track DYK etc, only selected articles etc |
-- don't track DYK etc, only selected articles etc |
||
if |
if not isSelectedPage(header) then |
||
return "" |
return "" |
||
end |
end |
||
-- no tracking unless we are in Portal namespace |
-- no tracking unless we are in Portal namespace |
||
if not isInPortalNamespace(currentTitle) then |
if not isInPortalNamespace(currentTitle) then |
Revision as of 03:36, 2 May 2019
![]() | This is the module sandbox page for Module:Random portal component (diff). |
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
This module implements {{random portal component}}. Please see the template page for documentation.
See also
-- This module implements [[Template:Random portal component]]
local p = {}
local mRandom = require('Module:Random')
local currentTitle = mw.title.getCurrentTitle()
local portalNamesContainingSlashes = {
["AC/DC"] = true,
}
local function isSelectedPage(header)
-- Whether a page is selected (a selected article, selected list, etc.)
return string.find(header, "^'*[sS]elected") ~= nil
end
local function isInPortalNamespace(title)
-- Whether a title object is in the portal namespace.
return title.namespace == 100
end
local function isPortalSubpage(title)
-- Whether a title object is a portal subpage
return (
isInPortalNamespace(title)
and title.isSubpage
and not portalNamesContainingSlashes[title.text]
)
end
local function subPageTrackingCategories(pages, max, header)
local retval = "";
local availableSubPageCount = 0;
local i;
-- don't track DYK etc, only selected articles etc
if not isSelectedPage(header) then
return ""
end
-- no tracking unless we are in Portal namespace
if not isInPortalNamespace(currentTitle) then
return ""
end
-- no tracking if this is a subpage
if isPortalSubpage(currentTitle) then
return ""
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 .. "[[Category:Random portal component with fewer available subpages than specified max]]"
elseif (availableSubPageCount > tonumber(max)) then
retval = retval .. "[[Category:Random portal component with more available subpages than specified max]]"
end
end
if (availableSubPageCount < 2) then
retval = retval .. "[[Category:Random portal component with less than 2 available subpages]]"
elseif (availableSubPageCount <= 5) then
retval = retval .. "[[Category:Random portal component with 2–5 available subpages]]"
elseif (availableSubPageCount <= 10) then
retval = retval .. "[[Category:Random portal component with 6–10 available subpages]]"
elseif (availableSubPageCount <= 15) then
retval = retval .. "[[Category:Random portal component with 11–15 available subpages]]"
elseif (availableSubPageCount <= 20) then
retval = retval .. "[[Category:Random portal component with 16–20 available subpages]]"
elseif (availableSubPageCount <= 25) then
retval = retval .. "[[Category:Random portal component with 21–25 available subpages]]"
elseif (availableSubPageCount <= 30) then
retval = retval .. "[[Category:Random portal component with 26–30 available subpages]]"
elseif (availableSubPageCount <= 40) then
retval = retval .. "[[Category:Random portal component with 31–40 available subpages]]"
elseif (availableSubPageCount <= 50) then
retval = retval .. "[[Category:Random portal component with 41–50 available subpages]]"
else
retval = retval .. "[[Category:Random portal component with over 50 available subpages]]"
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 = 'Template: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">The page "[[%s]]" does not exist.</strong>',
title
)
if isInPortalNamespace(currentTitle) then
msg = msg .. '[[Category:Portals needing attention]]'
end
return msg
end
end
local function getHeader(frame, pages, header, template)
return tryExpandTemplate(
frame,
template or pages.root .. '/box-header',
{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(
'[[/Nominate/%s|Suggest]] • [[%s|%s]] ',
expandArg(args, 'subpage'),
pages.subpage,
args.footer or 'Archive'
))
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