Module:Gallery
Appearance
Usage
random
A gallery with items in randomized order. The random order is cached on the server side, but MediaWiki:Gadget-gallery.js (enabled by default) shuffles the images again on the client side to ensure a different order on each page load.
{{#invoke: gallery | random
| mode = packed-hover
| heights = 200
| 1 = OSM edits in Wuhan 2020.jpg | c1 = Age of OpenStreetMap data in Wuhan
| 2 = City-roads.png | c2 = City Roads
| 3 = Road names in London OSMnx.jpg | c3 = Road names in London
| 4 = Brumadinho dam catastrophy 2.jpg | c4 = Brumadinho dam disaster
| 5 = Malaga-bus.jpg | c5 = A bus in Málaga
}}
-
Brumadinho dam disaster
-
City Roads
-
Road names in London
-
A bus in Málaga
-
Age of OpenStreetMap data in Wuhan
Any attribute supported by the <gallery> tag is supported as a parameter. Each image goes in a numbered parameter, along with corresponding parameters |cn = for the caption, |an = for the alt text, and |ln = for the link URL, all optional.
local p = {}
-- Fisher–Yates shuffle
-- https://stackoverflow.com/a/68486276
local function shuffle(t)
for i = #t, 2, -1 do
local j = math.random(i)
t[i], t[j] = t[j], t[i]
end
end
-- Returns a gallery with the items in random order.
function p.random(frame)
local files = {}
for i, arg in ipairs(frame.args) do
table.insert(files, arg)
end
local links = {}
for key, value in pairs(frame.args) do
local i = not tonumber(key) and key:match("l(%d+)")
if i and tonumber(i) then
links[tonumber(i)] = value
end
end
local alts = {}
for key, value in pairs(frame.args) do
local i = not tonumber(key) and key:match("a(%d+)")
if i and tonumber(i) then
alts[tonumber(i)] = value
end
end
local captions = {}
for key, value in pairs(frame.args) do
local i = not tonumber(key) and key:match("c(%d+)")
if i and tonumber(i) then
captions[tonumber(i)] = value
end
end
local items = {}
for i, file in ipairs(files) do
local components = { file }
if links[i] then
-- Append a space to prevent Gallery extension from interpreting the
-- rest of the word as part of the URL.
table.insert(components, mw.ustring.format("link=%s ", links[i]))
end
if alts[i] then
table.insert(components, mw.ustring.format("alt=%s", alts[i]))
end
if captions[i] then
table.insert(components, captions[i])
end
table.insert(items, table.concat(components, "|"))
end
local seed = tonumber(mw.getLanguage( "en" ):formatDate( "U" )) * 10000 + os.clock() * 10000
math.randomseed(seed)
shuffle(items)
local content = table.concat(items, "\n")
local args = {
caption = frame.args.caption,
widths = frame.args.widths,
heights = frame.args.heights,
perrow = frame.args.perrow,
showfilename = frame.args.showfilename,
mode = frame.args.mode,
showthumbnails = frame.args.showthumbnails,
id = frame.args.id,
title = frame.args.title,
class = frame.args.class,
style = frame.args.style,
}
local gallery = frame:extensionTag {
name = "gallery",
content = content,
args = args,
}
return mw.html.create("div")
:addClass("osm-gallery-random")
:wikitext(gallery)
end
return p