Module:PublicationRecommendedItems: Difference between revisions

From MaRDI portal
User15 (talk | contribs)
Undo revision 48468274 by User15 (talk)
Tag: Undo
User15 (talk | contribs)
try transform to take data from wikibase
Line 1: Line 1:
-- Required module containing helper methods
local helper = require('Module:HelperMethods')
local helper = require('Module:HelperMethods')
-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local mwHtml = require('mw.html')
local mwHtml = require('mw.html')


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


-- Function to convert JSON results into a comma-separated string
----------------------------------------------------------------------
function p.convertJsonToCommaSeparatedList(jsonResults, currentPublicationId)
-- Build recommendations list from direct Wikibase statements
----------------------------------------------------------------------
function p.buildRecommendationList(entityId)
     local ul = mw.html.create('ul')
     local ul = mw.html.create('ul')


    -- Like and dislike Icons
     local likeIcon = '👍'
     local likeIcon = '👍'
     local dislikeIcon = '👎'
     local dislikeIcon = '👎'


     if jsonResults and jsonResults.results and jsonResults.results.bindings then
     local statements =
         local bindings = jsonResults.results.bindings
        mw.wikibase.getBestStatements(entityId, 'P1643')
         for i = 0, #bindings do
 
             local binding = bindings[i]
    if not statements or #statements == 0 then
             if binding and binding.valueLabel and binding.valueLabel.value then
        return tostring(ul)
                local name = binding.valueLabel.value
    end
                 if string.find(name, "https://") then
 
                    name = helper.titleNotAvailableStr()
    for _, stmt in ipairs(statements) do
                end
         local mainsnak = stmt.mainsnak
 
         if mainsnak
             and mainsnak.snaktype == 'value'
             and mainsnak.datavalue
            and mainsnak.datavalue.type == 'wikibase-entityid'
        then
            local targetQid = 'Q' .. mainsnak.datavalue.value.numeric-id
 
            local label =
                 mw.wikibase.getLabel(targetQid)
                or helper.titleNotAvailableStr()


                local link = binding.value.value
            local baseLink =
                 local qidCurrPub = link:match("Q%d+")
                 'https://portal.mardi4nfdi.de/wiki/Publication:'
                link = link:gsub("entity/Q", "wiki/Publication:")
                .. targetQid


                local modifiedLink = link .. "?source=recommendation&referrer=" .. currentPublicationId
            local modifiedLink =
-- Use currentPublicationId for like/dislike redirection
                baseLink
        local currentPubLink = currentPublicationId:gsub("Q", "Publication:")
                 .. '?source=recommendation&referrer='
                 --local likeLink = link .. "?source=recommendation&referrer=like"
                 .. entityId
                 --local dislikeLink = link .. "?source=recommendation&referrer=dislike"
        local likeLink = "https://portal.mardi4nfdi.de/wiki/" .. currentPubLink .. "?reaction=like&target=" .. qidCurrPub
        local dislikeLink = "https://portal.mardi4nfdi.de/wiki/" .. currentPubLink .. "?reaction=dislike&target=" .. qidCurrPub


                -- Build list item using HTML
            local currentPubLink =
                local li = mw.html.create('li')
                 entityId:gsub('Q', 'Publication:')
               
                local wikiLink = helper.makeWikiLink(modifiedLink, name)
                 li:wikitext('[' .. modifiedLink .. ' ' .. name .. '] ')
                li:wikitext('[' .. likeLink .. ' ' .. likeIcon .. '] ')
                li:wikitext('[' .. dislikeLink .. ' ' .. dislikeIcon .. ']')


                 ul:node(li)
            local likeLink =
            end
                '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
     end
     end
Line 55: Line 71:
end
end


-- Function to build the list
----------------------------------------------------------------------
-- Entry point
----------------------------------------------------------------------
function p.getCitesWorkList(frame)
function p.getCitesWorkList(frame)
     local entityId = frame.args[1]
     -- Retrieve target1 from frame arguments or return error message if not set
 
local target1 = frame.args[1]
     if not entityId or entityId == '' then
     if not target1 or target1 == '' then
         return "No records found"
         return "No records found"
     end  
     end
   
    -- Constructing the SPARQL query with dynamic entity target1
    local sparqlQuery = [[
PREFIX target: <https://portal.mardi4nfdi.de/entity/]] .. target1 .. [[>
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>


SELECT ?property ?propertyLabel ?value ?valueLabel
     local list =
WHERE {
        p.buildRecommendationList(entityId)
    target: wdt:P1643 ?value .
    OPTIONAL {
        ?value rdfs:label ?valueLabel .
        FILTER(LANG(?valueLabel) = "en")
    }
    BIND(wdt:P1643 AS ?property)
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?propertyLabel
]]
      
    -- mw.log( sparqlQuery )
   
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
mw.logObject(jsonResults)


-- Handle error in SPARQL query execution
    if not list or list == '' then
if jsonResults and jsonResults.error then
    mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
    return nil
end
 
if not jsonResults then
         return nil
         return nil
end
    end
if helper.countElementsInBindings(jsonResults.results.bindings) == 0 then
        return nil
end
 
local rawList = p.convertJsonToCommaSeparatedList(jsonResults, target1)
local linkList = mw.getCurrentFrame():preprocess(rawList)
-- mw.log(linkList)


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


-- Return the created html table
return p
return p

Revision as of 23:28, 4 January 2026

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

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

local p = {}

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

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

    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'
        then
            local targetQid = 'Q' .. mainsnak.datavalue.value.numeric-id

            local label =
                mw.wikibase.getLabel(targetQid)
                or helper.titleNotAvailableStr()

            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
----------------------------------------------------------------------
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