Module:Soldier: Difference between revisions
Related rail: include MII/|teams=/notes teams; roster co-assignment note |
Related rail: teams only; names from team categories (incl. MII) |
||
| Line 2,396: | Line 2,396: | ||
function p.relatedSoldiers(frame) | function p.relatedSoldiers(frame) | ||
local pageTitle = mw.title.getCurrentTitle().prefixedText | local pageTitle = mw.title.getCurrentTitle().prefixedText | ||
local sections = {} | local sections = {} | ||
local function | -- Members from the team category (works for IPW, MII, and note-derived teams). | ||
local | -- Do not sample classmates: classes are huge and a short list looks arbitrary. | ||
local function teamCategoryMemberLinks(teamName, maxLinks) | |||
maxLinks = maxLinks or 24 | |||
local ok, html = pcall(function() | |||
return frame:preprocess( | |||
string.format( | |||
'{{#categorytree:%s|mode=pages|hideroot=on|namespaces=0}}', | |||
'Team ' .. teamName | |||
) | |||
) | |||
end) | |||
if not ok or type(html) ~= 'string' or html == '' then | |||
return {} | |||
end | |||
local links = {} | local links = {} | ||
local seen = {} | |||
for title in mw.ustring.gmatch(html, 'title="([^"]+)"') do | |||
title = mw.text.decode(title, true) | |||
if ( | |||
title ~= pageTitle | |||
and not seen[title] | |||
' | and mw.ustring.find(title, '%(%d') | ||
) | and not mw.ustring.find(title, '^Category:') | ||
) then | |||
seen[title] = true | |||
table.insert( | |||
links, | |||
string.format('[[%s|%s]]', title, titleToCaption(title)) | |||
) | |||
if #links >= maxLinks then | |||
break | |||
end | |||
end | |||
end | end | ||
return links | |||
end | |||
local function addTeamBlock(teamName) | |||
local head = teamNameLink(teamName) | |||
local cat = 'Category:Team ' .. teamName | |||
local links = teamCategoryMemberLinks(teamName, 24) | |||
if #links > 0 then | if #links > 0 then | ||
table.insert( | table.insert( | ||
| Line 2,429: | Line 2,453: | ||
) | ) | ||
else | else | ||
table.insert( | table.insert( | ||
sections, | sections, | ||
| Line 2,455: | Line 2,478: | ||
for _, teamName in ipairs(teamNames) do | for _, teamName in ipairs(teamNames) do | ||
addTeamBlock(teamName) | |||
addTeamBlock(teamName | |||
end | end | ||
| Line 2,517: | Line 2,485: | ||
end | end | ||
return '<div class="soldier-related">' | return '<div class="soldier-related">' | ||
.. '<div class="soldier-related-heading">Same | .. '<div class="soldier-related-heading">Same team</div>' | ||
.. table.concat(sections, '\n') | .. table.concat(sections, '\n') | ||
.. '<div class="soldier-related-note">These are other soldiers listed on the same | .. '<div class="soldier-related-note">These are other soldiers listed on the same team roster. ' | ||
.. 'People moved between teams; overlapping service dates are not always documented.</div>' | .. 'People moved between teams; overlapping service dates are not always documented.</div>' | ||
.. '</div>' | .. '</div>' | ||