Module:Species identity
Appearance
Documentation for this module may be created at Module:Species identity/doc
--[=[
Module:Species identity
Species by MDD ID instead of by name -- WhiskerWiki's equivalent of HopperWiki's
OTU lookups (get_otu_from_title() in HopperWiki's Module:JSON pipeline).
A species' MDD ID (the Mammal Diversity Database's taxon ID) stays the same when
the MDD renames it; its scientific name, which is also its page title, does not.
So anything stored about a species is keyed on the ID, and this module turns
names into IDs and back:
p.id_for_title(name) name -> MDD ID. Knows current names AND earlier ones,
so an old page title or {{T|old name}} still resolves.
p.species_for_id(id) MDD ID -> { MDD_ID, Page_title, Common_name,
Order_taxon, Family }
p.link_for_id(id) MDD ID -> "[[Page title|Common name (''Page title'')]]"
p.is_mdd_id(id) true for a real MDD ID (digits). Taxa the MDD doesn't
have carry a "WW-0001"-style placeholder instead.
Data: JSON:Title_to_MDD_ID_map and JSON:MDD_ID_to_species_map, both written by
the compiler's scripts/02b_species_id_maps.R from its species ledger
(reference/species_rosetta_stone.csv). Each page is loaded at most once per
page render.
From wikitext:
{{#invoke:Species identity|mdd_id}} this page's MDD ID ("" if none)
{{#invoke:Species identity|mdd_id|Sorex vagrans}}
{{#invoke:Species identity|current_title|Microdipodops oregonus}} -> Microdipodops californicus
--]=]
local json = require("Module:JSON pipeline")
local p = {}
local TITLE_MAP = "JSON:Title_to_MDD_ID_map"
local ID_MAP = "JSON:MDD_ID_to_species_map"
-- ================================================================
-- Loading (once per render)
-- ================================================================
local cache = {}
local function dataset(page)
if cache[page] == nil then
local records, fields = json.load_dataset_multi(page)
cache[page] = { records = records or {}, fields = fields or {} }
end
return cache[page]
end
local function lookup(page, key)
if key == nil or key == "" then return nil end
local d = dataset(page)
local raw = json.get(d.records, key)
return raw and json.zip(d.fields, raw) or nil
end
-- Positional records store every field as an array; unwrap single values
local function first(v)
if type(v) == "table" then return v[1] end
return v
end
-- ================================================================
-- Lookups
-- ================================================================
function p.is_mdd_id(id)
return type(id) == "string" and id:match("^%d+$") ~= nil
end
function p.id_for_title(name)
if not name then return nil end
local rec = lookup(TITLE_MAP, json.normalize_key(name))
local id = rec and first(rec.MDD_ID)
if id == nil or id == "" then return nil end
return tostring(id)
end
function p.species_for_id(id)
if id == nil then return nil end
id = mw.text.trim(tostring(id))
local rec = lookup(ID_MAP, id)
if not rec then return nil end
return {
MDD_ID = id,
Page_title = first(rec.Page_title),
Common_name = first(rec.Common_name) or "",
Order_taxon = first(rec.Order_taxon),
Family = first(rec.Family),
}
end
-- The species' current page title for any name it has had (nil if unknown)
function p.current_title_for(name)
local sp = p.species_for_id(p.id_for_title(name))
return sp and sp.Page_title or nil
end
-- "[[Title|Common name (''Title'')]]" -- the species-list format the geography
-- pages have always used; just the italic name when there's no common name.
function p.link_for_id(id)
local sp = p.species_for_id(id)
if not sp or not sp.Page_title or sp.Page_title == "" then return nil end
if sp.Common_name ~= "" then
return "[[" .. sp.Page_title .. "|" .. sp.Common_name .. " (''" .. sp.Page_title .. "'')]]"
end
return "[[" .. sp.Page_title .. "|''" .. sp.Page_title .. "'']]"
end
-- ================================================================
-- Wikitext entry points
-- ================================================================
local function name_arg(frame)
local name = frame.args[1]
if not name or mw.text.trim(name) == "" then
name = mw.title.getCurrentTitle().text
end
return mw.text.trim(name)
end
function p.mdd_id(frame)
return p.id_for_title(name_arg(frame)) or ""
end
function p.current_title(frame)
return p.current_title_for(name_arg(frame)) or ""
end
return p