Модуль:NotableWorks
Тӱжвал сын
Для документации этого модуля может быть создана страница Модуль:NotableWorks/doc
-- Module: NotableWorks
local p = {}
-- Function to get the current page's RUWIKI.Data item ID
function p.getPageWikidataId()
local pageWikidata = mw.wikibase.getEntityIdForCurrentPage()
return pageWikidata
end
-- Function to retrieve creator (P170) from the page's RUWIKI.Data item
function p.getCreator(entityId)
local wd = mw.wikibase.getEntity(entityId)
if wd and wd.claims and wd.claims['P170'] then
for _, claim in ipairs(wd.claims['P170']) do
local creator = claim.mainsnak.datavalue and claim.mainsnak.datavalue.value
if creator then
return creator.id
end
end
end
return nil -- No creator found
end
-- Get all notable works (P800) for the entity
function p.processNotableWorksClaims(notableWorks, wd)
if wd and wd.claims and wd.claims['P800'] then
local lang_code = mw.language.getContentLanguage():getCode():lower() -- "ru", "ba", "mhr", ...
local project_dbname = mw.site.dbName -- "ruwiki", "bawiki", "mhrwiki", ...
for _, claim in ipairs(wd.claims['P800']) do
-- Add the label of each notable work to the list
local workEntityId = claim.mainsnak.datavalue and claim.mainsnak.datavalue.value and claim.mainsnak.datavalue.value.id
local label = mw.wikibase.getLabel( workEntityId, lang_code )
-- if not label then
-- label = 'Label should be filled. WorkEntityId = '..workEntityId
--end
local oldLabel = label
local wwd = mw.wikibase.getEntity(workEntityId)
article_title = wwd:getSitelink(project_dbname)
if label then
local bracketPos
local words_to_remove = {
'скульптур', 'бюст', 'барельеф', 'статуй', -- 'чапкӱ', 'портрет',
}
for _, word in ipairs(words_to_remove) do
label = label:gsub(word, '')
end
label = label
:gsub(', ,', ',')
:gsub('%(, ', '(')
:gsub('%(,', '(')
:gsub('%( %)', '')
:gsub('%(%)', '')
-- if label then
-- bracketPos = string.find(label, " %(") - 1
-- end
-- if bracketPos then
-- label = string.sub(label, 1, bracketPos)..'<font color=gray>'..string.sub(label, bracketPos)..'</font>'
-- end
if article_title then
label = "[["..article_title.."|"..label.."]]"
end
--label = label..' ! '..oldLabel
table.insert(notableWorks, label)
end
end
end
end
function p.getCreatorLabel(frame)
local entityId = frame.args[1]
-- If no entityId is provided, use the current page's Wikidata ID
if not entityId or entityId == "" then
entityId = p.getPageWikidataId()
local creatorEntityId = p.getCreator(entityId)
if creatorEntityId then
entityId = creatorEntityId
end
end
creatorLabel = mw.wikibase.getLabel( entityId, 'mhr' )
return creatorLabel
end
-- Main function to return notable works as comma-separated text
function p.getNotableWorks(frame)
-- Get the Wikidata ID passed as a parameter
local entityId = frame.args[1]
-- If no entityId is provided, use the current page's Wikidata ID
if not entityId or entityId == "" then
entityId = p.getPageWikidataId()
end
local notableWorks = {}
-- Fetch the entity from RW.Data
local wd = mw.wikibase.getEntity(entityId)
p.processNotableWorksClaims(notableWorks, wd)
if #notableWorks == 0 then
local creatorEntityId = p.getCreator(entityId)
if creatorEntityId then
wd = mw.wikibase.getEntity(creatorEntityId)
p.processNotableWorksClaims(notableWorks, wd)
end
end
-- Return the notable works as a comma-separated list
local worksList = "\n* "..table.concat(notableWorks, "\n*").."\n"
return worksList
end
return p