Editing Module:Portal

Jump to: navigation, search

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.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
--[==[ This module is a Lua implementation of the old {{Portal}} template. As of February 2019 it is used on nearly 7,900,000 articles.
+
--[==[ This module is a Lua implementation of the old {{Portal}} template. As of August 2013 it is used on nearly 5,000,000 articles.
-- Please take care when updating it! It outputs two functions: p.portal, which generates a list of portals, and p.image, which
+
-- Please take care when updating it! It outputs two functions: p.portal, which generates a table of portals, and p.image, which
 
-- produces the image name for an individual portal.
 
-- produces the image name for an individual portal.
  
Line 41: Line 41:
  
 
local p = {}
 
local p = {}
 
local trackingEnabled = true
 
 
local templatestyles = 'Module:Portal/styles.css'
 
 
local yesno = require('Module:Yesno')
 
 
-- Check whether to do tracking in this namespace
 
-- Returns true unless the page is one of the banned namespaces
 
local function checkTrackingNamespace()
 
local thisPage = mw.title.getCurrentTitle()
 
if (thisPage.namespace == 1) -- Talk
 
or (thisPage.namespace == 2) -- User
 
or (thisPage.namespace == 3) -- User talk
 
or (thisPage.namespace == 5) -- Wikipedia talk
 
or (thisPage.namespace == 7) -- File talk
 
or (thisPage.namespace == 11) -- Template talk
 
or (thisPage.namespace == 15) -- Category talk
 
or (thisPage.namespace == 101) -- Portal talk
 
or (thisPage.namespace == 109) -- Book talk
 
or (thisPage.namespace == 118) -- Draft
 
or (thisPage.namespace == 119) -- Draft talk
 
or (thisPage.namespace == 829) -- Module talk
 
then
 
return false
 
end
 
return true
 
end
 
 
-- Check whether to do tracking on this pagename
 
-- Returns false if the page title matches one of the banned strings
 
-- Otherwise returns true
 
local function checkTrackingPagename()
 
local thisPage = mw.title.getCurrentTitle()
 
local thisPageLC = mw.ustring.lower(thisPage.text)
 
if (string.match(thisPageLC, "/archive") ~= nil) then
 
return false
 
end
 
if (string.match(thisPageLC, "/doc") ~= nil) then
 
return false
 
end
 
if (string.match(thisPageLC, "/test") ~= nil) then
 
return false
 
end
 
return true
 
end
 
 
  
 
local function matchImagePage(s)
 
local function matchImagePage(s)
Line 117: Line 70:
 
local function getImageName(s)
 
local function getImageName(s)
 
-- Gets the image name for a given string.
 
-- Gets the image name for a given string.
local default = 'Portal-puzzle.svg|link=|alt='
 
 
if type(s) ~= 'string' or #s < 1 then
 
if type(s) ~= 'string' or #s < 1 then
return default
+
return 'Portal-puzzle.svg'
 
end
 
end
 
s = mw.ustring.lower(s)
 
s = mw.ustring.lower(s)
return matchImagePage(s) or matchImagePage(getAlias(s)) or default
+
return matchImagePage(s) or matchImagePage(getAlias(s)) or 'Portal-puzzle.svg'
end
 
 
 
local function checkPortalExists(portal)
 
return not (mw.title.makeTitle(100, portal).id == 0)
 
 
end
 
end
  
Line 132: Line 80:
 
-- This function builds the portal box used by the {{portal}} template.
 
-- This function builds the portal box used by the {{portal}} template.
 
local root = mw.html.create('div')
 
local root = mw.html.create('div')
:attr('role', 'navigation')
+
:addClass('noprint portal')
:attr('aria-label', 'Portals')
 
:addClass('noprint portal plainlist')
 
 
:addClass(args.left and 'tleft' or 'tright')
 
:addClass(args.left and 'tleft' or 'tright')
:css('margin', args.margin or nil)
+
:css('border', 'solid #aaa 1px')
 +
:css('margin', args.margin or (args.left == 'yes' and '0.5em 1em 0.5em 0') or '0.5em 0 0.5em 1em')
 
:newline()
 
:newline()
  
-- Tracking is on by default.
+
-- Start the table. This corresponds to the start of the wikitext table in the old [[Template:Portal]].
-- It is disabled if any of the following is true
+
local tableroot = root:tag('table')
-- 1/ the parameter "tracking" is set to 'no, 'n', or 'false'
+
:css('background', '#f9f9f9')
-- 2/ the current page fails the namespace tests in checkTrackingNamespace()
+
:css('font-size', '85%')
-- 3/ the current page fails the pagename tests in checkTrackingPagename()
+
:css('line-height', '110%')
trackingEnabled = yesno(args.tracking, trackingEnabled)
+
:css('max-width', '175px')
if (checkTrackingNamespace() == false) then
+
:css('width', type(args.boxsize) == 'string' and (args.boxsize .. 'px') or nil)
trackingEnabled = false
 
end
 
if (checkTrackingPagename() == false) then
 
trackingEnabled = false
 
end
 
  
 
-- If no portals have been specified, display an error and add the page to a tracking category.
 
-- If no portals have been specified, display an error and add the page to a tracking category.
 
if not portals[1] then
 
if not portals[1] then
if yesno(args.nominimum) then
+
tableroot:wikitext('<strong class="error">No portals specified: please specify at least one portal</strong>[[Category:Portal templates without a parameter]]')
-- if nominimum as been set to yes (or similar), omit the warning
 
 
else
 
root:wikitext('<strong class="error">No portals specified: please specify at least one portal</strong>')
 
end
 
if (trackingEnabled) then
 
root:wikitext('[[Category:Portal templates without a parameter]]')
 
end
 
return tostring(root)
 
end
 
 
-- scan for nonexistent portals, if they exist remove them from the portals table. If redlinks=yes, then don't remove
 
local portallen = #portals
 
-- traverse the list backwards to ensure that no portals are missed (table.remove also moves down the portals in the list, so that the next portal isn't checked if going fowards.
 
-- going backwards allows us to circumvent this issue
 
for i=portallen,1,-1 do
 
-- the use of pcall here catches any errors that may occour when attempting to locate pages when the page name is invalid
 
-- if pcall returns true, then rerun the function to find if the page exists
 
if not pcall(checkPortalExists, portals[i]) or not checkPortalExists(portals[i]) then
 
-- Getting here means a redlinked portal has been found
 
if yesno(args.redlinks) or (args.redlinks == 'include') then
 
-- if redlinks as been set to yes (or similar), add the cleanup category and then break the loop before the portal is removed from the list
 
if (trackingEnabled) then
 
root:wikitext('[[Category:Portal templates with redlinked portals]]')
 
end
 
break
 
end
 
-- remove the portal (this does not happen if redlinks=yes)
 
table.remove(portals,i)
 
end
 
end
 
 
-- if the length of the table is different, then rows were removed from the table, so portals were removed. If this is the case add the cleanup category
 
if not (portallen == #portals) then
 
if (trackingEnabled) then
 
if #portals == 0 then
 
return '[[Category:Portal templates with all redlinked portals]]'
 
else
 
root:wikitext('[[Category:Portal templates with redlinked portals]]')
 
end
 
end
 
 
end
 
end
 
-- Start the list. This corresponds to the start of the wikitext table in the old [[Template:Portal]].
 
local listroot = root:tag('ul')
 
:css('width', type(args.boxsize) == 'string' and (args.boxsize .. 'px') or nil)
 
  
 
-- Display the portals specified in the positional arguments.
 
-- Display the portals specified in the positional arguments.
Line 207: Line 104:
  
 
-- Generate the html for the image and the portal name.
 
-- Generate the html for the image and the portal name.
listroot
+
tableroot
 
:newline()
 
:newline()
:tag('li')
+
:tag('tr')
:tag('span')
+
:css('vertical-align', 'middle')
:wikitext(string.format('[[File:%s|32x28px|class=noviewer]]', image))
+
:tag('td')
 +
:css('text-align', 'center')
 +
:wikitext(string.format('[[File:%s|32x28px|alt=Portal icon|class=noviewer]]', image))
 
:done()
 
:done()
:tag('span')
+
:tag('td')
 +
:css('padding', '0 0.2em')
 +
:css('vertical-align', 'middle')
 +
:css('font-style', 'italic')
 +
:css('font-weight', 'bold')
 
:wikitext(string.format('[[Portal:%s|%s%sportal]]', portal, portal, args['break'] and '<br />' or ' '))
 
:wikitext(string.format('[[Portal:%s|%s%sportal]]', portal, portal, args['break'] and '<br />' or ' '))
 
end
 
end
Line 220: Line 123:
  
 
function p._image(portals)
 
function p._image(portals)
 +
 
-- Wrapper function to allow getImageName() to be accessed through #invoke.
 
-- Wrapper function to allow getImageName() to be accessed through #invoke.
local name = getImageName(portals[1])
+
return getImageName(portals[1])
return name:match('^(.-)|') or name -- FIXME: use a more elegant way to separate borders etc. from the image name
 
 
end
 
end
  
Line 319: Line 222:
 
end
 
end
 
end
 
end
+
return p[funcName](processPortalArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments.
local results = ''
 
if funcName == '_portal' or funcName == '_displayAll' then
 
results = frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} }
 
end
 
return results .. p[funcName](processPortalArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments.
 
 
end
 
end
 
end
 
end

Please note that all contributions to All About Ayrshire 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 All About Ayrshire:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)

Template used on this page: