Module:Taxonbar/candidate: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Use Module:Taxonbar/whitelist & Module:Taxonbar/blacklist centralized lists (whitelist contains 3 new items from what was previously used here: ootaxon d:Q59278506, extinct taxon d:Q98961713, and paraphyletic group d:Q58051350); from sandboxes
m Process "<!--{{Taxonbar}}-->" as if it doesn't exist, per Template talk:Taxonbar#'Exists' module misses cases where the taxonbar is commented out
 
Line 3: Line 3:
function p.check(frame)
function p.check(frame)
local taxonbarExists = require('Module:Taxonbar/exists').check
local taxonbarExists = require('Module:Taxonbar/exists').check
if taxonbarExists(frame) then
if taxonbarExists(frame) == true then
return ''
return ''
else
else

Latest revision as of 14:48, 17 April 2024

local p = {}

function p.check(frame)
	local taxonbarExists = require('Module:Taxonbar/exists').check
	if taxonbarExists(frame) == true then
		return ''
	else
		local resolveEntity = require( 'Module:ResolveEntityId' )
		local currentTitle = mw.title.getCurrentTitle()
		local currentItem = nil
		local categories = {
			'[[Category:Taxobox articles possibly missing a taxonbar]]' --unset if acceptable or unacceptable found
		}
		
		--preliminary exclusion criteria
		if string.match( currentTitle.text, '^List of ') then
			return ''
		end
		
		--find Wikidata item
		if currentTitle.namespace == 0 then --mainspace
			local currentEntityId = mw.wikibase.getEntityIdForCurrentPage()
			if resolveEntity._id(currentEntityId) then
				currentItem = mw.wikibase.getEntity(currentEntityId)
			else --currentEntityId == nil/unresolvable
				return categories[1]
			end
		else
			return ''
		end
		
		if currentItem then --currentItem should not be nil here, but check just in case
			--determine if {{Taxonbar}} is *definitely* missing, *possibly* missing, or not missing at all
			local whitelist = require( 'Module:Taxonbar/whitelist' ).whitelist
			local blacklist = require( 'Module:Taxonbar/blacklist' ).blacklist
			local acceptableInstanceOf_All = whitelist{ args = { 'all' } }
			local unacceptableInstanceOf = blacklist{ args = {} }
			
			local acceptableCount = 0
			local unacceptableCount = 0
			for _, instanceOfState in pairs ( currentItem:getBestStatements('P31') ) do --instance of
				local instanceOf = instanceOfState.mainsnak.datavalue.value.id
				if acceptableInstanceOf_All[instanceOf] then
					acceptableCount = acceptableCount + 1
				elseif unacceptableInstanceOf[instanceOf] then
					unacceptableCount = unacceptableCount + 1
				end
			end
			for _, subclassOfState in pairs ( currentItem:getBestStatements('P279') ) do --subclass of
				local subclassOf = subclassOfState.mainsnak.datavalue.value.id
				if acceptableInstanceOf_All[subclassOf] then
					acceptableCount = acceptableCount + 1
				elseif unacceptableInstanceOf[subclassOf] then
					unacceptableCount = unacceptableCount + 1
				end
			end
			
			if acceptableCount > 0 then
				categories[1] = ''
				categories[2] = '[[Category:Taxobox articles missing a taxonbar]]'
			elseif unacceptableCount > 0 then
				categories[1] = ''
				categories[2] = ''
			end
			
			return table.concat(categories)
			
		else
			return ''
		end
	end
end

return p