Module:PublicationRecommendedItems: Difference between revisions

From MaRDI portal
User15 (talk | contribs)
try transform to take data from wikibase
User15 (talk | contribs)
changing wikbase installation to numeric from numeric-id
Line 1: Line 1:
-- Required module containing helper methods
local helper = require('Module:HelperMethods')
local helper = require('Module:HelperMethods')
local mwHtml = require('mw.html')
local mwHtml = require('mw.html')
Line 5: Line 6:


----------------------------------------------------------------------
----------------------------------------------------------------------
-- Build recommendations list from direct Wikibase statements
-- Build recommendations list from direct Wikibase statements (P1643)
----------------------------------------------------------------------
----------------------------------------------------------------------
function p.buildRecommendationList(entityId)
function p.buildRecommendationList(entityId)
Line 13: Line 14:
     local dislikeIcon = '👎'
     local dislikeIcon = '👎'


     local statements =
    -- Fetch direct statements for P1643
        mw.wikibase.getBestStatements(entityId, 'P1643')
     local statements = mw.wikibase.getBestStatements(entityId, 'P1643')


     if not statements or #statements == 0 then
     if not statements or #statements == 0 then
Line 27: Line 28:
             and mainsnak.datavalue
             and mainsnak.datavalue
             and mainsnak.datavalue.type == 'wikibase-entityid'
             and mainsnak.datavalue.type == 'wikibase-entityid'
            and mainsnak.datavalue.value
            and mainsnak.datavalue.value.numeric
         then
         then
             local targetQid = 'Q' .. mainsnak.datavalue.value.numeric-id
            -- Build target Q-ID (custom Wikibase uses `numeric`)
             local targetQid =
                'Q' .. tostring(mainsnak.datavalue.value.numeric)


            -- Resolve label (fallback if missing)
             local label =
             local label =
                 mw.wikibase.getLabel(targetQid)
                 mw.wikibase.getLabel(targetQid)
                 or helper.titleNotAvailableStr()
                 or helper.titleNotAvailableStr()


            -- Base publication link
             local baseLink =
             local baseLink =
                 'https://portal.mardi4nfdi.de/wiki/Publication:'
                 'https://portal.mardi4nfdi.de/wiki/Publication:'
Line 72: Line 79:


----------------------------------------------------------------------
----------------------------------------------------------------------
-- Entry point
-- Entry point invoked from wikitext
----------------------------------------------------------------------
----------------------------------------------------------------------
function p.getCitesWorkList(frame)
function p.getCitesWorkList(frame)
Line 81: Line 88:
     end
     end


     local list =
     local list = p.buildRecommendationList(entityId)
        p.buildRecommendationList(entityId)


     if not list or list == '' then
     if not list or list == '' then

Revision as of 23:31, 4 January 2026

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

-- Required module containing helper methods
local helper = require('Module:HelperMethods')
local mwHtml = require('mw.html')

local p = {}

----------------------------------------------------------------------
-- Build recommendations list from direct Wikibase statements (P1643)
----------------------------------------------------------------------
function p.buildRecommendationList(entityId)
    local ul = mw.html.create('ul')

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

    -- Fetch direct statements for P1643
    local statements = mw.wikibase.getBestStatements(entityId, 'P1643')

    if not statements or #statements == 0 then
        return tostring(ul)
    end

    for _, stmt in ipairs(statements) do
        local mainsnak = stmt.mainsnak

        if mainsnak
            and mainsnak.snaktype == 'value'
            and mainsnak.datavalue
            and mainsnak.datavalue.type == 'wikibase-entityid'
            and mainsnak.datavalue.value
            and mainsnak.datavalue.value.numeric
        then
            -- Build target Q-ID (custom Wikibase uses `numeric`)
            local targetQid =
                'Q' .. tostring(mainsnak.datavalue.value.numeric)

            -- Resolve label (fallback if missing)
            local label =
                mw.wikibase.getLabel(targetQid)
                or helper.titleNotAvailableStr()

            -- Base publication link
            local baseLink =
                'https://portal.mardi4nfdi.de/wiki/Publication:'
                .. targetQid

            local modifiedLink =
                baseLink
                .. '?source=recommendation&referrer='
                .. entityId

            local currentPubLink =
                entityId: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

            local li = mw.html.create('li')

            li:wikitext('[' .. modifiedLink .. ' ' .. label .. '] ')
            li:wikitext('[' .. likeLink .. ' ' .. likeIcon .. '] ')
            li:wikitext('[' .. dislikeLink .. ' ' .. dislikeIcon .. ']')

            ul:node(li)
        end
    end

    return tostring(ul)
end

----------------------------------------------------------------------
-- Entry point invoked from wikitext
----------------------------------------------------------------------
function p.getCitesWorkList(frame)
    local entityId = frame.args[1]

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

    local list = p.buildRecommendationList(entityId)

    if not list or list == '' then
        return nil
    end

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

return p