https://mw.lojban.org/index.php?title=Module:Portal&feed=atom&action=historyModule:Portal - Revision history2024-03-29T02:16:01ZRevision history for this page on the wikiMediaWiki 1.38.4https://mw.lojban.org/index.php?title=Module:Portal&diff=97405&oldid=prevGleki: 1 revision2014-06-26T07:45:24Z<p>1 revision</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 07:45, 26 June 2014</td>
</tr>
<!-- diff cache key mediawiki:diff::1.12:old-85267:rev-97405 -->
</table>Glekihttps://mw.lojban.org/index.php?title=Module:Portal&diff=85267&oldid=prevGleki: 1 revision2014-03-01T11:51:09Z<p>1 revision</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 11:51, 1 March 2014</td>
</tr>
<!-- diff cache key mediawiki:diff::1.12:old-85266:rev-85267 -->
</table>Glekihttps://mw.lojban.org/index.php?title=Module:Portal&diff=85266&oldid=prevGleki: 1 revision2014-02-28T11:55:04Z<p>1 revision</p>
<p><b>New page</b></p><div>--[==[ This module is a Lua implementation of the old {{Portal}} template. As of August 2013 it is used on nearly 5,000,000 articles.<br />
-- Please take care when updating it! It outputs two functions: p.portal, which generates a table of portals, and p.image, which<br />
-- produces the image name for an individual portal.<br />
<br />
-- The portal image data is kept in submodules of [[Module:Portal/images]], listed below:<br />
-- [[Module:Portal/images/a]] - for portal names beginning with "A".<br />
-- [[Module:Portal/images/b]] - for portal names beginning with "B".<br />
-- [[Module:Portal/images/c]] - for portal names beginning with "C".<br />
-- [[Module:Portal/images/d]] - for portal names beginning with "D".<br />
-- [[Module:Portal/images/e]] - for portal names beginning with "E".<br />
-- [[Module:Portal/images/f]] - for portal names beginning with "F".<br />
-- [[Module:Portal/images/g]] - for portal names beginning with "G".<br />
-- [[Module:Portal/images/h]] - for portal names beginning with "H".<br />
-- [[Module:Portal/images/i]] - for portal names beginning with "I".<br />
-- [[Module:Portal/images/j]] - for portal names beginning with "J".<br />
-- [[Module:Portal/images/k]] - for portal names beginning with "K".<br />
-- [[Module:Portal/images/l]] - for portal names beginning with "L".<br />
-- [[Module:Portal/images/m]] - for portal names beginning with "M".<br />
-- [[Module:Portal/images/n]] - for portal names beginning with "N".<br />
-- [[Module:Portal/images/o]] - for portal names beginning with "O".<br />
-- [[Module:Portal/images/p]] - for portal names beginning with "P".<br />
-- [[Module:Portal/images/q]] - for portal names beginning with "Q".<br />
-- [[Module:Portal/images/r]] - for portal names beginning with "R".<br />
-- [[Module:Portal/images/s]] - for portal names beginning with "S".<br />
-- [[Module:Portal/images/t]] - for portal names beginning with "T".<br />
-- [[Module:Portal/images/u]] - for portal names beginning with "U".<br />
-- [[Module:Portal/images/v]] - for portal names beginning with "V".<br />
-- [[Module:Portal/images/w]] - for portal names beginning with "W".<br />
-- [[Module:Portal/images/x]] - for portal names beginning with "X".<br />
-- [[Module:Portal/images/y]] - for portal names beginning with "Y".<br />
-- [[Module:Portal/images/z]] - for portal names beginning with "Z".<br />
-- [[Module:Portal/images/other]] - for portal names beginning with any other letters. This includes numbers,<br />
-- letters with diacritics, and letters in non-Latin alphabets.<br />
-- [[Module:Portal/images/aliases]] - for adding aliases for existing portal names. Use this page for variations<br />
-- in spelling and diacritics, etc., no matter what letter the portal begins with.<br />
--<br />
-- The images data pages are separated by the first letter to reduce server load when images are added, changed, or removed.<br />
-- Previously all the images were on one data page at [[Module:Portal/images]], but this had the disadvantage that all<br />
-- 5,000,000 pages using this module needed to be refreshed every time an image was added or removed.<br />
]==]<br />
<br />
local htmlBuilder = require('Module:HtmlBuilder')<br />
<br />
local p = {}<br />
<br />
local function matchImagePage(s)<br />
-- Finds the appropriate image subpage given a lower-case<br />
-- portal name plus the first letter of that portal name.<br />
if type(s) ~= 'string' or #s < 1 then return end<br />
local firstLetter = mw.ustring.sub(s, 1, 1)<br />
local imagePage<br />
if mw.ustring.find(firstLetter, '^[a-z]') then<br />
imagePage = 'Module:Portal/images/' .. firstLetter<br />
else<br />
imagePage= 'Module:Portal/images/other'<br />
end<br />
local images = mw.loadData(imagePage)<br />
local image = images[s]<br />
if image then<br />
return image<br />
end<br />
end<br />
<br />
local function getAlias(s)<br />
-- Gets an alias from the image alias data page.<br />
local aliasData = mw.loadData('Module:Portal/images/aliases')<br />
for portal, aliases in pairs(aliasData) do<br />
for _, alias in ipairs(aliases) do<br />
if alias == s then<br />
return portal<br />
end<br />
end<br />
end<br />
end<br />
<br />
local function getImageName(s)<br />
-- Gets the image name for a given string.<br />
if type(s) ~= 'string' or #s < 1 then<br />
return 'Portal-puzzle.svg'<br />
end<br />
s = mw.ustring.lower(s)<br />
local image = matchImagePage(s)<br />
if image then<br />
return image<br />
else<br />
local alias = getAlias(s)<br />
image = matchImagePage(alias) -- If no alias was found this returns nil.<br />
if image then<br />
return image<br />
else<br />
return 'Portal-puzzle.svg'<br />
end<br />
end<br />
end<br />
<br />
function p._portal(portals, args)<br />
-- This function builds the portal box used by the {{portal}} template.<br />
local root = htmlBuilder.create('div')<br />
root<br />
.addClass('noprint')<br />
.addClass(args.left and 'tleft' or 'tright')<br />
.addClass('portal')<br />
.css('border', 'solid #aaa 1px')<br />
.css('margin', args.margin or (args.left == 'yes' and '0.5em 1em 0.5em 0') or '0.5em 0 0.5em 1em')<br />
.newline()<br />
<br />
-- Start the table. This corresponds to the start of the wikitext table in the old [[Template:Portal]].<br />
local tableroot = root.tag('table')<br />
.css('background', '#f9f9f9')<br />
.css('font-size', '85%')<br />
.css('line-height', '110%')<br />
.css('max-width', '175px')<br />
.css('width', type(args.boxsize) == 'string' and (args.boxsize .. 'px'))<br />
<br />
-- If no portals have been specified, display an error and add the page to a tracking category.<br />
if not portals[1] then<br />
tableroot.wikitext('<strong class="error">No portals specified: please specify at least one portal</strong>[[Category:Portal templates without a parameter]]')<br />
end<br />
<br />
-- Display the portals specified in the positional arguments.<br />
for i, portal in ipairs(portals) do<br />
local image = getImageName(portal)<br />
<br />
-- Generate the html for the image and the portal name.<br />
tableroot<br />
.newline()<br />
.tag('tr')<br />
.attr('valign', 'middle')<br />
.tag('td')<br />
.css('text-align', 'center')<br />
.wikitext(mw.ustring.format('[[File:%s|32x28px|alt=Portal icon]]', image))<br />
.done()<br />
.tag('td')<br />
.css('padding', '0 0.2em')<br />
.css('vertical-align', 'middle')<br />
.css('font-style', 'italic')<br />
.css('font-weight', 'bold')<br />
.wikitext(mw.ustring.format('[[Portal:%s|%s%sportal]]', portal, portal, args['break'] and '<br />' or ' '))<br />
end<br />
return tostring(root)<br />
end<br />
<br />
function p._image(portals)<br />
<br />
-- Wrapper function to allow getImageName() to be accessed through #invoke.<br />
return getImageName(portals[1])<br />
end<br />
<br />
local function getAllImageTables()<br />
-- Returns an array containing all image subpages (minus aliases) as loaded by mw.loadData.<br />
local images = {}<br />
local subpages = {'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'}<br />
for i, subpage in ipairs(subpages) do<br />
table.insert(images, mw.loadData('Module:Portal/images/' .. subpage))<br />
end<br />
return images<br />
end<br />
<br />
function p._displayAll(portals, args)<br />
-- This function displays all portals that have portal images. This function is for maintenance purposes and should not be used in<br />
-- articles, for two reasons: 1) there are over 1500 portals with portal images, and 2) the module doesn't record how the portal<br />
-- names are capitalized, so the portal links may be broken.<br />
local lang = mw.language.getContentLanguage()<br />
local images = getAllImageTables()<br />
local count = 1<br />
for i, imageTable in ipairs(images) do<br />
for portal in pairs(imageTable) do<br />
portals[count] = lang:ucfirst(portal)<br />
count = count + 1<br />
end<br />
end<br />
return p._portal(portals, args)<br />
end<br />
<br />
function p._imageDupes()<br />
-- This function searches the image subpages to find duplicate images. If duplicate images exist, it is not necessarily a bad thing,<br />
-- as different portals might just happen to choose the same image. However, this function is helpful in identifying images that<br />
-- should be moved to a portal alias for ease of maintenance.<br />
local exists, dupes = {}, {}<br />
local images = getAllImageTables()<br />
for i, imageTable in ipairs(images) do<br />
for portal, image in pairs(imageTable) do<br />
if not exists[image] then<br />
exists[image] = portal<br />
else<br />
table.insert(dupes, mw.ustring.format('The image "[[:File:%s|%s]]" is used for both portals "%s" and "%s".', image, image, exists[image], portal))<br />
end<br />
end<br />
end<br />
if #dupes < 1 then<br />
return 'No duplicate images found.'<br />
else<br />
return 'The following duplicate images were found:\n* ' .. table.concat(dupes, '\n* ')<br />
end<br />
end<br />
<br />
local function processPortalArgs(args)<br />
-- This function processes a table of arguments and returns two tables: an array of portal names for processing by ipairs, and a table of<br />
-- the named arguments that specify style options, etc. We need to use ipairs because we want to list all the portals in the order<br />
-- they were passed to the template, but we also want to be able to deal with positional arguments passed explicitly, for example<br />
-- {{portal|2=Politics}}. The behaviour of ipairs is undefined if nil values are present, so we need to make sure they are all removed.<br />
args = type(args) == 'table' and args or {}<br />
local portals = {}<br />
local namedArgs = {}<br />
for k, v in pairs(args) do<br />
if type(k) == 'number' and type(v) == 'string' then -- Make sure we have no non-string portal names.<br />
table.insert(portals, k)<br />
elseif type(k) ~= 'number' then<br />
namedArgs[k] = v<br />
end<br />
end<br />
table.sort(portals)<br />
for i, v in ipairs(portals) do<br />
portals[i] = args[v]<br />
end<br />
return portals, namedArgs<br />
end<br />
<br />
local function makeWrapper(funcName)<br />
-- Processes external arguments and sends them to the other functions.<br />
return function (frame)<br />
-- If called via #invoke, use the args passed into the invoking<br />
-- template, or the args passed to #invoke if any exist. Otherwise<br />
-- assume args are being passed directly in from the debug console<br />
-- or from another Lua module.<br />
local origArgs<br />
if frame == mw.getCurrentFrame() then<br />
origArgs = frame:getParent().args<br />
for k, v in pairs(frame.args) do<br />
origArgs = frame.args<br />
break<br />
end<br />
else<br />
origArgs = frame<br />
end<br />
-- Trim whitespace and remove blank arguments.<br />
local args = {}<br />
for k, v in pairs(origArgs) do<br />
if type(v) == 'string' then<br />
v = mw.text.trim(v)<br />
end<br />
if v ~= '' then<br />
args[k] = v<br />
end<br />
end<br />
return p[funcName](processPortalArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments.<br />
end<br />
end<br />
<br />
local funcNames = {'portal', 'image', 'imageDupes', 'displayAll'}<br />
<br />
for _, funcName in ipairs(funcNames) do<br />
p[funcName] = makeWrapper('_' .. funcName)<br />
end<br />
<br />
return p</div>Gleki