Jump to content

Module:Taxonomic schema

From WhiskerWiki

This Module serves as the master control for the taxonomic schema of the wiki. It accesses the stored data in both Module:Taxonomic rank dictionary and Module:Taxonomic rank metadata and parses them for downstream use.


-- ================================================================================
-- Module dependencies
-- ================================================================================
local utils = require("Module:Utilities")
local cargo = mw.ext.cargo -- for cargo queries if needed
local taxonomic_rank_dictionary = require("Module:Taxonomic_rank_dictionary")
local taxonomic_rank_metadata = require("Module:Taxonomic_rank_metadata")



-- ================================================================================
-- Main table
-- ================================================================================
local ts = {}



-- ========================================
-- Define global convenience variables
-- ========================================
-- String and lookup table of full list of all ranks in the wiki as they are in defined in Cargo
local all_ranks = taxonomic_rank_dictionary.all_ranks
local all_ranks_string = utils.list_to_csv_string(all_ranks)
ts.all_ranks_string = all_ranks_string
ts.all_ranks = all_ranks

-- String and lookup table of full list of all ranks in WhiskerWiki as they should be displayed for the user
local all_ranks_display = taxonomic_rank_dictionary.all_ranks_display
local all_ranks_display_string = utils.list_to_csv_string(all_ranks_display)
ts.all_ranks_display_string = all_ranks_display_string
ts.all_ranks_display = all_ranks_display



-- ========================================
-- Define the rank dictionary for the wiki
-- ========================================
-- Lookup table for custom Linnaean rank list for certain focal taxa
ts.taxonomic_rank_dictionary = taxonomic_rank_dictionary



-- ========================================
-- Define the rank metadata for the wiki
-- ========================================
-- Lookup table for various forms of the names of the Linnaean ranks used in WhiskerWiki
ts.taxonomic_rank_metadata = taxonomic_rank_metadata
ts.rank_lookup = taxonomic_rank_metadata -- Store this also as `rank_lookup` for backwards compatibility reasons



-- ========================================
-- Functions
-- ========================================
-- Define functions that are used to parse and interact with the taxonomic schema
function ts.rank_finder(cargo_table, value)
    
    for _, field in ipairs(all_ranks) do
        
        local where_clause = field .. ' = "' .. value .. '"'
        
        -- Query the Cargo table for the specific field and value
        local results = cargo.query(cargo_table, field, { where = where_clause, limit = 1 })

        if #results > 0 then
            -- Match found in this field
            return field
        end
    end
    return nil  -- No match was found in any field
end

function ts.get_child_ranks(all_ranks, target_rank)
    local result = {}
    local found = false
    for i, rank in ipairs(all_ranks) do
        if found then
            table.insert(result, rank)
        elseif rank == target_rank then
            found = true
        end
    end
    return result
end

return ts
Cookies help us deliver our services. By using our services, you agree to our use of cookies.