Module:Biota infobox/Wikidata
Appearance
From Wikipedia, the free encyclopedia
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
require('strict') localp={} localtemplateArgs={} --local info = {} ----------------------------------AUTOMATIC TAXONOMY (module and wikidata versions) ----------- functionp.addModuleTaxonomy(frame,taxoboxTable,templateArgs)-- use invoke of module function localmoduleData=require("Module:Sandbox/trappist the monk/taxonomy") localtree_t={}; localtaxon=templateArgs['taxon']ortemplateArgs['parent'] tree_t=moduleData._crawl_tree(taxon,tree_t) --local output = '\n|-\n|' .. table.concat (tree_t, '<br />') -- dump the whole table localoutput='' fork,vinpairs(tree_t)do localparts=mw.text.split(v,": ",true) ifstring.sub(parts[1],1,3)=="'''"then localstyled_rank=string.gsub(parts[1],"'''","") locallinked_taxon=string.gsub(parts[2]," <small>.*","") output=output..'\n|-\n|'..styled_rank..'||'..linked_taxon end end returnoutput end --[[ p.WikidataTaxonomy(frame) - entry point for Template:Biota infobox/Wikidata ]] functionp.WikidataTaxonomy(frame) -- get taxon name. This must be the Wikipedia page for the taxon (which is linked to the qid), e.g. Helix (gastropod) -- an alternative is to have a qid parameter localtaxon=frame:getParent().args['taxon']orframe.args['taxon']--or "Felis" localqid=frame:getParent().args['qid']orframe.args['qid'] ifqidandnottaxonthen taxon=mw.wikibase.getSitelink(qid)-- only works if taxon name, page title and taxonomy template match end ifnotqidandnottaxonthen return"No valid taxon or qid." end templateArgs['taxon']=taxon-- p.addWikidataTaxonomy() gets the taxon from template parameters templateArgs['qid']=qid -- contruct table comparing wikidata taxonomy and automated taxobox taxonomy localheaderColor="#ddeeff" localcomment='<p>Ancestral taxa taken from Wikidata. <br/>Bold taxa show those that will be displayed in taxobox.</p>' localoutput='{|\n|-\n| style="vertical-align:top;" |'..comment-- outer table ..'\n{| class="infobox biota" style="text-align: left; font-size:100%;"'-- wikidata ancestry table output=output..'\n|- \n! colspan="5" class="section-header" style="text-align:center;background:'..headerColor..';" ' ..'|'.."Ancestral taxa (from Wikidata)" ..'\n|- style="text-align:center;background:'..headerColor..';"' ..'\n! Rank !! Taxon !! colspan="2" | Wikidata ID !! Parents at Wikdata' templateArgs['display_parents']="all" templateArgs['show_alt_parents']=true templateArgs['select_parent']=frame:getParent().args['select_parent']orframe.args['select_parent'] output=output..p._addWikidataTaxonomy(frame) ifnot(frame:getParent().args['suppress_autotaxonomy_comparison']orframe.args['suppress_autotaxonomy_comparison'])then taxon=taxon:gsub("(.*)%s%(.*%)","%1")-- remove disambiguation localautomatedTaxonomyList=frame:callParserFunction('#invoke',{'Autotaxobox/sandbox','taxonomyList',taxon}) automatedTaxonomyList=automatedTaxonomyList:gsub("Ancestral taxa","Ancestral taxa <br/>(from automated taxonomy templates)") output=output..'\n|}'-- close wikidata table ..'\n| style="vertical-align:top;" |'-- cell for automated taxonomy table ..automatedTaxonomyList ..'\n|}' else output=output..'\n|}'..'\n|}'-- close wikidata table and outer table end returnoutput end functionp.getAnchor(frame) --return "Felis" --mw.title.getCurrentTitle().fragment end functionp.addWikidataTaxonomy(frame,taxoboxTable,args)-- use invoke of module function templateArgs=args returnp._addWikidataTaxonomy(frame) end functionp._addWikidataTaxonomy(frame)-- use invoke of module function localqid localtaxon=templateArgs['taxon']ortemplateArgs['parent']-- parent set for species and subspeciesbox iftemplateArgs['qid']then qid=templateArgs['qid']-- get qid from parameter else qid=mw.wikibase.getEntityIdForTitle(taxon)-- get qid from Wikipedia page on taxon end ifnotqidthen return"The taxon name must be the Wikipedia page title. Alternatively provide the Wikidata qid (qid=)." end localrows="" localrow="| rank || taxon" localcount=0 whileqidandcount<100do localtaxonInfo=p.getTaxonInfo(qid) ifnottaxonInfo.rankthentaxonInfo.rank="unranked"end ifnottaxonInfo.taxonthentaxonInfo.taxon="no taxon name"end ifp.showTaxonRow(taxonInfo.rank,taxonInfo.taxon,count)then-- display required rows locallink="" localitalics="" iftaxonInfo.rank=="genus"thenitalics="''"end iftaxonInfo.siteLinkandtaxonInfo.siteLink~=taxonInfo.taxonthenlink=taxonInfo.siteLink.."|"end row="\n|-\n| "..p.firstToUpper(taxonInfo.rank) .."||"..italics.."[["..link..taxonInfo.taxon.."]]"..italics iftemplateArgs['show_alt_parents']andtaxonInfo.altParentsthen row=row..'||[[:d:'..qid..'|'..qid..']]' ..'||[[:d:'..qid..'#P171|[edit]]]' .."||"..taxonInfo.altParents end rows=row..rows end iftaxonInfo.parentthen qid=taxonInfo.parent-- next parent else qid=nil-- no more parents so stop end count=count+1 end returnrows end localmajorRanks={"domain","superkingdom","kingdom","phylum","division","class","order","family","genus","species"} functionp.showTaxonRow(rank,taxon,count) localmajorRank=false fork,vinpairs(majorRanks)do ifrank==vthen majorRank=true-- flag major ranks for display majorRanks[k]=nil-- don't show this class again in hierarchy (e.g. for mammals, birds) end end ifmajorRankthenreturntrueend ifcount<(tonumber(templateArgs['display_parents'])or2)thenreturntrueend iftemplateArgs['display_parents']=="all"thenreturntrueend iftemplateArgs['display_parent_taxa']then ifstring.find(templateArgs['display_parent_taxa'],taxon)thenreturntrueend end iftemplateArgs['display_parent_ranks']then forvinmw.text.gsplit(templateArgs['display_parent_ranks'],",",true)do ifrank==mw.text.trim(v)thenreturntrueend end end returnfalse end functionp.getTaxonInfo(qid) --local rank, taxon, parent localtaxonInfo={} --[[ local item = mw.wikibase.getEntity(qid) -- this increments the expensive parser function count so instead of item:getBestStatements(property) use mw.wikibase.getBestStatements(qid, property) ]] localstatements=mw.wikibase.getBestStatements(qid,'P105')[1]--taxon rank ifstatementsandstatements.mainsnakandstatements.mainsnak.datavaluethen taxonInfo.rank=mw.wikibase.getLabel(statements.mainsnak.datavalue.value.id) end localstatements=mw.wikibase.getBestStatements(qid,'P225')[1]-- taxon name ifstatementsandstatements.mainsnakandstatements.mainsnak.datavaluethen taxonInfo.taxon=statements.mainsnak.datavalue.value end localstatements=mw.wikibase.getBestStatements(qid,'P171')-- taxon parent localstatement=statements[1] ifstatementandstatement.mainsnakandstatement.mainsnak.datavaluethen taxonInfo.parent=statement.mainsnak.datavalue.value.id-- use first value end forindex,statementinpairs(statements)do localid ifstatementandstatement.mainsnakandstatement.mainsnak.datavaluethen id=statement.mainsnak.datavalue.value.id end ifidthen locallabel=mw.wikibase.getLabel(id) localparentString=label.." ([[:d:"..id.."#P171|"..id.."]])" iftaxonInfo.altParentsthen taxonInfo.altParents=taxonInfo.altParents.."</br/>"..parentString else taxonInfo.altParents=parentString end localselectParent=templateArgs['select_parent']or"1"-- template parameters read as strings ifselectParent==tostring(index)orselectParent=="last"then taxonInfo.parent=id end end end locallabel=mw.wikibase.getLabel(qid) iflabelthentaxonInfo.label=labelend localsiteLink=mw.wikibase.getSitelink(qid) ifsiteLinkthentaxonInfo.siteLink=siteLinkend returntaxonInfo--rank, taxon, parent end functionp.firstToUpper(str) return(str:gsub("^%l",string.upper)) end returnp