Module:Taxonomy

From Sagan 4 Beta Wiki
Jump to navigation Jump to search

JSON template for adding a new taxon to Module:Taxonomy/data.json:

    "Taxon Name": {
        "parent": "The next taxon up from this one when going from lowest to highest",
        "type": "Flora, Fauna, etc, remove this line if unchanged from parent taxon",
        "page": "General Page's Title, remove this line if there is none",
        "rank": "Clade"
    },

(Remove the last comma if appending to the end of the document)

Genera and below should not be placed in the data, as they can change too easily.

main

Not finished yet

TaxoCheck

{{#invoke:Taxonomy|TaxoCheck|<name of taxon>}}

Basically just checks if a given taxon is in Module:Taxonomy/data.json. Returns "yes" if it is, "no" if it isn't, and "error" if you messed something up. This is intended to be used on species templates so that all taxa can be rounded up and migrated to the new system.


local p = {}

-- This module will allow taxonomy to be updated far more quickly, based only on a change to a single file. Work in progress.

local data = mw.loadJsonData('Module:Taxonomy/data.json')


function p.TaxoCheck(frame,other)
	-- determines if a given taxon is in the new taxonomy system (will be used primarily to help find all taxa that need to be added)
	local args = {}
	local clade = ""
	if other then
		clade = other
	else
		args = frame.args
		clade = args[1]
	end
	
	local taxocheckresult = "no"
	
	if clade then
		-- do the calculation
		for k in pairs(data) do -- this is dumb. Who made it where this is how you retrieve data from a table in lua.
			if k == clade then
				taxocheckresult="yes"
				break --this will make the taxocheck code slightly faster
			end
		end
		--if data[clade] then taxoncheckresult = "yes" end
	else
		taxocheckresult = "error (Adding a new taxon? Make sure to add it to [[Module:Taxonomy/data.json]]!)"
	end
	
	return taxocheckresult
	
end


function p.getBiotaType(frame,other)
	--Retrieves the biota type of the specified taxon
	local taxon
	
	if other then
		taxon = other
	else
		taxon = frame.args[1]
	end
	
	local biotaType = ""
	local timeoutCounter = 0
	while biotaType == "" do
		--iterate up the taxon's parentage until a type is defined
		if data[taxon] then --In one part of the code this works, and in another it doesn't. may as well take the opportunity to troubleshoot now!
			if data[taxon]["type"] then
				biotaType = data[taxon]["type"]
			else
				if data[taxon]["parent"] then
					taxon = data[taxon]["parent"]
				else
					biotaType = "error"
				end
			end
			
		else
			biotaType = "error"
		end
		timeoutCounter = timeoutCounter + 1
		if timeoutCounter == 100 then
			biotaType = "error"
			break --failsafe if I screwed up my code, there's no circumstance where there will be a chain of 100 parent taxa before a type is defined
		end
	end
	
	return biotaType
end


function p.jsongen(frame)
	-- Generates taxonomy json based on an inputted category. This will be deprecated after migration is complete.
	
	local errorStuff = ""
	
	local category = frame.args[1]
	
	local categoryContents = frame:callParserFunction{name = "#dpl:", args = {
	    namespace     = "",
    	category      = category,
    	mode          = "userformat",
    	inlinetext = "@@",
    	format        = ",%PAGE%,@@,",
		}
	}
	if categoryContents == "" or categoryContents == "@@" then
		errorstuff = errorStuff.."Category is empty"
	end
	
	if frame.args[2] then
		errorStuff = errorStuff.."\n\nDPL category results: "..categoryContents
	end
	
	
	local pages = {}
	
	for item in categoryContents:gmatch("[^@@]+") do
		table.insert(pages,item)
	end
	
	local taxa = {}
	local inconsistentTaxa = {} -- will figure out what to do with this later
	local invalidPages = ""
	
	
	for i, page in pairs (pages) do
		-- Taxon table format: taxonname = {parent = taxonparent, rank = rank}
		-- Getting taxa: Searching for the use of every taxon parameter and getting its content, in order
		local currentPage = mw.title.new(page)
		if currentPage then pageContent = currentPage:getContent()
		local lasttaxon = "Eukaryota"
		local currenttaxon = ""
		local override = ""
		local overrideRank = ""
		
		indexStart, indexEnd, override = pageContent:find("|parent%s*=%s*(.-)%s*[}|]")
		indexStart, indexEnd, overrideRank = pageContent:find("|Classification%s*=%s*(.-)%s*[}|]")
		
		--if pageContent:find("|parent%s*=%s*(.-)%s*[}|]") then
		--	indexStart, indexEnd, currenttaxon = pageContent:find("|parent%s*=%s*(.-)%s*[}|]")
		--	if currenttaxon then
		--		currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
		--		if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then --[[taxa[currenttaxon] = {parent = lasttaxon, rank = "Kingdom"}]]  end end
		--		if currenttaxon ~= "" then lasttaxon = currenttaxon end
		--	end
		--end
		if pageContent:find("|name%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|name%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = override, rank = overrideRank}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|domain%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|domain%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" and currenttaxon ~= "Eukaryota" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = "Life", rank = "Domain"} end  end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|superkingdom%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|superkingdom%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Superkingdom"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|kingdom%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|kingdom%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Kingdom"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|subkingdom%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|subkingdom%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Subkingdom"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		
		if pageContent:find("|superphylum%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|superphylum%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Superphylum"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|phylum%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|phylum%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Phylum"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|subphylum%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|subphylum%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Subphylum"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		
		if pageContent:find("|superclass%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|superclass%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Superclass"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|class%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|class%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Class"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|subclass%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|subclass%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Subclass"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		
		if pageContent:find("|superorder%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|superorder%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Superorder"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|order%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|order%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Order"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|suborder%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|suborder%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Suborder"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		
		if pageContent:find("|superfamily%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|superfamily%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Superfamily"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|family%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|family%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Family"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|subfamily%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|subfamily%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Subfamily"}  end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		if pageContent:find("|tribe%s*=%s*(.-)%s*[}|]") then
			indexStart, indexEnd, currenttaxon = pageContent:find("|tribe%s*=%s*(.-)%s*[}|]")
			if currenttaxon then
				currenttaxon = string.gsub(currenttaxon,"\n","") --removes linebreaks if there are any
				if not taxa[currenttaxon] and currenttaxon ~= "" then if p.TaxoCheck(frame,currenttaxon) == "no" then taxa[currenttaxon] = {parent = lasttaxon, rank = "Tribe"} end end
				if currenttaxon ~= "" then lasttaxon = currenttaxon end
			end
		end
		else invalidPages = invalidPages..", "..page end
		-- that was a lot! just goes to show how inefficient the old system was
	end
	if frame.args[2] then errorStuff = errorStuff.."\n\nPages that came up invalid: "..invalidPages end
	local taxaJsonVersion = '<pre>{\n    "Life": {\n        "parent": "none",\n        "type": "Undefined",\n        "rank": "Clade"\n    }' -- which is actually not a json object, but a string
	
	for nameoftaxon,data in pairs (taxa) do
		taxaJsonVersion = taxaJsonVersion..',\n        "'..nameoftaxon..'": {\n        "parent": "'..data["parent"]..'",\n        "rank": "'..data["rank"]..'"\n    }'
	end
	
	
	return taxaJsonVersion.."\n}</pre>".."\n\n"..errorStuff
end

function p.warnings(frame,taxinput)
	-- Finds any warnings about an organism's taxa
	local taxon
	if taxinput then
		taxon = taxinput
	else
		taxon = frame.args[1]
	end
	
	local finished = false
	local warningString = ""
	
	while finished == false do
		if data[taxon] then
			if data[taxon]["warn"] then
				finished = true
				warningString = data[taxon]["warn"]
			else
				if data[taxon]["parent"] then
					taxon = data[taxon]["parent"]
				else
					finished = true
				end
			end
		else
			--error
			finished = true
			warningString = "Taxon data not found."
		end
	end
	
	return warningString
	
end




function p.main(frame)
	-- Gets the parent taxa of the inputted taxon
	local currentaxon = frame.args[1]
	local errorflag = false
	
	local rankline = "'''Genus'''<br>'''Species'''"
	local taxonline = ""
	local errorline = ""
	local smwline = ''
	
	local taxa = {}
	
	while currentaxon ~= "Life" do
		if p.TaxoCheck(frame,currentaxon) == "no" then
			errorflag = true
			currentaxon = "Life"
		else
			local rank = "Clade"
			local parent = "Life"
				
			--for k in pairs(data) do -- this is dumb. Who made it where this is how you retrieve data from a table in lua.
				--if k == clade then
					--if k["parent"] then parent = k["parent"] else parent = "Life" errorflag = true end
					--if k["rank"] then rank = k["rank"] else rank = "Clade" end
					
				--end
			--end
			
			if data[currentaxon]["parent"] then parent = data[currentaxon]["parent"] end
			if data[currentaxon]["rank"] then rank = data[currentaxon]["rank"] end
			
			rankline = "'''"..rank.."'''<br>"..rankline
			taxonline = "[["..currentaxon.."]][[Category:"..currentaxon.."]]<br>"..taxonline
			if rank ~= "Clade" then smwline = smwline.."{{#set:"..rank.."="..currentaxon.."}}" end --backwards compatability
			table.insert(taxa,currentaxon)
			currentaxon = parent
		end
	end
	
	taxonline = '<td style="border-top: solid 1px #999999; padding: 0.4em 1em 0.4em 0; text-align: left; vertical-align: top;" >'..taxonline
	rankline = '<td style="border-top: solid 1px #999999; padding: 0.4em 1em 0.4em 0; vertical-align: right;" >'..rankline
	
	
	if errorflag == true then errorline = "error (Adding a new taxon? Make sure to add it to [[Module:Taxonomy/data.json]]!){{#set:Maintenance=Migrated taxonomy error}}" end
	
	smwline = smwline.."{{#set:|taxonomy="
	local commadone = false
	for i, taxon in pairs (taxa) do
		if commadone == false then
			smwline = smwline..taxon
			commadone = true
		else
			smwline = smwline..", "..taxon
		end
	end
	smwline = smwline..", {{#if:{{{genus|}}}|, {{{genus}}}}}|+sep=, }}"
	
	local finaltext = smwline..errorline..rankline.."</td>"..taxonline
	finaltext = frame:preprocess(finaltext)
	return finaltext
end



return p