Modul:Taxonbar: Versiyalar orasidagi farq
Programmer (munozara | hissa) Oʻzbekcha Vikipediyadan import qilindi. |
Programmer (munozara | hissa) kTahrir izohi yoʻq |
||
| Qator 3: | Qator 3: | ||
local conf = require( 'Module:Taxonbar/conf' ) --configuration module | local conf = require( 'Module:Taxonbar/conf' ) --configuration module | ||
local TaxonItalics = require( 'Module:TaxonItalics' ) --use a function to conditionally italicize taxon names | local TaxonItalics = require( 'Module:TaxonItalics' ) --use a function to conditionally italicize taxon names | ||
-- Ziyosfera compatibility: run safely even when Wikibase is not installed. | |||
local hasWikibase = type(mw.wikibase) == 'table' | |||
local function wikibaseGetEntity(id) | |||
if not hasWikibase or type(mw.wikibase.getEntity) ~= 'function' then | |||
return nil | |||
end | |||
if id == nil or id == '' then | |||
return nil | |||
end | |||
local ok, entity = pcall(function() | |||
return mw.wikibase.getEntity(id) | |||
end) | |||
if ok then | |||
return entity | |||
end | |||
return nil | |||
end | |||
local function wikibaseGetEntityIdForCurrentPage() | |||
if not hasWikibase or type(mw.wikibase.getEntityIdForCurrentPage) ~= 'function' then | |||
return nil | |||
end | |||
local ok, id = pcall(function() | |||
return mw.wikibase.getEntityIdForCurrentPage() | |||
end) | |||
if ok then | |||
return id | |||
end | |||
return nil | |||
end | |||
local function getBestStatements(entity, property) | |||
if not entity or type(entity.getBestStatements) ~= 'function' then | |||
return {} | |||
end | |||
local ok, statements = pcall(function() | |||
return entity:getBestStatements(property) | |||
end) | |||
if ok and type(statements) == 'table' then | |||
return statements | |||
end | |||
return {} | |||
end | |||
local function getBestStatement(entity, property, index) | |||
return getBestStatements(entity, property)[index or 1] | |||
end | |||
local function getEntityLabel(entity) | |||
if not entity or type(entity.getLabel) ~= 'function' then | |||
return nil | |||
end | |||
local ok, label = pcall(function() | |||
return entity:getLabel() | |||
end) | |||
if ok then | |||
return label | |||
end | |||
return nil | |||
end | |||
--[[==========================================================================]] | --[[==========================================================================]] | ||
| Qator 37: | Qator 99: | ||
end | end | ||
]] | ]] | ||
local statements = item | local statements = getBestStatement(item, property) | ||
if statements and | if statements and | ||
statements.mainsnak and | statements.mainsnak and | ||
| Qator 58: | Qator 120: | ||
else | else | ||
if type(property) == 'number' and property > 0 then | if type(property) == 'number' and property > 0 then | ||
local entityObject = | local entityObject = wikibaseGetEntity('P'..property) | ||
local dataType | local dataType | ||
| Qator 70: | Qator 132: | ||
property == 5397 --Tierstimmenarchiv | property == 5397 --Tierstimmenarchiv | ||
then | then | ||
formatterURL = entityObject | formatterURL = getBestStatement(entityObject, 'P1630', 2) --use 2nd formatterURL for English version | ||
end | end | ||
if formatterURL == nil then formatterURL = entityObject | if formatterURL == nil then formatterURL = getBestStatement(entityObject, 'P1630') end --default to [1] | ||
if formatterURL then | if formatterURL then | ||
if formatterURL.mainsnak.datavalue and formatterURL.mainsnak.datavalue.value then --nil check for ABA | if formatterURL.mainsnak.datavalue and formatterURL.mainsnak.datavalue.value then --nil check for ABA | ||
| Qator 82: | Qator 144: | ||
end | end | ||
elseif dataType == 'url' then | elseif dataType == 'url' then | ||
local subjectItem = entityObject | local subjectItem = getBestStatement(entityObject, 'P1629') | ||
if subjectItem then | if subjectItem then | ||
local | local subjectId = subjectItem.mainsnak.datavalue.value.id | ||
if officialWebsite then link = officialWebsite.mainsnak.datavalue.value end | local subjectEntity = wikibaseGetEntity(subjectId) | ||
local officialWebsite = getBestStatement(subjectEntity, 'P856') | |||
if officialWebsite and officialWebsite.mainsnak and officialWebsite.mainsnak.datavalue then | |||
link = officialWebsite.mainsnak.datavalue.value | |||
end | |||
end | end | ||
elseif dataType == 'string' then | elseif dataType == 'string' then | ||
local formatterURL = entityObject | local formatterURL = getBestStatement(entityObject, 'P1630') | ||
if formatterURL then | if formatterURL then | ||
link = formatterURL.mainsnak.datavalue.value | link = formatterURL.mainsnak.datavalue.value | ||
else | else | ||
local subjectItem = entityObject | local subjectItem = getBestStatement(entityObject, 'P1629') | ||
if subjectItem then | if subjectItem then | ||
local | local subjectId = subjectItem.mainsnak.datavalue.value.id | ||
if officialWebsite then link = officialWebsite.mainsnak.datavalue.value end | local subjectEntity = wikibaseGetEntity(subjectId) | ||
local officialWebsite = getBestStatement(subjectEntity, 'P856') | |||
if officialWebsite and officialWebsite.mainsnak and officialWebsite.mainsnak.datavalue then | |||
link = officialWebsite.mainsnak.datavalue.value | |||
end | |||
end | end | ||
end | end | ||
| Qator 155: | Qator 225: | ||
function p.authorityControlTaxon( frame ) | function p.authorityControlTaxon( frame ) | ||
local resolveEntity = require | local resolveEntity = nil | ||
do | |||
local ok, mod = pcall(require, 'Module:ResolveEntityId') | |||
if ok and type(mod) == 'table' then | |||
resolveEntity = mod | |||
end | |||
end | |||
local function resolveEntityId(value) | |||
if not resolveEntity or type(resolveEntity._id) ~= 'function' then | |||
return nil | |||
end | |||
local ok, id = pcall(resolveEntity._id, value) | |||
if ok then | |||
return id | |||
end | |||
return nil | |||
end | |||
local parentArgs = copyTable(frame:getParent().args) | local parentArgs = copyTable(frame:getParent().args) | ||
local currentTitle = mw.title.getCurrentTitle() | local currentTitle = mw.title.getCurrentTitle() | ||
local currentEntityId = | local currentEntityId = wikibaseGetEntityIdForCurrentPage() | ||
local stringArgs = false | local stringArgs = false | ||
| Qator 224: | Qator 310: | ||
local currentItem = nil | local currentItem = nil | ||
if currentTitle.namespace == 10 then --i.e. Module:Taxonbar/sandbox, Template:Taxonbar/doc, etc. | if currentTitle.namespace == 10 then --i.e. Module:Taxonbar/sandbox, Template:Taxonbar/doc, etc. | ||
if | if resolveEntityId(parentArgs['from']) then | ||
currentItem = | currentItem = wikibaseGetEntity(parentArgs['from']) | ||
end | end | ||
if currentItem == nil then | if currentItem == nil then | ||
if | if resolveEntityId(parentArgs['from1']) then | ||
currentItem = | currentItem = wikibaseGetEntity(parentArgs['from1']) | ||
end | end | ||
end | end | ||
elseif | elseif resolveEntityId(currentEntityId) then | ||
currentItem = | currentItem = wikibaseGetEntity(currentEntityId) | ||
else --currentEntityId == nil/unresolvable | else --currentEntityId == nil/unresolvable | ||
tCats[3] = '[[Turkum:Taxonbar pages requiring a Wikidata item]]' | tCats[3] = '[[Turkum:Taxonbar pages requiring a Wikidata item]]' | ||
| Qator 239: | Qator 325: | ||
if currentItem then | if currentItem then | ||
tCats[4] = '[[Turkum:Taxonbars on possible non-taxon pages]]' --unset if acceptable found | tCats[4] = '[[Turkum:Taxonbars on possible non-taxon pages]]' --unset if acceptable found | ||
for _, instanceOfState in pairs ( | for _, instanceOfState in pairs ( getBestStatements(currentItem, 'P31') ) do --instance of | ||
local instanceOf = instanceOfState.mainsnak.datavalue.value.id | local instanceOf = instanceOfState.mainsnak.datavalue.value.id | ||
if acceptableInstanceOf_All[instanceOf] then | if acceptableInstanceOf_All[instanceOf] then | ||
| Qator 354: | Qator 440: | ||
--Append basionym to arg list, if not already provided | --Append basionym to arg list, if not already provided | ||
if currentItem then | if currentItem then | ||
local currentBasState = currentItem | local currentBasState = getBestStatement(currentItem, 'P566') --basionym | ||
if currentBasState then | if currentBasState then | ||
local basionymId = currentBasState.mainsnak.datavalue.value.id | local basionymId = currentBasState.mainsnak.datavalue.value.id | ||
if basionymId and | if basionymId and resolveEntityId(basionymId) and tFroms[basionymId] == nil then | ||
--check that basionym is a strict instance of taxon | --check that basionym is a strict instance of taxon | ||
local basionymItem = | local basionymItem = wikibaseGetEntity(basionymId) | ||
if basionymItem then | if basionymItem then | ||
for _, instanceOfState in pairs ( | for _, instanceOfState in pairs ( getBestStatements(basionymItem, 'P31') ) do --instance of | ||
local instanceOf = instanceOfState.mainsnak.datavalue.value.id | local instanceOf = instanceOfState.mainsnak.datavalue.value.id | ||
if acceptableInstanceOf_Strict[instanceOf] then | if acceptableInstanceOf_Strict[instanceOf] then | ||
| Qator 376: | Qator 462: | ||
--Append original combination to arg list, if not already provided | --Append original combination to arg list, if not already provided | ||
if currentItem then | if currentItem then | ||
local currentOCState = currentItem | local currentOCState = getBestStatement(currentItem, 'P1403') --original combination | ||
if currentOCState then | if currentOCState then | ||
local orcoId = currentOCState.mainsnak.datavalue.value.id | local orcoId = currentOCState.mainsnak.datavalue.value.id | ||
if orcoId and | if orcoId and resolveEntityId(orcoId) and tFroms[orcoId] == nil then | ||
--check that orco is a strict instance of taxon | --check that orco is a strict instance of taxon | ||
local orcoItem = | local orcoItem = wikibaseGetEntity(orcoId) | ||
if orcoItem then | if orcoItem then | ||
for _, instanceOfState in pairs ( | for _, instanceOfState in pairs ( getBestStatements(orcoItem, 'P31') ) do --instance of | ||
local instanceOf = instanceOfState.mainsnak.datavalue.value.id | local instanceOf = instanceOfState.mainsnak.datavalue.value.id | ||
if acceptableInstanceOf_Strict[instanceOf] then | if acceptableInstanceOf_Strict[instanceOf] then | ||
| Qator 398: | Qator 484: | ||
--Append monotypic genus/species to arg list of monotypic species/genus, if not already provided | --Append monotypic genus/species to arg list of monotypic species/genus, if not already provided | ||
if currentItem then | if currentItem then | ||
for _, instanceOfState in pairs ( | for _, instanceOfState in pairs ( getBestStatements(currentItem, 'P31') ) do --instance of | ||
local taxonRank = nil | local taxonRank = nil | ||
local parentItem = nil | local parentItem = nil | ||
| Qator 406: | Qator 492: | ||
local instanceOf = instanceOfState.mainsnak.datavalue.value.id | local instanceOf = instanceOfState.mainsnak.datavalue.value.id | ||
if instanceOf and (instanceOf == 'Q310890' or instanceOf == 'Q47487597') then --monotypic/fossil taxon | if instanceOf and (instanceOf == 'Q310890' or instanceOf == 'Q47487597') then --monotypic/fossil taxon | ||
local taxonRankState = currentItem | local taxonRankState = getBestStatement(currentItem, 'P105') --taxon rank | ||
if taxonRankState then taxonRank = taxonRankState.mainsnak.datavalue.value.id end | if taxonRankState then taxonRank = taxonRankState.mainsnak.datavalue.value.id end | ||
if taxonRank and taxonRank == 'Q7432' then --species | if taxonRank and taxonRank == 'Q7432' then --species | ||
--is monotypic species; add genus | --is monotypic species; add genus | ||
local parentTaxonState = currentItem | local parentTaxonState = getBestStatement(currentItem, 'P171') --parent taxon | ||
if parentTaxonState then parentTaxon = parentTaxonState.mainsnak.datavalue.value.id end | if parentTaxonState then parentTaxon = parentTaxonState.mainsnak.datavalue.value.id end | ||
--confirm parent taxon rank == genus & monotypic | --confirm parent taxon rank == genus & monotypic | ||
if parentTaxon and | if parentTaxon and resolveEntityId(parentTaxon) then | ||
parentItem = | parentItem = wikibaseGetEntity(parentTaxon) | ||
if parentItem then | if parentItem then | ||
local parentTaxonRankState = parentItem | local parentTaxonRankState = getBestStatement(parentItem, 'P105') --taxon rank | ||
if parentTaxonRankState then parentTaxonRank = parentTaxonRankState.mainsnak.datavalue.value.id end | if parentTaxonRankState then parentTaxonRank = parentTaxonRankState.mainsnak.datavalue.value.id end | ||
if parentTaxonRank and parentTaxonRank == 'Q34740' then --parent == genus | if parentTaxonRank and parentTaxonRank == 'Q34740' then --parent == genus | ||
for _, parentInstanceOfState in pairs ( | for _, parentInstanceOfState in pairs ( getBestStatements(parentItem, 'P31') ) do --instance of | ||
local parentInstanceOf = parentInstanceOfState.mainsnak.datavalue.value.id | local parentInstanceOf = parentInstanceOfState.mainsnak.datavalue.value.id | ||
if parentInstanceOf and | if parentInstanceOf and | ||
| Qator 478: | Qator 564: | ||
end | end | ||
--Fetch Wikidata item | --Fetch Wikidata item | ||
local from = | local from = resolveEntityId(parentArgs['from'..f]) | ||
local item = | local item = wikibaseGetEntity(from) | ||
local label = nil | local label = nil | ||
if type(item) == 'table' then | if type(item) == 'table' then | ||
local statements = item | local statements = getBestStatement(item, 'P225') --taxon name | ||
if statements then | if statements then | ||
local datavalue = statements.mainsnak.datavalue | local datavalue = statements.mainsnak.datavalue | ||
| Qator 489: | Qator 575: | ||
end | end | ||
end | end | ||
label = label or item | label = label or getEntityLabel(item) | ||
else | else | ||
if parentArgs['from'..f] then | if parentArgs['from'..f] then | ||