Module:Portal: Difference between revisions

Content deleted Content added
add class=noviewer to images to hide from mediaviewer
Jackmcbarn (talk | contribs)
use mw.html and other fixes from sandbox
Line 4:
 
-- The portal image data is kept in submodules of [[Module:Portal/images]], listed below:
-- [[Module:Portal/images/a]] - for portal names beginning with "A".
-- [[Module:Portal/images/b]] - for portal names beginning with "B".
-- [[Module:Portal/images/c]] - for portal names beginning with "C".
-- [[Module:Portal/images/d]] - for portal names beginning with "D".
-- [[Module:Portal/images/e]] - for portal names beginning with "E".
-- [[Module:Portal/images/f]] - for portal names beginning with "F".
-- [[Module:Portal/images/g]] - for portal names beginning with "G".
-- [[Module:Portal/images/h]] - for portal names beginning with "H".
-- [[Module:Portal/images/i]] - for portal names beginning with "I".
-- [[Module:Portal/images/j]] - for portal names beginning with "J".
-- [[Module:Portal/images/k]] - for portal names beginning with "K".
-- [[Module:Portal/images/l]] - for portal names beginning with "L".
-- [[Module:Portal/images/m]] - for portal names beginning with "M".
-- [[Module:Portal/images/n]] - for portal names beginning with "N".
-- [[Module:Portal/images/o]] - for portal names beginning with "O".
-- [[Module:Portal/images/p]] - for portal names beginning with "P".
-- [[Module:Portal/images/q]] - for portal names beginning with "Q".
-- [[Module:Portal/images/r]] - for portal names beginning with "R".
-- [[Module:Portal/images/s]] - for portal names beginning with "S".
-- [[Module:Portal/images/t]] - for portal names beginning with "T".
-- [[Module:Portal/images/u]] - for portal names beginning with "U".
-- [[Module:Portal/images/v]] - for portal names beginning with "V".
-- [[Module:Portal/images/w]] - for portal names beginning with "W".
-- [[Module:Portal/images/x]] - for portal names beginning with "X".
-- [[Module:Portal/images/y]] - for portal names beginning with "Y".
-- [[Module:Portal/images/z]] - for portal names beginning with "Z".
-- [[Module:Portal/images/other]] - for portal names beginning with any other letters. This includes numbers,
-- letters with diacritics, and letters in non-Latin alphabets.
-- [[Module:Portal/images/aliases]] - 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.
Line 39:
-- 5,000,000 pages using this module needed to be refreshed every time an image was added or removed.
]==]
 
local htmlBuilder = require('Module:HtmlBuilder')
 
local p = {}
Line 53 ⟶ 51:
imagePage = 'Module:Portal/images/' .. firstLetter
else
imagePage = 'Module:Portal/images/other'
end
local images = mw.loadData(imagePage)
local image = images[s]
if image then
return image
end
local images =return mw.loadData(imagePage)[s]
end
 
Line 80 ⟶ 74:
end
s = mw.ustring.lower(s)
localreturn imagematchImagePage(s) =or matchImagePage(getAlias(s)) or 'Portal-puzzle.svg'
if image then
return image
else
local alias = getAlias(s)
image = matchImagePage(alias) -- If no alias was found this returns nil.
if image then
return image
else
return 'Portal-puzzle.svg'
end
end
end
 
function p._portal(portals, args)
-- This function builds the portal box used by the {{portal}} template.
local root = htmlBuildermw.html.create('div')
.:addClass('noprint portal')
root
.:addClass(args.left and 'tleft' or 'noprinttright')
.:css('border', 'solid #aaa 1px')
.addClass(args.left and 'tleft' or 'tright')
.:css('margin', args.margin or (args.left == 'yes' and '0.5em 1em 0.5em 0') or '0.5em 0 0.5em 1em')
.addClass('portal')
.:newline()
.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()
 
-- Start the table. This corresponds to the start of the wikitext table in the old [[Template:Portal]].
local tableroot = root.:tag('table')
.:css('background', '#f9f9f9')
.:css('font-size', '85%')
.:css('line-height', '110%')
.:css('max-width', '175px')
.:css('width', type(args.boxsize) == 'string' and (args.boxsize .. 'px') or nil)
 
-- If no portals have been specified, display an error and add the page to a tracking category.
if not portals[1] then
tableroot.:wikitext('<strong class="error">No portals specified: please specify at least one portal</strong>[[Category:Portal templates without a parameter]]')
end
 
-- Display the portals specified in the positional arguments.
for i_, portal in ipairs(portals) do
local image = getImageName(portal)
 
-- Generate the html for the image and the portal name.
tableroot
.:newline()
.:tag('tr')
.:css('vertical-align', 'middle')
.:tag('td')
.:css('text-align', 'center')
.:wikitext(mw.ustringstring.format('[[File:%s|32x28px|alt=Portal icon|class=noviewer]]', image))
.:done()
.:tag('td')
.:css('padding', '0 0.2em')
.:css('vertical-align', 'middle')
.:css('font-style', 'italic')
.:css('font-weight', 'bold')
.:wikitext(mw.ustringstring.format('[[Portal:%s|%s%sportal]]', portal, portal, args['break'] and '<br />' or ' '))
end
return tostring(root)
Line 150 ⟶ 131:
-- Returns an array containing all image subpages (minus aliases) as loaded by mw.loadData.
local images = {}
localfor subpagesi, subpage =in ipairs{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'other'} do
table.insert(images,[i] = mw.loadData('Module:Portal/images/' .. subpage))
for i, subpage in ipairs(subpages) do
table.insert(images, mw.loadData('Module:Portal/images/' .. subpage))
end
return images
Line 162 ⟶ 142:
-- names are capitalized, so the portal links may be broken.
local lang = mw.language.getContentLanguage()
local images = getAllImageTables()
local count = 1
for i_, imageTable in ipairs(imagesgetAllImageTables()) do
for portal in pairs(imageTable) do
portals[count] = lang:ucfirst(portal)
Line 178 ⟶ 157:
-- should be moved to a portal alias for ease of maintenance.
local exists, dupes = {}, {}
localfor images_, =imageTable in ipairs(getAllImageTables()) do
for i, imageTable in ipairs(images) do
for portal, image in pairs(imageTable) do
if not exists[image] then
exists[image] = portal
else
table.insert(dupes, mw.ustringstring.format('The image "[[:File:%s|%s]]" is used for both portals "%s" and "%s".', image, image, exists[image], portal))
end
end
Line 194 ⟶ 172:
end
end
 
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
Line 225 ⟶ 203:
-- or from another Lua module.
local origArgs
if type(frame.getParent) == mw.getCurrentFrame()'function' then
origArgs = frame:getParent().args
for k, v in pairs(frame.args) do
Line 248 ⟶ 226:
end
 
localfor funcNames_, funcName =in ipairs{'portal', 'image', 'imageDupes', 'displayAll'} do
 
for _, funcName in ipairs(funcNames) do
p[funcName] = makeWrapper('_' .. funcName)
end