Module:Changelog: Difference between revisions
m removed unneeded invokes |
m swap mw.getCurrentFrame() with frame |
||
(32 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
local p = {}; | local p = {}; | ||
local date_module = require('Module:Date') | local date_module = require('Module:Date') | ||
local | local changelog_configs = mw.loadJsonData("Data:ChangelogConfigs.json") | ||
local tag_tree = mw.loadJsonData("Data:ChangelogTagTree.json") | |||
local dictionary_module = require('Module:Dictionary') | |||
-- Takes in changelog_configs | |||
-- Returns [oldest_id, next_oldest_id, ..., newest_id] | |||
local function get_ordered_ids(data) | |||
-- Collect the keys into a table | |||
local keys = {} | |||
for key in pairs(data) do | |||
table.insert(keys, key) | |||
end | |||
-- Sort the keys (ascending order) | |||
table.sort(keys) | |||
-- Build an ordered array | |||
local ordered_array = {} | |||
for _, key in ipairs(keys) do | |||
table.insert(ordered_array, key) | |||
end | |||
return ordered_array | |||
end | |||
--Global scope | |||
local ordered_ids = get_ordered_ids(changelog_configs) | |||
--Only Data:Changelog_05-03-2024.json is loaded right now, as it still needs work and for now it was uploaded manually, need an uploader script to handle it | --Only Data:Changelog_05-03-2024.json is loaded right now, as it still needs work and for now it was uploaded manually, need an uploader script to handle it | ||
--Returns all lines of a given date that has that tag | --Returns all lines of a given date that has that tag | ||
local function | local function write_changelog_lines(id, config, tag_to_filter) | ||
-- Get the data for the respective line | -- Get the data for the respective line | ||
local changelog_data = mw.loadJsonData( | local changelog_data = mw.loadJsonData(id_to_changelog_data_page(id)) | ||
local lines = "" | local lines = "" | ||
for index, line in ipairs(changelog_data) do | for index, line in ipairs(changelog_data) do | ||
description = line["Description"] | description = line["Description"] | ||
for index2, tag in ipairs(line["Tags"]) do | if tag_to_filter == 'All' then | ||
lines = lines .. description .. "\n" | |||
else | |||
for index2, tag in ipairs(line["Tags"]) do | |||
if tag == tag_to_filter then | |||
lines = lines .. description .. "\n" | |||
end | |||
end | end | ||
end | end | ||
end | end | ||
return lines | return lines | ||
end | end | ||
-- Write changelogs table | -- Write changelogs table | ||
p. | p.write_changelog_by_tag = function(frame) | ||
tag = frame.args[1] | tag = frame.args[1] | ||
num_dates_str = frame.args[2] | num_dates_str = frame.args[2] | ||
Line 50: | Line 67: | ||
-- Write changelog for each date | -- Write changelog for each date | ||
for index, | for index, id in ipairs(ordered_ids) do | ||
config = changelog_configs[id] | |||
if (num_dates_str ~= nil and index > num_dates) then | if (num_dates_str ~= nil and index > num_dates) then | ||
break | break | ||
else | else | ||
content = content .. | local date = config["date"] | ||
local frontfacing_date_id = config["frontfacing_date_id"] | |||
local template_title_row = "Update history table/row" | |||
local template_args_row = {} | |||
local changes | |||
--05-03-2024 to May 3, 2024 | |||
template_args_row["update"] = frontfacing_date_id | |||
template_args_row["changes"] = write_changelog_lines(id, config, tag) | |||
if (template_args_row["changes"] ~= "") then | |||
changes = frame:expandTemplate{ title = template_title_row, args = template_args_row } | |||
else | |||
changes = "" | |||
end | |||
content = content .. frame:preprocess(changes) .. "\n" | |||
end | end | ||
index = index+1 | |||
end | end | ||
template_args["contents"] = content | template_args["contents"] = content | ||
return | return frame:expandTemplate{ title = template_title, args = template_args } | ||
end | |||
function id_to_changelog_data_page(id) | |||
return "Data:Changelog " .. id .. ".json" | |||
end | end | ||
Line 67: | Line 104: | ||
local str = "" | local str = "" | ||
for | for i, id in ipairs(ordered_ids) do | ||
str = str .. "[[ | str = str .. "[[" .. id_to_changelog_data_page(id) .. "]]<br>" | ||
end | end | ||
return str | return str | ||
end | |||
p.write_changelog_by_date_id = function(frame) | |||
local req_date_id = frame.args[1] | |||
--req = requested, as this is the requested changelog | |||
local config = changelog_configs[req_date_id] | |||
if config == nil then return "date id " .. req_date_id .. " not found" end | |||
local frontfacing_req_date_id = config["frontfacing_date_id"] | |||
local req_is_herolab = config["is_hero_lab"] | |||
-- Determine the previous and next update | |||
-- Find the index in dates list | |||
--ordered_ids defined at global scope | |||
local previous_ff_id = '' | |||
local the_ff_id = '' | |||
local next_ff_id = '' | |||
local id_found = false | |||
local frontfacing_date_id | |||
local is_herolab | |||
for i, id in ipairs(ordered_ids) do | |||
frontfacing_date_id = changelog_configs[id]["frontfacing_date_id"] | |||
is_herolab = changelog_configs[id]["is_hero_lab"] | |||
if is_herolab == req_is_herolab then --only look at herolab or non-herolab patches, not mix and match | |||
if frontfacing_date_id == frontfacing_req_date_id then | |||
--id found | |||
the_ff_id = frontfacing_date_id | |||
the_id = id | |||
id_found = true | |||
elseif id_found and next_ff_id == '' then --on next iteration, save the next id | |||
next_ff_id = frontfacing_date_id | |||
break | |||
end | |||
if not id_found then | |||
previous_ff_id = frontfacing_date_id -- only update previous if the_id isnt found | |||
end | |||
end | |||
end | |||
if not id_found then | |||
return "date_id " .. req_date_id .. " not found" | |||
end | |||
local template_title = 'Update layout' | |||
local template_args = { | |||
['prev_update'] = prev_ff_id, | |||
['current_update'] = the_ff_id, | |||
['next_update'] = next_ff_id, | |||
['source'] = config[link], | |||
['source_title'] = the_ff_id .. ' Update', | |||
['is_herolab'] = req_is_herolab, | |||
['notes'] = frame:preprocess(write_changelog_lines(the_id, config, 'All')) | |||
} | |||
return frame:expandTemplate{title = template_title, args = template_args} | |||
end | |||
p.get_last_updated = function(frame) | |||
local tag_to_search = frame.args[1] | |||
local last_or_first = frame.args["last_or_first"] | |||
if last_or_first == nil or last_or_first == 'last' then | |||
last_or_first = 'last' | |||
else | |||
last_or_first = 'first' | |||
end | |||
local list = {} | |||
if last_or_first == 'last' then | |||
list = ordered_ids | |||
else | |||
-- Create the list in reverse | |||
for i = #ordered_ids, 1, -1 do | |||
table.insert(list, ordered_ids[i]) | |||
end | |||
end | |||
-- Iterate changelog dates | |||
local frontfacing_date_id | |||
for i, id in ipairs(list) do | |||
-- Get frontfacing date from id | |||
frontfacing_date_id = changelog_configs[id]["frontfacing_date_id"] | |||
-- If next changelog data page doesn't yet exist, return latest page that exists | |||
local page_name = id_to_changelog_data_page(id) | |||
local title = mw.title.new(page_name) | |||
if not (title and title.exists) then | |||
return '[[Update:' .. frontfacing_date_id .. '|' .. frontfacing_date_id .. ']]' | |||
end | |||
local changelog_data = mw.loadJsonData(page_name) | |||
-- Iterate lines | |||
for _, line in ipairs(changelog_data) do | |||
local tags = line['Tags'] | |||
-- Iterate tags | |||
for _, tag in ipairs(tags) do | |||
if tag_to_search == tag then | |||
return '[[Update:' .. frontfacing_date_id .. '|' .. frontfacing_date_id .. ']]' | |||
end | |||
end | |||
end | |||
end | |||
return dictionary_module.translate('N/A') | |||
end | |||
function p.write_tag_tree(frame) | |||
return p.write_branch(tag_tree, 1) | |||
end | |||
function p.write_branch(branch, depth) | |||
local list_str = "" | |||
if branch == nil then return "" end | |||
for parent, children in pairs(branch) do | |||
-- If parent starts with < or ends with >, its referencing individual page | |||
-- the link won't work, so instead we just display the parent raw | |||
local parent_str | |||
if string.sub(parent, 1, 1) == '<' or string.sub(parent, -1) == '>' then | |||
parent_str = parent | |||
else | |||
parent_str = string.format("[[%s#Update history|%s]]", parent, parent) | |||
end | |||
list_str = list_str .. string.rep("*", depth) .. parent_str .. "\n" | |||
if children ~= nil then | |||
list_str = list_str .. p.write_branch(children, depth+1) | |||
end | |||
end | |||
return list_str | |||
end | end | ||
return p | return p |