Module:Portal: Difference between revisions
alphabetize "default image" tracking category
Tag: Rollback |
ww>Hike395 (alphabetize "default image" tracking category) |
||
Line 99: | Line 99: | ||
end | end | ||
end | end | ||
local defaultImage = 'Portal-puzzle.svg|link=|alt=' | |||
local function getImageName(s) | local function getImageName(s) | ||
-- Gets the image name for a given string. | -- Gets the image name for a given string. | ||
if type(s) ~= 'string' or #s < 1 then | if type(s) ~= 'string' or #s < 1 then | ||
return | return defaultImage | ||
end | end | ||
s = mw.ustring.lower(s) | s = mw.ustring.lower(s) | ||
return matchImagePage(s) or matchImagePage(getAlias(s)) or | return matchImagePage(s) or matchImagePage(getAlias(s)) or defaultImage | ||
end | end | ||
Line 236: | Line 237: | ||
local contents = {} | local contents = {} | ||
-- Display the portals specified in the positional arguments. | -- Display the portals specified in the positional arguments. | ||
local defaultUsed = nil | |||
for _, portal in ipairs(portals) do | for _, portal in ipairs(portals) do | ||
local portalImage = getImageName(portal) | |||
if portalImage == defaultImage then | |||
defaultUsed = portal | |||
end | |||
local image = string.format('[[File:%s|32x28px|class=noviewer]]', | local image = string.format('[[File:%s|32x28px|class=noviewer]]', | ||
portalImage) | |||
local link = string.format('[[Portal:%s|%s%sportal]]', | local link = string.format('[[Portal:%s|%s%sportal]]', | ||
portal, portal, args.addBreak and '<br />' or ' ') | portal, portal, args.addBreak and '<br />' or ' ') | ||
table.insert(contents, {image, link}) | table.insert(contents, {image, link}) | ||
end | |||
if defaultUsed and checkTracking() then | |||
local cat = string.format('[[Category:Portal templates with default image|%s]]', | |||
defaultUsed) | |||
root:wikitext(cat) | |||
end | end | ||
return tostring(fillBox(root, contents)) | return tostring(fillBox(root, contents)) | ||
Line 265: | Line 276: | ||
end | end | ||
function p._image(portal, | function p._image(portal,keep) | ||
-- Wrapper function to allow getImageName() to be accessed through #invoke. | -- Wrapper function to allow getImageName() to be accessed through #invoke. | ||
-- backward compatibility: if table passed, take first element | -- backward compatibility: if table passed, take first element | ||
Line 272: | Line 283: | ||
end | end | ||
local name = getImageName(portal) | local name = getImageName(portal) | ||
-- If | -- If keep is yes (or equivalent), then allow all metadata (like image borders) to be returned | ||
local | local keepargs = yesno(keep) | ||
local args = mw.text.split(name, "|", true) | |||
local result = {args[1]} -- the filename always comes first | |||
local category = '' | |||
-- parse name, looking for category arguments | |||
for i = 2,#args do | |||
local m = mw.ustring.match(args[i], "^%s*category%s*=") | |||
if keepargs or m then | |||
table.insert(result, args[i]) | |||
end | |||
end | |||
-- reassemble arguments | |||
return table.concat(result,"|") | |||
end | end | ||
local function | |||
local function getAllImageTable() | |||
-- Returns an array containing all image subpages (minus aliases) as loaded by mw.loadData. | -- Returns an array containing all image subpages (minus aliases) as loaded by mw.loadData. | ||
local images = {} | local images = {} | ||
for i, 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 | for i, 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 | ||
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 | end | ||
return images | return images | ||
Line 291: | Line 318: | ||
-- names are capitalized, so the portal links may be broken. | -- names are capitalized, so the portal links may be broken. | ||
local lang = mw.language.getContentLanguage() | local lang = mw.language.getContentLanguage() | ||
portals = portals or {} | |||
for | for portal in pairs(getAllImageTable()) do | ||
table.insert(portals,lang:ucfirst(portal)) | |||
end | end | ||
table.sort(portals) | |||
args.redlinks = args.redlinks or "yes" | |||
return p._portal(portals, args) | return p._portal(portals, args) | ||
end | end | ||
Line 306: | Line 332: | ||
-- should be moved to a portal alias for ease of maintenance. | -- should be moved to a portal alias for ease of maintenance. | ||
local exists, dupes = {}, {} | local exists, dupes = {}, {} | ||
for | for portal, image in pairs(getAllImageTable()) do | ||
if not exists[image] then | |||
exists[image] = portal | |||
else | |||
table.insert(dupes, string.format('The image "[[:File:%s|%s]]" is used for both portals "%s" and "%s".', image, image, exists[image], portal)) | |||
end | end | ||
end | end |