--[==[ This module is a Lua implementation of the old {{Portal}} template. As of February 2019 it is used on nearly 7,900,000 articles. -- Please take care when updating it! It outputs two functions: p.portal, which generates a list of portals, and p.image, which -- produces the image name for an individual portal. -- The portal image data is kept in submodules of [[Module:Portal/images]], listed below:-- - for portal names beginning with "A".-- - for portal names beginning with "B".-- - for portal names beginning with "C".-- - for portal names beginning with "D".-- - for portal names beginning with "E".-- - for portal names beginning with "F".-- - for portal names beginning with "G".-- - for portal names beginning with "H".-- - for portal names beginning with "I".-- - for portal names beginning with "J".-- - for portal names beginning with "K".-- - for portal names beginning with "L".-- - for portal names beginning with "M".-- - for portal names beginning with "N".-- - for portal names beginning with "O".-- - for portal names beginning with "P".-- - for portal names beginning with "Q".-- - for portal names beginning with "R".-- - for portal names beginning with "S".-- - for portal names beginning with "T".-- - for portal names beginning with "U".-- - for portal names beginning with "V".-- - for portal names beginning with "W".-- - for portal names beginning with "X".-- - for portal names beginning with "Y".-- - for portal names beginning with "Z".-- - for portal names beginning with any other letters. This includes numbers,-- letters with diacritics, and letters in non-Latin alphabets.-- - for adding aliases for existing portal names. Use this page for variations-- in spelling and diacritics, etc., no matter what letter the portal begins with.---- The images data pages are separated by the first letter to reduce server load when images are added, changed, or removed.-- Previously all the images were on one data page at, but this had the disadvantage that all-- 5,000,000 pages using this module needed to be refreshed every time an image was added or removed.]
local p =
-- determine whether we're being called from a sandboxlocal isSandbox = mw.getCurrentFrame:getTitle:find('sandbox', 1, true)local sandbox = isSandbox and '/sandbox' or
local function sandboxVersion(s) return isSandbox and s..'-sand' or send
local templatestyles = 'Module:Portal'..sandbox..'/styles.css'
local getArgs = require('Module:Arguments').getArgslocal yesno = require('Module:Yesno')
-- List of non-talk namespaces which should not be tracked (Talk pages are never tracked)local badNamespaces =
-- Check whether to do tracking in this namespace-- Returns true unless the page is one of the banned namespaceslocal function checkTracking(title) local thisPage = title or mw.title.getCurrentTitle if thisPage.isTalkPage then return false end local ns = thisPage.nsText:lower for _, v in ipairs(badNamespaces) do if ns
local function matchImagePage(s) -- Finds the appropriate image subpage given a lower-case -- portal name plus the first letter of that portal name. if type(s) ~= 'string' or #s < 1 then return end local firstLetter = mw.ustring.sub(s, 1, 1) local imagePage if mw.ustring.find(firstLetter, '^[a-z]') then imagePage = 'Module:Portal/images/' .. firstLetter .. sandbox else imagePage = 'Module:Portal/images/other' .. sandbox end return mw.loadData(imagePage)[s]end
local function getAlias(s) -- Gets an alias from the image alias data page. local aliasData = mw.loadData('Module:Portal/images/aliases'..sandbox) for portal, aliases in pairs(aliasData) do for _, alias in ipairs(aliases) do if alias
local defaultImage = 'Portal-puzzle.svg|link=|alt='
local function getImageName(s) -- Gets the image name for a given string. if type(s) ~= 'string' or #s < 1 then return defaultImage end s = mw.ustring.lower(s) return matchImagePage(s) or matchImagePage(getAlias(s)) or defaultImageendlocal function exists(title) local success, exists = pcall(function return title.exists end) -- If success = false, then we're out of expensive parser function calls and can't check whether it exists -- in that case, don't throw a Lua error return not success or existsend
-- Function to check argument portals for errors, generate tracking categories if needed -- Function first checks for too few/many portals provided -- Then checks the portal list to purge any portals that don't exist -- Arguments: -- portals: raw list of portals -- args.tracking: is tracking requested? (will not track on bad titles or namespaces) -- args.redlinks: should redlinks be displayed? -- args.minPortals: minimum number of portal arguments -- args.maxPortals: maximum number of portal arguments -- Returns: -- portals = list of portals, with redlinks purged (if args.redlinks=false) -- trackingCat = possible tracking category -- errorMsg = error messagefunction p._checkPortals(portals, args) local trackingCat = local errMsg = nil -- Tracking is on by default. -- It is disabled if any of the following is true -- 1/ the parameter "tracking" is set to 'no, 'n', or 'false' -- 2/ the current page fails the namespace or pagename tests local trackingEnabled = args.tracking and checkTracking args.minPortals = args.minPortals or 1 args.maxPortals = args.maxPortals or -1 -- check for too few portals if #portals < args.minPortals then errMsg = 'please specify at least '..args.minPortals..' portal'..(args.minPortals > 1 and 's' or ) trackingCat = (trackingEnabled and '' or ) return portals, trackingCat, errMsg end -- check for too many portals if args.maxPortals >= 0 and #portals > args.maxPortals then errMsg = 'too many portals (maximum = '..args.maxPortals..')' trackingCat = (trackingEnabled and '' or ) return portals, trackingCat, errMsg end if not args.redlinks or trackingEnabled then -- make new list of portals that exist local existingPortals = for _, portal in ipairs(portals) do local portalTitle = mw.title.new(portal,"Portal") -- if portal exists, put it into list if portalTitle and exists(portalTitle) then table.insert(existingPortals,portal) -- otherwise set tracking cat elseif trackingEnabled then trackingCat = "" end end -- If redlinks is off, use portal list purged of redlinks portals = args.redlinks and portals or existingPortals -- if nothing left after purge, set tracking cat if #portals
local function portalBox(args) return mw.html.create('ul') :attr('role', 'navigation') :attr('aria-label', 'Portals') :addClass('noprint') :addClass(args.error and or sandboxVersion('portalbox')) :addClass(args.border and sandboxVersion('portalborder') or ) :addClass(sandboxVersion(args.left and 'portalleft' or 'portalright')) :css('margin', args.margin or nil) :newlineend
local function fillBox(root, contents) for _, item in ipairs(contents) do local entry = root:tag('li') entry:addClass(sandboxVersion('portalbox-entry')) local image = entry:tag('span') image:addClass(sandboxVersion('portalbox-image')) image:wikitext(item[1]) local link = entry:tag('span') link:addClass(sandboxVersion('portalbox-link')) link:wikitext(item[2]) end return rootend
function p._portal(portals, args) -- This function builds the portal box used by the template. -- Normalize all arguments if args.redlinks
nil then args[key] = default end args[key] = yesno(args[key], default) end
local root = portalBox(args)
local trackingCat = local errMsg = nil args.minPortals = args.nominimum and 0 or 1 args.maxPortals = -1 portals, trackingCat, errMsg = p._checkPortals(portals, args) root:wikitext(trackingCat) -- if error message, put it in the box and return if errMsg then if args.border then -- suppress error message when border=no args.error = true -- recreate box without fancy formatting root = portalBox(args) root:wikitext(trackingCat) local errTag = root:tag('strong') errTag:addClass('error') errTag:css('padding','0.2em') errTag:wikitext('Error: '..errMsg) end return tostring(root) end -- if no portals (and no error), just return tracking category if #portals
local contents = -- Display the portals specified in the positional arguments. local defaultUsed = nil for _, portal in ipairs(portals) do local portalImage = getImageName(portal) if portalImage
function p._demo(imageList, args) for key, default in pairs do if args[key]
local contents = -- Display the portals specified in the positional arguments. for _, fn in ipairs(imageList) do local image = string.format('',fn) local link = string.format('%s',fn,fn) table.insert(contents,) end
return tostring(fillBox(root,contents))end
function p._image(portal,keep) -- Wrapper function to allow getImageName to be accessed through #invoke. -- backward compatibility: if table passed, take first element if type(portal)
local function getAllImageTable -- Returns an array containing all image subpages (minus aliases) as loaded by mw.loadData. local images = for i, subpage in ipairs do local imageTable = mw.loadData('Module:Portal/images/' .. subpage .. sandbox) for portal, image in pairs(imageTable) do local args = mw.text.split(image,"|") images[portal] = args[1] -- just use image filename end end return imagesend
function p._displayAll(portals, args) -- This function displays all portals that have portal images. This function is for maintenance purposes and should not be used in -- articles, for two reasons: 1) there are over 1500 portals with portal images, and 2) the module doesn't record how the portal -- names are capitalized, so the portal links may be broken. local lang = mw.language.getContentLanguage portals = portals or for portal in pairs(getAllImageTable) do table.insert(portals,lang:ucfirst(portal)) end table.sort(portals) args.redlinks = args.redlinks or "yes" return p._portal(portals, args)end
function p._imageDupes -- This function searches the image subpages to find duplicate images. If duplicate images exist, it is not necessarily a bad thing, -- as different portals might just happen to choose the same image. However, this function is helpful in identifying images that -- should be moved to a portal alias for ease of maintenance. local exists, dupes =, for portal, image in pairs(getAllImageTable) do if not exists[image] then exists[image] = portal else table.insert(dupes, string.format('The image "%s" is used for both portals "%s" and "%s".', image, image, exists[image], portal)) end end if #dupes < 1 then return 'No duplicate images found.' else return 'The following duplicate images were found:\n* ' .. table.concat(dupes, '\n* ') endend
local function processPortalArgs(args) -- This function processes a table of arguments and returns two tables: an array of portal names for processing by ipairs, and a table of -- the named arguments that specify style options, etc. We need to use ipairs because we want to list all the portals in the order -- they were passed to the template, but we also want to be able to deal with positional arguments passed explicitly, for example -- . The behaviour of ipairs is undefined if nil values are present, so we need to make sure they are all removed. args = type(args)
'number' and type(v)
-- Entry point for sorting portals from other named argumentsfunction p._processPortalArgs(args) return processPortalArgs(args)end
function p.image(frame) local origArgs = getArgs(frame) local portals, args = processPortalArgs(origArgs) return p._image(portals[1],args.border)end
function p.demo(frame) local args = getArgs(frame) local styles = frame:extensionTag return styles..p._demo(args,args)end
local function makeWrapper(funcName) -- Processes external arguments and sends them to the other functions. return function (frame) -- If called via #invoke, use the args passed into the invoking -- template, or the args passed to #invoke if any exist. Otherwise -- assume args are being passed directly in from the debug console -- or from another Lua module. -- Also: trim whitespace and remove blank arguments local origArgs = getArgs(frame) -- create two tables to pass to func: an array of portal names, and a table of named arguments. local portals, args = processPortalArgs(origArgs) local results = if funcName
'_displayAll' then results = frame:extensionTag end return results .. p[funcName](portals, args) endend
for _, funcName in ipairs do p[funcName] = makeWrapper('_' .. funcName)end
return p