Mine sisu juurde

Modul':NotableWorks

Мегавики-späi

-- 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
       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, ' vep' )
           -- if not label then
           --	label = 'Label should be filled. WorkEntityId = '..workEntityId
           --end
           local oldLabel = label
           local wwd = mw.wikibase.getEntity(workEntityId)
           local  vepwiki = wwd and wwd.sitelinks and wwd.sitelinks. vepwiki and wwd.sitelinks. vepwiki.title
           if label then
           	local bracketPos
           	label = string.gsub(label, 'vestatuz', )
           	label = string.gsub(label, 'modkuva', )
           	label = string.gsub(label, 'rindkuva', )
           	label = string.gsub(label, 'statui', )

-- label = string.gsub(label, 'muštpacaz', ) -- label = string.gsub(label, 'modkuva', )

           	label = string.gsub(label, ', ,', ',')
           	label = string.gsub(label, '%(, ', '(')
           	label = string.gsub(label, '%(,', '(')
           	label = string.gsub(label, '%( %)', )
           	label = string.gsub(label, '%(%)', )

-- if label then -- bracketPos = string.find(label, " %(") - 1 -- end -- if bracketPos then -- label = string.sub(label, 1, bracketPos)..''..string.sub(label, bracketPos)..'' -- end

           	if vepwiki then
           		label = ""..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, 'vep' )
   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