Module:FishData

From Fish Wrangler
Jump to navigation Jump to search

Module:FishData lets any page read one field of any fish without pulling in the whole fish page.

Why it exists[edit]

Tables like Chum, Fish population table and the points/gold tables used to read each fish like this:

{{:Miny Fish|param=habitat}}

That loads the entire Miny Fish article just to get one word. A chum page did it about 470 times, went over the wiki's 2 MB page limit, and showed an empty list. It also meant every fish edit forced dozens of big pages to rebuild, which slowed the whole wiki down.

How to use it[edit]

One field of one fish (same field names as Template:FishInfo):

{{#invoke:FishData|get|Miny Fish|habitat}}

Fields: image, fishid, habitat, population, skill, pole, prefers, points, faroviapoints, roperiapoints, goodevilpoints, faroviatourneypoints, roperiatourneypoints, greatcatchtourney, gold, silver.

Every fish that prefers a chum (used by What chum, so chum pages fill themselves in):

{{#invoke:FishData|byChum|chum=[[Algae Chum]]}}

Editing fish[edit]

Nothing changes. Edit the fish page (or its Info page) the normal way, with its FishInfo or RoperiaFishInfo box. The data table at Module:FishData/data is rebuilt from those boxes automatically every hour, so a change shows up in the tables within the hour. Please do not edit Module:FishData/data by hand; it gets overwritten.

A fish only shows up on its chum page when its prefers field names that chum, so filling in a prefers=? is the quickest way to make the chum lists more complete.

The old {{:Fish|param=field}} form still works, it is just slow. New tables should use the module.


-- Module:FishData - one-field lookups and lists over every fish page, read from
-- Module:FishData/data (generated hourly by genFishData.php, FW repo _ai/tools/wiki-fishdata).
--
-- Replaces {{:Some Fish|param=habitat}}, which transcluded the WHOLE fish article for one
-- field: a chum page did that ~470 times, hit the 2 MB post-expand limit and listed nothing.
--
--   {{#invoke:FishData|get|Miny Fish|habitat}}        one field (same names as {{FishInfo}})
--   {{#invoke:FishData|byChum|chum=[[Algae Chum]]}}   table rows of fish that prefer a chum
local p = {}
local db = mw.loadData('Module:FishData/data')

local ALIAS = {['g-e_pts'] = 'goodevilpoints', tourney_pts = 'faroviatourneypoints',
	['g-c_tourney'] = 'greatcatchtourney', ['tourney-pts'] = 'faroviatourneypoints'}

local function key(name)
	name = mw.text.trim(name or ''):gsub('_', ' ')
	if name == '' then return name end
	name = mw.ustring.upper(mw.ustring.sub(name, 1, 1)) .. mw.ustring.sub(name, 2)
	return db.alias[name] or name
end

local function entry(name)
	return db.entries[key(name)]
end

-- Same output the {{FishInfo}} / {{RoperiaFishInfo}} getter #switch gave for a field.
local function field(e, f)
	f = ALIAS[f] or f
	if f == 'image' then
		return e.image and ('Image:' .. e.image) or ''
	end
	if f == 'points' then
		local v = e.faroviapoints or e.roperiapoints or e.points or ''
		if e.t == 'R' and v ~= '' then return "'''<font color=\"blue\">" .. v .. "</font>'''" end
		return v
	end
	if e.t == 'R' and (f == 'gold' or f == 'silver') and e[f] then
		return "'''<font color=\"" .. f .. "\">" .. e[f] .. "</font>'''"
	end
	return e[f] or ''
end

function p.get(frame)
	local e = entry(frame.args[1])
	if not e then return '' end
	return field(e, mw.text.trim(frame.args[2] or ''))
end

-- "[[Algae Chum]]", "Algae Chum", "[[Algae Chum|Algae]]" all compare equal.
local function bare(s)
	s = mw.text.trim(s or '')
	s = s:gsub('%[%[([^|%]]+)[^%]]*%]%]', '%1')
	return mw.ustring.lower(mw.text.trim(s))
end

function p.byChum(frame)
	local want = bare(frame.args.chum)
	if want == '' then return '' end
	local out = {}
	for _, name in ipairs(db.order) do
		local e = db.entries[name]
		if e.prefers and bare(e.prefers) == want then
			out[#out + 1] = '| [[' .. name .. ']]\n|-'
		end
	end
	return table.concat(out, '\n')
end

-- How many fish pages the table knows about (for docs / sanity checks).
-- (# does not work on mw.loadData tables, so count by walking.)
function p.count()
	local n = 0
	for _ in ipairs(db.order) do n = n + 1 end
	return n
end

return p