Module:CRAN
From MaRDI portal
Documentation for this module may be created at Module:CRAN/doc
local p = {}
local linkedwiki = require 'linkedwiki'
local host = "https://portal.mardi4nfdi.de"
local object = linkedwiki.new()
object:setConfig("http://wikibase.svc")
local newline=[[
]]
-- Entities
local instance_of = "wdt:P31"
local author_prop_nr = "wdt:P16"
local dep_prop_nr = "wdt:P342"
local import_prop_nr = "wdt:P585"
local license_prop_nr = "wdt:P163"
local update_prop_nr = "wdt:P170"
local version_prop_nr = "wdt:P132"
local maintainer_prop_nr = "wdt:P19"
local human = "wd:Q57162"
local scholarly_article = "wd:Q56887"
local r_package = "wd:Q57080"
function p.format_link(link, el, label, link_to_qid)
local link_to_qid = link_to_qid or false
if link_to_qid == true then
return "<span class='plainlinks'>[" .. host .. link .. el .. " " .. label .. "]</span>"
else
return "<span class='plainlinks'>[" .. host .. link .. label .. " " .. label .. "]</span>"
end
end
function p.simple_link(link, el)
return "<span class='plainlinks'>[" .. host .. link .. el .. " " .. el .. "]</span>"
end
function p.page_button(page, page_number)
local text
if page == "next" then
text = "Next page"
else
text = "Previous page"
end
return "<span class='plainlinks'>[" .. host .. "/wiki/CRAN?page=" .. page_number .. "<span class='mw-ui-button mw-ui-constructive mw-ui-small page-button'>" .. text .. "</span>]</span>"
end
function p.list_elements(f, qid, prop_nr, name, link)
local text, plural
local link_to_qid = false
if name == "Author" then
plural = "Authors"
link_to_qid = true
elseif name == "License" then
plural = "License"
link_to_qid = true
elseif name == "Dependency" then
plural = "Dependencies"
elseif name == "Imports" then
plural = "Imports"
end
local elements = object:query ( [[
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>
SELECT DISTINCT ?qid ?elementLabel WHERE {
wd:]] .. qid .. " " .. prop_nr .. [[ ?element ;
BIND(STRAFTER(STR(?element), STR(wd:)) AS ?qid) .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
]] )
local total_elements = #elements['result']['rows']
if next(elements['result']['rows']) == nil then
return ""
elseif total_elements == 0 then
local el = elements['result']['rows'][0]['qid']
local label = elements['result']['rows'][0]['elementLabel']
text = f:preprocess( "'''" .. name .. ": ''' " .. p.format_link(link, el, label, link_to_qid)) .. newline
else
local el = elements['result']['rows'][0]['qid']
local label = elements['result']['rows'][0]['elementLabel']
text = f:preprocess( "'''" .. plural .. ": ''' " .. p.format_link(link, el, label, link_to_qid))
local index = 1
while( index < total_elements )
do
el = elements['result']['rows'][index]['qid']
label = elements['result']['rows'][index]['elementLabel']
text = text .. ', ' .. f:preprocess(p.format_link(link, el, label, link_to_qid))
index = index + 1
end
el = elements['result']['rows'][index]['qid']
label = elements['result']['rows'][index]['elementLabel']
text = text .. ' and ' .. f:preprocess(p.format_link(link, el, label, link_to_qid)) .. newline
end
return newline .. text
end
function p.list_resources(f, author, link, resource_type)
local text, name
local link_to_qid = false
if resource_type == scholarly_article then
name = "Publications"
link_to_qid = true
elseif resource_type == r_package then
name = "Authored packages"
end
local resources = object:query ( [[
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>
SELECT DISTINCT ?title ?publicationid WHERE {
wd:]] .. author .. " " .. instance_of .. " " .. human .. [[ .
?publication rdfs:label ?title ;
]] .. instance_of .. " " .. resource_type .. [[ ;
]] .. author_prop_nr .. [[ wd:]] .. author .. [[ .
BIND(STRAFTER(STR(?publication), STR(wd:)) AS ?publicationid) .
}
]] )
local total_resources = #resources['result']['rows']
local i = 0
local row = nil
row = resources['result']['rows'][i]
if row ~= nil then
text = f:preprocess( "'''" .. name ..":'''") .. newline
while (row ~= nil)
do
local el = resources['result']['rows'][i]['publicationid']
local label = resources['result']['rows'][i]['title']
text = text .. '* ' .. f:preprocess(p.format_link(link, el, label, link_to_qid)) .. newline
i = i + 1
row = resources['result']['rows'][i]
end
return text
end
return ""
end
function p.section(f)
local total_results = 80
object:setConfig("http://wikibase.svc")
if #f.args['package'] > 0 then
local result = object:query ( [[
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>
SELECT DISTINCT ?qid ?publicationDate ?version ?maintainerqid ?maintainerLabel ?desc WHERE {
?item rdfs:label "]] .. f.args['package'] .. [[" @en ;
]] .. instance_of .. " " .. r_package .. [[ ;
]] .. update_prop_nr .. [[ ?publicationDate ;
]] .. version_prop_nr .. [[ ?version ;
]] .. maintainer_prop_nr .. [[ ?maintainer ;
schema:description ?desc .
BIND(STRAFTER(STR(?item), STR(wd:)) AS ?qid) .
BIND(STRAFTER(STR(?maintainer), STR(wd:)) AS ?maintainerqid) .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
]] )
local text = [[
== R package: ]] .. f.args['package'] .. [[ ==
]]
local qid, description, publicationDate, version, maintainerqid, maintainerlabel
if result['result']['rows'][0] ~= nil then
qid = result['result']['rows'][0]['qid']
description = result['result']['rows'][0]['desc']
publicationDate = result['result']['rows'][0]['publicationDate']
version = result['result']['rows'][0]['version']
maintainerqid = result['result']['rows'][0]['maintainerqid']
maintainerlabel = result['result']['rows'][0]['maintainerLabel']
else
text = text .. "Package not found."
return text
end
text = text .. description .. newline
text = text .. newline .. f:preprocess( "'''Knowledge Graph Item: '''" .. p.simple_link("/wiki/Item:", qid)) .. newline
text = text .. newline .. f:preprocess( "'''Version: '''") .. version .. newline
text = text .. newline .. f:preprocess( "'''Last update: '''" ) .. string.sub(publicationDate, 1, 10) .. newline
text = text .. p.list_elements(f, qid, author_prop_nr, 'Author', "/wiki/CRAN?author=")
text = text .. newline .. f:preprocess( "'''Maintainer: ''' " .. p.format_link("/wiki/Item:", maintainerqid, maintainerlabel, true) ) .. newline
text = text .. p.list_elements(f, qid, dep_prop_nr, 'Dependency', "/wiki/CRAN?package=")
text = text .. p.list_elements(f, qid, import_prop_nr, 'Imports', "/wiki/CRAN?package=")
text = text .. p.list_elements(f, qid, license_prop_nr, 'License', "/wiki/Item:")
text = text .. newline .. f:preprocess( "'''URL: '''" .. "<span class='plainlinks'>https://CRAN.R-project.org/package=" .. f.args['package'] .. "</span>") .. newline
return text
end
if #f.args['author'] > 0 then
-- instance of human
-- author of the package
local qid = f.args['author']
local metadata = object:query ( [[
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>
SELECT DISTINCT ?label ?orcid WHERE {
wd:]] .. qid .. [[ rdfs:label ?label ;
]] .. instance_of .. " " .. human .. [[ .
OPTIONAL { wd:]] .. qid .. [[ wdt:P148 ?orcid . }
?package ]] .. instance_of .. " " .. r_package .. [[ ;
]] .. author_prop_nr .. [[ wd:]] .. qid .. [[ .
}
]] )
local author, orcid
if metadata['result']['rows'][0] ~= nil then
author = metadata['result']['rows'][0]['label']
local text = [[
== Author: ]] .. author .. [[ ==
]]
text = text .. newline .. f:preprocess( "'''Knowledge Graph Item: '''" .. p.simple_link("/wiki/Item:", qid)) .. newline
if metadata['result']['rows'][0]['orcid'] ~= nil then
orcid = metadata['result']['rows'][0]['orcid']
text = text .. newline .. f:preprocess( "'''ORCID iD: '''") .. orcid .. newline
end
text = text .. newline .. p.list_resources(f, qid, "/wiki/CRAN?package=", r_package)
text = text .. p.list_resources(f, qid, "/wiki/Item:", scholarly_article) -- Publications
return text
else
local text = [[
== Author: ]] .. f.args['author'] .. [[ ==
]]
text = text .. "Author not found."
return text
end
end
local offset = 0
local current_page = 1
if tonumber(f.args['page']) and tonumber(f.args['page']) > 0 then
offset = (tonumber(f.args['page'])-1) * total_results
current_page = tonumber(f.args['page'])
end
local previous_page = current_page - 1
local next_page = current_page + 1
local result = object:query( [[
PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/>
PREFIX wd: <https://portal.mardi4nfdi.de/entity/>
SELECT ?package ?packageLabel ?version
WHERE {?package ]] .. instance_of .. " " .. r_package .. [[ .
?package wdt:P114 ?version
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} order by asc(UCASE(str(?packageLabel)))
LIMIT ]] .. total_results .. [[
OFFSET ]] .. offset .. [[
]])
local text = [[
== List of R Packages ==
]]
local i = 0
local row = nil
local total_length = #result['result']['rows']
row = result['result']['rows'][i]
text = text .. '<div>'
text = text .. '<ul class="cran-list">'
while( row ~= nil )
do
text = text .. "<li>" .. p.simple_link("/wiki/CRAN?package=", row["packageLabel"]) .. "</li>"
i = i + 1
row = result['result']['rows'][i]
end
text = text .. '</ul>'
text = text .. '</div>'
-- Pagination
text = text .. "<div class='mw-ui-button-group'>"
if total_length == total_results-1 and current_page == 1 then
text = text .. p.page_button("next", next_page)
elseif total_length == total_results-1 then
text = text .. p.page_button("previous", previous_page)
text = text .. p.page_button("next", next_page)
else
text = text .. p.page_button("previous", previous_page)
end
text = text .. "</div>"
return text
end
return p