Moduldokumentasjon
local p = {}

--[[
Taksoboksens seksjon som viser biologisk klassifisering har en etikett i venstre kolonne og navnet på et nivå i hierarkiet i høyre kolonne. De forskjellige nivåenes etiketter baseres på de latinske navnene i tabellen translations. De oversatte etikettene på nivåene svarer til Petter Bøckmans Bruker:Petter_Bøckman/fornorske_rang.

Funksjonen tester først om parameteret inneholder virus, unranked_ eller ikke_rankert_ og vil returnere «Gruppe» hvis den oppdager det. Hvis ikke vil den gå gjennom nøklene i translations og returnere oversettelsen straks den finner den.
]]

p.translateRank = function (frame)
    
    local translations = {
		['superdomain'] = 'Overdomene', ['domain'] = 'Domene', ['superregnum'] = 'Overrike', ['regnum'] = 'Rike', ['subregnum'] = 'Underrike', ['superdivisio'] = 'Overdivisjon', ['superphylum'] = 'Overrekke', ['divisio'] = 'Divisjon', ['phylum'] = 'Rekke', ['zoodivisio'] = 'Divisjon', ['subdivisio'] = 'Underdivisjon', ['subphylum'] = 'Underrekke', ['infraphylum'] = 'Infrarekke', ['microphylum'] = 'Mikrorekke', ['nanophylum'] = 'Nanorekke', ['superclassis'] = 'Overklasse', ['classis'] = 'Klasse', ['subclassis'] = 'Underklasse', ['infraclassis'] = 'Infraklasse', ['magnordo'] = 'Hyperorden', ['superordo'] = 'Overorden', ['ordo'] = 'Orden', ['subordo'] = 'Underorden', ['infraordo'] = 'Infraorden', ['parvordo'] = 'Parvorden', ['superfamilia'] = 'Overfamilie', ['familia'] = 'Familie', ['subfamilia'] = 'Underfamilie', ['supertribus'] = 'Overtribus', ['tribus'] = 'Tribus', ['subtribus'] = 'Undertribus', ['alliance'] = 'Allianse', ['genus'] = 'Slekt', ['subgenus'] = 'Underslekt', ['sectio'] = 'Seksjon', ['zoosectio'] = 'Seksjon', ['subsectio'] = 'Underseksjon', ['zoosubsectio'] = 'Underseksjon', ['series'] = 'Serien', ['subseries'] = 'Underserie', ['species_group'] = 'Artsgruppe', ['species_subgroup'] = 'Artsundergruppe', ['species_complex'] = 'Artskompleks', ['species'] = 'Art', ['subspecies'] = 'Underart', ['variety'] = 'Varietet', ['form'] = 'Form', ['infraspecies'] = 'Infraart', ['infratribus'] = 'Infratribus', ['subzoodivisio'] = 'Subdivisjon', ['micrordo'] = 'Mikro-orden', ['grandordo'] = 'Grandorden', ['cohort'] = 'Kohort', ['informal'] = 'Uformell gruppe', ['informal_group'] = 'Uformell gruppe', ['stem group'] = 'Kronegruppe', ['crown group'] = 'Kronegruppe', ['total group'] = 'Kronegruppe', ['legion'] = 'Legion', ['infralegion'] = 'Infralegion', ['superlegion'] = 'Overlegion', ['sublegion'] = 'Underlegion', ['cladus'] = 'Gruppe', ['clade'] = 'Gruppe', ['ichnostem-group'] = 'Iknokronegruppe', ['ichnosuperclassis'] = 'Iknooverklasse', ['ichnoclassis'] = 'Iknoklasse', ['ichnosubclassis'] = 'Iknounderklasse', ['ichnoinfraclassis'] = 'Iknoinfraklasse', ['ichnodivisio'] = 'Iknodivisjon', ['ichnosubdivisio'] = 'Iknounderdivisjon', ['ichnoinfradivisio'] = 'Iknoinfradivisjon', ['ichnomagnordo'] = 'Iknohyperorden', ['ichnosuperordo'] = 'Iknooverorden', ['ichnograndordo'] = 'Iknograndorden', ['ichnomicrordo'] = 'Iknomikrorden', ['ichnoordo'] = 'Iknoorden', ['ichnosubordo'] = 'Iknoudnerorden', ['ichnoinfraordo'] = 'Iknoinfraorden', ['ichnoparvordo'] = 'Iknoparvorden', ['ichnosuperfamilia'] = 'Iknooverfamilie', ['ichnofamilia'] = 'Iknofamilie', ['ichnosubfamilia'] = 'Iknounderfamilie', ['ichnogenus'] = 'Iknoslekt', ['ichnosubgenus'] = 'Iknounderslekt', ['ichnospecies'] = 'Iknoart', ['ichnosubspecies'] = 'Iknounderart', ['ichnoinfraspecies'] = 'Iknoinfraart', ['ooclassis'] = 'Ooklasse', ['oosubclassis'] = 'Oounderklasse', ['oosupercohort'] = 'Oooverkohort', ['oocohort'] = 'Ookohort', ['oomagnordo'] = 'Oohyperorden', ['oosuperordo'] = 'Ooverorden', ['oordo'] = 'Oorden', ['morphotype'] = 'Morfotype', ['oofamilia'] = 'Oofamilie', ['oogenus'] = 'Ooslekt', ['oosubgenus'] = 'Oounderslekt', ['oospecies'] = 'Ooart', ['oosubspecies'] = 'Oounderart', ['ooinfraspecies'] = 'Ooinfraart',
    }
    local label = ""
    local param_name = frame.args['param_name']
    
    local unranked = {
    	"ikke_rankert_",
    	"unranked_",
    	"virus",
    }
    
    for _,s in ipairs(unranked) do
    	if mw.ustring.match( param_name , s ) ~= nil then
    		return "Gruppe"
    	end
    end
    
    for k,v in pairs(translations) do

    	if param_name == k then
    		label = v
    	end
    	
    	if label ~= "" then
    		return label	
    	end
    	
    end

	return label
