Module:PublicationRecommendedItems

From MaRDI portal

Documentation for this module may be created at Module:PublicationRecommendedItems/doc

-- Module:PublicationRecommendedItems

-- Required module containing helper methods
local helper = require('Module:HelperMethods')

-- Required module for HTML generation
local mwHtml = require('mw.html')

-- Main table to hold all functions
local p = {}

----------------------------------------------------------------------
-- Render a list of recommended items directly from Wikibase claims
----------------------------------------------------------------------

function p.renderRecommendationList(claims, currentPublicationId)
    local ul = mwHtml.create('ul')

    -- Reaction icons
    local likeIcon = '👍'
    local dislikeIcon = '👎'

    if not claims or #claims == 0 then
        return nil
    end

    for _, claim in ipairs(claims) do
        if claim.mainsnak
            and claim.mainsnak.datavalue
            and claim.mainsnak.datavalue.value
            and claim.mainsnak.datavalue.value.id
        then
            local targetQid = claim.mainsnak.datavalue.value.id

            -- Resolve label (fallback handled)
            local label = mw.wikibase.getLabel(targetQid)
            if not label or label == '' then
                label = helper.titleNotAvailableStr()
            end

            -- Build publication links
            local targetLink =
                "https://portal.mardi4nfdi.de/wiki/Publication:"
                .. targetQid
                .. "?source=recommendation&referrer="
                .. currentPublicationId

            local currentPubLink =
                currentPublicationId:gsub("^Q", "Publication:")

            local likeLink =
                "https://portal.mardi4nfdi.de/wiki/"
                .. currentPubLink
                .. "?reaction=like&target="
                .. targetQid

            local dislikeLink =
                "https://portal.mardi4nfdi.de/wiki/"
                .. currentPubLink
                .. "?reaction=dislike&target="
                .. targetQid

            -- Build list item
            local li = mwHtml.create('li')
            li:wikitext('[' .. targetLink .. ' ' .. label .. '] ')
            li:wikitext('[' .. likeLink .. ' ' .. likeIcon .. '] ')
            li:wikitext('[' .. dislikeLink .. ' ' .. dislikeIcon .. ']')

            ul:node(li)
        end
    end

    return tostring(ul)
end

----------------------------------------------------------------------
-- Entry point called from templates
----------------------------------------------------------------------

function p.getCitesWorkList(frame)
    local targetId = frame.args[1]

    if not targetId or targetId == '' then
        return "No records found"
    end

    -- Fetch entity directly from Wikibase
    local entity = mw.wikibase.getEntity(targetId)
    if not entity or not entity.claims then
        return nil
    end

    -- Property P1643 = recommended / cites / related work
    local claims = entity.claims.P1643
    if not claims or #claims == 0 then
        return nil
    end

    -- Render list
    local rawList = p.renderRecommendationList(claims, targetId)
    if not rawList then
        return nil
    end

    local linkList = mw.getCurrentFrame():preprocess(rawList)

    local result = [[
<div class="keywords-header">
    <span class="keywords-title">Recommendations</span>
    <div class="keywords-line"></div>
</div>
<div class="keywords-list">
]] .. linkList .. [[
</div>
]]

    return result
end

----------------------------------------------------------------------
-- Module export
----------------------------------------------------------------------

return p