Menyang kontèn

Modhul:Autotaxobox

Saka Wikipédia Jawa, bauwarna mardika basa Jawa

Dhokumèntasi modhul iki bisa digawé ingModhul:Autotaxobox/doc

--[[
This module provides support to the automated taxobox system – the templates
Automatic taxobox, Speciesbox, Subspeciesbox, Infraspeciesbox, etc.

In particular it provides a way of traversing the taxonomic hierarchy encoded
in taxonomy templates (templates with names of the form
"Template:Taxonomy/TAXON_NAME" ) without causing template expansion depth errors.
]]

localp={}

--[[=========================================================================
Limit the maximum depth of a taxonomic hierarchy that can be traversed;
avoids excessive processing time and protects against incorrectly set up
hierarchies, e.g. loops.
=============================================================================]]
localMaxSearchLevels=100

functionp.getMaxSearchLevels()
returnMaxSearchLevels
end

--[[========================== taxoboxColour ================================
Determines the correct colour for a taxobox, by searching up the taxonomic
hierarchy from the supplied taxon for the first taxon (other than
'incertae sedis') that sets a taxobox colour. It is assumed that a valid
taxobox colour is defined using CSS rgb() syntax.
If no taxon that sets a taxobox colour is found, then 'transparent' is
returned unless the taxonomic hierarchy is too deep, when the error colour is
returned.
Usage: {{#invoke:Autotaxobox|taxoboxColour|TAXON}}
=============================================================================]]
functionp.taxoboxColour(frame)
localcurrTaxon=frame.args[1]or''
locali=1-- count levels processed
localsearching=currTaxon~=''-- still searching for a colour?
localfoundICTaxon=false-- record whether 'incertae sedis' found
localcolour=''-- default is no colour
whilesearchingandi<=MaxSearchLevelsdo
localplainCurrTaxon=p.stripExtra(currTaxon)-- remove trailing text after /
ifstring.lower(plainCurrTaxon)=='incertae sedis'then
foundICTaxon=true
else
localpossibleColour=frame:expandTemplate{title='Template:Taxobox colour',args={plainCurrTaxon}}
ifstring.sub(possibleColour,1,3)=='rgb'then
colour=possibleColour
searching=false
end
end
ifsearchingthen
localok,parent=p.getTaxonInfoItem(frame,currTaxon,'parent')
ifokandparent~=''then
currTaxon=parent
i=i+1
else
searching=false-- run off the top of the hierarchy or tried to use non-existent taxonomy template
end
end
end
ifcolour~=''then
returncolour
elseiffoundICTaxonthen
returnframe:expandTemplate{title='Template:Taxobox colour',args={'incertae sedis'}}
elseifsearchingthen
-- hierarchy exceeds MaxSearchLevels levels
returnframe:expandTemplate{title='Template:Taxobox/Error colour',args={}}
else
return'transparent'
end
end

--[[=========================== taxoboxList =================================
Returns the rows of taxa in an automated taxobox, based on the taxonomic
hierarchy for the supplied taxon.
Usage:
{{#invoke:Autotaxobox|taxoboxList|TAXON
|display_taxa = the number of taxa *above* TAXON to force to be displayed
|authority = taxonomic authority for TAXON
|parent_authority = taxonomic authority for TAXON's parent
|gparent_authority = taxonomic authority for TAXON's grandparent
|ggparent_authority = taxonomic authority for TAXON's greatgrandparent
|ggparent_authority = taxonomic authority for TAXON's greatgreatgrandparent
|bold_first = 'bold' to bold TAXON in its row
}}
=============================================================================]]
functionp.taxoboxList(frame)
localcurrTaxon=frame.args[1]or''
localdisplayN=(tonumber(frame.args['display_taxa'])or1)+1
localauth=frame.args['authority']or''
localparentAuth=frame.args['parent_authority']or''
localgParentAuth=frame.args['gparent_authority']or''
localggParentAuth=frame.args['ggparent_authority']or''
localgggParentAuth=frame.args['gggparent_authority']or''
localboldFirst=frame.args['bold_first']or'link'-- values 'link' or 'bold'
localtaxonTable=p.makeTable(frame,currTaxon)
localres=''
-- display all taxa above possible greatgreatgrandparent
fori=taxonTable.n,6,-1do
res=res..frame:expandTemplate{title='Template:Taxobox/showtaxon',args={taxonTable[i],fc=tostring(displayN>=i)}}
end
-- display greatgreatgrandparent, if it exists
iftaxonTable.n>=5then
res=res..frame:expandTemplate{title='Template:Taxobox/showtaxon',args={taxonTable[5],authority=gggParentAuth,fc=tostring(displayN>=5)}}
end
-- display greatgrandparent, if it exists; force the display if an infrataxon is below
iftaxonTable.n>=4then
localforce=tostring(displayN>=4)or
frame.expandTemplate{title='Template:Infrataxon()',args={taxonTable[3]}}=='true'or
frame.expandTemplate{title='Template:Infrataxon()',args={taxonTable[2]}}=='true'
res=res..frame:expandTemplate{title='Template:Taxobox/showtaxon',args={taxonTable[4],authority=ggParentAuth,fc=tostring(force)}}
end
-- display grandparent, if it exists; force the display if an infrataxon is below
iftaxonTable.n>=3then
localforce=tostring(displayN>=3)or
frame.expandTemplate{title='Template:Infrataxon()',args={taxonTable[2]}}=='true'
res=res..frame:expandTemplate{title='Template:Taxobox/showtaxon',args={taxonTable[3],authority=gParentAuth,fc=tostring(force)}}
end
-- display parent, if it exists
iftaxonTable.n>=2then
res=res..frame:expandTemplate{title='Template:Taxobox/showtaxon',args={taxonTable[2],authority=parentAuth,fc=tostring(displayN>=2)}}
end
-- display target taxon
res=res..frame:expandTemplate{title='Template:Taxobox/showtaxon',args={taxonTable[1],authority=auth,fc='true',format=boldFirst}}
returnres
end

--[[========================== taxonomyList =================================
Returns the cells of the taxonomy table displayed on "Template:Taxonomy...."
pages.
Usage: {{#invoke:Autotaxobox|taxonomyList|TAXON}}
=============================================================================]]
functionp.taxonomyList(frame)
localcurrTaxon=frame.args[1]or''
ifcurrTaxon==''thenreturn'| ||ERROR: no taxon supplied\n|-'end
localtaxonTable=p.makeTable(frame,currTaxon)
localrankTable=p.getRankTable()
locallastRankVal=1000000
localorderOk
localres=''
fori=taxonTable.n,1,-1do
-- check ranks are in right order in the hierarchy
localok,rank=p.getTaxonInfoItem(frame,taxonTable[i],'rank')
localcurrRankVal=rankTable[rank]
ifcurrRankValthen
orderOk=currRankVal<lastRankVal
iforderOkthenlastRankVal=currRankValend
else
orderOk=true
end
-- now return a row of the taxonomy table with anomalous ranks marked
iforderOkthen
res=res..frame:expandTemplate{title='Template:Taxonomy links',args={taxonTable[i]}}
else
ifi~=1then
res=res..frame:expandTemplate{title='Template:Taxonomy links',args={taxonTable[i],error='true'}}
else
res=res..frame:expandTemplate{title='Template:Taxonomy links',args={taxonTable[i],error='true',last_error='true'}}
end
end
end
returnres
end

--[[========================= callTaxonomyKey ===============================
Prepares for, and then calls, Template:Taxonomy key to display a taxonomy
template page. It does this by building up the information the template
requires, following one 'same as' link, if required.
Usage:
{{#invoke:Autotaxobox|callTaxonomyKey
|parent=
|rank=
|extinct=
|always_display=
|link_target=value of 'link' parameter in taxonomy template
|link_text=value of parameter 2 in taxonomy template
|same_as=
}}
=============================================================================]]
localSAME_AS=7
localPARENT=1
localRANK=2
localLINK_TARGET=3
localLINK_TEXT=4
localALWAYS_DISPLAY=5
localEXTINCT=6
localREFS=8

functionp.callTaxonomyKey(frame)
localparent=frame.args['parent']or''
localrank=frame.args['rank']or''
localextinct=string.lower(frame.args['extinct'])or''
localalwaysDisplay=string.lower(frame.args['always_display'])or''
locallinkTarget=frame.args['link_target']or''
locallinkText=frame.args['link_text']or''-- this is the "raw" link text, and can be ''
localrefs=frame.args['refs']or''
localsameAsTaxon=frame.args['same_as']or''
ifsameAsTaxon~=''then
-- try using the 'same as' taxon; it's an error if it doesn't exist
localok,sameAsInfoStr=pcall(frame.expandTemplate,frame,{title='Template:Taxonomy/'..sameAsTaxon,args={['machine code']='all'}})
ifokthen
localsameAsInfo=mw.text.split(sameAsInfoStr,'$',true)
--'same as' taxon's taxonomy template must not have a 'same as' link
ifsameAsInfo[SAME_AS]==''then
ifparent==''thenparent=sameAsInfo[PARENT]end
ifrank==''thenrank=sameAsInfo[RANK]end
ifextinct==''thenextinct=string.lower(sameAsInfo[EXTINCT])end
ifalwaysDisplay==''thenalwaysDisplay=string.lower(sameAsInfo[ALWAYS_DISPLAY])end
iflinkTarget==''thenlinkTarget=sameAsInfo[LINK_TARGET]end
iflinkText==''thenlinkText=sameAsInfo[LINK_TEXT]end
ifrefs==''andparent==sameAsInfo[PARENT]thenrefs=sameAsInfo[REFS]end
else
return'<span style= "color:red; font-size:1.1em" >Error: attempt to follow two "same as" links</span>: <code>same_as = '..sameAsTaxon..'</code>, but [[Template:Taxonomy/'..sameAsTaxon..']] also has a<code>same_as</code> parameter.'
end
else
returnframe:expandTemplate{title='Template:Taxonomy key/missing template',args={taxon=sameAsTaxon,msg='minangka <code>same as</code>'}}
end
end
locallink=linkTarget
iflinkText~=''andlinkText~=linkTargetthenlink=link.."|"..linkTextend
returnframe:expandTemplate{title='Template:Taxonomy key',
args={parent=parent,rank=rank,extinct=extinct,always_display=alwaysDisplay,link_target=linkTarget,link=link,refs=refs,same_as=sameAsTaxon}}
end

--[[============================ taxonInfo ==================================
Extracts and returns information from Template:Taxonomy/TAXON, following
one 'same as' link if required.
Usage: {{#invoke:Autotaxobox|taxonInfo|TAXON|ITEM}}
ITEM is one of: 'parent', 'rank', 'link target', 'link text', 'link', 'extinct',
'always display', 'refs', 'same as' or 'all'.
If ITEM is not specified, the default is 'all' – all values in a single string
separated by '$'.
=============================================================================]]
functionp.taxonInfo(frame)
localtaxon=frame.args[1]or''
localitem=frame.args[2]or''
ifitem==''thenitem='all'end
localok,info=p.getTaxonInfoItem(frame,taxon,item)
returninfo
end

--[[============================ taxonLink ==================================
Returns a wikilink to a taxon, if required including '†' before it and
' (?)' after it, and optionally italicized or bolded without a wikilink.
Usage:
{{#invoke:Autotaxobox|taxonLink
|taxon=: having '/?' at the end triggers the output of ' (?)'
|extinct=: 'yes' or 'true' trigger the output of '†'
|bold=: 'yes' makes the core output bold and not wikilinked
|italic=: 'yes' makes the core output italic
|link_target=: target for the wikilink
link_text=: text of the wikilink (may be same as link_target), without †, italics, etc.
}}
=============================================================================]]
functionp.taxonLink(frame)
localtaxon=frame.args['taxon']or''
localextinct=string.lower(frame.args['extinct'])or''
localbold=frame.args['bold']or''
localitalic=frame.args['italic']or''
locallinkTarget=frame.args['link_target']or''
locallinkText=frame.args['link_text']orframe.args['plain_link_text']or''--temporarily allow alternative args
-- if link text is missing, try to find a replacement
iflinkText==''then
ifstring.find(taxon,'Incertae sedis',1,true)then
linkText="''incertae sedis''"
linkTarget='Incertae sedis'
else
linkText=p.stripExtra(taxon)
end
end
locallink=''
ifbold=='yes'thenlink='<b>'..linkText..'</b>'
else
iflinkTarget==linkTextorlinkTarget==''thenlink=linkText
elselink=linkTarget..'|'..linkText
end
link='[['..link..']]'
end
ifitalic=='yes'andnotstring.find(link,"''",1,true)thenlink='<i>'..link..'</i>'end
if(extinct=='yes'orextinct=='true')andnotstring.find(link,'†',1,true)then
link='<span style= "font-style:normal;font-weight:normal;" >†</span>'..link
end
ifstring.sub(taxon,-2)=='/?'andnotstring.find(link,'?',1,true)then
link=link..'<span style= "font-style:normal;font-weight:normal;" > (?)</span>'
end
returnlink
end

--[[========================== showRankTable ================================
Returns a wikitable showing the ranks and their values as set up by
getRankTable().
Usage: {{#invoke:Autotaxobox|showRankTable}}
=============================================================================]]

functionp.showRankTable(frame)
localrankTable=p.getRankTable()
localres='{| class= "wikitable sortable"\n|+ Ranks checked in taxonomy templates\n!Rank!! Shown as!! Value\n'
fork,vinpairs(rankTable)do
localrankShown=frame:expandTemplate{title='Template:Anglicise rank',args={k}}
res=res..'|-\n|'..k..'||'..rankShown..'||'..v..'\n'
end
returnres..'|}\n'
end

--[[=============================== nth =====================================
External utility function primarily intended for use in checking and debugging.
Returns the nth level above a taxon in a taxonomic hierarchy, where the taxon
itself is counted as the first level.
Usage: {{#invoke:Autotaxobox|nth|TAXON|n=N}}
=============================================================================]]
functionp.nth(frame)
localcurrTaxon=frame.args[1]or''
ifcurrTaxon==''thenreturn'ERROR: no taxon supplied'end
localn=tonumber(frame.args['n']or1)
ifn>MaxSearchLevelsthen
return'Exceeded maximum number of levels allowed ('..MaxSearchLevels..')'
end
locali=1
localinHierarchy=true-- still in the taxonomic hierarchy or off the top?
whilei<nandinHierarchydo
localok,parent=p.getTaxonInfoItem(frame,currTaxon,'parent')
ifokandparent~=''then
currTaxon=parent
i=i+1
else
inHierarchy=false
end
end
ifinHierarchythenreturncurrTaxon
elsereturn'Level '..n..' is past the top of the taxonomic hierarchy'
end
end

--[[============================= nLevels ===================================
External utility function primarily intended for use in checking and debugging.
Returns number of levels in a taxonomic hierarchy, starting from
the supplied taxon as level 1.
Usage: {{#invoke:Autotaxobox|nLevels|TAXON}}
=============================================================================]]
functionp.nLevels(frame)
localcurrTaxon=frame.args[1]or''
ifcurrTaxon==''thenreturn'ERROR: no taxon supplied'end
locali=1
localinHierarchy=true-- still in the taxonomic hierarchy or off the top?
whileinHierarchyandi<MaxSearchLevelsdo
localok,parent=p.getTaxonInfoItem(frame,currTaxon,'parent')
ifokandparent~=''then
currTaxon=parent
i=i+1
else
inHierarchy=false
end
end
ifinHierarchythenreturnMaxSearchLevels..'+'
elsereturni
end
end

--[[============================= listAll ===================================
External utility function primarily intended for use in checking and debugging.
Returns a comma separated list of a taxonomic hierarchy, starting from
the supplied taxon.
Usage: {{#invoke:Autotaxobox|listAll|TAXON}}
=============================================================================]]
functionp.listAll(frame)
localcurrTaxon=frame.args[1]or''
ifcurrTaxon==''thenreturn'ERROR: no taxon supplied'end
returnp.listTaxa(p.makeTable(frame,currTaxon))
end

--[[=========================================================================
Internal functions
=============================================================================]]

--[[= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Internal utility function to strip off any extra parts of a taxon name, i.e.
anything after a '/'. Thus "Felidae/?" would be reduced to "Felidae".
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =]]
functionp.stripExtra(taxonName)
locali=string.find(taxonName,'/')
ifithen
returnstring.sub(taxonName,1,i-1)
else
returntaxonName
end
end

--[[= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Internal utility function to convert a taxon table to a comma-separated list.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =]]
functionp.listTaxa(taxonTable)
locallst=taxonTable[1]
fori=2,taxonTable.n,1do
lst=lst..', '..taxonTable[i]
end
returnlst
end

--[[= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Internal utility function to extract an item of information from a
taxonomy template, following one 'same as' link if required.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =]]
functionp.getTaxonInfoItem(frame,taxon,item)
-- item == 'link' is a special case
ifitem=='link'then
returnp.getTaxonInfoLink(frame,taxon)
end
-- item ~= 'link'
localok,info=pcall(frame.expandTemplate,frame,{title='Template:Taxonomy/'..taxon,args={['machine code']=item}})
ifokthen
ifinfo==''then
-- try 'same as'
localsameAsTaxon=frame:expandTemplate{title='Template:Taxonomy/'..taxon,args={['machine code']='same as'}}
ifsameAsTaxon~=''then
ok,info=pcall(frame.expandTemplate,frame,{title='Template:Taxonomy/'..sameAsTaxon,args={['machine code']=item}})
end
end
end
ifokthen
-- if item is 'link_text' check whether '(?)' needs to be added
ifitem=='link_text'andstring.sub(taxon,-2)=='/?'andnotstring.find(info,'?',1,true)then
info=info..'<span style= "font-style:normal;font-weight:normal;" > (?)</span>'
end
else
info='[[Template:Taxonomy/'..taxon..']]'--error indicator in code before conversion to Lua
end
returnok,info
end

functionp.getTaxonInfoLink(frame,taxon)
localok,linkText,linkTarget
locallink=''
ok,linkText=p.getTaxonInfoItem(frame,taxon,'link_text')
ifokthen
ok,linkTarget=p.getTaxonInfoItem(frame,taxon,'link_target')
ifokthen
iflinkText==linkTargetthenlink=linkText
elselink=linkTarget..'|'..linkText
end
end
end
returnok,link
end

--[[= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Internal utility function to return a table (array) constructed from a
taxonomic hierarchy stored in "Template:Taxonomy/..." templates.
TABLE.n holds the total number of taxa; TABLE[1]..TABLE[TABLE.n] the taxon
names.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =]]
functionp.makeTable(frame,currTaxon)
locali=1
localinHierarchy=true-- still in the taxonomic hierarchy or off the top?
localtaxonTable={}
taxonTable[1]=currTaxon;
whilei<MaxSearchLevelsandinHierarchydo
localok,parent=p.getTaxonInfoItem(frame,currTaxon,'parent')
ifokandparent~=''then
currTaxon=parent
i=i+1
taxonTable[i]=currTaxon
else
inHierarchy=false-- run off the top of the hierarchy or tried to use non-existent taxonomy template
end
end
taxonTable.n=i
returntaxonTable
end

--[[= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Internal utility function to set up a table of numerical values corresponding
to 'Linnaean' ranks, with upper ranks having higher values. In a valid
taxonomic hierarchy, a lower rank should never have a higher value than a
higher rank. The actual numerical values are arbitrary so long as they are
ordered.
The ranks should correspond to those in Template:Anglicise ranks.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =]]
functionp.getRankTable()
return{
classis=1400,
cohort=1100,
divisio=1500,
domain=1700,
familia=800,
forma=100,
genus=600,
grandordo=1005,
['grandordo-mb']=1002,
infraclassis=1397,
infralegio=1197,
infraordo=997,
infraphylum=1497,
infraregnum=1597,
infratribus=697,
legio=1200,
magnordo=1006,
microphylum=1495,
micrordo=995,
mirordo=1004,
['mirordo-mb']=1001,
nanophylum=1499,
nanordo=999,
ordo=1000,
parafamilia=800,
parvordo=996,
phylum=1500,
regnum=1600,
sectio=500,
--series = 400, used too inconsistently to check
species=300,
subclassis=1398,
subcohort=1098,
subdivisio=1498,
subfamilia=798,
subgenus=598,
sublegio=1198,
subordo=998,
subphylum=1498,
subregnum=1598,
subsectio=498,
subspecies=298,
subtribus=698,
superclassis=1403,
supercohort=1103,
superdivisio=1503,
superdomain=1703,
superfamilia=803,
superlegio=1203,
superordo=1003,
superphylum=1503,
superregnum=1603,
supertribus=703,
tribus=700,
varietas=200,
zoodivisio=1300,
zoosectio=900,
zoosubdivisio=1298,
zoosubsectio=898,
}
end

returnp