Kontentke ótiw

Modul:Taxonbar

Wikipedia — erkin enciklopediya

Bul moduldiń hújjetin Modul:Taxonbar/doc betinde jarata alasız

require('strict')

local conf = require( 'Modul:Taxonbar/conf' ) -- konfiguraciya modulu
local TaxonItalics = require( 'Modul:TaxonItalics' ) -- takson atamaların qıya qılıw ushın

--[[==========================================================================]]
--[[                             Local functions                              ]]
--[[==========================================================================]]

local function isNilOrEmpty( thing )
	if thing == nil or thing == '' then
		return true
	end
	return nil
end

local function getIdFromWikidata( item, property )
	local id = nil
	if property == 'PWikispecies:$1' then
		local siteLinks = item.sitelinks
		if siteLinks then
			local speciesWiki = item.sitelinks.specieswiki
			if speciesWiki then
				id = speciesWiki.title
			end
		end
		return id
	elseif item.claims[property] == nil then
		return id
	end

	local statements = item:getBestStatements(property)[1] 
	if statements and
	   statements.mainsnak and
	   statements.mainsnak.datavalue and
	   statements.mainsnak.datavalue.value
	then
		id = statements.mainsnak.datavalue.value
	end

	return id
end

local function getLink( property, db, val )
	local link, returnVal = '', {}
	
	returnVal.isError = false
	
	if mw.ustring.find( val, '//' ) then
		link = val
	else
		if type(property) == 'number' and property > 0 then
			local entityObject = mw.wikibase.getEntity('P'..property)
			local dataType
			
			if entityObject then dataType = entityObject.datatype
			else returnVal.isError = true end
			
			if dataType == 'external-id' then
				local formatterURL = nil
				if property == 3746 or property == 3795 or property == 5397 then
					formatterURL = entityObject:getBestStatements('P1630')[2] 
				end
				if formatterURL == nil then formatterURL = entityObject:getBestStatements('P1630')[1] end
				if formatterURL then
					if formatterURL.mainsnak.datavalue and formatterURL.mainsnak.datavalue.value then
						link = formatterURL.mainsnak.datavalue.value
					end
				end
				if db == 'bow' then link = 'https://birdsoftheworld.org/bow/species/$1'
				elseif db == 'speciesfungorum' then link = 'https://www.speciesfungorum.org/Names/NamesRecord.asp?RecordID=$1'
				elseif db == 'fossilworks' then link = 'https://paleobiodb.org/classic/basicTaxonInfo?taxon_no=$1'
				end
			elseif dataType == 'url' then
				local subjectItem = entityObject:getBestStatements('P1629')[1]
				if subjectItem then
					local officialWebsite = mw.wikibase.getEntity(subjectItem.mainsnak.datavalue.value.id):getBestStatements('P856')[1]
					if officialWebsite then	link = officialWebsite.mainsnak.datavalue.value end
				end
			elseif dataType == 'string' then
				local formatterURL = entityObject:getBestStatements('P1630')[1]
				if formatterURL then link = formatterURL.mainsnak.datavalue.value
				else
					local subjectItem = entityObject:getBestStatements('P1629')[1]
					if subjectItem then
						local officialWebsite = mw.wikibase.getEntity(subjectItem.mainsnak.datavalue.value.id):getBestStatements('P856')[1]
						if officialWebsite then link = officialWebsite.mainsnak.datavalue.value end
					end
				end
			else returnVal.isError = true end
		elseif type(property) == 'string' then
			link = property
		end
		
		local valurl = mw.uri.encode( val, 'PATH' )
		valurl = string.gsub (valurl, '%%2F', '/')
		
		if type(property) == 'number' then
			if property == 3746 then link = mw.ustring.gsub(link, '/hebrew/', '/english/') end
			if property == 4301 then valurl = mw.ustring.gsub(valurl, '%%20', '+') end
		end
		valurl = mw.ustring.gsub(valurl,'%%','%%%%')
		link = mw.ustring.gsub(link, '$1', valurl)
	end
	
	link = mw.ustring.gsub(link, '^[Hh][Tt][Tt][Pp]([Ss]?)://', 'http%1://')
	val = mw.ustring.match(val, '([^=/]*)/?$')
	if mw.ustring.find( link, '//' ) then
		returnVal.text = '['..link..' '..mw.text.encode(mw.uri.decode(val, 'PATH'),'%[%]')..']'
	elseif link == '' then
		returnVal.text = val
	else
		returnVal.text = '<span class="external">[['..link..'|'..val..']]</span>'
	end
	return returnVal
end

