Module:Soldier: Difference between revisions
Remove assignment from soldier infobox |
Auto-match uploaded soldier photos by filename |
||
| Line 1,051: | Line 1,051: | ||
function p.classLinks(frame) | function p.classLinks(frame) | ||
return table.concat(classLinkList(parentArg(frame, 'class_number'), false), ', ') | return table.concat(classLinkList(parentArg(frame, 'class_number'), false), ', ') | ||
end | |||
local PHOTO_EXTENSIONS = { 'jpg', 'jpeg', 'png', 'webp', 'gif' } | |||
local function photoFileExists(filename) | |||
filename = trim(filename) | |||
if filename == '' then | |||
return false | |||
end | |||
local title = mw.title.new('File:' .. filename) | |||
return title ~= nil and title.exists | |||
end | |||
local function pushPhotoCandidate(candidates, seen, value) | |||
value = trim(value) | |||
if value == '' or seen[value] then | |||
return | |||
end | |||
seen[value] = true | |||
table.insert(candidates, value) | |||
end | |||
local function resolvePhoto(frame) | |||
local explicit = trim(parentArg(frame, 'photo')) | |||
if explicit ~= '' then | |||
return explicit | |||
end | |||
local first = trim(parentArg(frame, 'first')) | |||
local surname = trim(parentArg(frame, 'surname')) | |||
local asn = trim(parentArg(frame, 'asn')) | |||
if first == '' or surname == '' then | |||
return '' | |||
end | |||
local candidates = {} | |||
local seen = {} | |||
local lowerFirst = mw.ustring.lower(first) | |||
local lowerSurname = mw.ustring.lower(surname) | |||
local bases = { | |||
first .. ' ' .. surname, | |||
first .. ' ' .. lowerSurname, | |||
lowerFirst .. ' ' .. lowerSurname, | |||
first .. '_' .. surname, | |||
first .. '_' .. lowerSurname, | |||
lowerFirst .. '_' .. lowerSurname, | |||
surname .. ', ' .. first, | |||
surname .. '_' .. first, | |||
} | |||
if asn ~= '' then | |||
table.insert(bases, surname .. ', ' .. first .. ' (' .. asn .. ')') | |||
end | |||
for _, base in ipairs(bases) do | |||
pushPhotoCandidate(candidates, seen, base) | |||
end | |||
for _, base in ipairs(candidates) do | |||
for _, ext in ipairs(PHOTO_EXTENSIONS) do | |||
local filename = base .. '.' .. ext | |||
if photoFileExists(filename) then | |||
return filename | |||
end | |||
local upperExt = mw.ustring.upper(ext) | |||
if upperExt ~= ext then | |||
filename = base .. '.' .. upperExt | |||
if photoFileExists(filename) then | |||
return filename | |||
end | |||
end | |||
end | |||
end | |||
return '' | |||
end | end | ||
| Line 1,063: | Line 1,135: | ||
:wikitext(name) | :wikitext(name) | ||
local photo = | local photo = resolvePhoto(frame) | ||
if photo ~= '' then | if photo ~= '' then | ||
local size = parentArg(frame, 'image_size') | local size = parentArg(frame, 'image_size') | ||