Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Transclude random subpage/BHG-test
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
-- This module implements [[Template:Transclude random subpage]]. It is alpha software. 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) local retval = ""; local availableSubPageCount = 0; local i; -- don't track DYK etc, only selected articles etc if (string.find(pages.subpage, "/[sS]elected") == -1) then return retval end -- no tracking unless we are in Portal namespace if (mw.title.getCurrentTitle().nsText ~= "Portal") 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) 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 mw.title.getCurrentTitle().namespace == 100 then -- is in the portal namespace msg = msg .. '[[Category:Portals needing attention]]' end return msg end end local function getRandomSubpageContent(frame, pages) return tryExpandTemplate( frame, pages.random ) end function p._main(args, frame) frame = frame or mw.getCurrentFrame() local pages = getPages(args) local ret = {} ret[#ret + 1] = getRandomSubpageContent(frame, pages) 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] = getRandomSubpageContent(frame, pages) 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:Transclude random subpage', 'Template:Transclude random subpage/BHG-test', } }) return func(args, frame) end end p.main = makeInvokeFunction(p._main) return p
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Transclude random subpage/BHG-test/doc
(
edit
)