local function createRow( id, label, rawValue, link, withUid )
	if link then
		local outStr = '*<span style="white-space:nowrap;">'..label..' <span'
		if withUid then outStr = outStr..' class="uid"' end
		return outStr..'>'..link..'</span></span>\n'
	else
		return '* '..mw.text.tag('span', {class='error'}, id..' '..rawValue..' identifikatorı jaramsız.')..'\n'
	end
end

local function copyTable(inTable)
	if type(inTable) ~= 'table' then return inTable end
	local outTable = setmetatable({}, getmetatable(inTable))
	for key, value in pairs (inTable) do outTable[copyTable(key)] = copyTable(value) end
	return outTable
end

local p = {}

--[[==========================================================================]]
--[[                                   Main                                   ]]
--[[==========================================================================]]

function p.authorityControlTaxon( frame )
	local resolveEntity = require( 'Modul:ResolveEntityId' )
	local whitelist = require( 'Modul:Taxonbar/whitelist' ).whitelist
	local parentArgs = copyTable(frame:getParent().args)
	local currentTitle = mw.title.getCurrentTitle()
	local currentEntityId = mw.wikibase.getEntityIdForCurrentPage()
	
	local stringArgs = false
	local fromTitleCount, firstRow, rowCount = 1, 0, 0
	local outString, errors = '', ''
	local iFroms = 0
	local tFroms = {}
	local tCats = {
		'[[Kategoriya:from parametri joq bolǵan takson-barlar]]',
		'[[Kategoriya:Wikidata menen sáykeslespegen takson-barlar]]',
		'', -- [3]
		'', -- [4]
		'', -- [5]
		'', -- [6]
		'', -- [7]
		'', -- [8]
		'', -- [9]
		'', --[10]
		'', --[11]
		'', --[12]
		'', --[13]
		'', --[14]
	}
	
	local acceptableInstanceOf_Strict = whitelist{ args = { 'strict' } }
	local acceptableInstanceOf_All = whitelist{ args = { 'all' } }
	
	local currentItem = nil
	if currentTitle.namespace == 10 then 
		if resolveEntity._id(parentArgs['from']) then currentItem = mw.wikibase.getEntity(parentArgs['from']) end
	elseif resolveEntity._id(currentEntityId) then
		currentItem = mw.wikibase.getEntity(currentEntityId)
	else
		tCats[3] = '[[Kategoriya:Wikidata elementi kerek bolǵan takson-bar betleri]]'
	end

	-- Setup navbox
	local navboxParams = {
		name  = 'Taxonbar',
		bodyclass = 'hlist',
		listclass = '',
		groupstyle = 'text-align: left;',
	}
	
	for f = 1, fromTitleCount, 1
	do
		local elements, title = {}, nil
		local from = resolveEntity._id(parentArgs['from'..f])
		local item = mw.wikibase.getEntity(from)
		local label = nil
		if type(item) == 'table' then
			local statements = item:getBestStatements('P225')[1]
			if statements then
				local datavalue = statements.mainsnak.datavalue
				if datavalue then label = datavalue.value end
			end
			label = label or item:getLabel()
		end
		
		title = label and mw.title.new(label) or currentTitle
		
		if title then
			if title.namespace == 0 or stringArgs then
				local sourceCount = 0
				for _, params in pairs( conf.databases ) do
					local val = parentArgs[mw.ustring.lower(params[1])..f]
					local propId = params[3]
					
					if (item and item.claims) and type(propId) == 'number' and propId > 0 then
						local wikidataId = getIdFromWikidata( item, 'P'..propId )
						if isNilOrEmpty(val) then val = wikidataId end
					end

					if val and val ~= '' and mw.ustring.lower(val) ~= 'no' then
						table.insert( elements, createRow( params[1], params[2]..':', val, getLink( propId, params[1], val ).text, true ) )
						sourceCount = sourceCount + 1
					end
				end

				if sourceCount > 0 then
					rowCount = rowCount + 1
					navboxParams['group'..f] = TaxonItalics.italicizeTaxonName(title.text, false)
					navboxParams['list'..f] = table.concat( elements )
				end
			end
		end
	end
	
	if rowCount > 0 then
		local Navbox = require('Modul:Navbox')
		navboxParams['title'] = '[[Anıqlama:Takson identifikatorları|Takson identifikatorları]]'
		if rowCount < 4 then
			navboxParams['state'] = 'off'
			navboxParams['navbar'] = 'off'
		end
		outString = Navbox._navbox(navboxParams)
	end
	
	return outString .. table.concat(tCats) .. errors
end

function p.taxonbar(frame) return p.authorityControlTaxon( frame:newChild{title = frame:getTitle()} ) end

return p