Module:Process Query Output 3: Difference between revisions

From Livermore History Collaborative
Jump to navigation Jump to search
No edit summary
No edit summary
Line 11: Line 11:
     end
     end


     local concatenated_string = "<ul>"
     local concatenated_string = "<ul><li>Elements of page_names_array:</li>"
     for i, v in ipairs(page_names_array) do
     for i, v in ipairs(page_names_array) do
         concatenated_string = concatenated_string .. "<li>" .. i .. ": " .. v .. "</li>"
         concatenated_string = concatenated_string .. "<li>" .. i .. ": " .. v .. "</li>"
Line 28: Line 28:
         query_output_tbl[page_name] = match
         query_output_tbl[page_name] = match
       end
       end
    end
    local check_for_unmatched_parentheses = ""
    local checkmatch = mw.ustring.match("(" .. full_query_arg .. ")", "%b()")
    if checkmatch == nil then
      check_for_unmatched_parentheses = "full_query_arg has unmatched '(' or ')'"
    else
      check_for_unmatched_parentheses = "full_query_arg is OK"
     end
     end


     ordered_array_of_page_name_positions = {}
     ordered_array_of_page_name_positions = {}


     --check that the concatenation of the parts equals the whole:
     --positions of page names in full_query_arg:
     for i, page_name in ipairs(page_names_array) do
     for i, page_name in ipairs(page_names_array) do
       pos1, pos2 = mw.ustring.find(full_query_arg, page_name)
       pos1, pos2 = mw.ustring.find(full_query_arg, page_name)
       ordered_array_of_page_name_positions[tostring(pos1)] = page_name
       ordered_array_of_page_name_positions[tostring(pos1)] = page_name
    end
    concatenated_positions_for_output = "<ul>"
    for key, val in pairs(ordered_array_of_page_name_positions) do
      concatenated_positions_for_output = concatenated_positions_for_output .. "<li>" .. key .. ": " .. val .. "</li>"
    end
    concatenated_positions_for_output = concatenated_positions_for_output .. "</ul>"
    table.sort(ordered_array_of_page_name_positions)
    local concatenated_parts = ""
    for k, v in pairs(ordered_array_of_page_name_positions) do
      concatenated_parts = concatenated_parts .. v  .. " " .. query_output_tbl[v] .. ", "
    end
    concatenated_parts = mw.ustring.sub(concatenated_parts, 1, -3)
    local advisory = ""
    if concatenated_parts ~= full_query_arg then
      advisory = "some part is missing"
    else
      advisory = "separated data equals the original"
     end
     end


     local concatenated_string_2 = "<ul>"
     local concatenated_string_2 = "<ul><li>Elements of query_output_tbl</li>"


     for k, v in pairs(query_output_tbl) do
     for k, v in pairs(query_output_tbl) do
Line 62: Line 53:
     concatenated_string_2 = concatenated_string_2 .. "</ul>"
     concatenated_string_2 = concatenated_string_2 .. "</ul>"


     return concatenated_string .. concatenated_string_2 .. concatenated_positions_for_output .. "<ul><li>" .. advisory .. "</li></ul>" .. "<ul><li>" .. concatenated_parts .. "</li></ul><ul><li>" .. full_query_arg .. "</li></ul>"
    -- see if the length of the concatenated query_ouput_table is the same as the original full_query_arg
 
    local concatenated_full_query_tbl = table.concat(query_output_tbl, ", ")
    local length_match_advisory = ""
    if nw.ustring.len(concatenated_full_query_tbl) == nw.ustring.len(full_query_arg) then
      length_match_advisory = "length of the concatenated query_ouput_table is the same as the original full_query_arg"
    else
      length_match_advisory = "length of the concatenated query_ouput_table is the same as the original full_query_arg"
    end
 
 
     return "<ul><li>" .. pages_only_arg .. "</ul></li>" .. concatenated_string  .. "<ul><li>" .. full_query_arg .."</li></ul>" .. "<ul><li>" .. check_for_unmatched_parentheses .. "</ul></li>"  .. concatenated_string_2 .. "<ul><li>" .. length_match_advisory .. "</ul></li>"


   
   

Revision as of 08:50, 5 December 2025

Documentation for this module may be created at Module:Process Query Output 3/doc

local p = {}

function p.processData(frame)
    local pages_only_arg = frame.args["pages_only"]
    local full_query_arg = frame.args["full_query"]

    local page_names_array = mw.text.split(pages_only_arg, ",")
    
    for i = 1, #page_names_array do
      page_names_array[i] = mw.text.trim(page_names_array[i])
    end

    local concatenated_string = "<ul><li>Elements of page_names_array:</li>"
    for i, v in ipairs(page_names_array) do
        concatenated_string = concatenated_string .. "<li>" .. i .. ": " .. v .. "</li>"
    end
    concatenated_string = concatenated_string .. "</ul>"

    query_output_tbl = {}

    for i, page_name in ipairs(page_names_array) do
      match = mw.ustring.match(full_query_arg, page_name .. "%s" .. "(%b())")
      -- if for some reason (for example, there is an unmatched "(" in the text for one of the fields) no match was found for one of the pages,
      -- the page is listed and a note indicates the missing data:
      if match == nil then
        query_output_tbl[page_name] = '<span class="error-advisory">Data for page [[' .. page_name .. "]] could not be added to the list.</span>"
      else
        query_output_tbl[page_name] = match
      end
    end
 
    local check_for_unmatched_parentheses = ""
    local checkmatch = mw.ustring.match("(" .. full_query_arg .. ")", "%b()")
    if checkmatch == nil then
      check_for_unmatched_parentheses = "full_query_arg has unmatched '(' or ')'"
    else
      check_for_unmatched_parentheses = "full_query_arg is OK"
    end

    ordered_array_of_page_name_positions = {}

    --positions of page names in full_query_arg:
    for i, page_name in ipairs(page_names_array) do
      pos1, pos2 = mw.ustring.find(full_query_arg, page_name)
      ordered_array_of_page_name_positions[tostring(pos1)] = page_name
    end

    local concatenated_string_2 = "<ul><li>Elements of query_output_tbl</li>"

    for k, v in pairs(query_output_tbl) do
      concatenated_string_2 = concatenated_string_2 .. "<li>" .. k .. ": " .. v .. "</li>"
    end
    concatenated_string_2 = concatenated_string_2 .. "</ul>"

    -- see if the length of the concatenated query_ouput_table is the same as the original full_query_arg

    local concatenated_full_query_tbl = table.concat(query_output_tbl, ", ")
    local length_match_advisory = ""
    if nw.ustring.len(concatenated_full_query_tbl) == nw.ustring.len(full_query_arg) then
      length_match_advisory = "length of the concatenated query_ouput_table is the same as the original full_query_arg"
    else
      length_match_advisory = "length of the concatenated query_ouput_table is the same as the original full_query_arg"
    end


    return "<ul><li>" .. pages_only_arg .. "</ul></li>" .. concatenated_string  .. "<ul><li>" .. full_query_arg .."</li></ul>" .. "<ul><li>" .. check_for_unmatched_parentheses .. "</ul></li>"  .. concatenated_string_2 .. "<ul><li>" .. length_match_advisory .. "</ul></li>"

 
    
    
end

return p