Mô đun:CurrentCandidateList/sandbox
Đây là trang chỗ thử mô đun cho Mô đun:CurrentCandidateList (khác). |
Tài liệu mô đun[tạo]
local p = {}
local titles = {
FA = "Wikipedia:Ứng cử viên bài viết chọn lọc",
FL = "Wikipedia:Ứng cử viên danh sách chọn lọc",
FT = "Wikipedia:Ứng cử viên chủ điểm chọn lọc",
GA = "Wikipedia:Ứng cử viên bài viết tốt",
GT = "Wikipedia:Ứng cử viên chủ điểm tốt",
FAR = "Wikipedia:Đề nghị rút sao bài viết chọn lọc",
FLR = "Wikipedia:Đề nghị rút sao danh sách chọn lọc",
FAR2 = "Wikipedia:Đề nghị giáng sao bài viết chọn lọc",
GAR = "Wikipedia:Đề nghị rút sao bài viết tốt",
AfD = "Wikipedia:Biểu quyết xoá bài",
RfE = "Wikipedia:Biểu quyết chọn điều phối viên",
RfA = "Wikipedia:Biểu quyết chọn bảo quản viên",
RfB = "Wikipedia:Biểu quyết chọn hành chính viên"
}
local alts = {
FA = "Bài viết chọn lọc",
FL = "Danh sách chọn lọc",
FT = "Chủ điểm chọn lọc",
GA = "Bài viết tốt",
GT = "Chủ điểm tốt",
FAR = "Rút sao",
FLR = "Rút sao danh sách chọn lọc",
FAR2 = "Giáng sao",
GAR = "Rút sao",
AfD = "Xóa bài",
RfE = "Chọn điều phối viên",
RfA = "Chọn bảo quản viên",
RfB = "Chọn hành chính viên"
}
-- @type table
function p.list(title)
local content = mw.title.new(title):getContent()
local list = {}
for token in string.gmatch(content, "{{/(.-)}}") do
if token ~= "Quy trình" and token ~= "Gợi ý" then
table.insert(list, token)
end
end
if #list > 10 then
local tempList = {}
local listShuffled = FYShuffle(list)
for i = 1, 10, 1 do
table.insert(tempList, listShuffled[i])
end
list = tempList
end
return list
end
-- @type string
function p.num(frame)
local title = titles[frame.args[1]]
return p._num(title)
end
-- @type string
function p._num(title)
return #p.list(title)
end
-- @type string
function p.str(frame)
local title = titles[frame.args[1]]
local sep = " • "
return p._str(title, sep)
end
-- @type string
function p._str(title, sep)
local str = {}
for _, candidate in ipairs(p.list(title)) do
table.insert(str, anchor(title, candidate))
end
return table.concat(str, sep)
end
-- @type string
function p.group(frame)
local pages = frame.args
local sep = " • "
return p._group(pages, sep)
end
-- @type string
function p._group(pages, sep)
local group = {}
for _, page in ipairs(pages) do
local title = titles[page]
local alt = alts[page]
local list = p.list(title)
if (#list ~= 0) then
table.insert(group, prefix(title, alt) .. p._str(title, sep))
end
end
return table.concat(group, sep)
end
-- @type string
function anchor(title, candidate)
return "[[" .. title .. "/" .. candidate .. "|" .. candidate .. "]]"
end
-- @type string
function prefix(title, alt)
return "'''[[" .. title .. "|" .. alt .. "]]''': "
end
-- Source: https://stackoverflow.com/questions/35572435/how-do-you-do-the-fisher-yates-shuffle-in-lua
function FYShuffle(tInput)
math.randomseed(os.time())
local tReturn = {}
for i = #tInput, 1, -1 do
local j = math.random(i)
tInput[i], tInput[j] = tInput[j], tInput[i]
table.insert(tReturn, tInput[i])
end
return tReturn
end
return p