Misplaced Pages

Module:Random portal component: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editContent deleted Content added
Revision as of 18:21, 11 May 2019 view sourceBrownHairedGirl (talk | contribs)Autopatrolled, Extended confirmed users, File movers, Pending changes reviewers, Rollbackers2,942,733 edits categorise panoramas as images← Previous edit Latest revision as of 10:08, 3 October 2020 view source Certes (talk | contribs)Extended confirmed users243,573 edits ‎Check that subpage being presented exists. Reduce checks that subpages not being presented exist, to reduce Portal:San Francisco Bay Area from >500 expensive functions to 50 
Line 10: Line 10:
local function subPageTrackingCategories(pages, max, header) local function subPageTrackingCategories(pages, max, header)
local retval = ""; local retval = "";
local availableSubPageCount = 0;
local i;
local thispagetitle = mw.title.getCurrentTitle().text local thispagetitle = mw.title.getCurrentTitle().text


Line 23: Line 21:
end end


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


local maxNum = tonumber(max)
-- limit checking to prevent Lua overload
local myMaxCheck = 60 local availableSubPageCount = maxNum

if tonumber(max) < myMaxCheck then
-- Check for missing subpages at end of alleged number range
myMaxCheck = tonumber(max)
while availableSubPageCount > 0 and not mw.title.new(pages.subpage .. '/' .. tostring(availableSubPageCount)).exists do
availableSubPageCount = availableSubPageCount - 1
end end
if availableSubPageCount < maxNum then
for i=1,myMaxCheck do
retval = retval .. "]"
local aSubPage = mw.title.new(pages.subpage .. '/' .. i)
else
if (aSubPage.exists) then
-- Check for spurious subpages beyond end of alleged number range
availableSubPageCount = availableSubPageCount + 1;
while mw.title.new(pages.subpage .. '/' .. tostring(availableSubPageCount + 1)).exists do
availableSubPageCount = availableSubPageCount + 1
end end
if availableSubPageCount > maxNum then
end
if myMaxCheck >= tonumber(max) then
if (availableSubPageCount < tonumber(max)) then
retval = retval .. "]"
elseif (availableSubPageCount > tonumber(max)) then
retval = retval .. "]" retval = retval .. "]"
end end
end end

-- before categorising, check what type of subpage we are categorising, and if detected, categorise images separately -- before categorising, check what type of subpage we are categorising, and if detected, categorise images separately
local subpageType = "subpages" -- generic type local subpageType = "subpages" -- generic type
Line 100: Line 99:
pages.root = args.rootpage or currentTitle.prefixedText pages.root = args.rootpage or currentTitle.prefixedText
pages.subpage = pages.root .. '/' .. expandArg(args, 'subpage') pages.subpage = pages.root .. '/' .. expandArg(args, 'subpage')
local tries = 10
pages.random = pages.subpage .. '/' .. getRandomNumber(args.max)
repeat
pages.random = pages.subpage .. '/' .. getRandomNumber(args.max)
tries = tries - 1
until tries < 1 or mw.title.new(pages.random).exists
pages.footer = 'Template:Box-footer' pages.footer = 'Template:Box-footer'
return pages return pages

Latest revision as of 10:08, 3 October 2020

Module documentation[view] [edit] [history] [purge]
BetaThis 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 depends on the following other modules:

This module implements {{random portal component}}. Please see the template page for documentation.

See also

Portal templates
Visual overview of template usage
Layout and formatting
Content transclusion
Content slideshows
Content randomisation
General
Linking templates
Talk pages / Maintenance
Images
Modules
Usage
For more information, see: Category:Template-Class Portal pages and Category:Misplaced Pages Portal templates
The above documentation is transcluded from Module:Random portal component/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.

-- This module implements ]

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 thispagetitle = mw.title.getCurrentTitle().text

	-- don't track DYK etc, only selected/featured articles, biogs etc
	if ((string.find(header, "/elected") == -1) and (string.find(header, "/eatured") == -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 ~= "AC/DC")) then
		return retval
	end

	local maxNum = tonumber(max)
	local availableSubPageCount = maxNum

	-- Check for missing subpages at end of alleged number range
	while availableSubPageCount > 0 and not mw.title.new(pages.subpage .. '/' .. tostring(availableSubPageCount)).exists do
		availableSubPageCount = availableSubPageCount - 1
	end
	if availableSubPageCount < maxNum then 
		retval = retval .. "]"
	else
		-- Check for spurious subpages beyond end of alleged number range
		while mw.title.new(pages.subpage .. '/' .. tostring(availableSubPageCount + 1)).exists do
			availableSubPageCount = availableSubPageCount + 1
		end
		if availableSubPageCount > maxNum then 
			retval = retval .. "]"
		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 .. "]"
	elseif (availableSubPageCount <= 5) then
		retval = retval .. "]"
	elseif (availableSubPageCount <= 10) then
		retval = retval .. "]"
	elseif (availableSubPageCount <= 15) then
		retval = retval .. "]"
	elseif (availableSubPageCount <= 20) then
		retval = retval .. "]"
	elseif (availableSubPageCount <= 25) then
		retval = retval .. "]"
	elseif (availableSubPageCount <= 30) then
		retval = retval .. "]"
	elseif (availableSubPageCount <= 40) then
		retval = retval .. "]"
	elseif (availableSubPageCount <= 50) then
		retval = retval .. "]"
	else
		retval = retval .. "]"
	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
	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')
	local tries = 10
	repeat
		pages.random = pages.subpage .. '/' .. getRandomNumber(args.max)
		tries = tries - 1
	until tries < 1 or mw.title.new(pages.random).exists
	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 "]" does not exist.</strong>',
			title
		)
		if mw.title.getCurrentTitle().namespace == 100 then -- is in the portal namespace
			msg = msg .. ']'
		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 = getHeader(frame, pages, args.header or 'subpage', args.headertemplate)
	ret = getRandomSubpageContent(frame, pages)
	if not args.footer or not args.footer:find('%S') then
		ret = '<div style="clear:both;"></div></div>'
	else
		ret = getFooter(frame, pages, string.format(
			']',
			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 = getHeader(frame, pages, expandArg(args, 'header'), args.headertemplate)
	ret = getRandomSubpageContent(frame, pages)
	ret = getFooter(frame, pages, string.format(
		'] • ] ',
		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
Category: