Jump to content

Module:Organization

From HopperWiki

Documentation for this module may be created at Module:Organization/doc

-- ================================================================================
-- Module Dependencies
-- ================================================================================
local cargo = mw.ext.cargo
local html = mw.html.create()
local getArgs = require('Module:Arguments').getArgs -- for processing arguments
local ibf = require("Module:Infobox_functions")
local str = require("Module:String utilities")
local cfmt = require("Module:Cargo format utilities")
local out = require("Module:Wiki output utilities")


-- ================================================================================
-- Main Table
-- ================================================================================
local p = {}














-- ================================================================================
-- Return a list of the related organizations for a focal organization
-- ================================================================================

-- Cache org-name lookup so we only build it once per page render
local org_lookup_cache

local function get_org_lookup()
    if org_lookup_cache then
        return org_lookup_cache
    end

    local lookup = {}
    local all_orgs = cargo.query(
        'Organization',
        'Organization_name',
        { limit = 5000 }
    )

    for _, row in ipairs(all_orgs or {}) do
        local name = mw.text.trim(row.Organization_name or '')
        if name ~= '' then
            lookup[name] = true
        end
    end

    org_lookup_cache = lookup
    return lookup
end

-- Parse a raw related-org string into a list of bullet wikitext lines
local function build_related_bullets(related_raw, org_lookup)
    if not related_raw or related_raw == '' then
        return {}
    end

    related_raw = mw.text.trim(related_raw)
    if related_raw == '' then
        return {}
    end

    local pieces

    -- Preferred: canonical storage delimiter "~~"
    if related_raw:find('~~', 1, true) then
        pieces = mw.text.split(related_raw, '~~', true)

    -- Fallback: legacy data that used commas as separators
    elseif related_raw:find(',', 1, true) then
        pieces = mw.text.split(related_raw, ',', true)

    else
        pieces = { related_raw }
    end

    local bullets = {}

    for _, piece in ipairs(pieces) do
        local name = mw.text.trim(piece or '')
        if name ~= '' then
            -- Only link if there is a matching Organization_name
            if org_lookup[name] then
                name = string.format('[[%s]]', name)
            end
            table.insert(bullets, '* ' .. name)
        end
    end

    return bullets
end

function p.get_related_organization_list(frame)
    local args  = getArgs(frame)
    local title = args.title or mw.title.getCurrentTitle().text

    -- Escape title for SQL WHERE clause
    local escaped_title = str.escape_string_for_sql(title)

    --------------------------------------------------------------------
    -- 1. Get Related_organizations for this org
    --------------------------------------------------------------------
    local results = cargo.query(
        'Organization',
        'Related_organizations',
        {
            where = string.format("Organization_name = '%s'", escaped_title),
            limit = 1
        }
    )

    if not results or not results[1] then
        return ''
    end

    local related_raw = results[1].Related_organizations
    if not related_raw or related_raw == '' then
        return ''
    end

    --------------------------------------------------------------------
    -- 2. Build bullets (links where possible)
    --------------------------------------------------------------------
    local org_lookup = get_org_lookup()
    local bullets = build_related_bullets(related_raw, org_lookup)

    if #bullets == 0 then
        return ''
    end

    --------------------------------------------------------------------
    -- 3. Assemble display output
    --------------------------------------------------------------------
    local out = {}

    table.insert(out,
        tostring(
            mw.html.create('h2')
                :wikitext('Related organizations')
        )
    )

    table.insert(out,
        'The following organizations represented on HopperWiki are associated with this organization:'
    )

    table.insert(out, table.concat(bullets, '\n'))

    return table.concat(out, '\n') .. '\n'
end


















-- ================================================================================
-- Get organizations for a focal species
-- ================================================================================
function p.organization_by_species(frame)
	
	local args = getArgs(frame)
	local species = args.species or mw.title.getCurrentTitle().text
	
	-- run cargo query on resources so it can be accessed from functions below
	local cargo_table = "Organization"
	local fields = "Organization, Acronym, Non_english_name, Website , Type, Focus, Focus_keywords, Geographic_purview" 
	local cargo_args = {where = string.format("%s HOLDS '%s'", "Species_purview", species)}
	local cargo_results = cargo.query(cargo_table, fields, cargo_args)

    
	-- format cargo information in place
	local field_formats = {
	    Organization = "page",
	    Acronym = "string",
	    Non_english_name = "string",
	    Website = "url",
	    Type = "string",
	    Focus = "list_of_string",
	    Focus_keywords = "list_of_string",
	    Geographic_purview = "list_of_page"
	}
	
	-- modify field names for display
	local field_name_mappings = {}
	
	-- format values and headers
	local cargo_results_formatted = cfmt.format_cargo_results(cargo_results, field_formats, field_name_mappings)
	
	
	-- control row cutoff for collapsing display
    local row_cutoff = tonumber(args.row_cutoff)
    local character_cutoff = tonumber(args.character_cutoff)
	local collapse = false
	if #cargo_results_formatted > row_cutoff then
		collapse = true
	end
    
    -- select the fields that should be displayed
	local display_fields = "Organization, Acronym, Non_english_name, Website, Type, Focus, Focus_keywords, Geographic_purview"

    
	-- generate the html table
    if #cargo_results_formatted > 0 then
		local header = ""
    	return out.generate_wiki_table(cargo_results_formatted, header, display_fields, collapse, character_cutoff)
	else
    	return "No results found on the wiki for this species.."
	end
	
end











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