Module:PublicationRecommendedItems

From MaRDI portal

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

-- Module:PublicationRecommendedItems

----------------------------------------------------------------------
-- Required modules
----------------------------------------------------------------------

local helper = require('Module:HelperMethods')
local mwHtml = require('mw.html')

----------------------------------------------------------------------
-- Main export table
----------------------------------------------------------------------

local p = {}

----------------------------------------------------------------------
-- Internal helpers
----------------------------------------------------------------------

-- Convert QID → Publication page title
-- Example: Q811151 → Publication:811151
local function publicationPageFromQid(qid)
    if not qid then
        return nil
    end

    local numericId = tostring(qid):match("^Q(%d+)$")
    if not numericId then
        return nil
    end

    return "Publication:" .. numericId
end

-- Resolve label with fallback
local function getLabelOrFallback(qid)
    local label = mw.wikibase.getLabel(qid)
    if not label or label == '' then
        return ''
    end
    return '|' .. label
end

----------------------------------------------------------------------
-- Render recommendation list
----------------------------------------------------------------------

function p.renderRecommendationList(claims, currentPublicationId)
    if not claims or #claims == 0 then
        return nil
    end

    local ul = mwHtml.create('ul')
    local count = 0

    local likeIcon = '👍'
    local dislikeIcon = '👎'

    -- Resolve current publication page (mandatory)
    local currentPubPage = publicationPageFromQid(currentPublicationId)
    if not currentPubPage then
        return nil
    end

    for _, claim in ipairs(claims) do
        local snak = claim.mainsnak

        if snak
            and snak.datavalue
            and snak.datavalue.value
            and snak.datavalue.value.id
        then
            local targetQid = snak.datavalue.value.id
            local targetPubPage = publicationPageFromQid(targetQid)

            -- Only proceed if target is a valid publication
            if targetPubPage then
                local label = getLabelOrFallback(targetQid)
                local sitelink = mw.wikibase.getSitelink(targetQid)
                if not sitelink then
                	sitelink = "Item:Q" .. targetQid
				end 	
                -- Reaction links
                local likeLink =
                    "{fullurl:{{FULLPAGENAME}}|reaction=like&target="
                    .. targetQid
                    .. "}"

                local dislikeLink =
                    "{fullurl:{{FULLPAGENAME}}|reaction=dislike&target="
                    .. targetQid
                    .. "}"

                -- Build list item
                -- Read https://www.mediawiki.org/wiki/Help:Links
                local li = mwHtml.create('li')
                li:wikitext('[[:' .. sitelink .. label .. ']] ')
                --li:wikitext('[[#recommended|' .. likeIcon .. ']]')
                --li:wikitext('[[#recommended|' .. dislikeIcon .. ']]')

                ul:node(li)
                count = count + 1
            end
        end
    end
    
    if count == 0 then
    	return nil
    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

    local entity = mw.wikibase.getEntity(targetId)
    if not entity or not entity.claims then
        return nil
    end

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

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

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

    return [[
<div class="keywords-header">
<span class="keywords-title">Recommendations</span>
<div class="keywords-line"></div>
</div>
<div class="keywords-list" id="recommended">
]] .. linkList .. [[
</div>
]]
end

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

return p