local p = -- exposed functionslocal l = -- local functions
local botanicalAuthorites = require("Module:Taxon_authority/data").botanicalAuthorites -- get list from submodulelocal names = --------------------------------------------------------------------------------------- main entry pointp.main = function(frame) local option = mw.getCurrentFrame:getParent.args['option'] if option
--------------------------------------------------------------------------------------- function to adds wikilinks for valid botanical authorities this version uses the keyed table at the top of this module the name in the key needs to be escaped for the lua expression wildcard "." p.linkBotanicalAuthorites = function(name) if names[name] then name = l.wikilinkName(names[name], name) -- if the passed authority matches a single authority name else for k, v in pairs(names) do -- if the passed authority contains the authority name if string.find(name, k, 1, false) then -- plain=true as don't want to treat . as wildcard
name = string.gsub(name, l.escape(k), l.wikilinkName(v,k)) -- is the wildcard . a potential problem? end end end return '' .. name .. ''end--p.linkBotanicalAuthorites2 = function(frame) local name = mw.getCurrentFrame:getParent.args[1] for k, v in pairs(botanicalAuthorites) do if name
----------------------------------------------------------------------------------- general purpose function to experiment with wikidata etc p.misc = function (frame) local output = "" --get arguments from calling templates local parent = mw.getCurrentFrame:getParent local name = parent.args[1] -- first check the module list --p.linkBotanicalAuthorites2(frame)
-- if name not found so try wikidata if output
l.errmsg = function(text) return '
' .. text .. ''endl.getBotanicalAuthorityFromWikiData=function(name) if (2local WikidataId = mw.wikibase.getEntityIdForTitle(name)
if not (WikidataId and mw.wikibase.isValidEntityId(WikidataId)) then local titleObj = mw.title.new(name).redirectTarget if titleObj and titleObj.text ~= nil then name = titleObj.text end WikidataId = mw.wikibase.getEntityIdForTitle(name) end
if WikidataId and mw.wikibase.isValidEntityId(WikidataId) then -- valid wikidata id local value = "authority not found" local item = mw.wikibase.getEntity(WikidataId) local statements = item:getBestStatements('P428')[1] --botany authority if statements ~= nil then -- value = statements.mainsnak.datavalue.value end return value end --return l.errmsg("wikidata item not found for " .. name), true -- return error message return name, true -- return unlinked name, true to indicate errorend
return p