Module:Portal: Difference between revisions

alphabetize "default image" tracking category
m (Reverted edits by Insui (talk) to last revision by Heku)
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.
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 defaultImage
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 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]]',
getImageName(portal))
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,nostrip)
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 nostrip is yes (or equivalent), then allow all metadata (like image borders) to be returned
-- If keep is yes (or equivalent), then allow all metadata (like image borders) to be returned
local strip = not yesno(nostrip)
local keepargs = yesno(keep)
return strip and name:match('^(.-)|') or name -- FIXME: use a more elegant way to separate borders etc. from the image name
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 getAllImageTables()
 
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
images[i] = mw.loadData('Module:Portal/images/' .. subpage .. sandbox)
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()
local count = 1
portals = portals or {}
for _, imageTable in ipairs(getAllImageTables()) do
for portal in pairs(getAllImageTable()) do
for portal in pairs(imageTable) do
table.insert(portals,lang:ucfirst(portal))
portals[count] = lang:ucfirst(portal)
count = count + 1
end
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 _, imageTable in ipairs(getAllImageTables()) do
for portal, image in pairs(getAllImageTable()) do
for portal, image in pairs(imageTable) do
if not exists[image] then
if not exists[image] then
exists[image] = portal
exists[image] = portal
else
else
table.insert(dupes, string.format('The image "[[:File:%s|%s]]" is used for both portals "%s" and "%s".', image, image, exists[image], portal))
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
end
Anonymous user