end

function p.getScientificName( frame )
	-- check for wikibase connection, empty string if not exists
	if not mw.wikibase then mw.log('# Ingen wikibase-tilkobling') return "" end
	
	-- fetch the subject article wikidata entity, nil if not exists
	local subject = mw.wikibase.getEntity()
	
	local pArgs = frame:getParent().args -- arguments passed to Mal:Taksoboks
	local p225value = '' -- P225: vitenskapelig navn
	local p6507value = '' -- P6507: autorstreng
	local p105value = '' -- P105: taksonomisk rang
	local error = false -- has an error occurred?
	local p225missing = true -- is P225 missing on wikidata?
	local p6507missing = true -- is P6507 missing on wikidata?
	local autorMissing = true -- Mal:Taksoboks missing autor?
	local autoraarMissing = true -- Mal:Taksoboks missing autorår?
	local italicName = 0 -- should name be italicized?
	local autor = pArgs["autor"] -- autor from Mal:Taksoboks
	local autoraar = pArgs["autorår"] -- autorår from Mal:Taksoboks
	
	if autor == "" or not autor then 
		mw.log('* Autor ikke oppgitt i Mal:Taksoboks') 
	else 
		autorMissing = false
		autor = mw.text.trim( autor ) -- remove trailing whitespace
	end
	
	if autoraar == "" or not autoraar then 
		mw.log('* Autorår ikke oppgitt i Mal:Taksoboks') 
	else
		autoraarMissing = false
		autoraar = mw.text.trim( autoraar ) -- remove trailing whitespace
	end
	
	-- taxonomic ranks to be italicized
	local italicNames = {
		[1] = {['qid'] = 'Q34740', ['label'] = 'Slekt', ['abbr'] = nil},
		[2] = {['qid'] = 'Q3238261', ['label'] = 'Underslekt', ['abbr'] = ' subg. '},
		[3] = {['qid'] = 'Q3181348', ['label'] = 'Seksjon', ['abbr'] = ' sect. '},
		[4] = {['qid'] = 'Q3025161', ['label'] = 'Serie', ['abbr'] = ' ser. '},
		[5] = {['qid'] = 'Q7432', ['label'] = 'Art', ['abbr'] = nil},
		[6] = {['qid'] = 'Q68947', ['label'] = 'Underart', ['abbr'] = ' subsp. '},
		[7] = {['qid'] = 'Q767728', ['label'] = 'Varietet', ['abbr'] = ' var. '},
		[8] = {['qid'] = 'Q279749', ['label'] = 'Form', ['abbr'] = ' f. '},
	}
	
	if not subject then 
		mw.log( '* Ingen wikidataelement, prøver med qid' )
		local qid = frame.args['qid'] -- qid from #invoke
		if not qid then qid = pArgs['qid'] end -- qid from Mal:Taksoboks
		if not qid then mw.log( '# Ingen qid' ) return '' end
		subject = mw.wikibase.getEntity(qid)
		
		if not subject then
			mw.log( '# Ingen tilkoblede wikidataelement' ) 
			return "" 
		end
	end
	
	-- non-deprecated ranks from:
	local p225 = subject:getBestStatements( 'P225' ) -- taxon name
	local p6507 = subject:getBestStatements( 'P6507' ) -- taxon author citation
	local p105 = subject:getBestStatements( 'P105' ) -- taxon rank
	
	-- what to do if statement exists or not
	if not p225 or #p225 < 1  then 
		mw.log( '* Ingen aktuelle utsagn med P225' )
	else
		p225missing = false
		p225value = p225[1].mainsnak.datavalue.value
		mw.log( '* Wikidata takson: ' .. p225value )
	end
	
	-- what to do if statement exists or not
	if not p6507 or #p6507 < 1 then 
		mw.log( '* Ingen aktuelle utsagn med P6507' )
	else
		p6507missing = false
		p6507value = p6507[1].mainsnak.datavalue.value
		mw.log( '* Wikidata-autorstreng: ' .. p6507value )
	end
	
	-- what to do if statement exists or not
	if not p105 or #p105 < 1 then
		mw.log('# Ingen aktuelle utsagn med P105')
		error = true -- formatting impossible
	else
		-- fetch value
		p105value = p105[1].mainsnak.datavalue.value.id
		
		-- check if taxon rank should be italicized
		for k, v in ipairs(italicNames) do
			if v['qid'] == p105value then 
				italicName = k
				mw.log( '* ' .. v['label'] .. ': navnet kursiveres')
				break
			end
		end
	end
	
	-- format strings of ranks lower than genus
	if not p225missing then
		if italicName > 0 then
			
			-- rank abbriviations are not italicized
			if italicNames[italicName]['abbr'] then
				local oldStr = italicNames[italicName]['abbr']
				local newStr = "''" .. oldStr .. "''"
				p225value = mw.ustring.gsub(p225value, oldStr, newStr, 1)
			end
			
			p225value =  "''" .. p225value .. "''" -- italicize names
		end
	end
	
	-- create autorstring from parameters passed to template if not on wikidata
	if p6507missing then
		mw.log( '* Registrer utsagn med P6507 på wikidata' )
		
		if autorMissing then
			mw.log( '* Autor mangler' )
			autor = ""
		end
		
		-- check for autorår and add if it exists
		if autoraarMissing then
			mw.log ( '* Autorår mangler' )
			autoraar = ""
		else
			autoraar = ', ' .. autoraar
		end
		
		p6507value = autor .. autoraar
	
	end
	
	-- unless p105 is missing, return formatted string, else return empty string
	if not error then
		return p225value .. '<br/><small>' .. p6507value .. '</small>'
	else
		return ''
	end
end

return p